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