Merge pull request #2529 from alphagov/add-expiry-date-to-constraint

Allow api_keys names to be reused
This commit is contained in:
Rebecca Law
2019-06-07 13:19:28 +01:00
committed by GitHub
3 changed files with 50 additions and 9 deletions

View File

@@ -4,11 +4,13 @@ import pytest
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import NoResultFound
from app.dao.api_key_dao import (save_model_api_key,
get_model_api_keys,
get_unsigned_secrets,
get_unsigned_secret,
expire_api_key)
from app.dao.api_key_dao import (
save_model_api_key,
get_model_api_keys,
get_unsigned_secrets,
get_unsigned_secret,
expire_api_key
)
from app.models import ApiKey, KEY_TYPE_NORMAL
@@ -82,6 +84,22 @@ def test_should_not_allow_duplicate_key_names_per_service(sample_api_key, fake_u
save_model_api_key(api_key)
def test_save_api_key_can_create_key_with_same_name_if_other_is_expired(sample_service):
expired_api_key = ApiKey(**{'service': sample_service,
'name': "normal api key",
'created_by': sample_service.created_by,
'key_type': KEY_TYPE_NORMAL,
'expiry_date': datetime.utcnow()})
save_model_api_key(expired_api_key)
api_key = ApiKey(**{'service': sample_service,
'name': "normal api key",
'created_by': sample_service.created_by,
'key_type': KEY_TYPE_NORMAL})
save_model_api_key(api_key)
keys = ApiKey.query.all()
assert len(keys) == 2
def test_save_api_key_should_not_create_new_service_history(sample_service):
from app.models import Service
@@ -99,9 +117,9 @@ def test_save_api_key_should_not_create_new_service_history(sample_service):
@pytest.mark.parametrize('days_old, expected_length', [(5, 1), (8, 0)])
def test_should_not_return_revoked_api_keys_older_than_7_days(
sample_service,
days_old,
expected_length
sample_service,
days_old,
expected_length
):
expired_api_key = ApiKey(**{'service': sample_service,
'name': sample_service.name,