Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
course-discovery
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
course-discovery
Commits
a9ffdd1e
Commit
a9ffdd1e
authored
Mar 25, 2016
by
Clinton Blackburn
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #41 from edx/clintonb/drf-extensions-update
Updated edx-drf-extensions to 0.2.0
parents
0178d070
03a25abe
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
28 additions
and
21 deletions
+28
-21
course_discovery/apps/api/v1/tests/test_views.py
+15
-20
course_discovery/settings/base.py
+4
-0
course_discovery/settings/private.py.example
+4
-0
course_discovery/settings/test.py
+4
-0
requirements/base.txt
+1
-1
No files found.
course_discovery/apps/api/v1/tests/test_views.py
View file @
a9ffdd1e
# 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
))
course_discovery/settings/base.py
View file @
a9ffdd1e
...
...
@@ -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'
,
}
course_discovery/settings/private.py.example
View file @
a9ffdd1e
...
...
@@ -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',
}
course_discovery/settings/test.py
View file @
a9ffdd1e
...
...
@@ -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'
,
}
requirements/base.txt
View file @
a9ffdd1e
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment