Giter Club home page Giter Club logo

pytest-iam's Introduction

pytest-iam

pytest-iam spawns a lightweight OAuth2 / OpenID Server (OIDC) 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 tools to test your application authentication mechanism against a OAuth2/OIDC server:

  • It launches a Canaille instance
  • It provides a iam_server fixture that comes with several features:
    • the URL of the IAM server to configure your application
    • IAM models (Users, groups, clients, tokens etc.) to prepare your tests and check the side effects. More details on the reference
    • utilities to log-in users and give their consent to your application
    • utilities to generate random users and groups

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

def test_authentication(iam_server, testapp, client):
    # create a random user on the IAM server
    user = iam_server.random_user()

    # logs the user in give its consent to your application
    iam_server.login(user)
    iam_server.consent(user)

    # simulate an attempt to access a protected page of your app
    response = testapp.get("/protected", status=302)

    # get an 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!")

Check the client application or resource server tutorials for more usecases.

pytest-iam's People

Contributors

azmeuk avatar

Stargazers

George Kontridze avatar Laurent Paoletti avatar Michael Oliver avatar Benjamin avatar Andreas Motl avatar Nikolaus Schlemm avatar  avatar

Watchers

Pierre Bousquié avatar Colin Garriga-Salaün avatar  avatar  avatar Bruno Voisin avatar Camille avatar Loan Robert avatar  avatar

pytest-iam's Issues

pytest-iam reads local `.env` files

If a .env exists, with flask vars filled such as SERVER_NAME, then the values are read by the Canaille instance, leading it to be incorrectly configured. In the end the unit tests fail.
A solution to this could be to disable .env in Canaille.

Unit tests for authentication errors

Write some unit test, and document how to generate authentication errors.

https://openid.net/specs/openid-connect-core-1_0.html#AuthError
https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2.1

OAuth2 errors:

  • invalid_request
  • unauthorized_client
  • access_denied
  • unsupported_response_type
  • invalid_scope
  • server_error
  • temporarily_unavailable

OIDC errors:

  • interaction_required
  • login_required
  • account_selection_required
  • consent_required
  • invalid_request_uri
  • invalid_request_object
  • request_not_supported
  • request_uri_not_supported
  • registration_not_supported

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.