Skip to main content

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!")

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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file pytest_iam-0.0.2.tar.gz.

File metadata

  • Download URL: pytest_iam-0.0.2.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.3 Linux/6.4.11-arch2-1

File hashes

Hashes for pytest_iam-0.0.2.tar.gz
Algorithm Hash digest
SHA256 6934c2135e9c65e4b6b862b797d1796661ffac7bfd7e37f693f63ea024177670
MD5 a618d986ba6a3f7f889027507677152b
BLAKE2b-256 4cbdbbbc6dae2d03d1f70cf03c0ed3227e9a87d3632cb1a30a2ccb9d29c06a97

See more details on using hashes here.

File details

Details for the file pytest_iam-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pytest_iam-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.3 Linux/6.4.11-arch2-1

File hashes

Hashes for pytest_iam-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 57c3600fc9fe34f0399a9e0212e8af669dfdaadff80d2114deab353ad064dbb3
MD5 7ffeac5816989c9d04e6d0d9986676a4
BLAKE2b-256 0bf525e44dad519b0d713d018cc58ff4fac67843677dca0c652852030c790374

See more details on using hashes here.

Release history Release notifications | RSS feed

0.2.6

2 files

0.2.5

2 files

0.2.4

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.1

2 files

0.1.0

2 files

0.0.12

2 files

0.0.11

2 files

0.0.10

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

This release

0.0.2 This release

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page