import flask from atlas_um.tokens.bearer_tokens import M2MBearerToken from tests.atlas_um import factories class TestToken: def test_success(self, app, mocker, faker, pgdb_session): fake_token = faker.pystr() patched_encode = mocker.patch.object(M2MBearerToken, "encode") patched_encode.return_value = fake_token application = factories.ApplicationFactory.build() pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: res = client.post( flask.url_for("oauth.token"), data={ "audience": application.token_audience, "grant_type": "client_credentials", "client_id": application.client_id, "client_secret": application.client_secret, }, ) assert res.json == { "access_token": fake_token, "token_type": "Bearer", "expires_in": application.ttl, } def test_failure_params(self, app, pgdb_session): with app.test_client() as client: res = client.post(flask.url_for("oauth.token"), data={}) assert res.status_code == 422 def test_failure_no_app(self, app, faker, pgdb_session): with app.test_client() as client: res = client.post( flask.url_for("oauth.token"), data={ "audience": faker.pystr(), "grant_type": "client_credentials", "client_id": faker.pystr(), "client_secret": faker.pystr(), }, ) assert res.status_code == 401 def test_failure_deleted_app(self, app, mocker, faker, pgdb_session): fake_token = faker.pystr() patched_encode = mocker.patch.object(M2MBearerToken, "encode") patched_encode.return_value = fake_token application = factories.ApplicationFactory.build(is_deleted=True) pgdb_session.add(application) pgdb_session.commit() with app.test_client() as client: res = client.post( flask.url_for("oauth.token"), data={ "audience": application.token_audience, "grant_type": "client_credentials", "client_id": application.client_id, "client_secret": application.client_secret, }, ) assert res.status_code == 401