-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
48 lines (41 loc) · 1.54 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from contextlib import nullcontext
from pyquery import PyQuery as pq
from requests import Session
def keycloak_login(
login_url: str,
username: str = "testuser",
password: str = "testuser",
session: Session | None = None,
) -> str:
"""
Test helper to perform a keycloak login.
:param login_url: A login URL for keycloak with all query string parameters. E.g.
`client.get(reverse("login"))["Location"]`.
:returns: The redirect URI to consume in the django application, with the ``code``
``state`` query parameters. Consume this with ``response = client.get(url)``.
"""
cm = Session() if session is None else nullcontext(session)
with cm as session:
login_page = session.get(login_url)
assert login_page.status_code == 200
# process keycloak's login form and submit the username + password to
# authenticate
document = pq(login_page.text)
login_form = document("form#kc-form-login")
submit_url = login_form.attr("action")
assert isinstance(submit_url, str)
login_response = session.post(
submit_url,
data={
"username": username,
"password": password,
"credentialId": "",
"login": "Sign In",
},
allow_redirects=False,
)
assert login_response.status_code == 302
assert (redirect_uri := login_response.headers["Location"]).startswith(
"http://testserver/"
)
return redirect_uri