-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6563 from edx/ned/fix-6013
Fixes for #6013: Implement user service to return currently-logged-in user
- Loading branch information
Showing
18 changed files
with
277 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
57 changes: 57 additions & 0 deletions
57
common/djangoapps/xblock_django/tests/test_user_service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
Tests for the DjangoXBlockUserService. | ||
""" | ||
from django.test import TestCase | ||
from xblock_django.user_service import ( | ||
DjangoXBlockUserService, | ||
ATTR_KEY_IS_AUTHENTICATED, | ||
ATTR_KEY_USER_ID, | ||
ATTR_KEY_USERNAME, | ||
) | ||
from student.tests.factories import UserFactory, AnonymousUserFactory | ||
|
||
|
||
class UserServiceTestCase(TestCase): | ||
""" | ||
Tests for the DjangoXBlockUserService. | ||
""" | ||
def setUp(self): | ||
self.user = UserFactory(username="tester", email="test@tester.com") | ||
self.user.profile.name = "Test Tester" | ||
self.anon_user = AnonymousUserFactory() | ||
|
||
def assert_is_anon_xb_user(self, xb_user): | ||
""" | ||
A set of assertions for an anonymous XBlockUser. | ||
""" | ||
self.assertFalse(xb_user.opt_attrs[ATTR_KEY_IS_AUTHENTICATED]) | ||
self.assertIsNone(xb_user.full_name) | ||
self.assertListEqual(xb_user.emails, []) | ||
|
||
def assert_xblock_user_matches_django(self, xb_user, dj_user): | ||
""" | ||
A set of assertions for comparing a XBlockUser to a django User | ||
""" | ||
self.assertTrue(xb_user.opt_attrs[ATTR_KEY_IS_AUTHENTICATED]) | ||
self.assertEqual(xb_user.emails[0], dj_user.email) | ||
self.assertEqual(xb_user.full_name, dj_user.profile.name) | ||
self.assertEqual(xb_user.opt_attrs[ATTR_KEY_USERNAME], dj_user.username) | ||
self.assertEqual(xb_user.opt_attrs[ATTR_KEY_USER_ID], dj_user.id) | ||
|
||
def test_convert_anon_user(self): | ||
""" | ||
Tests for convert_django_user_to_xblock_user behavior when django user is AnonymousUser. | ||
""" | ||
django_user_service = DjangoXBlockUserService(self.anon_user) | ||
xb_user = django_user_service.get_current_user() | ||
self.assertTrue(xb_user.is_current_user) | ||
self.assert_is_anon_xb_user(xb_user) | ||
|
||
def test_convert_authenticate_user(self): | ||
""" | ||
Tests for convert_django_user_to_xblock_user behavior when django user is User. | ||
""" | ||
django_user_service = DjangoXBlockUserService(self.user) | ||
xb_user = django_user_service.get_current_user() | ||
self.assertTrue(xb_user.is_current_user) | ||
self.assert_xblock_user_matches_django(xb_user, self.user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
Support for converting a django user to an XBlock user | ||
""" | ||
from xblock.reference.user_service import XBlockUser, UserService | ||
|
||
ATTR_KEY_IS_AUTHENTICATED = 'edx-platform.is_authenticated' | ||
ATTR_KEY_USER_ID = 'edx-platform.user_id' | ||
ATTR_KEY_USERNAME = 'edx-platform.username' | ||
|
||
|
||
class DjangoXBlockUserService(UserService): | ||
""" | ||
A user service that converts Django users to XBlockUser | ||
""" | ||
def __init__(self, django_user, **kwargs): | ||
super(DjangoXBlockUserService, self).__init__(**kwargs) | ||
self._django_user = django_user | ||
|
||
def get_current_user(self): | ||
""" | ||
Returns the currently-logged in user, as an instance of XBlockUser | ||
""" | ||
return self._convert_django_user_to_xblock_user(self._django_user) | ||
|
||
def _convert_django_user_to_xblock_user(self, django_user): | ||
""" | ||
A function that returns an XBlockUser from the current Django request.user | ||
""" | ||
xblock_user = XBlockUser(is_current_user=True) | ||
|
||
if django_user is not None and django_user.is_authenticated(): | ||
# This full_name is dependent on edx-platform's profile implementation | ||
full_name = getattr(django_user.profile, 'name') if hasattr(django_user, 'profile') else None | ||
xblock_user.full_name = full_name | ||
xblock_user.emails = [django_user.email] | ||
xblock_user.opt_attrs[ATTR_KEY_IS_AUTHENTICATED] = True | ||
xblock_user.opt_attrs[ATTR_KEY_USER_ID] = django_user.id | ||
xblock_user.opt_attrs[ATTR_KEY_USERNAME] = django_user.username | ||
else: | ||
xblock_user.opt_attrs[ATTR_KEY_IS_AUTHENTICATED] = False | ||
|
||
return xblock_user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.