canaille-globuzma/tests/test_authorization_code_flow.py
2020-08-19 10:28:28 +02:00

53 lines
1.6 KiB
Python

from . import client_credentials
from urllib.parse import urlsplit, parse_qs
from web.models import AuthorizationCode, Token
def test_success(testclient, slapd_connection, user, client):
res = testclient.get(
"/oauth/authorize",
params=dict(
response_type="code",
client_id=client.oauthClientID,
scope="profile",
nonce="somenonce",
),
)
assert 200 == res.status_code
res.form["login"] = user.name
res.form["password"] = "valid"
res = res.form.submit()
assert 302 == res.status_code
res = res.follow()
assert 200 == res.status_code
res = res.forms["accept"].submit()
assert 302 == res.status_code
assert res.location.startswith(client.oauthRedirectURIs[0])
params = parse_qs(urlsplit(res.location).query)
code = params["code"][0]
authcode = AuthorizationCode.get(code, 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)}"},
)
assert 200 == res.status_code
access_token = res.json["access_token"]
token = Token.get(access_token, slapd_connection)
assert token is not None
res = testclient.get("/api/me", headers={"Authorization": f"Bearer {access_token}"})
assert 200 == res.status_code
assert {"foo": "bar"} == res.json