canaille-globuzma/tests/test_token_introspection.py

103 lines
3 KiB
Python
Raw Normal View History

2021-12-20 22:57:27 +00:00
from urllib.parse import parse_qs
from urllib.parse import urlsplit
from canaille.models import AuthorizationCode
from canaille.models import Client
from canaille.models import Token
2020-08-24 12:44:41 +00:00
from . import client_credentials
2020-08-24 12:47:55 +00:00
def test_token_introspection(testclient, user, client, token):
2020-08-24 12:44:41 +00:00
res = testclient.post(
"/oauth/introspect",
2021-10-13 09:52:02 +00:00
params=dict(
token=token.oauthAccessToken,
),
2020-08-24 12:44:41 +00:00
headers={"Authorization": f"Basic {client_credentials(client)}"},
2020-10-30 22:41:02 +00:00
status=200,
2020-08-24 12:44:41 +00:00
)
assert {
"active": True,
"client_id": client.oauthClientID,
2020-08-24 12:44:41 +00:00
"token_type": token.oauthTokenType,
"username": user.name,
"scope": token.get_scope(),
2021-10-03 18:26:47 +00:00
"sub": user.uid[0],
2021-10-13 09:52:02 +00:00
"aud": [client.oauthClientID],
2020-08-24 12:44:41 +00:00
"iss": "https://mydomain.tld",
"exp": token.get_expires_at(),
"iat": token.get_issued_at(),
} == res.json
2020-08-24 12:47:55 +00:00
def test_token_invalid(testclient, client):
res = testclient.post(
"/oauth/introspect",
params=dict(token="invalid"),
headers={"Authorization": f"Basic {client_credentials(client)}"},
2020-10-30 22:41:02 +00:00
status=200,
2020-08-24 12:47:55 +00:00
)
2020-08-24 13:38:11 +00:00
assert {"active": False} == res.json
2021-10-13 09:52:02 +00:00
def test_full_flow(
testclient, slapd_connection, logged_user, client, user, other_client
):
res = testclient.get(
"/oauth/authorize",
params=dict(
response_type="code",
client_id=client.oauthClientID,
scope="profile",
nonce="somenonce",
),
status=200,
)
res = res.form.submit(name="answer", value="accept", status=302)
assert res.location.startswith(client.oauthRedirectURIs[0])
params = parse_qs(urlsplit(res.location).query)
code = params["code"][0]
authcode = AuthorizationCode.get(code, conn=slapd_connection)
assert authcode is not None
res = testclient.post(
"/oauth/token",
params=dict(
grant_type="authorization_code",
code=code,
scope="profile",
redirect_uri=client.oauthRedirectURIs[0],
),
headers={"Authorization": f"Basic {client_credentials(client)}"},
status=200,
)
access_token = res.json["access_token"]
token = Token.get(access_token, conn=slapd_connection)
assert token.oauthClient == client.dn
assert token.oauthSubject == logged_user.dn
res = testclient.post(
"/oauth/introspect",
params=dict(
token=token.oauthAccessToken,
),
headers={"Authorization": f"Basic {client_credentials(client)}"},
status=200,
)
assert {
"aud": [client.oauthClientID, other_client.oauthClientID],
"active": True,
"client_id": client.oauthClientID,
"token_type": token.oauthTokenType,
"username": user.name,
"scope": token.get_scope(),
"sub": user.uid[0],
"iss": "https://mydomain.tld",
"exp": token.get_expires_at(),
"iat": token.get_issued_at(),
} == res.json