diff --git a/unittests/test_apiv2_methods.py b/unittests/test_apiv2_methods.py deleted file mode 100644 index 4f6694ef30f..00000000000 --- a/unittests/test_apiv2_methods.py +++ /dev/null @@ -1,46 +0,0 @@ -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 - - -class ApiEndpointMethods(DojoTestCase): - fixtures = ['dojo_testdata.json'] - - def setUp(self): - super().setUp() - - self.schema = get_open_api3_json_schema() - - self.registry = v2_api.registry - - def test_is_defined(self): - exempt_list = [ - 'import-scan', 'reimport-scan', 'notes', 'system_settings', 'roles', - 'import-languages', 'endpoint_meta_import', 'test_types', - 'configuration_permissions', 'risk_acceptance', 'questionnaire_questions', - 'questionnaire_answers', 'questionnaire_answered_questionnaires', - 'questionnaire_engagement_questionnaires', 'questionnaire_general_questionnaires', - 'dojo_group_members', 'product_members', 'product_groups', 'product_type_groups', - 'product_type_members' - ] - for reg, _, _ in sorted(self.registry): - if reg in exempt_list: - continue - for method in ['get', 'post']: - self.assertIsNotNone( - self.schema["paths"][f'{BASE_API_URL}/{reg}/'].get(method), - f"Endpoint: {reg}, Method: {method}", - ) - - for method in ['get', 'put', 'patch', 'delete']: - self.assertIsNotNone( - self.schema["paths"][f'{BASE_API_URL}/{reg}' + '/{id}/'].get(method), - f"Endpoint: {reg}, Method: {method}", - ) - - self.assertIsNotNone( - self.schema["paths"] - .get(f'{BASE_API_URL}/{reg}' + '/{id}/delete_preview/', {}) - .get('get'), - f"Endpoint: {reg}, Method: get - delete_preview", - ) diff --git a/unittests/test_apiv2_methods_and_endpoints.py b/unittests/test_apiv2_methods_and_endpoints.py new file mode 100644 index 00000000000..8163de19534 --- /dev/null +++ b/unittests/test_apiv2_methods_and_endpoints.py @@ -0,0 +1,126 @@ +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 +from dojo.models import ( + Contact, + Product_Line, + Report_Type, + CWE, + BurpRawRequestResponse, + FileAccessToken, + 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'] + + def setUp(self): + super().setUp() + + self.schema = get_open_api3_json_schema() + + self.registry = v2_api.registry + + def test_is_defined(self): + exempt_list = [ + 'import-scan', 'reimport-scan', 'notes', 'system_settings', 'roles', + 'import-languages', 'endpoint_meta_import', 'test_types', + 'configuration_permissions', 'risk_acceptance', 'questionnaire_questions', + 'questionnaire_answers', 'questionnaire_answered_questionnaires', + 'questionnaire_engagement_questionnaires', 'questionnaire_general_questionnaires', + 'dojo_group_members', 'product_members', 'product_groups', 'product_type_groups', + 'product_type_members' + ] + for reg, _, _ in sorted(self.registry): + if reg in exempt_list: + continue + for method in ['get', 'post']: + self.assertIsNotNone( + self.schema["paths"][f'{BASE_API_URL}/{reg}/'].get(method), + f"Endpoint: {reg}, Method: {method}", + ) + + for method in ['get', 'put', 'patch', 'delete']: + self.assertIsNotNone( + self.schema["paths"][f'{BASE_API_URL}/{reg}' + '/{id}/'].get(method), + f"Endpoint: {reg}, Method: {method}", + ) + + self.assertIsNotNone( + self.schema["paths"] + .get(f'{BASE_API_URL}/{reg}' + '/{id}/delete_preview/', {}) + .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) + 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, + 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(): + 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): + 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")