Rewrite other requires_auth tests to call function

This makes the tests easier to read by avoiding request boilerplate
and making a clearer link between the name of the test and that we
are actually testing that specific function.
This commit is contained in:
Ben Thorner
2021-07-29 11:18:43 +01:00
parent 4fc820c0cc
commit 193e7e0004

View File

@@ -1,10 +1,9 @@
import time import time
import uuid import uuid
from unittest.mock import call
import jwt import jwt
import pytest import pytest
from flask import current_app, json, request from flask import current_app, request
from notifications_python_client.authentication import create_jwt_token from notifications_python_client.authentication import create_jwt_token
from app import api_user from app import api_user
@@ -14,6 +13,7 @@ from app.authentication.auth import (
_decode_jwt_token, _decode_jwt_token,
_get_auth_token, _get_auth_token,
_get_token_issuer, _get_token_issuer,
requires_auth,
) )
from app.dao.api_key_dao import ( from app.dao.api_key_dao import (
expire_api_key, expire_api_key,
@@ -240,13 +240,11 @@ def test_requires_auth_should_not_allow_service_id_with_the_wrong_data_type(
client_id=service_id, client_id=service_id,
secret=service_jwt_secret, secret=service_jwt_secret,
) )
response = client.get(
'/notifications', request.headers = {'Authorization': "Bearer {}".format(token)}
headers={'Authorization': "Bearer {}".format(token)} with pytest.raises(AuthError) as exc:
) requires_auth()
assert response.status_code == 403 assert exc.value.short_message == 'Invalid token: service id is not the right data type'
data = json.loads(response.get_data())
assert data['message'] == {"token": ['Invalid token: service id is not the right data type']}
def test_requires_auth_returns_error_when_service_doesnt_exist( def test_requires_auth_returns_error_when_service_doesnt_exist(
@@ -256,15 +254,13 @@ def test_requires_auth_returns_error_when_service_doesnt_exist(
# get service ID and secret the wrong way around # get service ID and secret the wrong way around
token = create_jwt_token( token = create_jwt_token(
secret=str(sample_api_key.service_id), secret=str(sample_api_key.service_id),
client_id=str(sample_api_key.id)) client_id=str(sample_api_key.id),
response = client.get(
'/notifications',
headers={'Authorization': 'Bearer {}'.format(token)}
) )
assert response.status_code == 403
error_message = json.loads(response.get_data()) request.headers = {'Authorization': 'Bearer {}'.format(token)}
assert error_message['message'] == {'token': ['Invalid token: service not found']} with pytest.raises(AuthError) as exc:
requires_auth()
assert exc.value.short_message == 'Invalid token: service not found'
def test_requires_auth_returns_error_when_service_inactive( def test_requires_auth_returns_error_when_service_inactive(
@@ -273,26 +269,21 @@ def test_requires_auth_returns_error_when_service_inactive(
service_jwt_token, service_jwt_token,
): ):
sample_api_key.service.active = False sample_api_key.service.active = False
response = client.get('/notifications', headers={'Authorization': 'Bearer {}'.format(service_jwt_token)})
assert response.status_code == 403 request.headers = {'Authorization': 'Bearer {}'.format(service_jwt_token)}
error_message = json.loads(response.get_data()) with pytest.raises(AuthError) as exc:
assert error_message['message'] == {'token': ['Invalid token: service is archived']} requires_auth()
assert exc.value.short_message == 'Invalid token: service is archived'
def test_should_attach_the_current_api_key_to_current_app( def test_requires_auth_should_attach_the_current_api_key_to_current_app(
notify_api, client,
sample_service,
sample_api_key, sample_api_key,
service_jwt_token, service_jwt_token,
): ):
with notify_api.test_request_context(), notify_api.test_client() as client: request.headers = {'Authorization': 'Bearer {}'.format(service_jwt_token)}
response = client.get( requires_auth()
'/notifications', assert str(api_user.id) == str(sample_api_key.id)
headers={'Authorization': 'Bearer {}'.format(service_jwt_token)}
)
assert response.status_code == 200
assert str(api_user.id) == str(sample_api_key.id)
@pytest.mark.parametrize('check_proxy_header,header_value,expected_status', [ @pytest.mark.parametrize('check_proxy_header,header_value,expected_status', [
@@ -328,7 +319,6 @@ def test_requires_admin_auth_proxy_key(
def test_requires_auth_should_cache_service_and_api_key_lookups( def test_requires_auth_should_cache_service_and_api_key_lookups(
mocker, mocker,
client, client,
sample_api_key,
service_jwt_token service_jwt_token
): ):
mock_get_api_keys = mocker.patch( mock_get_api_keys = mocker.patch(
@@ -340,14 +330,9 @@ def test_requires_auth_should_cache_service_and_api_key_lookups(
wraps=dao_fetch_service_by_id, wraps=dao_fetch_service_by_id,
) )
for _ in range(5): request.headers = {'Authorization': f'Bearer {service_jwt_token}'}
client.get('/notifications', headers={ requires_auth()
'Authorization': f'Bearer {service_jwt_token}' requires_auth() # second request
})
assert mock_get_api_keys.call_args_list == [ mock_get_api_keys.assert_called_once()
call(str(sample_api_key.service_id)) mock_get_service.assert_called_once()
]
assert mock_get_service.call_args_list == [
call(sample_api_key.service_id)
]