2016-01-19 12:07:00 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
2016-06-24 16:10:25 +01:00
|
|
|
import pytest
|
|
|
|
|
from sqlalchemy.exc import IntegrityError
|
2016-01-19 12:07:00 +00:00
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
|
|
|
|
|
2016-06-29 14:15:32 +01:00
|
|
|
from app.authentication.utils import get_secret
|
2016-01-19 12:07:00 +00:00
|
|
|
from app.dao.api_key_dao import (save_model_api_key,
|
|
|
|
|
get_model_api_keys,
|
2016-01-19 18:25:21 +00:00
|
|
|
get_unsigned_secrets,
|
2016-01-19 12:07:00 +00:00
|
|
|
get_unsigned_secret,
|
2016-06-29 14:15:32 +01:00
|
|
|
expire_api_key)
|
2016-06-23 16:45:20 +01:00
|
|
|
from app.models import ApiKey, KEY_TYPE_NORMAL
|
2016-01-19 12:07:00 +00:00
|
|
|
|
|
|
|
|
|
2016-06-24 16:10:25 +01:00
|
|
|
def test_save_api_key_should_create_new_api_key_and_history(sample_service):
|
2016-04-20 17:25:20 +01:00
|
|
|
api_key = ApiKey(**{'service': sample_service,
|
|
|
|
|
'name': sample_service.name,
|
2016-06-23 16:45:20 +01:00
|
|
|
'created_by': sample_service.created_by,
|
|
|
|
|
'key_type': KEY_TYPE_NORMAL})
|
2016-01-19 12:07:00 +00:00
|
|
|
save_model_api_key(api_key)
|
|
|
|
|
|
2016-01-20 14:48:44 +00:00
|
|
|
all_api_keys = get_model_api_keys(service_id=sample_service.id)
|
2016-01-19 12:07:00 +00:00
|
|
|
assert len(all_api_keys) == 1
|
|
|
|
|
assert all_api_keys[0] == api_key
|
2016-04-20 17:25:20 +01:00
|
|
|
assert api_key.version == 1
|
2016-01-19 12:07:00 +00:00
|
|
|
|
2016-04-20 17:25:20 +01:00
|
|
|
all_history = api_key.get_history_model().query.all()
|
|
|
|
|
assert len(all_history) == 1
|
|
|
|
|
assert all_history[0].id == api_key.id
|
|
|
|
|
assert all_history[0].version == api_key.version
|
2016-01-19 12:07:00 +00:00
|
|
|
|
2016-04-20 17:25:20 +01:00
|
|
|
|
|
|
|
|
def test_expire_api_key_should_update_the_api_key_and_create_history_record(notify_api,
|
|
|
|
|
sample_api_key):
|
2016-06-22 15:27:28 +01:00
|
|
|
expire_api_key(service_id=sample_api_key.service_id, api_key_id=sample_api_key.id)
|
2016-01-20 14:48:44 +00:00
|
|
|
all_api_keys = get_model_api_keys(service_id=sample_api_key.service_id)
|
2016-01-19 12:07:00 +00:00
|
|
|
assert len(all_api_keys) == 1
|
2016-06-22 15:27:28 +01:00
|
|
|
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
|
2016-01-19 12:07:00 +00:00
|
|
|
|
2016-06-22 15:27:28 +01:00
|
|
|
all_history = sample_api_key.get_history_model().query.all()
|
2016-04-20 17:25:20 +01:00
|
|
|
assert len(all_history) == 2
|
2016-06-22 15:27:28 +01:00
|
|
|
assert all_history[0].id == sample_api_key.id
|
|
|
|
|
assert all_history[1].id == sample_api_key.id
|
2016-04-20 17:25:20 +01:00
|
|
|
sorted_all_history = sorted(all_history, key=lambda hist: hist.version)
|
|
|
|
|
sorted_all_history[0].version = 1
|
|
|
|
|
sorted_all_history[1].version = 2
|
|
|
|
|
|
2016-01-19 12:07:00 +00:00
|
|
|
|
2016-06-24 16:10:25 +01:00
|
|
|
def test_get_api_key_should_raise_exception_when_api_key_does_not_exist(sample_service, fake_uuid):
|
|
|
|
|
with pytest.raises(NoResultFound):
|
2016-04-08 13:34:46 +01:00
|
|
|
get_model_api_keys(sample_service.id, id=fake_uuid)
|
2016-01-19 12:07:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_return_api_key_for_service(notify_api, notify_db, notify_db_session, sample_api_key):
|
2016-01-20 14:48:44 +00:00
|
|
|
api_key = get_model_api_keys(service_id=sample_api_key.service_id, id=sample_api_key.id)
|
2016-01-19 12:07:00 +00:00
|
|
|
assert api_key == sample_api_key
|
|
|
|
|
|
|
|
|
|
|
2016-06-24 16:10:25 +01:00
|
|
|
def test_should_return_unsigned_api_keys_for_service_id(sample_api_key):
|
2016-01-19 18:25:21 +00:00
|
|
|
unsigned_api_key = get_unsigned_secrets(sample_api_key.service_id)
|
|
|
|
|
assert len(unsigned_api_key) == 1
|
|
|
|
|
assert sample_api_key.secret != unsigned_api_key[0]
|
2016-06-29 14:15:32 +01:00
|
|
|
assert unsigned_api_key[0] == get_secret(sample_api_key.secret)
|
2016-01-19 18:25:21 +00:00
|
|
|
|
|
|
|
|
|
2016-06-24 16:10:25 +01:00
|
|
|
def test_get_unsigned_secret_returns_key(sample_api_key):
|
2016-01-19 18:25:21 +00:00
|
|
|
unsigned_api_key = get_unsigned_secret(sample_api_key.id)
|
2016-01-19 12:07:00 +00:00
|
|
|
assert sample_api_key.secret != unsigned_api_key
|
2016-06-29 14:15:32 +01:00
|
|
|
assert unsigned_api_key == get_secret(sample_api_key.secret)
|
2016-01-21 16:53:53 +00:00
|
|
|
|
|
|
|
|
|
2016-06-24 16:10:25 +01:00
|
|
|
def test_should_not_allow_duplicate_key_names_per_service(sample_api_key, fake_uuid):
|
2016-04-20 17:25:20 +01:00
|
|
|
api_key = ApiKey(**{'id': fake_uuid,
|
|
|
|
|
'service': sample_api_key.service,
|
|
|
|
|
'name': sample_api_key.name,
|
2016-06-23 16:45:20 +01:00
|
|
|
'created_by': sample_api_key.created_by,
|
|
|
|
|
'key_type': KEY_TYPE_NORMAL})
|
2016-06-24 16:10:25 +01:00
|
|
|
with pytest.raises(IntegrityError):
|
2016-01-21 16:53:53 +00:00
|
|
|
save_model_api_key(api_key)
|
2016-04-21 18:10:57 +01:00
|
|
|
|
|
|
|
|
|
2016-06-24 16:10:25 +01:00
|
|
|
def test_save_api_key_should_not_create_new_service_history(sample_service):
|
2016-04-21 18:10:57 +01:00
|
|
|
from app.models import Service
|
|
|
|
|
|
|
|
|
|
assert Service.query.count() == 1
|
|
|
|
|
assert Service.get_history_model().query.count() == 1
|
|
|
|
|
|
|
|
|
|
api_key = ApiKey(**{'service': sample_service,
|
|
|
|
|
'name': sample_service.name,
|
2016-06-23 16:45:20 +01:00
|
|
|
'created_by': sample_service.created_by,
|
|
|
|
|
'key_type': KEY_TYPE_NORMAL})
|
2016-04-21 18:10:57 +01:00
|
|
|
save_model_api_key(api_key)
|
|
|
|
|
|
|
|
|
|
assert Service.get_history_model().query.count() == 1
|