Skip to main content

A fully functional OAUTH2 / OpenID Connect (OIDC) server to be used in your testsuite

Project description

pytest-iam

pytest-iam spawns a lightweight OAuth2 / OpenID Server in a thread to be used in your test suite. The machinery involves Canaille and Authlib.

Installation

pip install pytest-iam

Usage

pytest-iam provides a iam_server fixture that comes with several features:

  • iam_server.url returns the temporary server url
  • iam_server.models provides a modules containing different models (User, Group, Client, Token and AuthorizationCode). Read the canaille documentation to find how to handle those models.
  • iam_server.random_user() and iam_server.random_group() can generate random data for your tests

To run a full authentication process in your test, you can write something like this:

@pytest.fixture
def user(iam_server):
    # Creates a user on the identity provider
    user = iam_server.models.User(
        user_name="user",
        emails=["email@example.org"],
        password="password",
    )
    user.save()
    return user

@pytest.fixture
def client(iam_server):
    # Creates a client on the identity provider
    client = iam_server.models.Client(
        client_id="client_id",
        client_secret="client_secret",
        client_name="my super app",
        client_uri="http://example.org",
        redirect_uris=["http://example.org/authorize"],
        grant_types=["authorization_code"],
        response_types=["code", "token", "id_token"],
        token_endpoint_auth_method="client_secret_basic",
        scope=["openid", "profile", "groups"],
    )
    client.save()
    return client

def test_authentication(iam_server, testapp, user, client):
    iam_server.login(user)
    iam_server.consent(user)

    # attempt to access a protected page
    response = testapp.get("/protected", status=302)

    # authorization code request at the IAM
    res = requests.get(res.location, allow_redirects=False)

    # access to the redirection URI
    res = testclient.get(res.headers["Location"])
    res.mustcontain("Hello World!")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_iam-0.0.2.tar.gz (3.9 kB view hashes)

Uploaded Source

Built Distribution

pytest_iam-0.0.2-py3-none-any.whl (4.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page