mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 18:01:08 -05:00
add new key_type table
* single-column static data table that currently contains two types: 'normal' and 'team' * key_type foreign-keyed from api_keys - must be not null - existing rows set to 'normal' * key_type foreign-keyed from notifications - nullable - existing rows set to null * api_key foreign-keyed from notifications - nullable - existing rows set to null
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import uuid
|
||||
from flask import current_app
|
||||
from notifications_python_client.authentication import create_jwt_token
|
||||
from app.models import ApiKey
|
||||
from app.models import ApiKey, KEY_TYPE_NORMAL
|
||||
from app.dao.api_key_dao import (get_unsigned_secrets, save_model_api_key)
|
||||
from app.dao.services_dao import dao_fetch_service_by_id
|
||||
|
||||
@@ -14,7 +14,12 @@ def create_authorization_header(service_id=None):
|
||||
secret = secrets[0]
|
||||
else:
|
||||
service = dao_fetch_service_by_id(service_id)
|
||||
data = {'service': service, 'name': uuid.uuid4(), 'created_by': service.created_by}
|
||||
data = {
|
||||
'service': service,
|
||||
'name': uuid.uuid4(),
|
||||
'created_by': service.created_by,
|
||||
'key_type': KEY_TYPE_NORMAL
|
||||
}
|
||||
api_key = ApiKey(**data)
|
||||
save_model_api_key(api_key)
|
||||
secret = get_unsigned_secrets(service_id)[0]
|
||||
|
||||
@@ -3,7 +3,7 @@ from datetime import datetime, timedelta
|
||||
from notifications_python_client.authentication import create_jwt_token
|
||||
from flask import json, current_app
|
||||
from app.dao.api_key_dao import get_unsigned_secrets, save_model_api_key, get_unsigned_secret, expire_api_key
|
||||
from app.models import ApiKey
|
||||
from app.models import ApiKey, KEY_TYPE_NORMAL
|
||||
|
||||
|
||||
def test_should_not_allow_request_with_no_token(notify_api):
|
||||
@@ -78,7 +78,8 @@ def test_should_allow_valid_token_when_service_has_multiple_keys(notify_api, sam
|
||||
with notify_api.test_client() as client:
|
||||
data = {'service': sample_api_key.service,
|
||||
'name': 'some key name',
|
||||
'created_by': sample_api_key.created_by
|
||||
'created_by': sample_api_key.created_by,
|
||||
'key_type': KEY_TYPE_NORMAL
|
||||
}
|
||||
api_key = ApiKey(**data)
|
||||
save_model_api_key(api_key)
|
||||
@@ -121,13 +122,15 @@ def test_authentication_passes_when_service_has_multiple_keys_some_expired(
|
||||
expired_key_data = {'service': sample_api_key.service,
|
||||
'name': 'expired_key',
|
||||
'expiry_date': datetime.utcnow(),
|
||||
'created_by': sample_api_key.created_by
|
||||
'created_by': sample_api_key.created_by,
|
||||
'key_type': KEY_TYPE_NORMAL
|
||||
}
|
||||
expired_key = ApiKey(**expired_key_data)
|
||||
save_model_api_key(expired_key)
|
||||
another_key = {'service': sample_api_key.service,
|
||||
'name': 'another_key',
|
||||
'created_by': sample_api_key.created_by
|
||||
'created_by': sample_api_key.created_by,
|
||||
'key_type': KEY_TYPE_NORMAL
|
||||
}
|
||||
api_key = ApiKey(**another_key)
|
||||
save_model_api_key(api_key)
|
||||
@@ -148,13 +151,15 @@ def test_authentication_returns_token_expired_when_service_uses_expired_key_and_
|
||||
with notify_api.test_client() as client:
|
||||
expired_key = {'service': sample_api_key.service,
|
||||
'name': 'expired_key',
|
||||
'created_by': sample_api_key.created_by
|
||||
'created_by': sample_api_key.created_by,
|
||||
'key_type': KEY_TYPE_NORMAL
|
||||
}
|
||||
expired_api_key = ApiKey(**expired_key)
|
||||
save_model_api_key(expired_api_key)
|
||||
another_key = {'service': sample_api_key.service,
|
||||
'name': 'another_key',
|
||||
'created_by': sample_api_key.created_by
|
||||
'created_by': sample_api_key.created_by,
|
||||
'key_type': KEY_TYPE_NORMAL
|
||||
}
|
||||
api_key = ApiKey(**another_key)
|
||||
save_model_api_key(api_key)
|
||||
|
||||
@@ -18,7 +18,8 @@ from app.models import (
|
||||
Permission,
|
||||
ProviderStatistics,
|
||||
ProviderDetails,
|
||||
NotificationStatistics)
|
||||
NotificationStatistics,
|
||||
KEY_TYPE_NORMAL)
|
||||
from app.dao.users_dao import (save_model_user, create_user_code, create_secret_code)
|
||||
from app.dao.services_dao import (dao_create_service, dao_add_user_to_service)
|
||||
from app.dao.templates_dao import dao_create_template
|
||||
@@ -229,10 +230,11 @@ def sample_email_template_with_placeholders(notify_db, notify_db_session):
|
||||
@pytest.fixture(scope='function')
|
||||
def sample_api_key(notify_db,
|
||||
notify_db_session,
|
||||
service=None):
|
||||
service=None,
|
||||
key_type=KEY_TYPE_NORMAL):
|
||||
if service is None:
|
||||
service = sample_service(notify_db, notify_db_session)
|
||||
data = {'service': service, 'name': uuid.uuid4(), 'created_by': service.created_by}
|
||||
data = {'service': service, 'name': uuid.uuid4(), 'created_by': service.created_by, 'key_type': key_type}
|
||||
api_key = ApiKey(**data)
|
||||
save_model_api_key(api_key)
|
||||
return api_key
|
||||
|
||||
@@ -9,7 +9,7 @@ from app.dao.api_key_dao import (save_model_api_key,
|
||||
get_unsigned_secret,
|
||||
_generate_secret,
|
||||
_get_secret, expire_api_key)
|
||||
from app.models import ApiKey
|
||||
from app.models import ApiKey, KEY_TYPE_NORMAL
|
||||
|
||||
|
||||
def test_secret_is_signed_and_can_be_read_again(notify_api, mocker):
|
||||
@@ -26,7 +26,8 @@ def test_save_api_key_should_create_new_api_key_and_history(notify_api,
|
||||
sample_service):
|
||||
api_key = ApiKey(**{'service': sample_service,
|
||||
'name': sample_service.name,
|
||||
'created_by': sample_service.created_by})
|
||||
'created_by': sample_service.created_by,
|
||||
'key_type': KEY_TYPE_NORMAL})
|
||||
save_model_api_key(api_key)
|
||||
|
||||
all_api_keys = get_model_api_keys(service_id=sample_service.id)
|
||||
@@ -105,7 +106,8 @@ def test_should_not_allow_duplicate_key_names_per_service(notify_api,
|
||||
api_key = ApiKey(**{'id': fake_uuid,
|
||||
'service': sample_api_key.service,
|
||||
'name': sample_api_key.name,
|
||||
'created_by': sample_api_key.created_by})
|
||||
'created_by': sample_api_key.created_by,
|
||||
'key_type': KEY_TYPE_NORMAL})
|
||||
try:
|
||||
save_model_api_key(api_key)
|
||||
fail("should throw IntegrityError")
|
||||
@@ -122,7 +124,8 @@ def test_save_api_key_should_not_create_new_service_history(notify_api, notify_d
|
||||
|
||||
api_key = ApiKey(**{'service': sample_service,
|
||||
'name': sample_service.name,
|
||||
'created_by': sample_service.created_by})
|
||||
'created_by': sample_service.created_by,
|
||||
'key_type': KEY_TYPE_NORMAL})
|
||||
save_model_api_key(api_key)
|
||||
|
||||
assert Service.get_history_model().query.count() == 1
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import json
|
||||
from datetime import timedelta, datetime
|
||||
from datetime import datetime
|
||||
|
||||
from flask import url_for
|
||||
from app.models import ApiKey
|
||||
from app.dao.api_key_dao import save_model_api_key, expire_api_key
|
||||
from app.models import ApiKey, KEY_TYPE_NORMAL
|
||||
from app.dao.api_key_dao import expire_api_key
|
||||
from tests import create_authorization_header
|
||||
from tests.app.conftest import sample_api_key as create_sample_api_key
|
||||
from tests.app.conftest import sample_service as create_sample_service
|
||||
@@ -15,13 +15,17 @@ def test_api_key_should_create_new_api_key_for_service(notify_api, notify_db,
|
||||
sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
data = {'name': 'some secret name', 'created_by': str(sample_service.created_by.id)}
|
||||
data = {
|
||||
'name': 'some secret name',
|
||||
'created_by': str(sample_service.created_by.id),
|
||||
'key_type': KEY_TYPE_NORMAL
|
||||
}
|
||||
auth_header = create_authorization_header()
|
||||
response = client.post(url_for('service.create_api_key', service_id=sample_service.id),
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
assert response.status_code == 201
|
||||
assert response.get_data is not None
|
||||
assert 'data' in json.loads(response.get_data(as_text=True))
|
||||
saved_api_key = ApiKey.query.filter_by(service_id=sample_service.id).first()
|
||||
assert saved_api_key.service_id == sample_service.id
|
||||
assert saved_api_key.name == 'some secret name'
|
||||
@@ -60,20 +64,25 @@ def test_api_key_should_create_multiple_new_api_key_for_service(notify_api, noti
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
assert ApiKey.query.count() == 0
|
||||
data = {'name': 'some secret name', 'created_by': str(sample_service.created_by.id)}
|
||||
data = {
|
||||
'name': 'some secret name',
|
||||
'created_by': str(sample_service.created_by.id),
|
||||
'key_type': KEY_TYPE_NORMAL
|
||||
}
|
||||
auth_header = create_authorization_header()
|
||||
response = client.post(url_for('service.create_api_key', service_id=sample_service.id),
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
assert response.status_code == 201
|
||||
assert ApiKey.query.count() == 1
|
||||
data = {'name': 'another secret name', 'created_by': str(sample_service.created_by.id)}
|
||||
|
||||
data['name'] = 'another secret name'
|
||||
auth_header = create_authorization_header()
|
||||
response2 = client.post(url_for('service.create_api_key', service_id=sample_service.id),
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
assert response2.status_code == 201
|
||||
assert response2.get_data != response.get_data
|
||||
assert json.loads(response.get_data(as_text=True)) != json.loads(response2.get_data(as_text=True))
|
||||
assert ApiKey.query.count() == 2
|
||||
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ def notify_db_session(request, notify_db):
|
||||
def teardown():
|
||||
notify_db.session.remove()
|
||||
for tbl in reversed(notify_db.metadata.sorted_tables):
|
||||
if tbl.name not in ["provider_details"]:
|
||||
if tbl.name not in ["provider_details", "key_types"]:
|
||||
notify_db.engine.execute(tbl.delete())
|
||||
notify_db.session.commit()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user