diff --git a/dojo/api_v2/views.py b/dojo/api_v2/views.py index e6dd30dab4d..6dcbd3946ec 100644 --- a/dojo/api_v2/views.py +++ b/dojo/api_v2/views.py @@ -3087,6 +3087,29 @@ class QuestionnaireEngagementSurveyViewSet( def get_queryset(self): return Engagement_Survey.objects.all().order_by("id") + @extend_schema( + request=OpenApiTypes.NONE, + parameters=[ + OpenApiParameter( + "engagement_id", OpenApiTypes.INT, OpenApiParameter.PATH, + ), + ], + responses={status.HTTP_200_OK: serializers.QuestionnaireAnsweredSurveySerializer}, + ) + @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( diff --git a/unittests/test_rest_framework.py b/unittests/test_rest_framework.py index 8b2c75e7629..8a47c4288b3 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"]