| 1 |
from urllib.parse import parse_qs, urlparse |
| 2 |
|
| 3 |
from django.contrib.sites.models import Site |
| 4 |
from django.test import override_settings |
| 5 |
from django.urls import reverse |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from allauth.socialaccount.adapter import ( |
| 10 |
DefaultSocialAccountAdapter, |
| 11 |
_build_apps_from_settings, |
| 12 |
get_adapter, |
| 13 |
) |
| 14 |
from allauth.socialaccount.internal import statekit |
| 15 |
from allauth.socialaccount.models import SocialApp |
| 16 |
|
| 17 |
|
| 18 |
class PrefixStateSocialAccountAdapter(DefaultSocialAccountAdapter): |
| 19 |
def generate_state_param(self, state: dict) -> str: |
| 20 |
return f"prefix-{super().generate_state_param(state)}" |
| 21 |
|
| 22 |
|
| 23 |
def test_generate_state_param(settings, client, db, google_provider_settings): |
| 24 |
settings.SOCIALACCOUNT_ADAPTER = ( |
| 25 |
"tests.apps.socialaccount.test_adapter.PrefixStateSocialAccountAdapter" |
| 26 |
) |
| 27 |
resp = client.post(reverse("google_login")) |
| 28 |
parsed = urlparse(resp["location"]) |
| 29 |
query = parse_qs(parsed.query) |
| 30 |
state = query["state"][0] |
| 31 |
assert len(state) == len("prefix-") + statekit.STATE_ID_LENGTH |
| 32 |
assert state.startswith("prefix-") |
| 33 |
|
| 34 |
|
| 35 |
def test_list_db_based_apps(db, settings): |
| 36 |
app = SocialApp.objects.create( |
| 37 |
provider="saml", provider_id="urn:idp-identity-id", client_id="org-slug" |
| 38 |
) |
| 39 |
app.sites.add(Site.objects.get_current()) |
| 40 |
apps = get_adapter().list_apps(None, provider="saml", client_id="org-slug") |
| 41 |
assert app.pk in [a.pk for a in apps] |
| 42 |
|
| 43 |
|
| 44 |
def test_list_settings_based_apps(db, settings): |
| 45 |
settings.SOCIALACCOUNT_PROVIDERS = { |
| 46 |
"saml": { |
| 47 |
"APPS": [ |
| 48 |
{ |
| 49 |
"provider_id": "urn:idp-entity-id", |
| 50 |
"client_id": "org-slug", |
| 51 |
} |
| 52 |
] |
| 53 |
} |
| 54 |
} |
| 55 |
apps = get_adapter().list_apps(None, provider="saml", client_id="org-slug") |
| 56 |
assert len(apps) == 1 |
| 57 |
app = apps[0] |
| 58 |
assert not app.pk |
| 59 |
assert app.client_id == "org-slug" |
| 60 |
|
| 61 |
|
| 62 |
@override_settings( |
| 63 |
SOCIALACCOUNT_PROVIDERS={ |
| 64 |
"saml": { |
| 65 |
"APPS": [ |
| 66 |
{ |
| 67 |
"name": "IdP", |
| 68 |
"provider_id": "urn:idp-entity-id", |
| 69 |
"client_id": "org-slug", |
| 70 |
"secret": "sekret", |
| 71 |
"key": "the-key", |
| 72 |
"settings": {"sso_url": "https://idp.example.com/sso"}, |
| 73 |
} |
| 74 |
] |
| 75 |
} |
| 76 |
} |
| 77 |
) |
| 78 |
def test_build_apps_from_settings_fields(): |
| 79 |
provider_to_apps = _build_apps_from_settings() |
| 80 |
assert list(provider_to_apps.keys()) == ["saml"] |
| 81 |
apps = provider_to_apps["saml"] |
| 82 |
assert len(apps) == 1 |
| 83 |
app = apps[0] |
| 84 |
assert isinstance(app, SocialApp) |
| 85 |
assert not app.pk |
| 86 |
assert app.provider == "saml" |
| 87 |
assert app.name == "IdP" |
| 88 |
assert app.provider_id == "urn:idp-entity-id" |
| 89 |
assert app.client_id == "org-slug" |
| 90 |
assert app.secret == "sekret" |
| 91 |
assert app.key == "the-key" |
| 92 |
assert app.settings == {"sso_url": "https://idp.example.com/sso"} |
| 93 |
|
| 94 |
|
| 95 |
@override_settings( |
| 96 |
SOCIALACCOUNT_PROVIDERS={ |
| 97 |
"saml": { |
| 98 |
"APP": { |
| 99 |
"provider_id": "urn:idp-entity-id", |
| 100 |
"client_id": "org-slug", |
| 101 |
"certificate_key": "-----BEGIN CERTIFICATE-----", |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
) |
| 106 |
def test_build_apps_from_settings_certificate_key_deprecation(): |
| 107 |
with pytest.warns(UserWarning, match="certificate_key"): |
| 108 |
provider_to_apps = _build_apps_from_settings() |
| 109 |
app = provider_to_apps["saml"][0] |
| 110 |
assert app.settings["certificate_key"] == "-----BEGIN CERTIFICATE-----" |
| 111 |
|
| 112 |
|
| 113 |
@override_settings( |
| 114 |
SOCIALACCOUNT_PROVIDERS={ |
| 115 |
"saml": { |
| 116 |
"APPS": [ |
| 117 |
{"provider_id": "urn:idp-a", "client_id": "org-a"}, |
| 118 |
{"provider_id": "urn:idp-b", "client_id": "org-b"}, |
| 119 |
] |
| 120 |
} |
| 121 |
} |
| 122 |
) |
| 123 |
def test_build_apps_from_settings_filtering(): |
| 124 |
def client_ids(provider_to_apps): |
| 125 |
return [app.client_id for apps in provider_to_apps.values() for app in apps] |
| 126 |
|
| 127 |
assert client_ids(_build_apps_from_settings(client_id="org-b")) == ["org-b"] |
| 128 |
# provider= matches either the app's provider_id or its provider. |
| 129 |
assert client_ids(_build_apps_from_settings(provider="urn:idp-a")) == ["org-a"] |
| 130 |
assert client_ids(_build_apps_from_settings(provider="saml")) == ["org-a", "org-b"] |
| 131 |
assert client_ids(_build_apps_from_settings(provider="nope")) == [] |
| 132 |
|
| 133 |
|
| 134 |
def test_get_signup_form_initial_data(sociallogin_factory): |
| 135 |
sociallogin = sociallogin_factory(email="[email protected]") |
| 136 |
# it should pick up sociallogin.email_addresses |
| 137 |
sociallogin.user.email = "" |
| 138 |
initial_data = get_adapter().get_signup_form_initial_data(sociallogin) |
| 139 |
assert initial_data["email"] == "[email protected]" |