From 1b45106e5d6b2f7d84ccfd1839380f106bead068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Arranz?= Date: Wed, 15 Mar 2017 10:58:09 +0100 Subject: [PATCH] Fix social backend tests. See #276 --- src/wirecloud/fiware/plugins.py | 2 +- src/wirecloud/fiware/tests/social_backend.py | 32 +++++++++----------- src/wirecloud/fiware/views.py | 5 ++- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/wirecloud/fiware/plugins.py b/src/wirecloud/fiware/plugins.py index 220cb0ace4..63e6598aa0 100644 --- a/src/wirecloud/fiware/plugins.py +++ b/src/wirecloud/fiware/plugins.py @@ -43,7 +43,7 @@ def auth_fiware_token(auth_type, token): - from social.apps.django_app.default.models import UserSocialAuth + from social_django.models import UserSocialAuth user_data = FIWARE_SOCIAL_AUTH_BACKEND._user_data(token) return UserSocialAuth.objects.get(provider='fiware', uid=user_data['username']).user diff --git a/src/wirecloud/fiware/tests/social_backend.py b/src/wirecloud/fiware/tests/social_backend.py index f99e6384e3..e76ffd294c 100644 --- a/src/wirecloud/fiware/tests/social_backend.py +++ b/src/wirecloud/fiware/tests/social_backend.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2015-2016 Conwet Lab., Universidad Politécnica de Madrid +# Copyright (c) 2015-2017 Conwet Lab., Universidad Politécnica de Madrid # This file is part of Wirecloud. @@ -86,19 +86,17 @@ class TestSocialAuthBackend(WirecloudTestCase): USER_DATA_NO_LAST_NAME = {"username": "demo", "email": "demo@fiware.org", "fullname": "Demo", "first_name": "Demo", "last_name": ""} def setUp(self): - self.social = MagicMock() + self.social_core = MagicMock() + self.social_django = MagicMock() modules = { - 'social': self.social, - 'social.backends': self.social.backends, - 'social.backends.oauth': self.social.backends.oauth, - 'social.backends.utils': self.social.backends.utils, - 'social.apps': self.social.apps, - 'social.apps.django_app': self.social.apps.django_app, - 'social.apps.django_app.default': self.social.apps.django_app.default, - 'social.apps.django_app.default.models': self.social.apps.django_app.default.models, - 'social.apps.django_app.utils': self.social.apps.django_app.utils, + 'social_core': self.social_core, + 'social_core.backends': self.social_core.backends, + 'social_core.backends.oauth': self.social_core.backends.oauth, + 'social_django': self.social_django, + 'social_django.utils': self.social_django.utils, + 'social_django.models': self.social_django.models, } - self.social.backends.oauth.BaseOAuth2 = BasicClass + self.social_core.backends.oauth.BaseOAuth2 = BasicClass self.module_patcher = patch.dict('sys.modules', modules) self.module_patcher.start() @@ -187,7 +185,7 @@ def test_api_authentication_using_idm(self): def get_social_auth(provider, uid): if provider == 'fiware' and uid == 'demo': return auth_user_mock - self.social.apps.django_app.default.models.UserSocialAuth.objects.get.side_effect = get_social_auth + self.social_django.models.UserSocialAuth.objects.get.side_effect = get_social_auth with patch('wirecloud.fiware.plugins.FIWARE_SOCIAL_AUTH_BACKEND', create=True) as backend_mock: backend_mock._user_data.return_value = self.USER_DATA @@ -201,7 +199,7 @@ def test_create_organizations_ignores_other_backends(self): user = Mock() response = None - UserSocialAuth = self.social.apps.django_app.default.models.UserSocialAuth + UserSocialAuth = self.social_django.models.UserSocialAuth self.create_organizations(strategy, backend, user, response) @@ -212,7 +210,7 @@ def test_oauth_discovery(self): from wirecloud.fiware.views import oauth_discovery - backend = self.social.backends.utils.get_backend() + backend = self.social_django.utils.get_backend() backend.AUTHORIZATION_URL = "https://auth.example.com" backend.ACCESS_TOKEN_URL = "https://token.example.com" @@ -267,7 +265,7 @@ def test_organizations_are_created(self): user = Mock() response = deepcopy(self.NEW_RESPONSE) - UserSocialAuth = self.social.apps.django_app.default.models.UserSocialAuth + UserSocialAuth = self.social_django.models.UserSocialAuth UserSocialAuth.DoesNotExist = Exception select_related_mock = Mock() select_related_mock.get.side_effect = UserSocialAuth.DoesNotExist @@ -285,7 +283,7 @@ def test_existing_organization(self): response = deepcopy(self.NEW_RESPONSE) org_social = Mock() - UserSocialAuth = self.social.apps.django_app.default.models.UserSocialAuth + UserSocialAuth = self.social_django.models.UserSocialAuth UserSocialAuth.objects.select_related().get.return_value = org_social self.create_organizations(strategy, backend, user, response) diff --git a/src/wirecloud/fiware/views.py b/src/wirecloud/fiware/views.py index cf7773a606..e914404298 100644 --- a/src/wirecloud/fiware/views.py +++ b/src/wirecloud/fiware/views.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2012-2016 CoNWeT Lab., Universidad Politécnica de Madrid +# Copyright (c) 2012-2017 CoNWeT Lab., Universidad Politécnica de Madrid # This file is part of Wirecloud. @@ -34,8 +34,7 @@ @require_GET def oauth_discovery(request): - from social.apps.django_app.utils import BACKENDS - from social.backends.utils import get_backend + from social_django.utils import BACKENDS, get_backend fiware_auth_backend = get_backend(BACKENDS, 'fiware') endpoints = {