mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
Refactor the api_key_dao.
The only update we should be doing to an api key is to expire/revoke the api key. Removed the update_dict from the the save method. Added an expire_api_key method that only updates the api key with an expiry date.
This commit is contained in:
@@ -8,17 +8,16 @@ from app.dao.api_key_dao import (save_model_api_key,
|
||||
get_unsigned_secrets,
|
||||
get_unsigned_secret,
|
||||
_generate_secret,
|
||||
_get_secret)
|
||||
_get_secret, expire_api_key)
|
||||
from app.models import ApiKey
|
||||
|
||||
|
||||
def test_secret_is_signed_and_can_be_read_again(notify_api):
|
||||
import uuid
|
||||
def test_secret_is_signed_and_can_be_read_again(notify_api, mocker):
|
||||
with notify_api.test_request_context():
|
||||
token = str(uuid.uuid4())
|
||||
signed_secret = _generate_secret(token=token)
|
||||
assert token == _get_secret(signed_secret)
|
||||
assert signed_secret != token
|
||||
mocker.patch("uuid.uuid4", return_value='some_uuid')
|
||||
signed_secret = _generate_secret()
|
||||
assert 'some_uuid' == _get_secret(signed_secret)
|
||||
assert signed_secret != 'some_uuid'
|
||||
|
||||
|
||||
def test_save_api_key_should_create_new_api_key_and_history(notify_api,
|
||||
@@ -45,20 +44,18 @@ def test_expire_api_key_should_update_the_api_key_and_create_history_record(noti
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_api_key):
|
||||
now = datetime.utcnow()
|
||||
saved_api_key = get_model_api_keys(service_id=sample_api_key.service_id, id=sample_api_key.id)
|
||||
save_model_api_key(saved_api_key, update_dict={'id': saved_api_key.id, 'expiry_date': now})
|
||||
expire_api_key(service_id=sample_api_key.service_id, api_key_id=sample_api_key.id)
|
||||
all_api_keys = get_model_api_keys(service_id=sample_api_key.service_id)
|
||||
assert len(all_api_keys) == 1
|
||||
assert all_api_keys[0].expiry_date == now
|
||||
assert all_api_keys[0].secret == saved_api_key.secret
|
||||
assert all_api_keys[0].id == saved_api_key.id
|
||||
assert all_api_keys[0].service_id == saved_api_key.service_id
|
||||
assert all_api_keys[0].expiry_date <= datetime.utcnow()
|
||||
assert all_api_keys[0].secret == sample_api_key.secret
|
||||
assert all_api_keys[0].id == sample_api_key.id
|
||||
assert all_api_keys[0].service_id == sample_api_key.service_id
|
||||
|
||||
all_history = saved_api_key.get_history_model().query.all()
|
||||
all_history = sample_api_key.get_history_model().query.all()
|
||||
assert len(all_history) == 2
|
||||
assert all_history[0].id == saved_api_key.id
|
||||
assert all_history[1].id == saved_api_key.id
|
||||
assert all_history[0].id == sample_api_key.id
|
||||
assert all_history[1].id == sample_api_key.id
|
||||
sorted_all_history = sorted(all_history, key=lambda hist: hist.version)
|
||||
sorted_all_history[0].version = 1
|
||||
sorted_all_history[1].version = 2
|
||||
|
||||
Reference in New Issue
Block a user