Skip to content

Commit

Permalink
Fix rocky katalogus tests and delete unused fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
dekkers committed Nov 25, 2024
1 parent f751a99 commit d283f14
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 168 deletions.
143 changes: 0 additions & 143 deletions rocky/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,27 +222,13 @@ def redteamuser(django_user_model):
)


@pytest.fixture
def redteamuser_b(django_user_model):
return create_user(
django_user_model, "[email protected]", "RedteamBRedteamB123!!", "Redteam B name", "default_redteam_b"
)


@pytest.fixture
def redteam_member(redteamuser, organization):
member = create_member(redteamuser, organization)
add_redteam_group_permissions(member)
return member


@pytest.fixture
def redteam_member_b(redteamuser_b, organization_b):
member = create_member(redteamuser_b, organization_b)
add_redteam_group_permissions(member)
return member


@pytest.fixture
def clientuser(django_user_model):
return create_user(django_user_model, "[email protected]", "ClientClient123!!", "Client name", "default_client")
Expand Down Expand Up @@ -331,14 +317,6 @@ def mock_crisis_room_octopoes(mocker):
return mocker.patch("crisis_room.views.OctopoesAPIConnector")


@pytest.fixture
def lazy_task_list_empty() -> MagicMock:
mock = MagicMock()
mock.__getitem__.return_value = []
mock.count.return_value = 0
return mock


@pytest.fixture
def task() -> Task:
return Task.model_validate(
Expand Down Expand Up @@ -795,17 +773,6 @@ def tree_data_dns_findings():
}


@pytest.fixture
def finding_type_kat_no_caa() -> KATFindingType:
return KATFindingType(
id="KAT-NO-CAA",
description="Fake description...",
recommendation="Fake recommendation...",
risk_score=9.5,
risk_severity=RiskLevelSeverity.CRITICAL,
)


@pytest.fixture
def finding_type_kat_invalid_spf() -> KATFindingType:
return KATFindingType(
Expand Down Expand Up @@ -1322,93 +1289,6 @@ def report_recipe():
)


@pytest.fixture
def get_report_schedules():
report_schedule = [
{
"id": "06a783b3-af62-429d-8b48-5934c8702366",
"scheduler_id": "report-madelon123",
"hash": "f429f60fda539f9017544758163a955e",
"data": {
"type": "report",
"organisation_id": "madelon123",
"report_recipe_id": "6a121a53-f795-4bfe-9aea-f410a96fca59",
},
"enabled": "true",
"schedule": "0 * * * *",
"tasks": [
{
"id": "21ce00b8-57c4-4d65-a53e-275b4e44da6f",
"scheduler_id": "report-madelon123",
"schedule_id": "06a783b3-af62-429d-8b48-5934c8702366",
"priority": 1729520184,
"status": "completed",
"type": "report",
"hash": "f429f60fda539f9017544758163a955e",
"data": {
"organisation_id": "madelon123",
"report_recipe_id": "6a121a53-f795-4bfe-9aea-f410a96fca59",
},
"created_at": "2024-10-21T14:16:24.702759Z",
"modified_at": "2024-10-21T14:16:24.702761Z",
},
{
"id": "f63c2224-a4b2-4161-9790-9b4d665a4683",
"scheduler_id": "report-madelon123",
"schedule_id": "06a783b3-af62-429d-8b48-5934c8702366",
"priority": 1729580020,
"status": "completed",
"type": "report",
"hash": "f429f60fda539f9017544758163a955e",
"data": {
"organisation_id": "madelon123",
"report_recipe_id": "6a121a53-f795-4bfe-9aea-f410a96fca59",
},
"created_at": "2024-10-22T06:53:40.405185Z",
"modified_at": "2024-10-22T06:53:40.405192Z",
},
],
"deadline_at": "2024-10-22T12:00:00Z",
"created_at": "2024-10-21T14:14:00.359039Z",
"modified_at": "2024-10-21T14:14:00.359043Z",
},
{
"id": "e9a00bc1-850a-401a-89d9-252d98823bb3",
"scheduler_id": "report-madelon123",
"hash": "02e67c8676cb8135681d52ca62a9fe5b",
"data": {
"type": "report",
"organisation_id": "madelon123",
"report_recipe_id": "31c79490-fb51-440d-9108-0c388276f655",
},
"enabled": "true",
"schedule": "0 0 * * *",
"tasks": [
{
"id": "f4f938e0-5f2d-4bcd-9b28-11831b7835e4",
"scheduler_id": "report-madelon123",
"schedule_id": "e9a00bc1-850a-401a-89d9-252d98823bb3",
"priority": 1729521145,
"status": "completed",
"type": "report",
"hash": "02e67c8676cb8135681d52ca62a9fe5b",
"data": {
"organisation_id": "madelon123",
"report_recipe_id": "31c79490-fb51-440d-9108-0c388276f655",
},
"created_at": "2024-10-21T14:32:25.247525Z",
"modified_at": "2024-10-21T14:32:25.247527Z",
}
],
"deadline_at": "2024-10-23T00:00:00Z",
"created_at": "2024-10-21T13:34:42.791561Z",
"modified_at": "2024-10-21T13:34:42.791563Z",
},
]

return report_schedule


def setup_request(request, user):
request = SessionMiddleware(lambda r: r)(request)
request.session[DEVICE_ID_SESSION_KEY] = user.staticdevice_set.get().persistent_id
Expand Down Expand Up @@ -1710,29 +1590,6 @@ def reports_more_input_oois():
]


def onboarding_collect_data():
return {
"Hostname|internet|mispo.es": {
"input_ooi": "Hostname|internet|mispo.es",
"records": [
{"type": "A", "ttl": 480, "name": "mispo.es", "content": "134.209.85.72"},
{"type": "MX", "ttl": 480, "name": "mispo.es", "content": "10 mx.wijmailenveilig.nl."},
{"type": "NS", "ttl": 480, "name": "mispo.es", "content": "ns1.domaindiscount24.net."},
{"type": "NS", "ttl": 480, "name": "mispo.es", "content": "ns2.domaindiscount24.net."},
{"type": "NS", "ttl": 480, "name": "mispo.es", "content": "ns3.domaindiscount24.net."},
{
"type": "SOA",
"ttl": 480,
"name": "mispo.es",
"content": "ns1.domaindiscount24.net. tech.key-systems.net. 2023012324 10800 3600 604800 3600",
},
],
"security": {"spf": False, "dkim": False, "dmarc": False, "dnssec": False, "caa": False},
"finding_types": [],
}
}


@pytest.fixture
def rocky_health():
ServiceHealth(
Expand Down
68 changes: 43 additions & 25 deletions rocky/tests/katalogus/test_katalogus.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

def test_valid_plugin_id():
with pytest.raises(ValueError):
valid_plugin_id("123")
valid_plugin_id("test test")

with pytest.raises(ValueError):
valid_plugin_id("test$test")

assert valid_plugin_id("123") == "123"
assert valid_plugin_id("test-test") == "test-test"


Expand All @@ -28,38 +30,46 @@ def test_valid_organization_code():


@pytest.mark.parametrize("member", ["superuser_member", "admin_member", "redteam_member", "client_member"])
def katalogus_plugin_listing(request, rf, member, mocker):
def test_katalogus_plugin_listing(request, rf, member, mocker):
plugins = get_plugins_data()
mock_requests = mocker.patch("katalogus.client.httpx")
mock_response = mocker.MagicMock()
mock_requests.Session().get.return_value = mock_response
mock_requests.Client().get.return_value = mock_response
mock_response.json.return_value = plugins
member = request.getfixturevalue(member)

response = KATalogusView.as_view()(
setup_request(rf.get("katalogus"), member.user), organization_code=member.organization.code
)
request = setup_request(rf.get("all_plugins_list"), member.user)
request.resolver_match = mocker.Mock(url_name="all_plugins_list")
response = KATalogusView.as_view()(request, organization_code=member.organization.code)
assert response.status_code == 200
assertContains(response, "KAT-alogus")
assertContains(response, "An overview of all available plugins.")

# active toolbar, only one link is active, "All"
assertContains(
response, '<li aria-current="page"><a href="/en/' + member.organization.code + '/kat-alogus/">All</a></li>'
response,
'<li aria-current="page"><a href="/en/'
+ member.organization.code
+ '/kat-alogus/plugins/all/grid/">All</a></li>',
html=True,
)
assertNotContains(
response, '<li aria-current="page"><a href="/en/' + member.organization.code + '/kat-alogus/">Boefjes</a></li>'
response,
'<li aria-current="page"><a href="/en/' + member.organization.code + '/kat-alogus/">Boefjes</a></li>',
html=True,
)
assertNotContains(
response,
'<li aria-current="page"><a href="/en/' + member.organization.code + '/kat-alogus/">Normalizers</a></li>',
html=True,
)
assertNotContains(
response,
'<li aria-current="page"><a href="/en/' + member.organization.code + '/kat-alogus/">About plugins</a></li>',
html=True,
)

assertContains(response, str(len(plugins)) + " Plugins available")
assertContains(response, f"<strong>{len(plugins)}</strong>Plugins available", html=True)

# All plugins shows Boefjes and Normalizers, checking if one of each is available
assertContains(response, "kat_adr_finding_types_normalize")
Expand All @@ -70,56 +80,64 @@ def katalogus_plugin_listing(request, rf, member, mocker):


@pytest.mark.parametrize("member", ["superuser_member", "admin_member", "redteam_member", "client_member"])
def katalogus_plugin_listing_boefjes(request, rf, member, mocker):
def test_katalogus_plugin_listing_boefjes(request, rf, member, mocker):
boefjes = get_boefjes_data()
mock_requests = mocker.patch("katalogus.client.httpx")
mock_response = mocker.MagicMock()
mock_requests.Session().get.return_value = mock_response
mock_requests.Client().get.return_value = mock_response
mock_response.json.return_value = boefjes
member = request.getfixturevalue(member)

response = BoefjeListView.as_view()(
setup_request(rf.get("boefjes_list"), member.user), organization_code=member.organization.code
)
request = setup_request(rf.get("boefjes_list"), member.user)
request.resolver_match = mocker.Mock(url_name="boefjes_list")
response = BoefjeListView.as_view()(request, organization_code=member.organization.code)

assert response.status_code == 200
assertContains(response, "Boefjes")
assertContains(
response, '<li aria-current="page"><a href="/en/' + member.organization.code + '/kat-alogus/">Boefjes</a></li>'
response,
'<li aria-current="page"><a href="/en/'
+ member.organization.code
+ '/kat-alogus/plugins/boefjes/grid/">Boefjes</a></li>',
html=True,
)
assertContains(response, str(len(boefjes)) + " Boefjes available")
assertContains(response, f"<strong>{len(boefjes)}</strong>Boefjes available", html=True)
assertNotContains(response, '<span class="label-plugin-type normalizer">Normalizer</span>')
assertContains(response, '<span class="label-plugin-type boefje">Boefje</span>')
assertContains(response, "ssl-certificates")


@pytest.mark.parametrize("member", ["superuser_member", "admin_member", "redteam_member", "client_member"])
def katalogus_plugin_listing_normalizers(request, rf, member, mocker):
def test_katalogus_plugin_listing_normalizers(request, rf, member, mocker):
normalizers = get_normalizers_data()
mock_requests = mocker.patch("katalogus.client.httpx")
mock_response = mocker.MagicMock()
mock_requests.Session().get.return_value = mock_response
mock_requests.Client().get.return_value = mock_response
mock_response.json.return_value = normalizers
member = request.getfixturevalue(member)

response = NormalizerListView.as_view()(
setup_request(rf.get("normalizers_list"), member.user), organization_code=member.organization.code
)
request = setup_request(rf.get("normalizers_list"), member.user)
request.resolver_match = mocker.Mock(url_name="normalizers_list")
response = NormalizerListView.as_view()(request, organization_code=member.organization.code)
assert response.status_code == 200

assertContains(response, "Normalizers")
assertContains(
response,
'<li aria-current="page"><a href="/en/' + member.organization.code + '/kat-alogus/">Normalizers</a></li>',
'<li aria-current="page"><a href="/en/'
+ member.organization.code
+ '/kat-alogus/plugins/normalizers/grid/">Normalizers</a></li>',
html=True,
)
assertContains(response, str(len(normalizers)) + " Normalizers available")
assertContains(response, f"<strong>{len(normalizers)}</strong>Normalizers available", html=True)
assertContains(response, '<span class="label-plugin-type normalizer">Normalizer</span>')
assertNotContains(response, '<span class="label-plugin-type boefje">Boefje</span>')
assertNotContains(response, "ssl-certificates")
assertNotContains(response, "binaryedge")
assertContains(response, "binaryedge")


@pytest.mark.parametrize("member", ["superuser_member", "admin_member", "redteam_member", "client_member"])
def katalogus_about_plugins(request, rf, member):
def test_katalogus_about_plugins(request, rf, member):
member = request.getfixturevalue(member)

response = AboutPluginsView.as_view()(
Expand Down

0 comments on commit d283f14

Please sign in to comment.