"""Test auth.""" import pytest import application from oto import response from oto.adaptors.flask import flaskify from owsrequest import context from owsrequest.context import RequestContext from pytest_mock import MockerFixture from product_digital.auth import only_for_identity ben_identity = "10436b38-5e11-472d-b6a4-bf1ee2b1b438" jess_identity = "7f418dad-780b-4ab3-a4a2-2deba190f503" @pytest.mark.parametrize( "allowed_identity, jwt_identity, expected_status_code", [ pytest.param( ben_identity, ben_identity, 200, id="allowed identity and jwt identity are one and the same, so authorized" ), pytest.param( ben_identity, jess_identity, 403, id="jwt identity is not the same as the allowed identity, so not authorized" ), pytest.param( [ben_identity], jess_identity, 403, id="jwt identity is not in the list of allowed identity, so not authorized" ), pytest.param( [ben_identity, jess_identity], jess_identity, 200, id="jwt identity is in the list of allowed identity, so authorized" ), ] ) def test_only_for_identity_success( allowed_identity: str | list[str], jwt_identity: str, expected_status_code: int, mocker: MockerFixture, ) -> None: """Test for identity success. Tests that if the request context has the correct identity, we return the decorated function result. """ mock_context = mocker.MagicMock(spec=RequestContext) mock_context.jwt_identity_id = jwt_identity success = response.Response(status=200) @only_for_identity(allowed_identity) def to_be_decorated(): return flaskify(success) mocker.patch.object( context, "get_request_context_from_headers", return_value=mock_context, ) with application.app.test_request_context(): result = to_be_decorated() assert result assert result.status_code == expected_status_code