mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
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
This commit is contained in:
@@ -1,150 +1,96 @@
|
||||
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
|
||||
|
||||
def test_get_provider_details_in_type_and_identifier_order(notify_db, notify_db_session, notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header()
|
||||
response = client.get(
|
||||
'/provider-details',
|
||||
headers=[auth_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'
|
||||
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(notify_db, notify_db_session, notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header()
|
||||
response = client.get(
|
||||
'/provider-details',
|
||||
headers=[auth_header]
|
||||
)
|
||||
json_resp = json.loads(response.get_data(as_text=True))['provider_details']
|
||||
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=[auth_header]
|
||||
)
|
||||
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']
|
||||
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(notify_db, notify_db_session, notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header()
|
||||
response = client.get(
|
||||
'/provider-details',
|
||||
headers=[auth_header]
|
||||
)
|
||||
json_resp = json.loads(response.get_data(as_text=True))['provider_details']
|
||||
allowed_keys = {"id", "display_name", "identifier", "priority", 'notification_type', "active"}
|
||||
assert \
|
||||
allowed_keys == \
|
||||
set(json_resp[0].keys())
|
||||
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(notify_db, notify_db_session, notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header()
|
||||
response = client.get(
|
||||
'/provider-details',
|
||||
headers=[auth_header]
|
||||
)
|
||||
fetch_resp = json.loads(response.get_data(as_text=True))['provider_details']
|
||||
def test_should_be_able_to_update_priority(client, restore_provider_details):
|
||||
provider = ProviderDetails.query.first()
|
||||
|
||||
provider_id = fetch_resp[2]['id']
|
||||
|
||||
update_resp = client.post(
|
||||
'/provider-details/{}'.format(provider_id),
|
||||
headers=[('Content-Type', 'application/json'), auth_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'] == 'firetext'
|
||||
assert update_json['priority'] == 5
|
||||
|
||||
update_resp = client.post(
|
||||
'/provider-details/{}'.format(provider_id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
data=json.dumps({
|
||||
'priority': 20
|
||||
})
|
||||
)
|
||||
assert update_resp.status_code == 200
|
||||
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(notify_db, notify_db_session, notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header()
|
||||
response = client.get(
|
||||
'/provider-details',
|
||||
headers=[auth_header]
|
||||
)
|
||||
fetch_resp = json.loads(response.get_data(as_text=True))['provider_details']
|
||||
def test_should_be_able_to_update_status(client, restore_provider_details):
|
||||
provider = ProviderDetails.query.first()
|
||||
|
||||
firetext = next(x for x in fetch_resp if x['identifier'] == 'firetext')
|
||||
|
||||
update_resp_1 = client.post(
|
||||
'/provider-details/{}'.format(firetext['id']),
|
||||
headers=[('Content-Type', 'application/json'), auth_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'] == 'firetext'
|
||||
assert not update_resp_1['active']
|
||||
|
||||
update_resp_2 = client.post(
|
||||
'/provider-details/{}'.format(firetext['id']),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
data=json.dumps({
|
||||
'active': True
|
||||
})
|
||||
)
|
||||
assert update_resp_2.status_code == 200
|
||||
update_resp_2 = json.loads(update_resp_2.get_data(as_text=True))['provider_details']
|
||||
assert update_resp_2['identifier'] == 'firetext'
|
||||
assert update_resp_2['active']
|
||||
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
|
||||
|
||||
|
||||
def test_should_not_be_able_to_update_identifier(notify_db, notify_db_session, notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header()
|
||||
response = client.get(
|
||||
'/provider-details',
|
||||
headers=[auth_header]
|
||||
)
|
||||
fetch_resp = json.loads(response.get_data(as_text=True))['provider_details']
|
||||
@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()
|
||||
|
||||
provider_id = fetch_resp[2]['id']
|
||||
|
||||
update_resp = client.post(
|
||||
'/provider-details/{}'.format(provider_id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
data=json.dumps({
|
||||
'identifier': "new"
|
||||
})
|
||||
)
|
||||
assert update_resp.status_code == 400
|
||||
update_resp = json.loads(update_resp.get_data(as_text=True))
|
||||
assert update_resp['message']['identifier'][0] == 'Not permitted to be updated'
|
||||
assert update_resp['result'] == 'error'
|
||||
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'
|
||||
|
||||
Reference in New Issue
Block a user