import pytest from flask import url_for from atlas_um import consts from atlas_um.tokens.bearer_tokens import DNABearerToken from atlas_um.pgdb import pgdb from tests.atlas_um.factories import DNAAccountFactory class TestHome: def test_anonymous(self, app): with app.test_request_context(): with app.test_client() as client: res = client.get(url_for("common.home")) assert res.status_code == 302 assert res.location == url_for("common.userinfo") def test_authenticated_no_claims(self, app, authenticated): with app.test_request_context(): with app.test_client() as client: res = client.get(url_for("common.home")) assert res.status_code == 302 assert res.location == url_for("common.userinfo") def test_authenticated_admin(self, app, authorized_admin): with app.test_request_context(): with app.test_client() as client: res = client.get(url_for("common.home")) assert res.status_code == 302 assert res.location == url_for("dna_accounts.list") def test_authenticated_developer(self, app, authorized_developer): with app.test_request_context(): with app.test_client() as client: res = client.get(url_for("common.home")) assert res.status_code == 302 assert res.location == url_for("applications.list") def test_authenticated_manager(self, app, authorized_manager): with app.test_request_context(): with app.test_client() as client: res = client.get(url_for("common.home")) assert res.status_code == 302 assert res.location == url_for("dna_accounts.list") class TestUserinfo: FAKE_HOST = "atlas_um.test" @pytest.fixture def app_config(self): def inner(app): app.config["SERVER_NAME"] = self.FAKE_HOST return inner @pytest.fixture def client(self, app): return app.test_client() def test_userinfo_no_cookies(self, app, client, pgdb_session): with app.app_context(): req = url_for("common.userinfo") resp = client.get(req) assert resp.status_code == 200 def test_userinfo_usm_id_token_cookie(self, app, client, pgdb_session): fake_id_token = ( "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqYXNvbi53aGl0dGxl" "LnNtZUBzb255bXVzaWMuY29tIiwiYXVkIjoiUzNnaktuSTBVbTQxYUVkNVVWZG9hM" "mh0U2xjIiwiYXV0aF90aW1lIjoxNTk3OTQ0OTI5LCJpc3MiOiJodHRwczovL3VhdC" "11c20uc21lYW5hbHl0aWNzcG9ydGFsLmNvbS9vYXV0aDIvb3BlbmlkIiwicHJlZmV" "ycmVkX3VzZXJuYW1lIjoiamFzb24ud2hpdHRsZS5zbWVAc29ueW11c2ljLmNvbSIs" "ImdpdmVuX25hbWUiOiJKYXNvbiIsImV4cCI6MTU5Nzk0NjEyOSwibm9uY2UiOiJaY" "kRhSkxwUyIsImZhbWlseV9uYW1lIjoiV2hpdHRsZSIsImlhdCI6MTU5Nzk0NDkyOS" "wiZW1haWwiOiJqYXNvbi53aGl0dGxlLnNtZUBzb255bXVzaWMuY29tIiwiY2lkIjo" "iUzNnaktuSTBVbTQxYUVkNVVWZG9hMmh0U2xjIn0=.77+977+9A8uFfu+/vRvvv70" "S77+9UO+/vSx777+9aFsJ77+9Q39677+977+977+977+936fvv73vv70=" ) client.set_cookie( consts.USM_ID_TOKEN_COOKIE_NAME, fake_id_token, domain=self.FAKE_HOST, httponly=True, ) with app.app_context(): req = url_for("common.userinfo") resp = client.get(req) assert resp.status_code == 200 def test_userinfo_dna_bearer_token_cookie(self, app, client, pgdb_session): dna_account = DNAAccountFactory.build() with app.app_context(): pgdb.session.add(dna_account) fake_bearer_token = DNABearerToken(dna_account).encode() req = url_for("common.userinfo") client.set_cookie( app.config.get("DNA_BEARER_TOKEN_COOKIE_NAME"), fake_bearer_token, domain=self.FAKE_HOST, httponly=True, ) resp = client.get(req) assert resp.status_code == 200 class TestHealthcheck: def test_healthcheck(self, app, pgdb_session, redis_session): """ Always return 200 OK with an empty body. """ with app.test_client() as client: resp = client.get(url_for("common.health")) assert resp.status_code == 200 assert resp.data == b""