From cd65842c092c0753f4678bc09d26e9073e69d945 Mon Sep 17 00:00:00 2001 From: Nassim Tabchiche Date: Wed, 14 Feb 2024 18:12:57 +0100 Subject: [PATCH] Write API tests for /policies --- backend/app_tests/api/test_api_policies.py | 206 +++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 backend/app_tests/api/test_api_policies.py diff --git a/backend/app_tests/api/test_api_policies.py b/backend/app_tests/api/test_api_policies.py new file mode 100644 index 0000000000..4e0feff4c5 --- /dev/null +++ b/backend/app_tests/api/test_api_policies.py @@ -0,0 +1,206 @@ +import pytest +from rest_framework.test import APIClient +from core.models import SecurityFunction, Policy +from iam.models import Folder + +from test_api import EndpointTestsQueries + +# Generic security measure data for tests +POLICY_NAME = "Test Security Measure" +POLICY_DESCRIPTION = "Test Description" +POLICY_STATUS = ("planned", "Planned") +POLICY_STATUS2 = ("active", "Active") +POLICY_EFFORT = ("L", "Large") +POLICY_EFFORT2 = ("M", "Medium") +POLICY_LINK = "https://example.com" +POLICY_ETA = "2024-01-01" + + +@pytest.mark.django_db +class TestPolicysUnauthenticated: + """Perform tests on Security Measures API endpoint without authentication""" + + client = APIClient() + + def test_get_security_measures(self): + """test to get security measures from the API without authentication""" + + EndpointTestsQueries.get_object( + self.client, + "Security measures", + Policy, + { + "name": POLICY_NAME, + "description": POLICY_DESCRIPTION, + "folder": Folder.objects.create(name="test"), + }, + ) + + def test_create_security_measures(self): + """test to create security measures with the API without authentication""" + + EndpointTestsQueries.create_object( + self.client, + "Security measures", + Policy, + { + "name": POLICY_NAME, + "description": POLICY_DESCRIPTION, + "folder": Folder.objects.create(name="test").id, + }, + ) + + def test_update_security_measures(self): + """test to update security measures with the API without authentication""" + + EndpointTestsQueries.update_object( + self.client, + "Security measures", + Policy, + { + "name": POLICY_NAME, + "description": POLICY_DESCRIPTION, + "folder": Folder.objects.create(name="test"), + }, + { + "name": "new " + POLICY_NAME, + "description": "new " + POLICY_DESCRIPTION, + "folder": Folder.objects.create(name="test2").id, + }, + ) + + def test_delete_security_measures(self): + """test to delete security measures with the API without authentication""" + + EndpointTestsQueries.delete_object( + self.client, + "Security measures", + Policy, + { + "name": POLICY_NAME, + "folder": Folder.objects.create(name="test"), + }, + ) + + +@pytest.mark.django_db +class TestPolicysAuthenticated: + """Perform tests on Security Measures API endpoint with authentication""" + + def test_get_security_measures(self, authenticated_client): + """test to get security measures from the API with authentication""" + + EndpointTestsQueries.Auth.get_object( + authenticated_client, + "Security measures", + Policy, + { + "name": POLICY_NAME, + "description": POLICY_DESCRIPTION, + "status": POLICY_STATUS[0], + "link": POLICY_LINK, + "eta": POLICY_ETA, + "effort": POLICY_EFFORT[0], + "folder": Folder.get_root_folder(), + }, + { + "folder": {"str": Folder.get_root_folder().name}, + "security_function": None, + "status": POLICY_STATUS[1], + "effort": POLICY_EFFORT[1], + }, + ) + + def test_create_security_measures(self, authenticated_client): + """test to create security measures with the API with authentication""" + + security_function = SecurityFunction.objects.create( + name="test", typical_evidence={}, folder=Folder.objects.create(name="test") + ) + + EndpointTestsQueries.Auth.create_object( + authenticated_client, + "Security measures", + Policy, + { + "name": POLICY_NAME, + "description": POLICY_DESCRIPTION, + "status": POLICY_STATUS[0], + "link": POLICY_LINK, + "eta": POLICY_ETA, + "effort": POLICY_EFFORT[0], + "folder": str(Folder.get_root_folder().id), + }, + { + "folder": {"str": Folder.get_root_folder().name}, + "status": POLICY_STATUS[1], + "effort": POLICY_EFFORT[1], + }, + ) + + def test_update_security_measures(self, authenticated_client): + """test to update security measures with the API with authentication""" + + folder = Folder.objects.create(name="test") + security_function = SecurityFunction.objects.create( + name="test", typical_evidence={}, folder=folder + ) + + EndpointTestsQueries.Auth.update_object( + authenticated_client, + "Security measures", + Policy, + { + "name": POLICY_NAME, + "description": POLICY_DESCRIPTION, + "status": POLICY_STATUS[0], + "link": POLICY_LINK, + "eta": POLICY_ETA, + "effort": POLICY_EFFORT[0], + "folder": Folder.get_root_folder(), + }, + { + "name": "new " + POLICY_NAME, + "description": "new " + POLICY_DESCRIPTION, + "status": POLICY_STATUS2[0], + "link": "new " + POLICY_LINK, + "eta": "2025-01-01", + "effort": POLICY_EFFORT2[0], + "folder": str(folder.id), + }, + { + "folder": {"str": Folder.get_root_folder().name}, + "status": POLICY_STATUS[1], + "effort": POLICY_EFFORT[1], + }, + ) + + def test_delete_security_measures(self, authenticated_client): + """test to delete security measures with the API with authentication""" + + EndpointTestsQueries.Auth.delete_object( + authenticated_client, + "Security measures", + Policy, + { + "name": POLICY_NAME, + "folder": Folder.objects.create(name="test"), + }, + ) + + def test_get_effort_choices(self, authenticated_client): + """test to get security measures effort choices from the API with authentication""" + + EndpointTestsQueries.Auth.get_object_options( + authenticated_client, "Security measures", "effort", Policy.EFFORT + ) + + def test_get_status_choices(self, authenticated_client): + """test to get security measures status choices from the API with authentication""" + + EndpointTestsQueries.Auth.get_object_options( + authenticated_client, + "Security measures", + "status", + Policy.Status.choices, + )