Skip to content

Commit

Permalink
Fix AttributeError on later versions of django-allauth (#47)
Browse files Browse the repository at this point in the history
* Fixing the attribute check for token.app.pk

* Updating Middleware setings for django all auth
Fixing test code coverage
  • Loading branch information
vsbharath authored Jan 12, 2024
1 parent 5599778 commit 7c357f7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
28 changes: 22 additions & 6 deletions sfdo_template_helpers/oauth2/salesforce/tests/test_provider.py
Original file line number Diff line number Diff line change
@@ -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"}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
11 changes: 7 additions & 4 deletions sfdo_template_helpers/oauth2/salesforce/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"}
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion sfdo_template_helpers/oauth2/tests/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down

0 comments on commit 7c357f7

Please sign in to comment.