Skip to content

Commit

Permalink
Fix rest framework tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervdw committed Mar 20, 2024
1 parent ccf36d9 commit fdd3905
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions nens_auth_client/tests/test_rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,39 @@ def r():


@pytest.fixture
def mocked_authenticator(rf, mocker, rq_mocker, jwks_request, settings):
"""Mock necessary functions to test AccessTokenMiddleware request"""
# Mock the user association call
def mocked_oauth_client(mocker):
get_oauth_client = mocker.patch(
"nens_auth_client.rest_framework.authentication.get_oauth_client"
)
get_oauth_client.return_value.parse_access_token.return_value = {"scope": "foo"}
return get_oauth_client.return_value


@pytest.fixture
def mocked_authenticate(mocker):
authenticate = mocker.patch("django.contrib.auth.authenticate")
authenticate.return_value = UserModel(username="testuser")
# Disable the custom AWS Cognito Access Token mapping
mocker.patch("nens_auth_client.cognito.preprocess_access_token")
# Make a middleware that returns the request as a response
return authenticate


@pytest.fixture
def authenticator():
return OAuth2TokenAuthentication()


def test_authentication_class(r, mocked_authenticator, access_token_generator):
def test_authentication_class(
r, authenticator, access_token_generator, mocked_oauth_client, mocked_authenticate
):
r.META["HTTP_AUTHORIZATION"] = "Bearer " + access_token_generator()
user, auth = mocked_authenticator.authenticate(r)
user, auth = authenticator.authenticate(r)
assert user.username == "testuser"
assert auth.scope == "readwrite"
assert auth.scope == "foo"


def test_authentication_class_no_header(r, mocked_authenticator):
assert mocked_authenticator.authenticate(r) is None
def test_authentication_class_no_header(r, authenticator):
assert authenticator.authenticate(r) is None


def test_authentication_class_no_bearer(
r, mocked_authenticator, access_token_generator
):
def test_authentication_class_no_bearer(r, authenticator, access_token_generator):
r.META["HTTP_AUTHORIZATION"] = "Token xxx"
assert mocked_authenticator.authenticate(r) is None
assert authenticator.authenticate(r) is None

0 comments on commit fdd3905

Please sign in to comment.