From 7c357f778f7cd30c38eae05831230230c7058728 Mon Sep 17 00:00:00 2001 From: Bharath Chadarajupalli Date: Fri, 12 Jan 2024 13:01:11 -0800 Subject: [PATCH] Fix AttributeError on later versions of django-allauth (#47) * Fixing the attribute check for token.app.pk * Updating Middleware setings for django all auth Fixing test code coverage --- .../oauth2/salesforce/tests/test_provider.py | 28 +++++++++++++++---- .../oauth2/salesforce/tests/test_views.py | 2 +- .../oauth2/salesforce/views.py | 11 +++++--- .../oauth2/tests/test_adapter.py | 2 +- tests/settings.py | 2 +- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/sfdo_template_helpers/oauth2/salesforce/tests/test_provider.py b/sfdo_template_helpers/oauth2/salesforce/tests/test_provider.py index 396e3f7..e8407ae 100644 --- a/sfdo_template_helpers/oauth2/salesforce/tests/test_provider.py +++ b/sfdo_template_helpers/oauth2/salesforce/tests/test_provider.py @@ -1,22 +1,38 @@ from ..provider import SFDOSalesforceProvider +from allauth.socialaccount.models import SocialApp +import pytest -def test_get_auth_params(rf): +@pytest.fixture +def dummy_app(): + app = SocialApp.objects.create( + provider=SFDOSalesforceProvider.id, + name=SFDOSalesforceProvider.id, + client_id="app123id", + key=SFDOSalesforceProvider.id, + secret="dummy", + ) + return app + +@pytest.mark.django_db +def test_get_auth_params(rf, dummy_app): request = rf.get("/") - result = SFDOSalesforceProvider(request).get_auth_params(request, None) + result = SFDOSalesforceProvider(request, dummy_app).get_auth_params(request, None) assert "prompt" in result and result["prompt"] == "login" -def test_extract_uid(rf): +@pytest.mark.django_db +def test_extract_uid(rf, dummy_app): request = rf.get("/") - provider = SFDOSalesforceProvider(request) + provider = SFDOSalesforceProvider(request, dummy_app) result = provider.extract_uid({"organization_id": "ORG", "user_id": "USER"}) assert result == "ORG/USER" -def test_extract_common_fields(rf): +@pytest.mark.django_db +def test_extract_common_fields(rf, dummy_app): request = rf.get("/") - provider = SFDOSalesforceProvider(request) + provider = SFDOSalesforceProvider(request, dummy_app) result = provider.extract_common_fields( {"organization_id": "ORG", "user_id": "USER"} ) diff --git a/sfdo_template_helpers/oauth2/salesforce/tests/test_views.py b/sfdo_template_helpers/oauth2/salesforce/tests/test_views.py index 0244f6c..96bc94f 100644 --- a/sfdo_template_helpers/oauth2/salesforce/tests/test_views.py +++ b/sfdo_template_helpers/oauth2/salesforce/tests/test_views.py @@ -62,7 +62,7 @@ def test_complete_login(self, mocker, rf): token.token = fernet_encrypt("token") ret = adapter.complete_login( - request, None, token, response={"instance_url": "https://example.com"} + request, token.app, token, response={"instance_url": "https://example.com"} ) assert ret.account.extra_data["instance_url"] == "https://example.com" diff --git a/sfdo_template_helpers/oauth2/salesforce/views.py b/sfdo_template_helpers/oauth2/salesforce/views.py index 5261a20..1955680 100644 --- a/sfdo_template_helpers/oauth2/salesforce/views.py +++ b/sfdo_template_helpers/oauth2/salesforce/views.py @@ -46,7 +46,7 @@ def base_url(self): def complete_login(self, request, app, token, **kwargs): # make sure token is attached to a SocialApp in the db - ensure_socialapp_in_db(token) + ensure_socialapp_in_db(token, app) token = fernet_decrypt(token.token) headers = {"Authorization": f"Bearer {token}"} @@ -136,15 +136,18 @@ def parse_token(self, data): return super().parse_token(data) -def ensure_socialapp_in_db(token): +def ensure_socialapp_in_db(token, social_app): """Make sure that token is attached to a SocialApp in the db. Since we are using SocialApps constructed from settings, there are none in the db for tokens to be related to unless we create them here. """ - if token.app.pk is None: - provider = providers.registry.by_id(token.app.provider) + if social_app is None: + social_app = token.app + + if getattr(social_app ,'pk', None) is None: + provider = providers.registry.get_class(social_app.provider) app, created = SocialApp.objects.get_or_create( provider=provider.id, name=provider.name, diff --git a/sfdo_template_helpers/oauth2/tests/test_adapter.py b/sfdo_template_helpers/oauth2/tests/test_adapter.py index ff6ff92..9bc111e 100644 --- a/sfdo_template_helpers/oauth2/tests/test_adapter.py +++ b/sfdo_template_helpers/oauth2/tests/test_adapter.py @@ -6,7 +6,7 @@ def test_authentication_error_logs(mocker): mocker.patch( - "allauth.socialaccount.adapter.DefaultSocialAccountAdapter.authentication_error" + "allauth.socialaccount.adapter.DefaultSocialAccountAdapter.on_authentication_error" ) # noqa error = mocker.patch("sfdo_template_helpers.oauth2.adapter.logger.error") adapter = SFDOSocialAccountAdapter() diff --git a/tests/settings.py b/tests/settings.py index 50df17a..33ace78 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -25,7 +25,7 @@ SITE_ID = 1 -MIDDLEWARE = () +MIDDLEWARE = ["allauth.account.middleware.AccountMiddleware"] # A one-off key made to run tests. Obviously do not use the key in the test app for # anything real!