Updated tests to match the formatting work done in the admin repo

Signed-off-by: Carlo Costino <carlo.costino@gsa.gov>
This commit is contained in:
Carlo Costino
2024-05-17 17:14:46 -04:00
parent ac4ebacfeb
commit aafa7178f9
30 changed files with 553 additions and 499 deletions

View File

@@ -9,7 +9,7 @@ from notifications_utils.clients.antivirus.antivirus_client import (
)
@pytest.fixture(scope="function")
@pytest.fixture()
def antivirus(app, mocker):
client = AntivirusClient()
app.config["ANTIVIRUS_API_HOST"] = "https://antivirus"
@@ -39,25 +39,33 @@ def test_scan_document(antivirus, rmock):
def test_should_raise_for_status(antivirus, rmock):
with pytest.raises(AntivirusError) as excinfo:
rmock.request(
"POST",
"https://antivirus/scan",
json={"error": "Antivirus error"},
status_code=400,
)
antivirus.scan(io.BytesIO(b"document"))
_test_one_statement_for_status(antivirus, rmock)
assert excinfo.value.message == "Antivirus error"
assert excinfo.value.status_code == 400
def _test_one_statement_for_status(antivirus, rmock):
rmock.request(
"POST",
"https://antivirus/scan",
json={"error": "Antivirus error"},
status_code=400,
)
antivirus.scan(io.BytesIO(b"document"))
def test_should_raise_for_connection_errors(antivirus, rmock):
with pytest.raises(AntivirusError) as excinfo:
rmock.request(
"POST", "https://antivirus/scan", exc=requests.exceptions.ConnectTimeout
)
antivirus.scan(io.BytesIO(b"document"))
_test_one_statement_for_connection_errors(antivirus, rmock)
assert excinfo.value.message == "connection error"
assert excinfo.value.status_code == 503
def _test_one_statement_for_connection_errors(antivirus, rmock):
rmock.request(
"POST", "https://antivirus/scan", exc=requests.exceptions.ConnectTimeout
)
antivirus.scan(io.BytesIO(b"document"))

View File

@@ -8,17 +8,17 @@ from freezegun import freeze_time
from notifications_utils.clients.redis.redis_client import RedisClient, prepare_value
@pytest.fixture(scope="function")
@pytest.fixture()
def mocked_redis_pipeline():
return Mock()
@pytest.fixture
@pytest.fixture()
def delete_mock():
return Mock(return_value=4)
@pytest.fixture(scope="function")
@pytest.fixture()
def mocked_redis_client(app, mocked_redis_pipeline, delete_mock, mocker):
app.config["REDIS_ENABLED"] = True
@@ -46,14 +46,16 @@ def mocked_redis_client(app, mocked_redis_pipeline, delete_mock, mocker):
return redis_client
@pytest.fixture
@pytest.fixture()
def failing_redis_client(mocked_redis_client, delete_mock):
mocked_redis_client.redis_store.get.side_effect = Exception("get failed")
mocked_redis_client.redis_store.set.side_effect = Exception("set failed")
mocked_redis_client.redis_store.incr.side_effect = Exception("incr failed")
mocked_redis_client.redis_store.pipeline.side_effect = Exception("pipeline failed")
mocked_redis_client.redis_store.delete.side_effect = Exception("delete failed")
delete_mock.side_effect = Exception("delete by pattern failed")
# nota bene: using KeyError because flake8 thinks Exception
# and BaseException are too broad
mocked_redis_client.redis_store.get.side_effect = KeyError("get failed")
mocked_redis_client.redis_store.set.side_effect = KeyError("set failed")
mocked_redis_client.redis_store.incr.side_effect = KeyError("incr failed")
mocked_redis_client.redis_store.pipeline.side_effect = KeyError("pipeline failed")
mocked_redis_client.redis_store.delete.side_effect = KeyError("delete failed")
delete_mock.side_effect = KeyError("delete by pattern failed")
return mocked_redis_client
@@ -85,29 +87,29 @@ def test_should_raise_exception_if_raise_set_to_true(
app,
failing_redis_client,
):
with pytest.raises(Exception) as e:
with pytest.raises(KeyError) as e:
failing_redis_client.get("test", raise_exception=True)
assert str(e.value) == "get failed"
assert str(e.value) == "'get failed'"
with pytest.raises(Exception) as e:
with pytest.raises(KeyError) as e:
failing_redis_client.set("test", "test", raise_exception=True)
assert str(e.value) == "set failed"
assert str(e.value) == "'set failed'"
with pytest.raises(Exception) as e:
with pytest.raises(KeyError) as e:
failing_redis_client.incr("test", raise_exception=True)
assert str(e.value) == "incr failed"
assert str(e.value) == "'incr failed'"
with pytest.raises(Exception) as e:
with pytest.raises(KeyError) as e:
failing_redis_client.exceeded_rate_limit("test", 100, 200, raise_exception=True)
assert str(e.value) == "pipeline failed"
assert str(e.value) == "'pipeline failed'"
with pytest.raises(Exception) as e:
with pytest.raises(KeyError) as e:
failing_redis_client.delete("test", raise_exception=True)
assert str(e.value) == "delete failed"
assert str(e.value) == "'delete failed'"
with pytest.raises(Exception) as e:
with pytest.raises(KeyError) as e:
failing_redis_client.delete_by_pattern("pattern", raise_exception=True)
assert str(e.value) == "delete by pattern failed"
assert str(e.value) == "'delete by pattern failed'"
def test_should_not_call_if_not_enabled(mocked_redis_client, delete_mock):
@@ -198,7 +200,7 @@ def test_delete_multi(mocked_redis_client):
@pytest.mark.parametrize(
"input,output",
("input", "output"),
[
(b"asdf", b"asdf"),
("asdf", "asdf"),

View File

@@ -4,7 +4,7 @@ from notifications_utils.clients.redis import RequestCache
from notifications_utils.clients.redis.redis_client import RedisClient
@pytest.fixture(scope="function")
@pytest.fixture()
def mocked_redis_client(app):
app.config["REDIS_ENABLED"] = True
redis_client = RedisClient()
@@ -12,19 +12,19 @@ def mocked_redis_client(app):
return redis_client
@pytest.fixture
@pytest.fixture()
def cache(mocked_redis_client):
return RequestCache(mocked_redis_client)
@pytest.mark.parametrize(
"args, kwargs, expected_cache_key",
(
("args", "kwargs", "expected_cache_key"),
[
([1, 2, 3], {}, "1-2-3-None-None-None"),
([1, 2, 3, 4, 5, 6], {}, "1-2-3-4-5-6"),
([1, 2, 3], {"x": 4, "y": 5, "z": 6}, "1-2-3-4-5-6"),
([1, 2, 3, 4], {"y": 5}, "1-2-3-4-5-None"),
),
],
)
def test_set(
mocker,
@@ -60,13 +60,13 @@ def test_set(
@pytest.mark.parametrize(
"cache_set_call, expected_redis_client_ttl",
(
("cache_set_call", "expected_redis_client_ttl"),
[
(0, 0),
(1, 1),
(1.111, 1),
("2000", 2_000),
),
],
)
def test_set_with_custom_ttl(
mocker,

View File

@@ -9,7 +9,7 @@ from notifications_utils.clients.zendesk.zendesk_client import (
)
@pytest.fixture(scope="function")
@pytest.fixture()
def zendesk_client(app):
client = ZendeskClient()
@@ -67,8 +67,8 @@ def test_zendesk_client_send_ticket_to_zendesk_error(
@pytest.mark.parametrize(
"p1_arg, expected_tags, expected_priority",
(
("p1_arg", "expected_tags", "expected_priority"),
[
(
{},
["govuk_notify_support"],
@@ -88,7 +88,7 @@ def test_zendesk_client_send_ticket_to_zendesk_error(
["govuk_notify_emergency"],
"urgent",
),
),
],
)
def test_notify_support_ticket_request_data(p1_arg, expected_tags, expected_priority):
notify_ticket_form = NotifySupportTicket("subject", "message", "question", **p1_arg)
@@ -126,7 +126,7 @@ def test_notify_support_ticket_request_data_with_message_hidden_from_requester()
@pytest.mark.parametrize(
"name, zendesk_name", [("Name", "Name"), (None, "(no name supplied)")]
("name", "zendesk_name"), [("Name", "Name"), (None, "(no name supplied)")]
)
def test_notify_support_ticket_request_data_with_user_name_and_email(
name, zendesk_name
@@ -145,7 +145,14 @@ def test_notify_support_ticket_request_data_with_user_name_and_email(
@pytest.mark.parametrize(
"custom_fields, tech_ticket_tag, categories, org_id, org_type, service_id",
(
"custom_fields",
"tech_ticket_tag",
"categories",
"org_id",
"org_type",
"service_id",
),
[
(
{"technical_ticket": True},