mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-16 20:48:37 -04:00
Rebased migrations, all tests working.
This commit is contained in:
@@ -421,3 +421,8 @@ def sample_service_permission(notify_db,
|
||||
db.session.add(p_model)
|
||||
db.session.commit()
|
||||
return p_model
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def fake_uuid():
|
||||
return "6ce466d0-fd6a-11e5-82f5-e0accb9d11a6"
|
||||
|
||||
@@ -42,10 +42,13 @@ def test_save_api_key_should_update_the_api_key(notify_api, notify_db, notify_db
|
||||
assert all_api_keys[0].service_id == saved_api_key.service_id
|
||||
|
||||
|
||||
def test_get_api_key_should_raise_exception_when_api_key_does_not_exist(notify_api, notify_db, notify_db_session,
|
||||
sample_service):
|
||||
def test_get_api_key_should_raise_exception_when_api_key_does_not_exist(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service,
|
||||
fake_uuid):
|
||||
try:
|
||||
get_model_api_keys(sample_service.id, id=123)
|
||||
get_model_api_keys(sample_service.id, id=fake_uuid)
|
||||
fail("Should have thrown a NoResultFound exception")
|
||||
except NoResultFound:
|
||||
pass
|
||||
@@ -78,9 +81,10 @@ def test_get_unsigned_secret_returns_key(notify_api,
|
||||
def test_should_not_allow_duplicate_key_names_per_service(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_api_key):
|
||||
sample_api_key,
|
||||
fake_uuid):
|
||||
api_key = ApiKey(
|
||||
**{'id': sample_api_key.id + 1, 'service_id': sample_api_key.service_id, 'name': sample_api_key.name})
|
||||
**{'id': fake_uuid, 'service_id': sample_api_key.service_id, 'name': sample_api_key.name})
|
||||
try:
|
||||
save_model_api_key(api_key)
|
||||
fail("should throw IntegrityError")
|
||||
|
||||
@@ -42,20 +42,6 @@ def test_create_email_template(sample_service):
|
||||
assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'Sample Template'
|
||||
|
||||
|
||||
def test_create_email_template_fails_if_no_subject(sample_service):
|
||||
data = {
|
||||
'name': 'Sample Template',
|
||||
'template_type': "email",
|
||||
'content': "Template content",
|
||||
'service': sample_service
|
||||
}
|
||||
template = Template(**data)
|
||||
|
||||
with pytest.raises(IntegrityError) as e:
|
||||
dao_create_template(template)
|
||||
assert 'new row for relation "templates" violates check constraint "ch_email_template_has_subject"' in str(e.value)
|
||||
|
||||
|
||||
def test_update_template(sample_service):
|
||||
data = {
|
||||
'name': 'Sample Template',
|
||||
@@ -154,7 +140,7 @@ def test_get_template_by_id_and_service(notify_db, notify_db_session, sample_ser
|
||||
assert Template.query.count() == 1
|
||||
|
||||
|
||||
def test_get_template_by_id_and_service_returns_none_if_no_template(sample_service):
|
||||
def test_get_template_by_id_and_service_returns_none_if_no_template(sample_service, fake_uuid):
|
||||
with pytest.raises(NoResultFound) as e:
|
||||
dao_get_template_by_id_and_service_id(template_id=999, service_id=sample_service.id)
|
||||
dao_get_template_by_id_and_service_id(template_id=fake_uuid, service_id=sample_service.id)
|
||||
assert 'No row was found for one' in str(e.value)
|
||||
|
||||
@@ -54,9 +54,9 @@ def test_get_user(notify_api, notify_db, notify_db_session):
|
||||
assert get_model_users(user_id=another_user.id).email_address == email
|
||||
|
||||
|
||||
def test_get_user_not_exists(notify_api, notify_db, notify_db_session):
|
||||
def test_get_user_not_exists(notify_api, notify_db, notify_db_session, fake_uuid):
|
||||
try:
|
||||
get_model_users(user_id="12345")
|
||||
get_model_users(user_id=fake_uuid)
|
||||
pytest.fail("NoResultFound exception not thrown.")
|
||||
except NoResultFound as e:
|
||||
pass
|
||||
|
||||
@@ -19,7 +19,7 @@ def test_create_invited_user(notify_api, sample_service, mocker):
|
||||
data = {
|
||||
'service': str(sample_service.id),
|
||||
'email_address': email_address,
|
||||
'from_user': invite_from.id,
|
||||
'from_user': str(invite_from.id),
|
||||
'permissions': 'send_messages,manage_service,manage_api_keys'
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ def test_create_invited_user(notify_api, sample_service, mocker):
|
||||
|
||||
assert json_resp['data']['service'] == str(sample_service.id)
|
||||
assert json_resp['data']['email_address'] == email_address
|
||||
assert json_resp['data']['from_user'] == invite_from.id
|
||||
assert json_resp['data']['from_user'] == str(invite_from.id)
|
||||
assert json_resp['data']['permissions'] == 'send_messages,manage_service,manage_api_keys'
|
||||
assert json_resp['data']['id']
|
||||
invitation_expiration_days = notify_api.config['INVITATION_EXPIRATION_DAYS']
|
||||
@@ -70,7 +70,7 @@ def test_create_invited_user_invalid_email(notify_api, sample_service, mocker):
|
||||
data = {
|
||||
'service': str(sample_service.id),
|
||||
'email_address': email_address,
|
||||
'from_user': invite_from.id,
|
||||
'from_user': str(invite_from.id),
|
||||
'permissions': 'send_messages,manage_service,manage_api_keys'
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ def test_get_all_invited_users_by_service(notify_api, notify_db, notify_db_sessi
|
||||
|
||||
for invite in json_resp['data']:
|
||||
assert invite['service'] == str(sample_service.id)
|
||||
assert invite['from_user'] == invite_from.id
|
||||
assert invite['from_user'] == str(invite_from.id)
|
||||
assert invite['id']
|
||||
|
||||
|
||||
@@ -176,7 +176,7 @@ def test_get_invited_user_by_service_and_id(notify_api, sample_service, sample_i
|
||||
|
||||
assert json_resp['data']['service'] == str(sample_service.id)
|
||||
assert json_resp['data']['email_address'] == invite_email_address
|
||||
assert json_resp['data']['from_user'] == invite_from.id
|
||||
assert json_resp['data']['from_user'] == str(invite_from.id)
|
||||
assert json_resp['data']['id']
|
||||
|
||||
|
||||
@@ -218,12 +218,11 @@ def test_update_invited_user_set_status_to_cancelled(notify_api, sample_invited_
|
||||
assert json_resp['status'] == 'cancelled'
|
||||
|
||||
|
||||
def test_update_invited_user_for_wrong_service_returns_404(notify_api, sample_invited_user):
|
||||
def test_update_invited_user_for_wrong_service_returns_404(notify_api, sample_invited_user, fake_uuid):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
data = {'status': 'cancelled'}
|
||||
bad_service_id = uuid.uuid4()
|
||||
url = '/service/{0}/invite/{1}'.format(bad_service_id, sample_invited_user.id)
|
||||
url = '/service/{0}/invite/{1}'.format(fake_uuid, sample_invited_user.id)
|
||||
auth_header = create_authorization_header(
|
||||
path=url,
|
||||
method='POST',
|
||||
|
||||
@@ -54,12 +54,11 @@ def test_get_job_with_invalid_job_id_returns404(notify_api, sample_template):
|
||||
assert resp_json['message'] == 'No result found'
|
||||
|
||||
|
||||
def test_get_job_with_unknown_id_returns404(notify_api, sample_template):
|
||||
random_id = str(uuid.uuid4())
|
||||
def test_get_job_with_unknown_id_returns404(notify_api, sample_template, fake_uuid):
|
||||
service_id = sample_template.service.id
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
path = '/service/{}/job/{}'.format(service_id, random_id)
|
||||
path = '/service/{}/job/{}'.format(service_id, fake_uuid)
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_template.service.id,
|
||||
path=path,
|
||||
@@ -89,15 +88,14 @@ def test_get_job_by_id(notify_api, sample_job):
|
||||
assert resp_json['data']['id'] == job_id
|
||||
|
||||
|
||||
def test_create_job(notify_api, sample_template, mocker):
|
||||
def test_create_job(notify_api, sample_template, mocker, fake_uuid):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.process_job.apply_async')
|
||||
job_id = uuid.uuid4()
|
||||
data = {
|
||||
'id': str(job_id),
|
||||
'id': fake_uuid,
|
||||
'service': str(sample_template.service.id),
|
||||
'template': sample_template.id,
|
||||
'template': str(sample_template.id),
|
||||
'original_file_name': 'thisisatest.csv',
|
||||
'notification_count': 1
|
||||
}
|
||||
@@ -115,15 +113,15 @@ def test_create_job(notify_api, sample_template, mocker):
|
||||
assert response.status_code == 201
|
||||
|
||||
app.celery.tasks.process_job.apply_async.assert_called_once_with(
|
||||
([str(job_id)]),
|
||||
([str(fake_uuid)]),
|
||||
queue="process-job"
|
||||
)
|
||||
|
||||
resp_json = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert resp_json['data']['id'] == str(job_id)
|
||||
assert resp_json['data']['id'] == fake_uuid
|
||||
assert resp_json['data']['service'] == str(sample_template.service.id)
|
||||
assert resp_json['data']['template'] == sample_template.id
|
||||
assert resp_json['data']['template'] == str(sample_template.id)
|
||||
assert resp_json['data']['original_file_name'] == 'thisisatest.csv'
|
||||
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ def test_get_notification_by_id(notify_api, sample_notification):
|
||||
notification = json.loads(response.get_data(as_text=True))['data']['notification']
|
||||
assert notification['status'] == 'sent'
|
||||
assert notification['template'] == {
|
||||
'id': sample_notification.template.id,
|
||||
'id': str(sample_notification.template.id),
|
||||
'name': sample_notification.template.name,
|
||||
'template_type': sample_notification.template.template_type}
|
||||
assert notification['job'] == {
|
||||
@@ -76,7 +76,7 @@ def test_get_all_notifications(notify_api, sample_notification):
|
||||
notifications = json.loads(response.get_data(as_text=True))
|
||||
assert notifications['notifications'][0]['status'] == 'sent'
|
||||
assert notifications['notifications'][0]['template'] == {
|
||||
'id': sample_notification.template.id,
|
||||
'id': str(sample_notification.template.id),
|
||||
'name': sample_notification.template.name,
|
||||
'template_type': sample_notification.template.template_type}
|
||||
assert notifications['notifications'][0]['job'] == {
|
||||
@@ -504,14 +504,14 @@ def test_should_reject_bad_phone_numbers(notify_api, sample_template, mocker):
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
def test_send_notification_invalid_template_id(notify_api, sample_template, mocker):
|
||||
def test_send_notification_invalid_template_id(notify_api, sample_template, mocker, fake_uuid):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
|
||||
data = {
|
||||
'to': '+447700900855',
|
||||
'template': 9999
|
||||
'template': fake_uuid
|
||||
}
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_template.service.id,
|
||||
@@ -707,7 +707,7 @@ def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker
|
||||
|
||||
data = {
|
||||
'to': '07700 900 855',
|
||||
'template': sample_template.id
|
||||
'template': str(sample_template.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
@@ -767,7 +767,7 @@ def test_should_reject_email_notification_with_bad_email(notify_api, sample_emai
|
||||
to_address = "bad-email"
|
||||
data = {
|
||||
'to': to_address,
|
||||
'template': sample_email_template.service.id
|
||||
'template': str(sample_email_template.service.id)
|
||||
}
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_email_template.service.id,
|
||||
@@ -788,13 +788,13 @@ def test_should_reject_email_notification_with_bad_email(notify_api, sample_emai
|
||||
|
||||
|
||||
def test_should_reject_email_notification_with_template_id_that_cant_be_found(
|
||||
notify_api, sample_email_template, mocker):
|
||||
notify_api, sample_email_template, mocker, fake_uuid):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
data = {
|
||||
'to': 'ok@ok.com',
|
||||
'template': 1234
|
||||
'template': fake_uuid
|
||||
}
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_email_template.service.id,
|
||||
@@ -829,7 +829,7 @@ def test_should_not_allow_email_template_from_another_service(notify_api, servic
|
||||
|
||||
data = {
|
||||
'to': sample_user.email_address,
|
||||
'template': service_2_templates[0].id
|
||||
'template': str(service_2_templates[0].id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
@@ -861,7 +861,7 @@ def test_should_not_send_email_if_restricted_and_not_a_service_user(notify_api,
|
||||
|
||||
data = {
|
||||
'to': "not-someone-we-trust@email-address.com",
|
||||
'template': sample_email_template.id
|
||||
'template': str(sample_email_template.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
@@ -896,8 +896,8 @@ def test_should_not_send_email_for_job_if_restricted_and_not_a_service_user(
|
||||
|
||||
data = {
|
||||
'to': "not-someone-we-trust@email-address.com",
|
||||
'template': sample_job.template.id,
|
||||
'job': sample_job.id
|
||||
'template': str(sample_job.template.id),
|
||||
'job': (sample_job.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
@@ -927,7 +927,7 @@ def test_should_allow_valid_email_notification(notify_api, sample_email_template
|
||||
|
||||
data = {
|
||||
'to': 'ok@ok.com',
|
||||
'template': sample_email_template.id
|
||||
'template': str(sample_email_template.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
@@ -971,7 +971,7 @@ def test_should_block_api_call_if_over_day_limit(notify_db, notify_db_session, n
|
||||
|
||||
data = {
|
||||
'to': 'ok@ok.com',
|
||||
'template': email_template.id
|
||||
'template': str(email_template.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
@@ -1007,7 +1007,7 @@ def test_no_limit_for_live_service(notify_api,
|
||||
|
||||
data = {
|
||||
'to': 'ok@ok.com',
|
||||
'template': sample_email_template.id
|
||||
'template': str(sample_email_template.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
@@ -1041,7 +1041,7 @@ def test_should_block_api_call_if_over_day_limit_regardless_of_type(notify_db, n
|
||||
|
||||
data = {
|
||||
'to': '+447234123123',
|
||||
'template': sms_template.id
|
||||
'template': str(sms_template.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
@@ -1074,7 +1074,7 @@ def test_should_allow_api_call_if_under_day_limit_regardless_of_type(notify_db,
|
||||
|
||||
data = {
|
||||
'to': '+447634123123',
|
||||
'template': sms_template.id
|
||||
'template': str(sms_template.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
|
||||
@@ -22,7 +22,7 @@ def test_get_permission_list(notify_api, notify_db, notify_db_session, sample_pe
|
||||
assert len(json_resp['data']) == 1
|
||||
expected = {
|
||||
"permission": sample_permission.permission,
|
||||
"user": sample_permission.user.id,
|
||||
"user": str(sample_permission.user.id),
|
||||
"id": str(sample_permission.id),
|
||||
"service": None
|
||||
}
|
||||
@@ -52,7 +52,7 @@ def test_get_permission_filter(notify_api,
|
||||
service_id=str(sample_service.id)).first()
|
||||
expected = {
|
||||
"permission": another_permission.permission,
|
||||
"user": sample_user.id,
|
||||
"user": str(sample_user.id),
|
||||
"id": str(another_permission.id),
|
||||
"service": str(sample_service.id)
|
||||
}
|
||||
@@ -75,7 +75,7 @@ def test_get_permission(notify_api, notify_db, notify_db_session, sample_permiss
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
expected = {
|
||||
"permission": sample_permission.permission,
|
||||
"user": sample_permission.user.id,
|
||||
"user": str(sample_permission.user.id),
|
||||
"id": str(sample_permission.id),
|
||||
"service": None
|
||||
}
|
||||
|
||||
@@ -177,7 +177,7 @@ def test_create_service(notify_api, sample_user):
|
||||
with notify_api.test_client() as client:
|
||||
data = {
|
||||
'name': 'created service',
|
||||
'user_id': sample_user.id,
|
||||
'user_id': str(sample_user.id),
|
||||
'limit': 1000,
|
||||
'restricted': False,
|
||||
'active': False,
|
||||
@@ -238,12 +238,15 @@ def test_should_not_create_service_with_missing_user_id_field(notify_api):
|
||||
assert 'Missing data for required field.' in json_resp['message']['user_id']
|
||||
|
||||
|
||||
def test_should_not_create_service_with_missing_if_user_id_is_not_in_database(notify_api, notify_db, notify_db_session):
|
||||
def test_should_not_create_service_with_missing_if_user_id_is_not_in_database(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
fake_uuid):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
data = {
|
||||
'email_from': 'service',
|
||||
'user_id': 1234,
|
||||
'user_id': fake_uuid,
|
||||
'name': 'created service',
|
||||
'limit': 1000,
|
||||
'restricted': False,
|
||||
@@ -269,7 +272,7 @@ def test_should_not_create_service_if_missing_data(notify_api, sample_user):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
data = {
|
||||
'user_id': sample_user.id
|
||||
'user_id': str(sample_user.id)
|
||||
}
|
||||
auth_header = create_authorization_header(
|
||||
path='/service',
|
||||
@@ -299,7 +302,7 @@ def test_should_not_create_service_with_duplicate_name(notify_api,
|
||||
with notify_api.test_client() as client:
|
||||
data = {
|
||||
'name': sample_service.name,
|
||||
'user_id': sample_service.users[0].id,
|
||||
'user_id': str(sample_service.users[0].id),
|
||||
'limit': 1000,
|
||||
'restricted': False,
|
||||
'active': False,
|
||||
@@ -327,7 +330,7 @@ def test_create_service_should_throw_duplicate_key_constraint_for_existing_email
|
||||
with notify_api.test_client() as client:
|
||||
data = {
|
||||
'name': 'First SERVICE',
|
||||
'user_id': first_service.users[0].id,
|
||||
'user_id': str(first_service.users[0].id),
|
||||
'limit': 1000,
|
||||
'restricted': False,
|
||||
'active': False,
|
||||
@@ -543,7 +546,7 @@ def test_default_permissions_are_added_for_user_service(notify_api,
|
||||
with notify_api.test_client() as client:
|
||||
data = {
|
||||
'name': 'created service',
|
||||
'user_id': sample_user.id,
|
||||
'user_id': str(sample_user.id),
|
||||
'limit': 1000,
|
||||
'restricted': False,
|
||||
'active': False,
|
||||
@@ -650,7 +653,7 @@ def test_add_existing_user_to_another_service_with_all_permissions(notify_api,
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
json_resp = json.loads(resp.get_data(as_text=True))
|
||||
assert user_to_add.id in json_resp['data']['users']
|
||||
assert str(user_to_add.id) in json_resp['data']['users']
|
||||
|
||||
# check user has all permissions
|
||||
auth_header = create_authorization_header(
|
||||
|
||||
@@ -67,25 +67,24 @@ def test_should_create_a_new_email_template_for_a_service(notify_api, sample_ser
|
||||
assert json_resp['data']['id']
|
||||
|
||||
|
||||
def test_should_be_error_if_service_does_not_exist_on_create(notify_api):
|
||||
def test_should_be_error_if_service_does_not_exist_on_create(notify_api, fake_uuid):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
bad_id = str(uuid.uuid4())
|
||||
data = {
|
||||
'name': 'my template',
|
||||
'template_type': 'sms',
|
||||
'content': 'template content',
|
||||
'service': bad_id
|
||||
'service': fake_uuid
|
||||
}
|
||||
data = json.dumps(data)
|
||||
auth_header = create_authorization_header(
|
||||
path='/service/{}/template'.format(bad_id),
|
||||
path='/service/{}/template'.format(fake_uuid),
|
||||
method='POST',
|
||||
request_body=data
|
||||
)
|
||||
|
||||
response = client.post(
|
||||
'/service/{}/template'.format(bad_id),
|
||||
'/service/{}/template'.format(fake_uuid),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
data=data
|
||||
)
|
||||
@@ -95,22 +94,21 @@ def test_should_be_error_if_service_does_not_exist_on_create(notify_api):
|
||||
assert json_resp['message'] == 'No result found'
|
||||
|
||||
|
||||
def test_should_be_error_if_service_does_not_exist_on_update(notify_api):
|
||||
def test_should_be_error_if_service_does_not_exist_on_update(notify_api, fake_uuid):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
bad_id = str(uuid.uuid4())
|
||||
data = {
|
||||
'name': 'my template'
|
||||
}
|
||||
data = json.dumps(data)
|
||||
auth_header = create_authorization_header(
|
||||
path='/service/{}/template/123'.format(bad_id),
|
||||
path='/service/{}/template/{}'.format(fake_uuid, fake_uuid),
|
||||
method='POST',
|
||||
request_body=data
|
||||
)
|
||||
|
||||
response = client.post(
|
||||
'/service/{}/template/123'.format(bad_id),
|
||||
'/service/{}/template/{}'.format(fake_uuid, fake_uuid),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
data=data
|
||||
)
|
||||
@@ -142,9 +140,9 @@ def test_must_have_a_subject_on_an_email_template(notify_api, sample_service):
|
||||
data=data
|
||||
)
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert response.status_code == 500
|
||||
assert response.status_code == 400
|
||||
assert json_resp['result'] == 'error'
|
||||
assert json_resp['message'] == 'Failed to create template'
|
||||
assert json_resp['message'] == {'subject': ['Invalid template subject']}
|
||||
|
||||
|
||||
def test_must_have_a_uniqe_subject_on_an_email_template(notify_api, sample_service):
|
||||
@@ -393,18 +391,17 @@ def test_should_return_empty_array_if_no_templates_for_service(notify_api, sampl
|
||||
assert len(json_resp['data']) == 0
|
||||
|
||||
|
||||
def test_should_return_404_if_no_templates_for_service_with_id(notify_api, sample_service):
|
||||
def test_should_return_404_if_no_templates_for_service_with_id(notify_api, sample_service, fake_uuid):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
uuid_ = uuid.uuid4()
|
||||
auth_header = create_authorization_header(
|
||||
path='/service/{}/template/{}'.format(sample_service.id, 9999),
|
||||
path='/service/{}/template/{}'.format(sample_service.id, fake_uuid),
|
||||
method='GET'
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template/{}'.format(sample_service.id, 9999),
|
||||
'/service/{}/template/{}'.format(sample_service.id, fake_uuid),
|
||||
headers=[auth_header]
|
||||
)
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ def test_get_user_list(notify_api, notify_db, notify_db_session, sample_service)
|
||||
expected_permissions = default_service_permissions
|
||||
fetched = json_resp['data'][0]
|
||||
|
||||
assert sample_user.id == fetched['id']
|
||||
assert str(sample_user.id) == fetched['id']
|
||||
assert sample_user.name == fetched['name']
|
||||
assert sample_user.mobile_number == fetched['mobile_number']
|
||||
assert sample_user.email_address == fetched['email_address']
|
||||
@@ -53,7 +53,7 @@ def test_get_user(notify_api, notify_db, notify_db_session, sample_service):
|
||||
expected_permissions = default_service_permissions
|
||||
fetched = json_resp['data']
|
||||
|
||||
assert sample_user.id == fetched['id']
|
||||
assert str(sample_user.id) == fetched['id']
|
||||
assert sample_user.name == fetched['name']
|
||||
assert sample_user.mobile_number == fetched['mobile_number']
|
||||
assert sample_user.email_address == fetched['email_address']
|
||||
@@ -90,9 +90,8 @@ def test_post_user(notify_api, notify_db, notify_db_session):
|
||||
assert resp.status_code == 201
|
||||
user = User.query.filter_by(email_address='user@digital.cabinet-office.gov.uk').first()
|
||||
json_resp = json.loads(resp.get_data(as_text=True))
|
||||
json_resp['data'] == {"email_address": user.email_address, "id": user.id}
|
||||
assert json_resp['data']['email_address'] == user.email_address
|
||||
assert json_resp['data']['id'] == user.id
|
||||
assert json_resp['data']['id'] == str(user.id)
|
||||
|
||||
|
||||
def test_post_user_missing_attribute_email(notify_api, notify_db, notify_db_session):
|
||||
@@ -186,7 +185,7 @@ def test_put_user(notify_api, notify_db, notify_db_session, sample_service):
|
||||
expected_permissions = default_service_permissions
|
||||
fetched = json_resp['data']
|
||||
|
||||
assert sample_user.id == fetched['id']
|
||||
assert str(sample_user.id) == fetched['id']
|
||||
assert sample_user.name == fetched['name']
|
||||
assert sample_user.mobile_number == fetched['mobile_number']
|
||||
assert new_email == fetched['email_address']
|
||||
@@ -230,13 +229,13 @@ def test_put_user_update_password(notify_api,
|
||||
request_body=json.dumps(data))
|
||||
headers = [('Content-Type', 'application/json'), auth_header]
|
||||
resp = client.post(
|
||||
url_for('user.verify_user_password', user_id=sample_user.id),
|
||||
url_for('user.verify_user_password', user_id=str(sample_user.id)),
|
||||
data=json.dumps(data),
|
||||
headers=headers)
|
||||
assert resp.status_code == 204
|
||||
|
||||
|
||||
def test_put_user_not_exists(notify_api, notify_db, notify_db_session, sample_user):
|
||||
def test_put_user_not_exists(notify_api, notify_db, notify_db_session, sample_user, fake_uuid):
|
||||
"""
|
||||
Tests PUT endpoint '/' to update a user doesn't exist.
|
||||
"""
|
||||
@@ -245,17 +244,17 @@ def test_put_user_not_exists(notify_api, notify_db, notify_db_session, sample_us
|
||||
assert User.query.count() == 1
|
||||
new_email = 'new@digital.cabinet-office.gov.uk'
|
||||
data = {'email_address': new_email}
|
||||
auth_header = create_authorization_header(path=url_for('user.update_user', user_id="9999"),
|
||||
auth_header = create_authorization_header(path=url_for('user.update_user', user_id=fake_uuid),
|
||||
method='PUT',
|
||||
request_body=json.dumps(data))
|
||||
headers = [('Content-Type', 'application/json'), auth_header]
|
||||
resp = client.put(
|
||||
url_for('user.update_user', user_id="9999"),
|
||||
url_for('user.update_user', user_id=fake_uuid),
|
||||
data=json.dumps(data),
|
||||
headers=headers)
|
||||
assert resp.status_code == 404
|
||||
assert User.query.count() == 1
|
||||
user = User.query.filter_by(id=sample_user.id).first()
|
||||
user = User.query.filter_by(id=str(sample_user.id)).first()
|
||||
json_resp = json.loads(resp.get_data(as_text=True))
|
||||
assert json_resp['result'] == "error"
|
||||
assert json_resp['message'] == 'No result found'
|
||||
@@ -278,7 +277,7 @@ def test_get_user_by_email(notify_api, notify_db, notify_db_session, sample_serv
|
||||
expected_permissions = default_service_permissions
|
||||
fetched = json_resp['data']
|
||||
|
||||
assert sample_user.id == fetched['id']
|
||||
assert str(sample_user.id) == fetched['id']
|
||||
assert sample_user.name == fetched['name']
|
||||
assert sample_user.mobile_number == fetched['mobile_number']
|
||||
assert sample_user.email_address == fetched['email_address']
|
||||
@@ -325,9 +324,9 @@ def test_get_user_with_permissions(notify_api,
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
header = create_authorization_header(
|
||||
path=url_for('user.get_user', user_id=sample_service_permission.user.id),
|
||||
path=url_for('user.get_user', user_id=str(sample_service_permission.user.id)),
|
||||
method='GET')
|
||||
response = client.get(url_for('user.get_user', user_id=sample_service_permission.user.id),
|
||||
response = client.get(url_for('user.get_user', user_id=str(sample_service_permission.user.id)),
|
||||
headers=[header])
|
||||
assert response.status_code == 200
|
||||
permissions = json.loads(response.get_data(as_text=True))['data']['permissions']
|
||||
@@ -345,7 +344,7 @@ def test_set_user_permissions(notify_api,
|
||||
header = create_authorization_header(
|
||||
path=url_for(
|
||||
'user.set_permissions',
|
||||
user_id=sample_user.id,
|
||||
user_id=str(sample_user.id),
|
||||
service_id=str(sample_service.id)),
|
||||
method='POST',
|
||||
request_body=data)
|
||||
@@ -353,7 +352,7 @@ def test_set_user_permissions(notify_api,
|
||||
response = client.post(
|
||||
url_for(
|
||||
'user.set_permissions',
|
||||
user_id=sample_user.id,
|
||||
user_id=str(sample_user.id),
|
||||
service_id=str(sample_service.id)),
|
||||
headers=headers,
|
||||
data=data)
|
||||
@@ -376,7 +375,7 @@ def test_set_user_permissions_multiple(notify_api,
|
||||
header = create_authorization_header(
|
||||
path=url_for(
|
||||
'user.set_permissions',
|
||||
user_id=sample_user.id,
|
||||
user_id=str(sample_user.id),
|
||||
service_id=str(sample_service.id)),
|
||||
method='POST',
|
||||
request_body=data)
|
||||
@@ -384,7 +383,7 @@ def test_set_user_permissions_multiple(notify_api,
|
||||
response = client.post(
|
||||
url_for(
|
||||
'user.set_permissions',
|
||||
user_id=sample_user.id,
|
||||
user_id=str(sample_user.id),
|
||||
service_id=str(sample_service.id)),
|
||||
headers=headers,
|
||||
data=data)
|
||||
@@ -411,7 +410,7 @@ def test_set_user_permissions_remove_old(notify_api,
|
||||
header = create_authorization_header(
|
||||
path=url_for(
|
||||
'user.set_permissions',
|
||||
user_id=sample_user.id,
|
||||
user_id=str(sample_user.id),
|
||||
service_id=str(sample_service.id)),
|
||||
method='POST',
|
||||
request_body=data)
|
||||
@@ -419,7 +418,7 @@ def test_set_user_permissions_remove_old(notify_api,
|
||||
response = client.post(
|
||||
url_for(
|
||||
'user.set_permissions',
|
||||
user_id=sample_user.id,
|
||||
user_id=str(sample_user.id),
|
||||
service_id=str(sample_service.id)),
|
||||
headers=headers,
|
||||
data=data)
|
||||
|
||||
@@ -336,7 +336,10 @@ def test_send_user_email_code(notify_api,
|
||||
queue='email-code')
|
||||
|
||||
|
||||
def test_send_user_email_code_returns_404_for_when_user_does_not_exist(notify_api, notify_db, notify_db_session):
|
||||
def test_send_user_email_code_returns_404_for_when_user_does_not_exist(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
fake_uuid):
|
||||
"""
|
||||
Tests POST endpoint /user/<user_id>/email-code return 404 for missing user
|
||||
"""
|
||||
@@ -344,11 +347,11 @@ def test_send_user_email_code_returns_404_for_when_user_does_not_exist(notify_ap
|
||||
with notify_api.test_client() as client:
|
||||
data = json.dumps({})
|
||||
auth_header = create_authorization_header(
|
||||
path=url_for('user.send_user_email_code', user_id=1),
|
||||
path=url_for('user.send_user_email_code', user_id=fake_uuid),
|
||||
method='POST',
|
||||
request_body=data)
|
||||
resp = client.post(
|
||||
url_for('user.send_user_email_code', user_id=1),
|
||||
url_for('user.send_user_email_code', user_id=fake_uuid),
|
||||
data=data,
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
assert resp.status_code == 404
|
||||
@@ -364,11 +367,11 @@ def test_send_user_email_verification(notify_api,
|
||||
with notify_api.test_client() as client:
|
||||
data = json.dumps({})
|
||||
auth_header = create_authorization_header(
|
||||
path=url_for('user.send_user_email_verification', user_id=sample_email_code.user.id),
|
||||
path=url_for('user.send_user_email_verification', user_id=str(sample_email_code.user.id)),
|
||||
method='POST',
|
||||
request_body=data)
|
||||
resp = client.post(
|
||||
url_for('user.send_user_email_verification', user_id=sample_email_code.user.id),
|
||||
url_for('user.send_user_email_verification', user_id=str(sample_email_code.user.id)),
|
||||
data=data,
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
assert resp.status_code == 204
|
||||
|
||||
@@ -27,24 +27,28 @@ def notify_api(request):
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def notify_db(notify_api, request):
|
||||
Migrate(notify_api, db)
|
||||
Manager(db, MigrateCommand)
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
||||
ALEMBIC_CONFIG = os.path.join(BASE_DIR, 'migrations')
|
||||
config = Config(ALEMBIC_CONFIG + '/alembic.ini')
|
||||
config.set_main_option("script_location", ALEMBIC_CONFIG)
|
||||
try:
|
||||
Migrate(notify_api, db)
|
||||
Manager(db, MigrateCommand)
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
||||
ALEMBIC_CONFIG = os.path.join(BASE_DIR, 'migrations')
|
||||
config = Config(ALEMBIC_CONFIG + '/alembic.ini')
|
||||
config.set_main_option("script_location", ALEMBIC_CONFIG)
|
||||
|
||||
with notify_api.app_context():
|
||||
upgrade(config, 'head')
|
||||
with notify_api.app_context():
|
||||
upgrade(config, 'head')
|
||||
|
||||
def teardown():
|
||||
db.session.remove()
|
||||
db.engine.execute("drop sequence services_id_seq cascade")
|
||||
db.drop_all()
|
||||
db.engine.execute("drop table alembic_version")
|
||||
db.get_engine(notify_api).dispose()
|
||||
def teardown():
|
||||
db.session.remove()
|
||||
db.engine.execute("drop sequence services_id_seq cascade")
|
||||
db.drop_all()
|
||||
db.engine.execute("drop table alembic_version")
|
||||
db.get_engine(notify_api).dispose()
|
||||
|
||||
request.addfinalizer(teardown)
|
||||
request.addfinalizer(teardown)
|
||||
except:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return db
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user