From c64574900eeba95a3ed9d595166b7f995f661cea Mon Sep 17 00:00:00 2001 From: Tomas Kubla Date: Thu, 30 Mar 2023 11:51:55 +0000 Subject: [PATCH 1/3] Rename unittest --- ...py => test_apiv2_methods_and_endpoints.py} | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) rename unittests/{test_apiv2_methods.py => test_apiv2_methods_and_endpoints.py} (67%) diff --git a/unittests/test_apiv2_methods.py b/unittests/test_apiv2_methods_and_endpoints.py similarity index 67% rename from unittests/test_apiv2_methods.py rename to unittests/test_apiv2_methods_and_endpoints.py index 4f6694ef30f..119af21cda5 100644 --- a/unittests/test_apiv2_methods.py +++ b/unittests/test_apiv2_methods_and_endpoints.py @@ -1,7 +1,8 @@ from dojo.urls import v2_api from .dojo_test_case import DojoTestCase from .test_rest_framework import get_open_api3_json_schema, BASE_API_URL - +import django.apps +from dojo.api_v2 import serializers class ApiEndpointMethods(DojoTestCase): fixtures = ['dojo_testdata.json'] @@ -44,3 +45,26 @@ def test_is_defined(self): .get('get'), f"Endpoint: {reg}, Method: get - delete_preview", ) + + +class ApiEndpoints(DojoTestCase): + fixtures = ['dojo_testdata.json'] + + def setUp(self): + super().setUp() + + self.used_models = [] + for serializer in serializers.__dict__.values(): + if hasattr(serializer, 'Meta'): + if hasattr(serializer.Meta, 'model'): + self.used_models.append(serializer.Meta.model) + + def test_is_defined(self): + for subclass in django.apps.apps.get_models(): + if subclass.__module__ == 'dojo.models': + if (subclass.__name__[:9] == "Tagulous_") and (subclass.__name__[-5:] == "_tags"): + continue + if subclass.__name__ in ['Alerts']: + continue + with self.subTest(subclass=subclass): + self.assertIn(subclass, self.used_models) From 379642eccf33f61153a21ef687e93dc38ce74fe7 Mon Sep 17 00:00:00 2001 From: Tomas Kubla Date: Wed, 29 Nov 2023 11:07:01 +0100 Subject: [PATCH 2/3] Define exceptions for now --- unittests/test_apiv2_methods_and_endpoints.py | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/unittests/test_apiv2_methods_and_endpoints.py b/unittests/test_apiv2_methods_and_endpoints.py index 119af21cda5..65b7ae5dd48 100644 --- a/unittests/test_apiv2_methods_and_endpoints.py +++ b/unittests/test_apiv2_methods_and_endpoints.py @@ -3,6 +3,34 @@ from .test_rest_framework import get_open_api3_json_schema, BASE_API_URL import django.apps from dojo.api_v2 import serializers +from dojo.models import ( + Contact, + Product_Line, + Report_Type, + CWE, + BurpRawRequestResponse, + FileAccessToken, + Announcement, + UserAnnouncement, + BannerConf, + GITHUB_Conf, + GITHUB_Issue, + GITHUB_Clone, + GITHUB_Details_Cache, + GITHUB_PKey, + Tool_Product_History, + Objects_Review, + Objects_Product, + Testing_Guide_Category, + Testing_Guide, + Benchmark_Type, + Benchmark_Category, + Benchmark_Requirement, + Benchmark_Product, + Benchmark_Product_Summary, + Choice, +) + class ApiEndpointMethods(DojoTestCase): fixtures = ['dojo_testdata.json'] @@ -58,6 +86,33 @@ def setUp(self): if hasattr(serializer, 'Meta'): if hasattr(serializer.Meta, 'model'): self.used_models.append(serializer.Meta.model) + self.no_api_models = [ # TODO: these models are excluded from check for now but implementation is needed + Contact, + Product_Line, + Report_Type, + CWE, + BurpRawRequestResponse, + FileAccessToken, + Announcement, + UserAnnouncement, + BannerConf, + GITHUB_Conf, + GITHUB_Issue, + GITHUB_Clone, + GITHUB_Details_Cache, + GITHUB_PKey, + Tool_Product_History, + Objects_Review, + Objects_Product, + Testing_Guide_Category, + Testing_Guide, + Benchmark_Type, + Benchmark_Category, + Benchmark_Requirement, + Benchmark_Product, + Benchmark_Product_Summary, + Choice, + ] def test_is_defined(self): for subclass in django.apps.apps.get_models(): @@ -67,4 +122,7 @@ def test_is_defined(self): if subclass.__name__ in ['Alerts']: continue with self.subTest(subclass=subclass): - self.assertIn(subclass, self.used_models) + if subclass in self.used_models: + self.assertNotIn(subclass, self.no_api_models, "Thank you, you just implemented API endpoint for the model which was needed. Please remove it from exception list 'self.no_api_models'") + if subclass not in self.no_api_models: + self.assertIn(subclass, self.used_models, "API endpoint for the managing mentioned model is need") From e67a4e70f5c10b52883cbbc478900e58c7c9b7cf Mon Sep 17 00:00:00 2001 From: Tomas Kubla Date: Thu, 11 Jan 2024 17:47:03 +0100 Subject: [PATCH 3/3] Announcement was implemented --- unittests/test_apiv2_methods_and_endpoints.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/unittests/test_apiv2_methods_and_endpoints.py b/unittests/test_apiv2_methods_and_endpoints.py index 65b7ae5dd48..8163de19534 100644 --- a/unittests/test_apiv2_methods_and_endpoints.py +++ b/unittests/test_apiv2_methods_and_endpoints.py @@ -10,7 +10,6 @@ CWE, BurpRawRequestResponse, FileAccessToken, - Announcement, UserAnnouncement, BannerConf, GITHUB_Conf, @@ -93,7 +92,6 @@ def setUp(self): CWE, BurpRawRequestResponse, FileAccessToken, - Announcement, UserAnnouncement, BannerConf, GITHUB_Conf,