Commit a9ffdd1e by Clinton Blackburn

Merge pull request #41 from edx/clintonb/drf-extensions-update

Updated edx-drf-extensions to 0.2.0
parents 0178d070 03a25abe
# pylint: disable=redefined-builtin
import datetime
import json
import urllib
from time import time
......@@ -8,7 +7,6 @@ import ddt
import jwt
import responses
from django.conf import settings
from django.test import override_settings
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase, APIRequestFactory
......@@ -19,28 +17,27 @@ from course_discovery.apps.core.tests.factories import UserFactory, USER_PASSWOR
from course_discovery.apps.core.tests.mixins import ElasticsearchTestMixin
from course_discovery.apps.course_metadata.tests.factories import CourseFactory
OAUTH2_ACCESS_TOKEN_URL = 'http://example.com/oauth2/access_token/'
class OAuth2Mixin(object):
def get_access_token(self, user):
""" Generates an OAuth2 access token for the user. """
return user.username
def generate_oauth2_token_header(self, user):
""" Generates a Bearer authorization header to simulate OAuth2 authentication. """
return 'Bearer {token}'.format(token=self.get_access_token(user))
return 'Bearer {token}'.format(token=user.username)
def mock_user_info_response(self, user, status=200):
""" Mock the user info endpoint response of the OAuth2 provider. """
def mock_access_token_response(self, user, status=200):
""" Mock the access token endpoint response of the OAuth2 provider. """
url = '{root}/{token}'.format(root=OAUTH2_ACCESS_TOKEN_URL.rstrip('/'), token=self.get_access_token(user))
expires = datetime.datetime.utcnow() + datetime.timedelta(days=1)
data = {
'family_name': user.last_name,
'preferred_username': user.username,
'given_name': user.first_name,
'email': user.email,
}
responses.add(
responses.GET,
url,
body=json.dumps({'username': user.username, 'scope': 'read', 'expires': expires.isoformat()}),
content_type="application/json",
settings.EDX_DRF_EXTENSIONS['OAUTH2_USER_INFO_URL'],
body=json.dumps(data),
content_type='application/json',
status=status
)
......@@ -151,10 +148,9 @@ class CatalogViewSetTests(ElasticsearchTestMixin, SerializationMixin, OAuth2Mixi
self.assert_catalog_created(HTTP_AUTHORIZATION=self.generate_jwt_token_header(self.user))
@responses.activate
@override_settings(OAUTH2_ACCESS_TOKEN_URL=OAUTH2_ACCESS_TOKEN_URL)
def test_create_with_oauth2_authentication(self):
self.client.logout()
self.mock_access_token_response(self.user)
self.mock_user_info_response(self.user)
self.assert_catalog_created(HTTP_AUTHORIZATION=self.generate_oauth2_token_header(self.user))
def test_courses(self):
......@@ -302,8 +298,7 @@ class CourseViewSetTests(ElasticsearchTestMixin, SerializationMixin, OAuth2Mixin
self.assertEqual(response.data, self.serialize_course(course))
@responses.activate
@override_settings(OAUTH2_ACCESS_TOKEN_URL=OAUTH2_ACCESS_TOKEN_URL)
def test_retrieve_with_oauth2_authentication(self):
self.client.logout()
self.mock_access_token_response(self.user)
self.mock_user_info_response(self.user)
self.assert_retrieve_success(HTTP_AUTHORIZATION=self.generate_oauth2_token_header(self.user))
......@@ -280,3 +280,7 @@ ELASTICSEARCH = {
# TODO Replace with None and document.
ECOMMERCE_API_URL = 'https://ecommerce.stage.edx.org/api/v2/'
COURSES_API_URL = 'https://courses.stage.edx.org/api/courses/v1/'
EDX_DRF_EXTENSIONS = {
'OAUTH2_USER_INFO_URL': 'http://localhost:8000/oauth2/user_info',
}
......@@ -2,3 +2,7 @@ SOCIAL_AUTH_EDX_OIDC_KEY = 'replace-me'
SOCIAL_AUTH_EDX_OIDC_SECRET = 'replace-me'
SOCIAL_AUTH_EDX_OIDC_URL_ROOT = 'http://127.0.0.1:8000/oauth'
SOCIAL_AUTH_EDX_OIDC_ID_TOKEN_DECRYPTION_KEY = SOCIAL_AUTH_EDX_OIDC_SECRET
EDX_DRF_EXTENSIONS = {
'OAUTH2_USER_INFO_URL': 'http://127.0.0.1:8000/oauth2/user_info',
}
......@@ -36,3 +36,7 @@ ELASTICSEARCH = {
}
JWT_AUTH['JWT_SECRET_KEY'] = 'course-discovery-jwt-secret-key'
EDX_DRF_EXTENSIONS = {
'OAUTH2_USER_INFO_URL': 'http://example.com/oauth2/user_info',
}
......@@ -5,7 +5,7 @@ djangorestframework==3.3.1
djangorestframework-jwt==1.7.2
django-rest-swagger[reST]==0.3.4
edx-auth-backends==0.1.3
edx-drf-extensions==0.1.1
edx-drf-extensions==0.2.0
edx-rest-api-client==1.5.0
elasticsearch>=1.0.0,<2.0.0
pytz==2015.7
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment