Skip to content

Commit

Permalink
Fix social backend tests. See Wirecloud#276
Browse files Browse the repository at this point in the history
  • Loading branch information
aarranz authored and Alejandro Rodriguez committed Jul 24, 2017
1 parent 0176cfb commit 1b45106
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/wirecloud/fiware/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 15 additions & 17 deletions src/wirecloud/fiware/tests/social_backend.py
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -86,19 +86,17 @@ class TestSocialAuthBackend(WirecloudTestCase):
USER_DATA_NO_LAST_NAME = {"username": "demo", "email": "[email protected]", "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()
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions src/wirecloud/fiware/views.py
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 1b45106

Please sign in to comment.