Files
notifications-api/tests/app/provider_details/test_rest.py
Leo Hemsted 8bb0261c79 dont let user update prov details version, and refactor tests
the provider details tests were previously very stateful - they
would update a value, and then because provider_details is a "static"
table that is not wiped by the notify_db_session fixture, the tests
were forced to make a second update that reverted that change. if the
test fails for whatever reason, the provider_details table ends up
permanently modified, playing havoc on tests further down the line.

this commit adds the fixture `restore_provider_details` to conftest.
this fixture stores a copy of the contents of ProviderDetails and
ProviderDetailsHistory tables outside of the session, runs the test,
and then clears the table and puts those old values in

this means that the tests have been cleaned up so that they do not
do operations twice in reverse. they've also been cleaned up
generally, including fixture optimisations and such
2016-12-19 17:13:10 +00:00

97 lines
3.5 KiB
Python

import pytest
from flask import json
from app.models import ProviderDetails
from tests import create_authorization_header
def test_get_provider_details_in_type_and_identifier_order(client, notify_db):
response = client.get(
'/provider-details',
headers=[create_authorization_header()]
)
assert response.status_code == 200
json_resp = json.loads(response.get_data(as_text=True))['provider_details']
assert len(json_resp) == 4
assert json_resp[0]['identifier'] == 'ses'
assert json_resp[1]['identifier'] == 'mmg'
assert json_resp[2]['identifier'] == 'firetext'
assert json_resp[3]['identifier'] == 'loadtesting'
def test_get_provider_details_by_id(client, notify_db):
response = client.get(
'/provider-details',
headers=[create_authorization_header()]
)
json_resp = json.loads(response.get_data(as_text=True))['provider_details']
provider_resp = client.get(
'/provider-details/{}'.format(json_resp[0]['id']),
headers=[create_authorization_header()]
)
provider = json.loads(provider_resp.get_data(as_text=True))['provider_details']
assert provider['identifier'] == json_resp[0]['identifier']
def test_get_provider_details_contains_correct_fields(client, notify_db):
response = client.get(
'/provider-details',
headers=[create_authorization_header()]
)
json_resp = json.loads(response.get_data(as_text=True))['provider_details']
allowed_keys = {"id", "display_name", "identifier", "priority", 'notification_type', "active", "version"}
assert allowed_keys == set(json_resp[0].keys())
def test_should_be_able_to_update_priority(client, restore_provider_details):
provider = ProviderDetails.query.first()
update_resp = client.post(
'/provider-details/{}'.format(provider.id),
headers=[('Content-Type', 'application/json'), create_authorization_header()],
data=json.dumps({
'priority': 5
})
)
assert update_resp.status_code == 200
update_json = json.loads(update_resp.get_data(as_text=True))['provider_details']
assert update_json['identifier'] == provider.identifier
assert update_json['priority'] == 5
assert provider.priority == 5
def test_should_be_able_to_update_status(client, restore_provider_details):
provider = ProviderDetails.query.first()
update_resp_1 = client.post(
'/provider-details/{}'.format(provider.id),
headers=[('Content-Type', 'application/json'), create_authorization_header()],
data=json.dumps({
'active': False
})
)
assert update_resp_1.status_code == 200
update_resp_1 = json.loads(update_resp_1.get_data(as_text=True))['provider_details']
assert update_resp_1['identifier'] == provider.identifier
assert not update_resp_1['active']
assert not provider.active
@pytest.mark.parametrize('field,value', [('identifier', 'new'), ('version', 7)])
def test_should_not_be_able_to_update_disallowed_fields(client, restore_provider_details, field, value):
provider = ProviderDetails.query.first()
update_resp = client.post(
'/provider-details/{}'.format(provider.id),
headers=[('Content-Type', 'application/json'), create_authorization_header()],
data=json.dumps({field: value})
)
assert update_resp.status_code == 400
update_resp = json.loads(update_resp.get_data(as_text=True))
print(update_resp)
assert update_resp['message'][field][0] == 'Not permitted to be updated'
assert update_resp['result'] == 'error'