Skip to content

Commit

Permalink
tests(socialaccount): Test storing tokens vs email authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Aug 14, 2024
1 parent 907ffe6 commit c970b33
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
9 changes: 8 additions & 1 deletion allauth/socialaccount/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import pytest

from allauth.account.models import EmailAddress
from allauth.socialaccount.models import SocialAccount, SocialLogin
from allauth.socialaccount.models import (
SocialAccount,
SocialLogin,
SocialToken,
)


@pytest.fixture
Expand All @@ -13,6 +17,7 @@ def factory(
provider="unittest-server",
uid="123",
email_verified=True,
with_token=False,
):
user = user_factory(
username=username, email=email, commit=False, with_email=with_email
Expand All @@ -23,6 +28,8 @@ def factory(
sociallogin.email_addresses = [
EmailAddress(email=user.email, verified=email_verified, primary=True)
]
if with_token:
sociallogin.token = SocialToken(token="123", token_secret="456")
return sociallogin

return factory
8 changes: 7 additions & 1 deletion allauth/socialaccount/tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def test_email_authentication(
settings.ACCOUNT_AUTHENTICATION_METHOD = "email"
settings.ACCOUNT_EMAIL_VERIFICATION = "mandatory"
settings.SOCIALACCOUNT_AUTO_SIGNUP = True
settings.SOCIALACCOUNT_STORE_TOKENS = True
if setting == "on-global":
settings.SOCIALACCOUNT_EMAIL_AUTHENTICATION = True
elif setting == "on-provider":
Expand All @@ -54,7 +55,9 @@ def test_email_authentication(

user = user_factory(with_emailaddress=with_emailaddress)

sociallogin = sociallogin_factory(email=user.email, provider="unittest-server")
sociallogin = sociallogin_factory(
email=user.email, provider="unittest-server", with_token=True
)

request = request_factory.get("/")
request.user = AnonymousUser()
Expand All @@ -78,6 +81,9 @@ def test_email_authentication(
assert resp["location"] == reverse("account_email_verification_sent")
assert get_user_model().objects.count() == 1
assert SocialAccount.objects.filter(user=user.pk).exists() == auto_connect
assert (
SocialToken.objects.filter(account__user=user.pk).exists() == auto_connect
)
assert added_signal.called == auto_connect
assert not updated_signal.called

Expand Down
5 changes: 4 additions & 1 deletion docs/socialaccount/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ Available settings:
Must be a function accepting a single parameter for the socialaccount object.

``SOCIALACCOUNT_STORE_TOKENS`` (default: ``False``)
Indicates whether or not the access tokens are stored in the database.
Indicates whether or not the access tokens are stored in the database. Note that
tokens can only be stored if the related social account is stored as well, which
is not the case when you are using ``SOCIALACCOUNT_EMAIL_AUTHENTICATION`` without
``SOCIALACCOUNT_EMAIL_AUTHENTICATION_AUTO_CONNECT``.

``SOCIALACCOUNT_ONLY`` (default: ``False``)
When enabled (``True``), all functionality with regard to local accounts is
Expand Down

0 comments on commit c970b33

Please sign in to comment.