canaille-globuzma/tests/test_password_flow.py

73 lines
2 KiB
Python
Raw Normal View History

2020-08-19 08:28:28 +00:00
from . import client_credentials
2020-10-21 12:04:40 +00:00
from canaille.models import Token
2020-08-19 07:09:22 +00:00
def test_password_flow_basic(testclient, slapd_connection, user, client):
2020-08-19 07:09:22 +00:00
res = testclient.post(
"/oauth/token",
2020-08-19 08:28:28 +00:00
params=dict(
2020-08-19 07:09:22 +00:00
grant_type="password",
2021-12-06 14:40:30 +00:00
username="John (johnny) Doe",
password="correct horse battery staple",
2020-08-19 07:09:22 +00:00
scope="profile",
),
2020-08-19 08:28:28 +00:00
headers={"Authorization": f"Basic {client_credentials(client)}"},
2020-10-30 18:19:34 +00:00
status=200,
2020-08-19 07:09:22 +00:00
)
2021-06-03 15:24:36 +00:00
assert res.json["scope"] == "openid profile groups"
2020-08-19 07:09:22 +00:00
assert res.json["token_type"] == "Bearer"
access_token = res.json["access_token"]
2020-08-19 14:20:57 +00:00
token = Token.get(access_token, conn=slapd_connection)
2020-08-19 08:28:28 +00:00
assert token is not None
2020-09-25 09:26:41 +00:00
res = testclient.get(
2020-10-30 18:19:34 +00:00
"/oauth/userinfo",
headers={"Authorization": f"Bearer {access_token}"},
status=200,
2020-09-25 09:26:41 +00:00
)
2021-09-28 07:30:41 +00:00
assert {
2021-12-06 14:40:30 +00:00
"name": "John (johnny) Doe",
2021-09-28 07:30:41 +00:00
"sub": "user",
"family_name": "Doe",
"groups": [],
} == res.json
def test_password_flow_post(testclient, slapd_connection, user, client):
client.oauthTokenEndpointAuthMethod = "client_secret_post"
client.save(conn=slapd_connection)
res = testclient.post(
"/oauth/token",
params=dict(
grant_type="password",
2021-12-06 14:40:30 +00:00
username="John (johnny) Doe",
password="correct horse battery staple",
scope="profile",
client_id=client.oauthClientID,
client_secret=client.oauthClientSecret,
),
status=200,
)
assert res.json["scope"] == "openid profile groups"
assert res.json["token_type"] == "Bearer"
access_token = res.json["access_token"]
token = Token.get(access_token, conn=slapd_connection)
assert token is not None
res = testclient.get(
"/oauth/userinfo",
headers={"Authorization": f"Bearer {access_token}"},
status=200,
)
assert {
2021-12-06 14:40:30 +00:00
"name": "John (johnny) Doe",
"sub": "user",
"family_name": "Doe",
"groups": [],
} == res.json