Test a client application#
This guide shows how to test the login and the logout of a client application against the IAM server.
It is meant for developers of an application that logs its users in with OAuth2 or OpenID Connect.
The examples use the user and client fixtures from Create users, groups and clients.
For applications that sign their authorization requests, see Test signed authorization requests.
Configure the application#
The tested application uses the IAM server at iam_server.url as its identity provider,
with the credentials of the client fixture.
The examples of this page use a Flask application with an Authlib client:
/protectedredirects anonymous users to the IAM server, and greets logged in users;/createredirects to the IAM server, to create an account;/authorizeexchanges the authorization code for a token, and stores the user and the ID token in the session;/logoutclears the session, and redirects to the IAM server to log the user out;/logged-outis the page where the IAM server sends users after they log out.
import uuid
from urllib.parse import urlencode
import pytest
from authlib.integrations.flask_client import OAuth
from flask import Flask
from flask import redirect
from flask import session
from flask import url_for
@pytest.fixture
def app(iam_server, client):
app = Flask(__name__)
app.config["SERVER_NAME"] = "client.test"
app.config["SECRET_KEY"] = str(uuid.uuid4())
oauth = OAuth(app)
oauth.register(
name="iam",
client_id=client.client_id,
client_secret=client.client_secret,
server_metadata_url=f"{iam_server.url}.well-known/openid-configuration",
client_kwargs={"scope": "openid profile"},
)
@app.route("/protected")
def protected():
if "user" not in session:
return oauth.iam.authorize_redirect(url_for("authorize", _external=True))
return f"Hello, {session['user']['sub']}!"
@app.route("/create")
def create():
return oauth.iam.authorize_redirect(
url_for("authorize", _external=True), prompt="create"
)
@app.route("/authorize")
def authorize():
token = oauth.iam.authorize_access_token()
session["user"] = token["userinfo"]
session["id_token"] = token["id_token"]
return redirect(url_for("protected"))
@app.route("/logout")
def logout():
query = urlencode(
{
"client_id": oauth.iam.client_id,
"id_token_hint": session.pop("id_token"),
"post_logout_redirect_uri": url_for("logged_out", _external=True),
}
)
session.pop("user")
metadata = oauth.iam.load_server_metadata()
return redirect(f"{metadata['end_session_endpoint']}?{query}")
@app.route("/logged-out")
def logged_out():
return "Goodbye!"
return app
@pytest.fixture
def test_client(app):
return app.test_client()
In a real project, the app fixture creates the tested application with the same settings.
Test the login#
The test drives the application and the IAM server with their test clients. It follows each redirection by hand, and fills the login, password and consent forms of the IAM server.
def test_login(iam_server, client, user, test_client):
# 1. the protected page redirects to the IAM server
res = test_client.get("/protected")
# 2. the IAM server redirects to its login form
res = iam_server.test_client.get(res.location)
# 3. fill the login form
res = iam_server.test_client.post(res.location, data={"login": "user"})
# 4. fill the password form
res = iam_server.test_client.post(
res.location, data={"password": "correct horse battery staple"}
)
# 5. fill the consent form, the IAM server redirects to the application with a code
res = iam_server.test_client.post(res.location, data={"answer": "accept"})
# 6. the application exchanges the code for a token
res = test_client.get(res.location)
# 7. the protected page is now accessible
res = test_client.get(res.location)
assert res.text == "Hello, user!"
Skip the login and consent pages#
login() opens a session for a user on the IAM server,
and consent() records the consent of a user for a client.
The IAM server then answers the authorization request with a code at once.
The session ends with the test.
def test_login_shortcuts(iam_server, client, user, test_client):
iam_server.login(user)
iam_server.consent(user, client)
# 1. the protected page redirects to the IAM server
res = test_client.get("/protected")
# 2. the IAM server redirects to the application with a code
res = iam_server.test_client.get(res.location)
# 3. the application exchanges the code for a token
res = test_client.get(res.location)
# 4. the protected page is now accessible
res = test_client.get(res.location)
assert res.text == "Hello, user!"
Test the account creation#
The prompt=create parameter of the authorization request makes the IAM server show its registration form,
as defined by Initiating User Registration via OpenID Connect 1.0.
The /create page of the application sends this parameter.
def test_account_creation(iam_server, client, test_client):
# 1. the creation page redirects to the IAM server, with prompt=create
res = test_client.get("/create")
# 2. the IAM server redirects to its registration form
res = iam_server.test_client.get(res.location)
res = iam_server.test_client.get(res.location)
# 3. fill the registration form
res = iam_server.test_client.post(
res.location,
data={
"user_name": "new_user",
"given_name": "John",
"family_name": "Doe",
"emails-0": "john@example.org",
"password1": "correct horse battery staple",
"password2": "correct horse battery staple",
},
)
# 4. fill the consent form, the IAM server redirects to the application with a code
res = iam_server.test_client.post(res.location, data={"answer": "accept"})
# 5. the application exchanges the code for a token
res = test_client.get(res.location)
# 6. the protected page is now accessible
res = test_client.get(res.location)
assert res.text == "Hello, new_user!"
user = iam_server.backend.get(iam_server.models.User, user_name="new_user")
iam_server.backend.delete(user)
pytest-iam has no shortcut for the account creation.
Test the logout#
The /logout page of the application redirects to the end_session_endpoint of the IAM server,
as defined by OpenID Connect RP-Initiated Logout 1.0.
It sends the ID token in id_token_hint, the client_id, and a post_logout_redirect_uri registered for the client.
The IAM server needs the client_id to redirect to the post_logout_redirect_uri.
With a valid id_token_hint, the IAM server logs the user out without confirmation.
A user logged in with login() is logged out too:
logged_user becomes None.
def test_logout(iam_server, client, user, test_client):
# log the user in the application
iam_server.login(user)
iam_server.consent(user, client)
res = test_client.get("/protected")
res = iam_server.test_client.get(res.location)
test_client.get(res.location)
# 1. the logout page redirects to the IAM server
res = test_client.get("/logout")
# 2. the IAM server logs the user out, and redirects to the application
res = iam_server.test_client.get(res.location)
assert iam_server.logged_user is None
# 3. the application shows its logged out page
res = test_client.get(res.location)
assert res.text == "Goodbye!"