diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 1f9904612..fa17f3fd3 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -612,7 +612,7 @@ def sample_user_service_permission(sample_user): return p_model -@pytest.fixture(scope="function") +@pytest.fixture(scope="session") def fake_uuid(): return "6ce466d0-fd6a-11e5-82f5-e0accb9d11a6" diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index 349230696..0d8205291 100644 --- a/tests/app/template/test_rest.py +++ b/tests/app/template/test_rest.py @@ -6,6 +6,8 @@ from datetime import datetime, timedelta import pytest from freezegun import freeze_time +from hypothesis import given +from hypothesis import strategies as st from sqlalchemy import select from app import db @@ -297,6 +299,33 @@ def test_should_be_error_if_service_does_not_exist_on_update(client, fake_uuid): assert json_resp["message"] == "No result found" +@given( + fuzzed_service_id=st.uuids(), + fuzzed_template_id=st.uuids(), + fuzzed_template_name=st.text(min_size=1, max_size=100), +) +def test_fuzz_should_be_error_if_service_does_not_exist_on_update( + client, fuzzed_service_id, fuzzed_template_id, fuzzed_template_name +): + """ + Hypothesis-based fuzz test to ensure that when updating a template + for a non-existent service, the API returns a 404 with the correct error message + """ + data = {"name": fuzzed_template_name} + data = json.dumps(data) + auth_header = create_admin_authorization_header() + response = client.post( + f"/service/{fuzzed_service_id}/tempalte/{fuzzed_template_id}", + headers=[("Content-Type", "application/json"), auth_header], + data=data, + ) + json_resp = json.loads(response.get_data(as_text=True)) + + assert response.status_code == 404 + assert json_resp["result"] == "error" + assert json_resp["message"] == "No result found" + + @pytest.mark.parametrize("template_type", [TemplateType.EMAIL]) def test_must_have_a_subject_on_an_email_template( client, sample_user, sample_service, template_type diff --git a/tests/conftest.py b/tests/conftest.py index 7ce2c8033..6a596e233 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -38,7 +38,7 @@ def notify_api(notify_app): ctx.pop() -@pytest.fixture(scope="function") +@pytest.fixture(scope="session") def client(notify_api): with notify_api.test_request_context(), notify_api.test_client() as client: yield client