Commit 8ea946b1 by Clinton Blackburn

Updated User.access_token

- Simplified logic by taking advantage of built-in PSA code
- Updated user model tests and removed django_dynamic_fixture
parent 03a9604b
...@@ -16,10 +16,12 @@ class User(GuardianUserMixin, AbstractUser): ...@@ -16,10 +16,12 @@ class User(GuardianUserMixin, AbstractUser):
Assumes user has authenticated at least once with edX Open ID Connect. Assumes user has authenticated at least once with edX Open ID Connect.
""" """
try: social_auth = self.social_auth.first() # pylint: disable=no-member
return self.social_auth.first().extra_data[u'access_token'] # pylint: disable=no-member
except Exception: # pylint: disable=broad-except if social_auth:
return None return social_auth.access_token
return None
class Meta(object): # pylint:disable=missing-docstring class Meta(object): # pylint:disable=missing-docstring
get_latest_by = 'date_joined' get_latest_by = 'date_joined'
......
""" Tests for core models. """ """ Tests for core models. """
from django.test import TestCase from django.test import TestCase
from django_dynamic_fixture import G
from social.apps.django_app.default.models import UserSocialAuth from social.apps.django_app.default.models import UserSocialAuth
from course_discovery.apps.core.models import User, Currency from course_discovery.apps.core.models import Currency
from course_discovery.apps.core.tests.factories import UserFactory
# pylint: disable=no-member
class UserTests(TestCase): class UserTests(TestCase):
""" User model tests. """ """ User model tests. """
TEST_CONTEXT = {'foo': 'bar', 'baz': None}
def test_access_token(self): def setUp(self):
user = G(User) super(UserTests, self).setUp()
self.assertIsNone(user.access_token) self.user = UserFactory()
def test_access_token_without_social_auth(self):
""" Verify the property returns None if the user is not associated with a UserSocialAuth. """
self.assertIsNone(self.user.access_token)
social_auth = G(UserSocialAuth, user=user) def test_access_token(self):
self.assertIsNone(user.access_token) """ Verify the property returns the value of the access_token stored with the UserSocialAuth. """
social_auth = UserSocialAuth.objects.create(user=self.user, provider='test', uid=self.user.username)
self.assertIsNone(self.user.access_token)
access_token = u'My voice is my passport. Verify me.' access_token = 'My voice is my passport. Verify me.'
social_auth.extra_data[u'access_token'] = access_token social_auth.extra_data.update({'access_token': access_token})
social_auth.save() social_auth.save()
self.assertEqual(user.access_token, access_token) self.assertEqual(self.user.access_token, access_token)
def test_get_full_name(self): def test_get_full_name(self):
""" Test that the user model concatenates first and last name if the full name is not set. """ """ Test that the user model concatenates first and last name if the full name is not set. """
full_name = "George Costanza" full_name = "George Costanza"
user = G(User, full_name=full_name) user = UserFactory(full_name=full_name)
self.assertEqual(user.get_full_name(), full_name) self.assertEqual(user.get_full_name(), full_name)
first_name = "Jerry" first_name = "Jerry"
last_name = "Seinfeld" last_name = "Seinfeld"
user = G(User, full_name=None, first_name=first_name, last_name=last_name) user = UserFactory(full_name=None, first_name=first_name, last_name=last_name)
expected = "{first_name} {last_name}".format(first_name=first_name, last_name=last_name) expected = "{first_name} {last_name}".format(first_name=first_name, last_name=last_name)
self.assertEqual(user.get_full_name(), expected) self.assertEqual(user.get_full_name(), expected)
user = G(User, full_name=full_name, first_name=first_name, last_name=last_name) user = UserFactory(full_name=full_name, first_name=first_name, last_name=last_name)
self.assertEqual(user.get_full_name(), full_name) self.assertEqual(user.get_full_name(), full_name)
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
coverage==4.0.2 coverage==4.0.2
ddt==1.0.1 ddt==1.0.1
django-dynamic-fixture==1.8.5
django-nose==1.4.2 django-nose==1.4.2
edx-lint==0.5.0 edx-lint==0.5.0
factory-boy==2.6.0 factory-boy==2.6.0
......
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