mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-19 05:58:53 -04:00
Remove unnecessary code to do with testing service name uniqueness
notifications-admin has now been changed to always pass the service_id to the 'service/unique' endpoint. This means we don't need to cover the case of there being no service_id and the tests can also be updated.
This commit is contained in:
@@ -677,13 +677,10 @@ def is_service_name_unique():
|
|||||||
|
|
||||||
name_exists = Service.query.filter_by(name=name).first()
|
name_exists = Service.query.filter_by(name=name).first()
|
||||||
|
|
||||||
if service_id:
|
email_from_exists = Service.query.filter(
|
||||||
email_from_exists = Service.query.filter(
|
Service.email_from == email_from,
|
||||||
Service.email_from == email_from,
|
Service.id != service_id
|
||||||
Service.id != service_id
|
).first()
|
||||||
).first()
|
|
||||||
else:
|
|
||||||
email_from_exists = Service.query.filter_by(email_from=email_from).first()
|
|
||||||
|
|
||||||
result = not (name_exists or email_from_exists)
|
result = not (name_exists or email_from_exists)
|
||||||
return jsonify(result=result), 200
|
return jsonify(result=result), 200
|
||||||
@@ -694,6 +691,8 @@ def check_request_args(request):
|
|||||||
name = request.args.get('name', None)
|
name = request.args.get('name', None)
|
||||||
email_from = request.args.get('email_from', None)
|
email_from = request.args.get('email_from', None)
|
||||||
errors = []
|
errors = []
|
||||||
|
if not service_id:
|
||||||
|
errors.append({'service_id': ["Can't be empty"]})
|
||||||
if not name:
|
if not name:
|
||||||
errors.append({'name': ["Can't be empty"]})
|
errors.append({'name': ["Can't be empty"]})
|
||||||
if not email_from:
|
if not email_from:
|
||||||
|
|||||||
@@ -2195,84 +2195,97 @@ def test_search_for_notification_by_to_field_returns_personlisation(
|
|||||||
assert notifications[0]['personalisation']['name'] == 'Foo'
|
assert notifications[0]['personalisation']['name'] == 'Foo'
|
||||||
|
|
||||||
|
|
||||||
def test_is_service_name_unique_returns_200_if_unique(client):
|
def test_is_service_name_unique_returns_200_if_unique(admin_request, notify_db, notify_db_session):
|
||||||
response = client.get('/service/unique?name=something&email_from=something',
|
|
||||||
headers=[create_authorization_header()])
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert json.loads(response.get_data(as_text=True)) == {"result": True}
|
|
||||||
|
|
||||||
|
|
||||||
def test_is_service_name_unique_returns_200_if_unique_and_service_id_given(
|
|
||||||
client,
|
|
||||||
notify_db,
|
|
||||||
notify_db_session
|
|
||||||
):
|
|
||||||
service = create_service(service_name='unique', email_from='unique')
|
service = create_service(service_name='unique', email_from='unique')
|
||||||
service_id = str(service.id)
|
|
||||||
|
|
||||||
response = client.get(
|
response = admin_request.get(
|
||||||
'/service/unique?service_id={}&name=something&email_from=something'.format(service_id),
|
'service.is_service_name_unique',
|
||||||
headers=[create_authorization_header()]
|
_expected_status=200,
|
||||||
|
service_id=service.id,
|
||||||
|
name='something',
|
||||||
|
email_from='something'
|
||||||
)
|
)
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response == {"result": True}
|
||||||
assert json.loads(response.get_data(as_text=True)) == {"result": True}
|
|
||||||
|
|
||||||
|
|
||||||
def test_is_service_name_unique_returns_200_when_capitalized(
|
|
||||||
client,
|
|
||||||
notify_db,
|
|
||||||
notify_db_session
|
|
||||||
):
|
|
||||||
service = create_service(service_name='unique', email_from='unique')
|
|
||||||
service_id = str(service.id)
|
|
||||||
|
|
||||||
response = client.get(
|
|
||||||
'/service/unique?service_id={}&name={}&email_from={}'.format(service_id, 'UNIQUE', 'unique'),
|
|
||||||
headers=[create_authorization_header()]
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert json.loads(response.get_data(as_text=True)) == {"result": True}
|
|
||||||
|
|
||||||
|
|
||||||
def test_is_service_name_unique_returns_false_if_checking_capitalization_of_different_service(
|
|
||||||
client,
|
|
||||||
notify_db,
|
|
||||||
notify_db_session
|
|
||||||
):
|
|
||||||
create_service(service_name='unique', email_from='unique')
|
|
||||||
different_service_id = '111aa111-2222-bbbb-aaaa-111111111111'
|
|
||||||
|
|
||||||
response = client.get(
|
|
||||||
'/service/unique?service_id={}&name={}&email_from={}'.format(
|
|
||||||
different_service_id, 'UNIQUE', 'unique'),
|
|
||||||
headers=[create_authorization_header()]
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert json.loads(response.get_data(as_text=True)) == {"result": False}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('name, email_from',
|
@pytest.mark.parametrize('name, email_from',
|
||||||
[("something unique", "something"),
|
[("UNIQUE", "unique"),
|
||||||
("unique", "something.unique"),
|
("Unique.", "unique"),
|
||||||
("something unique", "something.unique")
|
("**uniQUE**", "unique")
|
||||||
])
|
])
|
||||||
def test_is_service_name_unique_returns_200_and_false(client, notify_db, notify_db_session, name, email_from):
|
def test_is_service_name_unique_returns_200_with_name_capitalized_or_punctuation_added(
|
||||||
create_service(service_name='something unique', email_from='something.unique')
|
admin_request,
|
||||||
response = client.get('/service/unique?name={}&email_from={}'.format(name, email_from),
|
notify_db,
|
||||||
headers=[create_authorization_header()])
|
notify_db_session,
|
||||||
assert response.status_code == 200
|
name,
|
||||||
assert json.loads(response.get_data(as_text=True)) == {"result": False}
|
email_from
|
||||||
|
):
|
||||||
|
service = create_service(service_name='unique', email_from='unique')
|
||||||
|
|
||||||
|
response = admin_request.get(
|
||||||
|
'service.is_service_name_unique',
|
||||||
|
_expected_status=200,
|
||||||
|
service_id=service.id,
|
||||||
|
name=name,
|
||||||
|
email_from=email_from
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response == {"result": True}
|
||||||
|
|
||||||
|
|
||||||
def test_is_service_name_unique_returns_400_when_name_does_not_exist(client):
|
@pytest.mark.parametrize('name, email_from', [
|
||||||
response = client.get('/service/unique', headers=[create_authorization_header()])
|
("existing name", "email.from"),
|
||||||
assert response.status_code == 400
|
("name", "existing.name")
|
||||||
json_resp = json.loads(response.get_data(as_text=True))
|
])
|
||||||
assert json_resp["message"][0]["name"] == ["Can't be empty"]
|
def test_is_service_name_unique_returns_200_and_false_if_name_or_email_from_exist_for_a_different_service(
|
||||||
assert json_resp["message"][1]["email_from"] == ["Can't be empty"]
|
admin_request,
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
name,
|
||||||
|
email_from
|
||||||
|
):
|
||||||
|
create_service(service_name='existing name', email_from='existing.name')
|
||||||
|
different_service_id = '111aa111-2222-bbbb-aaaa-111111111111'
|
||||||
|
|
||||||
|
response = admin_request.get(
|
||||||
|
'service.is_service_name_unique',
|
||||||
|
_expected_status=200,
|
||||||
|
service_id=different_service_id,
|
||||||
|
name=name,
|
||||||
|
email_from=email_from
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response == {"result": False}
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_service_name_unique_returns_200_and_false_if_name_exists_for_the_same_service(
|
||||||
|
admin_request,
|
||||||
|
notify_db,
|
||||||
|
notify_db_session
|
||||||
|
):
|
||||||
|
service = create_service(service_name='unique', email_from='unique')
|
||||||
|
|
||||||
|
response = admin_request.get(
|
||||||
|
'service.is_service_name_unique',
|
||||||
|
_expected_status=200,
|
||||||
|
service_id=service.id,
|
||||||
|
name='unique',
|
||||||
|
email_from='unique2'
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response == {"result": False}
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_service_name_unique_returns_400_when_name_does_not_exist(admin_request):
|
||||||
|
response = admin_request.get(
|
||||||
|
'service.is_service_name_unique',
|
||||||
|
_expected_status=400
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response["message"][0]["service_id"] == ["Can't be empty"]
|
||||||
|
assert response["message"][1]["name"] == ["Can't be empty"]
|
||||||
|
assert response["message"][2]["email_from"] == ["Can't be empty"]
|
||||||
|
|
||||||
|
|
||||||
def test_get_email_reply_to_addresses_when_there_are_no_reply_to_email_addresses(client, sample_service):
|
def test_get_email_reply_to_addresses_when_there_are_no_reply_to_email_addresses(client, sample_service):
|
||||||
|
|||||||
Reference in New Issue
Block a user