From d5ba08526c74e80899a0dab8a5a55a12617f377b Mon Sep 17 00:00:00 2001 From: hblankenship Date: Fri, 8 Nov 2024 10:06:56 -0600 Subject: [PATCH 1/9] still not there --- dojo/api_v2/serializers.py | 7 +++++ dojo/api_v2/views.py | 64 ++++++++++++++++++++++++++++++++++++++ dojo/urls.py | 3 ++ 3 files changed, 74 insertions(+) diff --git a/dojo/api_v2/serializers.py b/dojo/api_v2/serializers.py index 2680e8f1adf..018f3361ffd 100644 --- a/dojo/api_v2/serializers.py +++ b/dojo/api_v2/serializers.py @@ -2997,6 +2997,13 @@ class Meta: fields = "__all__" +class QuestionnaireEngagementLinkSerializer(serializers.ModelSerializer): + + class Meta: + model = Answered_Survey + exclude = ("survey", "completed", "answered_on", "assignee", "responder") + + class QuestionnaireGeneralSurveySerializer(serializers.ModelSerializer): survey = QuestionnaireEngagementSurveySerializer() diff --git a/dojo/api_v2/views.py b/dojo/api_v2/views.py index 52978f3b241..9eb61826d27 100644 --- a/dojo/api_v2/views.py +++ b/dojo/api_v2/views.py @@ -3057,6 +3057,70 @@ class QuestionnaireEngagementSurveyViewSet( def get_queryset(self): return Engagement_Survey.objects.all().order_by("id") + @extend_schema( + methods=["POST"], + request=serializers.QuestionnaireEngagementLinkSerializer, + responses={status.HTTP_200_OK: serializers.QuestionnaireAnsweredSurveySerializer}# QuestionnaireAnsweredSurveySerializer} #QuestionnaireEngagementSurveySerializer}, # QuestionAnsweredSurveySerializer? + ) + @action(detail=True, methods=["post"]) + def link_engagement(self, request, pk=None): + # replace these two things with the right things.... + + if request.method == "POST": + engagement = serializers.EngagementSerializer(data=request.data) + if engagement.is_valid(): + msg = f"{engagement.validated_data['id']}" + raise Exception(msg) + + answered = Answered_Survey.objects.get_or_create() + # have engagement, need to query for the + # finding_close = serializers.FindingCloseSerializer( + # data=request.data, + # ) + # if finding_close.is_valid(): + # finding.is_mitigated = finding_close.validated_data[ + # "is_mitigated" + # ] + # if settings.EDITABLE_MITIGATED_DATA: + # finding.mitigated = ( + # finding_close.validated_data["mitigated"] + # or timezone.now() + # ) + # else: + # finding.mitigated = timezone.now() + # finding.mitigated_by = request.user + # finding.active = False + # finding.false_p = finding_close.validated_data.get( + # "false_p", False, + # ) + # finding.duplicate = finding_close.validated_data.get( + # "duplicate", False, + # ) + # finding.out_of_scope = finding_close.validated_data.get( + # "out_of_scope", False, + # ) + + # endpoints_status = finding.status_finding.all() + # for e_status in endpoints_status: + # e_status.mitigated_by = request.user + # if settings.EDITABLE_MITIGATED_DATA: + # e_status.mitigated_time = ( + # finding_close.validated_data["mitigated"] + # or timezone.now() + # ) + # else: + # e_status.mitigated_time = timezone.now() + # e_status.mitigated = True + # e_status.last_modified = timezone.now() + # e_status.save() + # finding.save() + # else: + # return Response( + # finding_close.errors, status=status.HTTP_400_BAD_REQUEST, + # ) + #serialized_finding = serializers.FindingCloseSerializer(finding) + serialized_answered_survey = serializers.QuestionnaireAnsweredSurveySerializer(answered) + return Response(serialized_answered_survey.data) @extend_schema_view(**schema_with_prefetch()) class QuestionnaireAnsweredSurveyViewSet( diff --git a/dojo/urls.py b/dojo/urls.py index 1e36b67d4f8..8f4f748c535 100644 --- a/dojo/urls.py +++ b/dojo/urls.py @@ -177,6 +177,9 @@ v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="engagement_survey") v2_api.register(r"questionnaire_general_questionnaires", QuestionnaireGeneralSurveyViewSet, basename="general_survey") v2_api.register(r"questionnaire_questions", QuestionnaireQuestionViewSet, basename="question") +#/api/v2/questionnaire_engagement_questionnaires/{id}/link_engagement +#v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="link_engagement") + ur = [] ur += dev_env_urls ur += endpoint_urls From e569ebbec40fd2326df82620864d016e1a78a75c Mon Sep 17 00:00:00 2001 From: hblankenship Date: Fri, 8 Nov 2024 13:49:52 -0600 Subject: [PATCH 2/9] Link 'Answered' Survey to Engagement --- dojo/api_v2/serializers.py | 7 ---- dojo/api_v2/views.py | 81 ++++++++++---------------------------- dojo/urls.py | 3 +- 3 files changed, 21 insertions(+), 70 deletions(-) diff --git a/dojo/api_v2/serializers.py b/dojo/api_v2/serializers.py index 018f3361ffd..2680e8f1adf 100644 --- a/dojo/api_v2/serializers.py +++ b/dojo/api_v2/serializers.py @@ -2997,13 +2997,6 @@ class Meta: fields = "__all__" -class QuestionnaireEngagementLinkSerializer(serializers.ModelSerializer): - - class Meta: - model = Answered_Survey - exclude = ("survey", "completed", "answered_on", "assignee", "responder") - - class QuestionnaireGeneralSurveySerializer(serializers.ModelSerializer): survey = QuestionnaireEngagementSurveySerializer() diff --git a/dojo/api_v2/views.py b/dojo/api_v2/views.py index 9eb61826d27..301f11458d9 100644 --- a/dojo/api_v2/views.py +++ b/dojo/api_v2/views.py @@ -3058,70 +3058,29 @@ def get_queryset(self): return Engagement_Survey.objects.all().order_by("id") @extend_schema( - methods=["POST"], - request=serializers.QuestionnaireEngagementLinkSerializer, - responses={status.HTTP_200_OK: serializers.QuestionnaireAnsweredSurveySerializer}# QuestionnaireAnsweredSurveySerializer} #QuestionnaireEngagementSurveySerializer}, # QuestionAnsweredSurveySerializer? + request=OpenApiTypes.NONE, + parameters=[ + OpenApiParameter( + "engagement_id", OpenApiTypes.INT, OpenApiParameter.PATH, + ), + ], + responses={status.HTTP_200_OK: serializers.QuestionnaireAnsweredSurveySerializer}, ) - @action(detail=True, methods=["post"]) - def link_engagement(self, request, pk=None): - # replace these two things with the right things.... - - if request.method == "POST": - engagement = serializers.EngagementSerializer(data=request.data) - if engagement.is_valid(): - msg = f"{engagement.validated_data['id']}" - raise Exception(msg) - - answered = Answered_Survey.objects.get_or_create() - # have engagement, need to query for the - # finding_close = serializers.FindingCloseSerializer( - # data=request.data, - # ) - # if finding_close.is_valid(): - # finding.is_mitigated = finding_close.validated_data[ - # "is_mitigated" - # ] - # if settings.EDITABLE_MITIGATED_DATA: - # finding.mitigated = ( - # finding_close.validated_data["mitigated"] - # or timezone.now() - # ) - # else: - # finding.mitigated = timezone.now() - # finding.mitigated_by = request.user - # finding.active = False - # finding.false_p = finding_close.validated_data.get( - # "false_p", False, - # ) - # finding.duplicate = finding_close.validated_data.get( - # "duplicate", False, - # ) - # finding.out_of_scope = finding_close.validated_data.get( - # "out_of_scope", False, - # ) - - # endpoints_status = finding.status_finding.all() - # for e_status in endpoints_status: - # e_status.mitigated_by = request.user - # if settings.EDITABLE_MITIGATED_DATA: - # e_status.mitigated_time = ( - # finding_close.validated_data["mitigated"] - # or timezone.now() - # ) - # else: - # e_status.mitigated_time = timezone.now() - # e_status.mitigated = True - # e_status.last_modified = timezone.now() - # e_status.save() - # finding.save() - # else: - # return Response( - # finding_close.errors, status=status.HTTP_400_BAD_REQUEST, - # ) - #serialized_finding = serializers.FindingCloseSerializer(finding) - serialized_answered_survey = serializers.QuestionnaireAnsweredSurveySerializer(answered) + @action( + detail=True, methods=["post"], url_path=r"link_engagement/(?P\d+)", + ) + def link_engagement(self, request, pk, engagement_id): + # Get the answered survey + engagement_survey = self.get_object() + # Safely get the engagement + engagement = get_object_or_404(Engagement.objects, pk=engagement_id) + # Link the engagement + answered_survey, _ = Answered_Survey.objects.get_or_create(engagement=engagement, survey=engagement_survey) + # Send a favorable response + serialized_answered_survey = serializers.QuestionnaireAnsweredSurveySerializer(answered_survey) return Response(serialized_answered_survey.data) + @extend_schema_view(**schema_with_prefetch()) class QuestionnaireAnsweredSurveyViewSet( prefetch.PrefetchListMixin, diff --git a/dojo/urls.py b/dojo/urls.py index 8f4f748c535..6c74b18a06c 100644 --- a/dojo/urls.py +++ b/dojo/urls.py @@ -177,8 +177,7 @@ v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="engagement_survey") v2_api.register(r"questionnaire_general_questionnaires", QuestionnaireGeneralSurveyViewSet, basename="general_survey") v2_api.register(r"questionnaire_questions", QuestionnaireQuestionViewSet, basename="question") -#/api/v2/questionnaire_engagement_questionnaires/{id}/link_engagement -#v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="link_engagement") +v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="link_engagement") ur = [] ur += dev_env_urls From c17ddb013ee919114b00b8df2982e99ae8b6ce4c Mon Sep 17 00:00:00 2001 From: hblankenship Date: Fri, 8 Nov 2024 14:23:51 -0600 Subject: [PATCH 3/9] initial test item --- unittests/test_rest_framework.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/unittests/test_rest_framework.py b/unittests/test_rest_framework.py index 8b2c75e7629..98b692c69f1 100644 --- a/unittests/test_rest_framework.py +++ b/unittests/test_rest_framework.py @@ -3003,6 +3003,19 @@ def __init__(self, *args, **kwargs): BaseClass.RESTEndpointTest.__init__(self, *args, **kwargs) +class QuestionnaireEngagementQuestionnairesLinkEngagementTest(BaseClass.BaseClassTest): + fixtures = ["questionnaire_testdata.json"] + + def __init__(self, *args, **kwargs): + self.endpoint_model = Answered_Survey + self.endpoint_path = "questionnaire_engagement_questionnaires" + self.viewname = "link_engagement" + self.viewset = QuestionnaireAnsweredSurveyViewSet + self.test_type = TestType.STANDARD + self.deleted_objects = 5 + BaseClass.RESTEndpointTest.__init__(self, *args, **kwargs) + + class AnnouncementTest(BaseClass.BaseClassTest): fixtures = ["dojo_testdata.json"] From 2747a992d25f7cb965c0dae3fdeb057074214b21 Mon Sep 17 00:00:00 2001 From: hblankenship Date: Sun, 10 Nov 2024 15:18:06 -0600 Subject: [PATCH 4/9] remove the registration --- dojo/urls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dojo/urls.py b/dojo/urls.py index 6c74b18a06c..54b547cf95c 100644 --- a/dojo/urls.py +++ b/dojo/urls.py @@ -177,7 +177,7 @@ v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="engagement_survey") v2_api.register(r"questionnaire_general_questionnaires", QuestionnaireGeneralSurveyViewSet, basename="general_survey") v2_api.register(r"questionnaire_questions", QuestionnaireQuestionViewSet, basename="question") -v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="link_engagement") +#v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="link_engagement") ur = [] ur += dev_env_urls From 01abce60c9d45c7e15922f5d2ad3b705d0de24ae Mon Sep 17 00:00:00 2001 From: hblankenship Date: Sun, 10 Nov 2024 15:31:50 -0600 Subject: [PATCH 5/9] fix ruff --- dojo/urls.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dojo/urls.py b/dojo/urls.py index 54b547cf95c..75a3c166a62 100644 --- a/dojo/urls.py +++ b/dojo/urls.py @@ -177,7 +177,6 @@ v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="engagement_survey") v2_api.register(r"questionnaire_general_questionnaires", QuestionnaireGeneralSurveyViewSet, basename="general_survey") v2_api.register(r"questionnaire_questions", QuestionnaireQuestionViewSet, basename="question") -#v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="link_engagement") ur = [] ur += dev_env_urls From ff4933d49d35a0cd10508e0fe28733e0f4577dbf Mon Sep 17 00:00:00 2001 From: hblankenship Date: Mon, 11 Nov 2024 07:09:42 -0600 Subject: [PATCH 6/9] re-add url and change base --- dojo/urls.py | 1 + unittests/test_rest_framework.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/dojo/urls.py b/dojo/urls.py index 75a3c166a62..6c74b18a06c 100644 --- a/dojo/urls.py +++ b/dojo/urls.py @@ -177,6 +177,7 @@ v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="engagement_survey") v2_api.register(r"questionnaire_general_questionnaires", QuestionnaireGeneralSurveyViewSet, basename="general_survey") v2_api.register(r"questionnaire_questions", QuestionnaireQuestionViewSet, basename="question") +v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="link_engagement") ur = [] ur += dev_env_urls diff --git a/unittests/test_rest_framework.py b/unittests/test_rest_framework.py index 98b692c69f1..99c3444cb77 100644 --- a/unittests/test_rest_framework.py +++ b/unittests/test_rest_framework.py @@ -3003,7 +3003,7 @@ def __init__(self, *args, **kwargs): BaseClass.RESTEndpointTest.__init__(self, *args, **kwargs) -class QuestionnaireEngagementQuestionnairesLinkEngagementTest(BaseClass.BaseClassTest): +class QuestionnaireEngagementQuestionnairesLinkEngagementTest(BaseClass.RESTEndpointTest): fixtures = ["questionnaire_testdata.json"] def __init__(self, *args, **kwargs): From b2e1083f0a1db8c435c0511814fb62fe94e8da24 Mon Sep 17 00:00:00 2001 From: hblankenship Date: Mon, 11 Nov 2024 07:29:24 -0600 Subject: [PATCH 7/9] Add an actual test --- unittests/test_rest_framework.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/unittests/test_rest_framework.py b/unittests/test_rest_framework.py index 99c3444cb77..479f23c99b8 100644 --- a/unittests/test_rest_framework.py +++ b/unittests/test_rest_framework.py @@ -3015,6 +3015,11 @@ def __init__(self, *args, **kwargs): self.deleted_objects = 5 BaseClass.RESTEndpointTest.__init__(self, *args, **kwargs) + def test_link_engagement_questionnaire(self): + end_url = self.url + "4/link_engagement/2/" + result = self.client.post(end_url) + self.assertEqual(result.status_code, status.HTTP_200_OK, f"Failed to link enagement survey to engagement: {result.content} on {end_url}") + class AnnouncementTest(BaseClass.BaseClassTest): fixtures = ["dojo_testdata.json"] From a8a26d91fecd02e3c15ff7125bb9d6de772e4d12 Mon Sep 17 00:00:00 2001 From: Cody Maffucci <46459665+Maffooch@users.noreply.github.com> Date: Fri, 15 Nov 2024 12:29:21 -0600 Subject: [PATCH 8/9] Remove extra URL entry --- dojo/urls.py | 1 - unittests/test_rest_framework.py | 7 ++++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/dojo/urls.py b/dojo/urls.py index 6c74b18a06c..75a3c166a62 100644 --- a/dojo/urls.py +++ b/dojo/urls.py @@ -177,7 +177,6 @@ v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="engagement_survey") v2_api.register(r"questionnaire_general_questionnaires", QuestionnaireGeneralSurveyViewSet, basename="general_survey") v2_api.register(r"questionnaire_questions", QuestionnaireQuestionViewSet, basename="question") -v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="link_engagement") ur = [] ur += dev_env_urls diff --git a/unittests/test_rest_framework.py b/unittests/test_rest_framework.py index 479f23c99b8..a04d73e99ae 100644 --- a/unittests/test_rest_framework.py +++ b/unittests/test_rest_framework.py @@ -2989,6 +2989,11 @@ def __init__(self, *args, **kwargs): self.deleted_objects = 5 BaseClass.RESTEndpointTest.__init__(self, *args, **kwargs) + def test_link_engagement_questionnaire(self): + end_url = self.url + "4/link_engagement/2/" + result = self.client.post(end_url) + self.assertEqual(result.status_code, status.HTTP_200_OK, f"Failed to link enagement survey to engagement: {result.content} on {end_url}") + class AnsweredSurveyTest(BaseClass.BaseClassTest): fixtures = ["questionnaire_testdata.json"] @@ -3018,7 +3023,7 @@ def __init__(self, *args, **kwargs): def test_link_engagement_questionnaire(self): end_url = self.url + "4/link_engagement/2/" result = self.client.post(end_url) - self.assertEqual(result.status_code, status.HTTP_200_OK, f"Failed to link enagement survey to engagement: {result.content} on {end_url}") + self.assertEqual(result.status_code, status.HTTP_200_OK, f"Failed to link engagement survey to engagement: {result.content} on {end_url}") class AnnouncementTest(BaseClass.BaseClassTest): From 863fbcb2811f49ae3dc73e7a42b1e7f0358a9e34 Mon Sep 17 00:00:00 2001 From: Cody Maffucci <46459665+Maffooch@users.noreply.github.com> Date: Fri, 15 Nov 2024 12:30:15 -0600 Subject: [PATCH 9/9] Delete extra test suite --- dojo/urls.py | 1 - unittests/test_rest_framework.py | 18 ------------------ 2 files changed, 19 deletions(-) diff --git a/dojo/urls.py b/dojo/urls.py index 75a3c166a62..1e36b67d4f8 100644 --- a/dojo/urls.py +++ b/dojo/urls.py @@ -177,7 +177,6 @@ v2_api.register(r"questionnaire_engagement_questionnaires", QuestionnaireEngagementSurveyViewSet, basename="engagement_survey") v2_api.register(r"questionnaire_general_questionnaires", QuestionnaireGeneralSurveyViewSet, basename="general_survey") v2_api.register(r"questionnaire_questions", QuestionnaireQuestionViewSet, basename="question") - ur = [] ur += dev_env_urls ur += endpoint_urls diff --git a/unittests/test_rest_framework.py b/unittests/test_rest_framework.py index a04d73e99ae..8a47c4288b3 100644 --- a/unittests/test_rest_framework.py +++ b/unittests/test_rest_framework.py @@ -3008,24 +3008,6 @@ def __init__(self, *args, **kwargs): BaseClass.RESTEndpointTest.__init__(self, *args, **kwargs) -class QuestionnaireEngagementQuestionnairesLinkEngagementTest(BaseClass.RESTEndpointTest): - fixtures = ["questionnaire_testdata.json"] - - def __init__(self, *args, **kwargs): - self.endpoint_model = Answered_Survey - self.endpoint_path = "questionnaire_engagement_questionnaires" - self.viewname = "link_engagement" - self.viewset = QuestionnaireAnsweredSurveyViewSet - self.test_type = TestType.STANDARD - self.deleted_objects = 5 - BaseClass.RESTEndpointTest.__init__(self, *args, **kwargs) - - def test_link_engagement_questionnaire(self): - end_url = self.url + "4/link_engagement/2/" - result = self.client.post(end_url) - self.assertEqual(result.status_code, status.HTTP_200_OK, f"Failed to link engagement survey to engagement: {result.content} on {end_url}") - - class AnnouncementTest(BaseClass.BaseClassTest): fixtures = ["dojo_testdata.json"]