Change services.id to a UUID

Ideally all the primary keys in the db would be UUID in order to guarantee unique ids across distributed dbs.
This updates the services.id to a UUID. All the tables with a foreign key to the services.id are also updated.
The endpoints no longer state a data type of the <service_id> path param.
All the tests are updated to reflect this update.

The thing to pay attention to is the 0011_uuid_service_id.py migration script.
This commit must go with a commit on the notifications_admin app to keep things working.
There will be a small outage until both deploys have happened.
This commit is contained in:
Rebecca Law
2016-02-02 14:16:08 +00:00
parent 1277837e00
commit b5c662eca8
15 changed files with 195 additions and 63 deletions

View File

@@ -41,7 +41,7 @@ def test_should_not_allow_incorrect_path(notify_api, notify_db, notify_db_sessio
token = create_jwt_token(request_method="GET",
request_path="/bad",
secret=get_unsigned_secrets(sample_api_key.service_id)[0],
client_id=sample_api_key.service_id)
client_id=str(sample_api_key.service_id))
response = client.get(url_for('service.get_service'),
headers={'Authorization': "Bearer {}".format(token)})
assert response.status_code == 403
@@ -64,7 +64,7 @@ def test_should_not_allow_invalid_secret(notify_api, notify_db, notify_db_sessio
with notify_api.test_request_context():
with notify_api.test_client() as client:
token = create_jwt_token(request_method="POST", request_path="/", secret="not-so-secret",
client_id=sample_api_key.service_id)
client_id=str(sample_api_key.service_id))
response = client.get(url_for('service.get_service'),
headers={'Authorization': "Bearer {}".format(token)})
assert response.status_code == 403
@@ -76,7 +76,7 @@ def test_should_allow_valid_token(notify_api, notify_db, notify_db_session, samp
with notify_api.test_request_context():
with notify_api.test_client() as client:
token = __create_get_token(sample_api_key.service_id)
response = client.get(url_for('service.get_service', service_id=sample_api_key.service_id),
response = client.get(url_for('service.get_service', service_id=str(sample_api_key.service_id)),
headers={'Authorization': 'Bearer {}'.format(token)})
assert response.status_code == 200
@@ -86,7 +86,7 @@ def test_should_allow_valid_token_for_request_with_path_params(notify_api, notif
with notify_api.test_request_context():
with notify_api.test_client() as client:
token = __create_get_token(sample_api_key.service_id)
response = client.get(url_for('service.get_service', service_id=sample_api_key.service_id),
response = client.get(url_for('service.get_service', service_id=str(sample_api_key.service_id)),
headers={'Authorization': 'Bearer {}'.format(token)})
assert response.status_code == 200
@@ -99,7 +99,7 @@ def test_should_allow_valid_token_when_service_has_multiple_keys(notify_api, not
api_key = ApiKey(**data)
save_model_api_key(api_key)
token = __create_get_token(sample_api_key.service_id)
response = client.get(url_for('service.get_service', service_id=sample_api_key.service_id),
response = client.get(url_for('service.get_service', service_id=str(sample_api_key.service_id)),
headers={'Authorization': 'Bearer {}'.format(token)})
assert response.status_code == 200
@@ -123,9 +123,9 @@ def test_should_allow_valid_token_with_post_body(notify_api, notify_db, notify_d
token = create_jwt_token(
request_method="PUT",
request_path=url_for('service.update_service', service_id=sample_api_key.service_id),
request_path=url_for('service.update_service', service_id=str(sample_api_key.service_id)),
secret=get_unsigned_secret(sample_api_key.id),
client_id=sample_api_key.service_id,
client_id=str(sample_api_key.service_id),
request_body=json.dumps(data)
)
headers = [('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(token))]
@@ -139,7 +139,7 @@ def test_should_allow_valid_token_with_post_body(notify_api, notify_db, notify_d
def test_should_not_allow_valid_token_with_invalid_post_body(notify_api, notify_db, notify_db_session, sample_api_key):
with notify_api.test_request_context():
with notify_api.test_client() as client:
token = __create_post_token(sample_api_key.service_id, JSON_BODY)
token = __create_post_token(str(sample_api_key.service_id), JSON_BODY)
response = client.post(url_for('service.create_service'),
data="spurious",
headers={'Authorization': 'Bearer {}'.format(token)})
@@ -179,7 +179,7 @@ def test_authentication_passes_when_service_has_multiple_keys_some_expired(notif
token = create_jwt_token(request_method="GET",
request_path=url_for('service.get_service'),
secret=get_unsigned_secret(api_key.id),
client_id=sample_api_key.service_id)
client_id=str(sample_api_key.service_id))
response = client.get(url_for('service.get_service'),
headers={'Authorization': 'Bearer {}'.format(token)})
assert response.status_code == 200
@@ -200,10 +200,10 @@ def test_authentication_returns_token_expired_when_service_uses_expired_key_and_
token = create_jwt_token(request_method="GET",
request_path=url_for('service.get_service'),
secret=get_unsigned_secret(expired_api_key.id),
client_id=sample_api_key.service_id)
client_id=str(sample_api_key.service_id))
# expire the key
expire_the_key = {'id': expired_api_key.id,
'service_id': sample_api_key.service_id,
'service_id': str(sample_api_key.service_id),
'name': 'expired_key',
'expiry_date': datetime.now() + timedelta(hours=-2)}
save_model_api_key(expired_api_key, expire_the_key)
@@ -219,7 +219,7 @@ def __create_get_token(service_id):
return create_jwt_token(request_method="GET",
request_path=url_for('service.get_service', service_id=service_id),
secret=get_unsigned_secrets(service_id)[0],
client_id=service_id)
client_id=str(service_id))
else:
return create_jwt_token(request_method="GET",
request_path=url_for('service.get_service'),
@@ -232,6 +232,6 @@ def __create_post_token(service_id, request_body):
request_method="POST",
request_path=url_for('service.create_service'),
secret=get_unsigned_secrets(service_id)[0],
client_id=service_id,
client_id=str(service_id),
request_body=request_body
)

View File

@@ -78,8 +78,7 @@ def sample_service(notify_db,
'users': [user],
'limit': 1000,
'active': False,
'restricted': False,
'queue_name': str(uuid.uuid4())}
'restricted': False}
service = Service.query.filter_by(name=service_name).first()
if not service:
service = Service(**data)

View File

@@ -82,7 +82,7 @@ def test_post_job(notify_api, notify_db, notify_db_session, sample_template):
file_name = '{}.csv'.format(job_id)
data = {
'id': str(job_id),
'service': service_id,
'service': str(service_id),
'template': template_id,
'original_file_name': original_file_name,
'bucket_name': bucket_name,
@@ -105,7 +105,7 @@ def test_post_job(notify_api, notify_db, notify_db_session, sample_template):
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['data']['id'] == str(job_id)
assert resp_json['data']['service'] == service_id
assert resp_json['data']['service'] == str(service_id)
assert resp_json['data']['template'] == template_id
assert resp_json['data']['original_file_name'] == original_file_name

View File

@@ -34,9 +34,12 @@ def test_api_key_should_return_error_when_service_does_not_exist(notify_api, not
sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
auth_header = create_authorization_header(path=url_for('service.renew_api_key', service_id="123"),
import uuid
missing_service_id = uuid.uuid4()
auth_header = create_authorization_header(path=url_for('service.renew_api_key',
service_id=missing_service_id),
method='POST')
response = client.post(url_for('service.renew_api_key', service_id=123),
response = client.post(url_for('service.renew_api_key', service_id=missing_service_id),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 404

View File

@@ -1,4 +1,5 @@
import json
import uuid
from collections import Set
from flask import url_for
@@ -25,7 +26,7 @@ def test_get_service_list(notify_api, notify_db, notify_db_session, sample_servi
# TODO assert correct json returned
assert len(json_resp['data']) == 2
assert json_resp['data'][0]['name'] == sample_service.name
assert json_resp['data'][0]['id'] == sample_service.id
assert json_resp['data'][0]['id'] == str(sample_service.id)
def test_get_service(notify_api, notify_db, notify_db_session, sample_service, sample_admin_service_id):
@@ -43,7 +44,7 @@ def test_get_service(notify_api, notify_db, notify_db_session, sample_service, s
assert resp.status_code == 200
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['data']['name'] == sample_service.name
assert json_resp['data']['id'] == sample_service.id
assert json_resp['data']['id'] == str(sample_service.id)
def test_get_service_for_user(notify_api, notify_db, notify_db_session, sample_service):
@@ -93,7 +94,6 @@ def test_post_service(notify_api, notify_db, notify_db_session, sample_user, sam
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['data']['name'] == service.name
assert json_resp['data']['limit'] == service.limit
assert service.queue_name is not None
def test_post_service_multiple_users(notify_api, notify_db, notify_db_session, sample_user, sample_admin_service_id):
@@ -204,12 +204,14 @@ def test_put_service_not_exists(notify_api, notify_db, notify_db_session, sample
'limit': 1000,
'restricted': False,
'active': False}
missing_service_id = uuid.uuid4()
auth_header = create_authorization_header(service_id=sample_admin_service_id,
path=url_for('service.update_service', service_id="123"),
path=url_for('service.update_service',
service_id=missing_service_id),
method='PUT',
request_body=json.dumps(data))
resp = client.put(
url_for('service.update_service', service_id="123"),
url_for('service.update_service', service_id=missing_service_id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 404
@@ -325,11 +327,13 @@ def test_delete_service_not_exists(notify_api, notify_db, notify_db_session, sam
with notify_api.test_request_context():
with notify_api.test_client() as client:
assert Service.query.count() == 2
missing_service_id = uuid.uuid4()
auth_header = create_authorization_header(service_id=sample_admin_service_id,
path=url_for('service.update_service', service_id="123"),
path=url_for('service.update_service',
service_id=missing_service_id),
method='DELETE')
resp = client.delete(
url_for('service.update_service', service_id="123"),
url_for('service.update_service', service_id=missing_service_id),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 404
assert Service.query.count() == 2
@@ -371,7 +375,7 @@ def test_create_template(notify_api, notify_db, notify_db_session, sample_servic
'name': template_name,
'template_type': template_type,
'content': template_content,
'service': sample_service.id
'service': str(sample_service.id)
}
auth_header = create_authorization_header(service_id=sample_admin_service_id,
path=url_for('service.create_template',
@@ -406,14 +410,16 @@ def test_create_template_service_not_exists(notify_api, notify_db, notify_db_ses
'name': template_name,
'template_type': template_type,
'content': template_content,
'service': sample_service.id
'service': str(sample_service.id)
}
missing_service_id = uuid.uuid4()
auth_header = create_authorization_header(service_id=sample_admin_service_id,
path=url_for('service.create_template', service_id="123"),
path=url_for('service.create_template',
service_id=missing_service_id),
method='POST',
request_body=json.dumps(data))
resp = client.post(
url_for('service.create_template', service_id="123"),
url_for('service.create_template', service_id=missing_service_id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 404
@@ -439,7 +445,7 @@ def test_update_template(notify_api, notify_db, notify_db_session, sample_templa
'name': template_name,
'template_type': template_type,
'content': template_content,
'service': sample_service.id
'service': str(sample_service.id)
}
auth_header = create_authorization_header(service_id=sample_admin_service_id,
path=url_for('service.update_template',
@@ -478,17 +484,18 @@ def test_update_template_service_not_exists(notify_api, notify_db, notify_db_ses
'name': template_name,
'template_type': template_type,
'content': template_content,
'service': sample_template.service_id
'service': str(sample_template.service_id)
}
missing_service_id = uuid.uuid4()
auth_header = create_authorization_header(service_id=sample_admin_service_id,
path=url_for('service.update_template',
service_id="123",
service_id=missing_service_id,
template_id=sample_template.id),
method='PUT',
request_body=json.dumps(data))
resp = client.put(
url_for('service.update_template',
service_id="123",
service_id=missing_service_id,
template_id=sample_template.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
@@ -515,7 +522,7 @@ def test_update_template_template_not_exists(notify_api, notify_db, notify_db_se
'name': template_name,
'template_type': template_type,
'content': template_content,
'service': sample_service.id
'service': str(sample_service.id)
}
auth_header = create_authorization_header(service_id=sample_admin_service_id,
path=url_for('service.update_template',
@@ -552,7 +559,7 @@ def test_create_template_unicode_content(notify_api, notify_db, notify_db_sessio
'name': template_name,
'template_type': template_type,
'content': template_content,
'service': sample_service.id
'service': str(sample_service.id)
}
auth_header = create_authorization_header(service_id=sample_admin_service_id,
path=url_for('service.create_template',

View File

@@ -315,7 +315,7 @@ def test_get_user_service(notify_api, notify_db, notify_db_session, sample_servi
assert resp.status_code == 200
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['data']['name'] == another_name
assert json_resp['data']['id'] == another_service.id
assert json_resp['data']['id'] == str(another_service.id)
def test_get_user_service_user_not_exists(notify_api, notify_db, notify_db_session, sample_service,
@@ -348,12 +348,14 @@ def test_get_user_service_service_not_exists(notify_api, notify_db, notify_db_se
with notify_api.test_client() as client:
user = User.query.first()
assert Service.query.count() == 2
import uuid
missing_service_id = uuid.uuid4()
auth_header = create_authorization_header(service_id=sample_admin_service_id,
path=url_for('user.get_service_by_user_id', user_id=user.id,
service_id="12323423"),
service_id=missing_service_id),
method='GET')
resp = client.get(
url_for('user.get_service_by_user_id', user_id=user.id, service_id="12323423"),
url_for('user.get_service_by_user_id', user_id=user.id, service_id=missing_service_id),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 404
json_resp = json.loads(resp.get_data(as_text=True))