| 1 |
import time |
| 2 |
from contextlib import contextmanager |
| 3 |
from http import HTTPStatus |
| 4 |
from unittest.mock import patch |
| 5 |
|
| 6 |
from django.urls import reverse |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from allauth.account.models import EmailAddress |
| 11 |
from allauth.socialaccount.adapter import get_adapter |
| 12 |
from allauth.socialaccount.internal import statekit |
| 13 |
from allauth.socialaccount.models import SocialAccount, SocialLogin, SocialToken |
| 14 |
from allauth.socialaccount.providers.base.constants import AuthProcess |
| 15 |
from tests.mocking import MockedResponse, mocked_response |
| 16 |
|
| 17 |
|
| 18 |
@pytest.fixture |
| 19 |
def sociallogin_factory(user_factory): |
| 20 |
def factory( |
| 21 |
email=None, |
| 22 |
username=None, |
| 23 |
with_email=True, |
| 24 |
provider="unittest-server", |
| 25 |
uid="123", |
| 26 |
email_verified=True, |
| 27 |
with_token=False, |
| 28 |
): |
| 29 |
user = user_factory( |
| 30 |
username=username, email=email, commit=False, with_email=with_email |
| 31 |
) |
| 32 |
provider_instance = get_adapter().get_provider(request=None, provider=provider) |
| 33 |
account = SocialAccount(provider=provider, uid=uid) |
| 34 |
sociallogin = SocialLogin( |
| 35 |
provider=provider_instance, user=user, account=account |
| 36 |
) |
| 37 |
if with_email: |
| 38 |
sociallogin.email_addresses = [ |
| 39 |
EmailAddress(email=user.email, verified=email_verified, primary=True) |
| 40 |
] |
| 41 |
if with_token: |
| 42 |
sociallogin.token = SocialToken(token="123", token_secret="456") # nosec |
| 43 |
return sociallogin |
| 44 |
|
| 45 |
return factory |
| 46 |
|
| 47 |
|
| 48 |
@pytest.fixture |
| 49 |
def jwt_decode_bypass(): |
| 50 |
@contextmanager |
| 51 |
def f(jwt_data): |
| 52 |
with patch("allauth.socialaccount.internal.jwtkit.verify_and_decode") as m: |
| 53 |
data = { |
| 54 |
"iss": "https://accounts.google.com", |
| 55 |
"aud": "client_id", |
| 56 |
"sub": "123sub", |
| 57 |
"hd": "example.com", |
| 58 |
"email": "[email protected]", |
| 59 |
"email_verified": True, |
| 60 |
"at_hash": "HK6E_P6Dh8Y93mRNtsDB1Q", |
| 61 |
"name": "Raymond Penners", |
| 62 |
"picture": "https://lh5.googleusercontent.com/photo.jpg", |
| 63 |
"given_name": "Raymond", |
| 64 |
"family_name": "Penners", |
| 65 |
"locale": "en", |
| 66 |
"iat": 123, |
| 67 |
"exp": 456, |
| 68 |
} |
| 69 |
data.update(jwt_data) |
| 70 |
m.return_value = data |
| 71 |
yield |
| 72 |
|
| 73 |
return f |
| 74 |
|
| 75 |
|
| 76 |
@pytest.fixture |
| 77 |
def provider_callback_response(): |
| 78 |
def f(client, process=AuthProcess.LOGIN): |
| 79 |
with mocked_response( |
| 80 |
{ |
| 81 |
"token_endpoint": "/", |
| 82 |
"userinfo_endpoint": "/", |
| 83 |
}, |
| 84 |
MockedResponse( |
| 85 |
HTTPStatus.OK, "access_token=456", {"content-type": "dummy"} |
| 86 |
), |
| 87 |
{ |
| 88 |
"sub": "sub123", |
| 89 |
}, |
| 90 |
): |
| 91 |
session = client.session |
| 92 |
session[statekit.STATES_SESSION_KEY] = { |
| 93 |
"state456": [{"process": process}, time.time()] |
| 94 |
} |
| 95 |
session.save() |
| 96 |
|
| 97 |
resp = client.post( |
| 98 |
reverse( |
| 99 |
"openid_connect_callback", |
| 100 |
kwargs={"provider_id": "unittest-server"}, |
| 101 |
) |
| 102 |
+ "?code=123&state=state456" |
| 103 |
) |
| 104 |
return resp |
| 105 |
|
| 106 |
return f |