From 154206303096a98ca2cde95ca284d5c4ffca4356 Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Sat, 10 Jul 2021 20:48:22 +0800 Subject: [PATCH] Move comment_case_runs view to testruns app (#913) Signed-off-by: Chenxiong Qi --- src/static/js/testrun_actions.js | 2 +- src/tcms/core/ajax.py | 28 ------------- src/tcms/testruns/forms.py | 16 +++++++- src/tcms/testruns/urls/runs_urls.py | 1 + src/tcms/testruns/views.py | 18 ++++++++ src/tcms/urls.py | 5 --- src/tests/core/test_views.py | 64 ----------------------------- src/tests/testruns/test_views.py | 63 ++++++++++++++++++++++++++++ 8 files changed, 97 insertions(+), 100 deletions(-) diff --git a/src/static/js/testrun_actions.js b/src/static/js/testrun_actions.js index 43c086df..041b6a81 100644 --- a/src/static/js/testrun_actions.js +++ b/src/static/js/testrun_actions.js @@ -1325,7 +1325,7 @@ function showCommentForm() { commentTextbox.value = ''; postRequest({ - url: '/caserun/comment-many/', + url: '/runs/case-runs/comment-many/', data: {comment: comment, run: caseRunIds}, traditional: true, }); diff --git a/src/tcms/core/ajax.py b/src/tcms/core/ajax.py index 11f6bb1f..53fb3721 100644 --- a/src/tcms/core/ajax.py +++ b/src/tcms/core/ajax.py @@ -27,9 +27,6 @@ from django.shortcuts import render from django.views import View from django.views.decorators.http import require_GET -from django.views.decorators.http import require_POST - -import tcms.comments.models from tcms.core.mailto import mailto from tcms.core.models import TCMSActionModel @@ -797,28 +794,3 @@ def _update_sortkey(self): append_changed(tcp) TestCasePlan.objects.bulk_update(changed, [self.target_field]) - - -@require_POST -def comment_case_runs(request): - """ - Add comment to one or more caseruns at a time. - """ - data = request.POST.copy() - comment = data.get("comment", None) - if not comment: - return JsonResponseBadRequest({"message": "Comments needed"}) - run_ids = [int(item) for item in data.getlist("run")] - if not run_ids: - return JsonResponseBadRequest({"message": "No runs selected."}) - case_run_ids = TestCaseRun.objects.filter(pk__in=run_ids).values_list("pk", flat=True) - if not case_run_ids: - return JsonResponseBadRequest({"message": "No caserun found."}) - tcms.comments.models.add_comment( - request.user, - "testruns.testcaserun", - case_run_ids, - comment, - request.META.get("REMOTE_ADDR"), - ) - return JsonResponse({}) diff --git a/src/tcms/testruns/forms.py b/src/tcms/testruns/forms.py index 19bf7b83..92e9b412 100644 --- a/src/tcms/testruns/forms.py +++ b/src/tcms/testruns/forms.py @@ -14,8 +14,7 @@ ) from tcms.testplans.models import TestPlan from tcms.testcases.models import TestCase -from .models import TestRun, TestCaseRunStatus - +from .models import TestRun, TestCaseRunStatus, TestCaseRun STATUS_CHOICES = (("", "---------"), ("running", "Running"), ("finished", "Finished")) @@ -308,6 +307,19 @@ class ChangeRunEnvValueForm(forms.Form): # Case run form # =========================================================================== + +class CommentCaseRunsForm(forms.Form): + run = forms.ModelMultipleChoiceField( + queryset=TestCaseRun.objects.only("pk"), + error_messages={ + "required": "No test case run id is passed to comment out.", + "invalid_choice": "Test case run %(value)s does not exist.", + "invalid_pk_value": "%(pk)s is not a valid test case run id.", + }, + ) + comment = forms.CharField(error_messages={"required": "Comment is needed."}) + + # =========== Forms for create/update ============== diff --git a/src/tcms/testruns/urls/runs_urls.py b/src/tcms/testruns/urls/runs_urls.py index bdad886b..a0c09da1 100644 --- a/src/tcms/testruns/urls/runs_urls.py +++ b/src/tcms/testruns/urls/runs_urls.py @@ -22,4 +22,5 @@ views.DeleteRunEnvValueView.as_view(), name="runs-delete-env-value", ), + path("case-runs/comment-many/", views.comment_case_runs, name="caserun-comment-caseruns"), ] diff --git a/src/tcms/testruns/views.py b/src/tcms/testruns/views.py index e0b0274d..001e438b 100644 --- a/src/tcms/testruns/views.py +++ b/src/tcms/testruns/views.py @@ -37,6 +37,7 @@ from django_comments.models import Comment +from tcms.comments.models import add_comment from tcms.core.raw_sql import RawSQL from tcms.core.responses import JsonResponseBadRequest @@ -60,6 +61,7 @@ PlanFilterRunForm, RunAndEnvValueForm, ChangeRunEnvValueForm, + CommentCaseRunsForm, ) from tcms.testruns.forms import NewRunForm, SearchRunForm, EditRunForm, RunCloneForm from tcms.testruns.helpers.serializer import TCR2File @@ -1401,3 +1403,19 @@ def get_redirect_url(self, *args, **kwargs): case_run = get_object_or_404(TestCaseRun, pk=case_run_id) bz_model = IssueTracker.objects.get(pk=tracker_id) return find_service(bz_model).make_issue_report_url(case_run) + + +@require_POST +def comment_case_runs(request): + """Add comment to one or more case runs at a time.""" + form = CommentCaseRunsForm(request.POST) + if not form.is_valid(): + return JsonResponseBadRequest({"message": form_error_messages_to_list(form)}) + add_comment( + request.user, + "testruns.testcaserun", + [cr.pk for cr in form.cleaned_data["run"]], + form.cleaned_data["comment"], + request.META.get("REMOTE_ADDR"), + ) + return JsonResponse({}) diff --git a/src/tcms/urls.py b/src/tcms/urls.py index c206dc7b..c1d01954 100644 --- a/src/tcms/urls.py +++ b/src/tcms/urls.py @@ -27,11 +27,6 @@ # Testruns zone path("run/", include("tcms.testruns.urls.run_urls")), path("runs/", include("tcms.testruns.urls.runs_urls")), - path( - "caserun/comment-many/", - tcms_core_ajax.comment_case_runs, - name="caserun-comment-caseruns", - ), path("accounts/", include("tcms.profiles.urls")), path("linkref/", include("tcms.linkreference.urls")), path("comments/", include("tcms.comments.urls")), diff --git a/src/tests/core/test_views.py b/src/tests/core/test_views.py index 6725aa52..33a2ec5a 100644 --- a/src/tests/core/test_views.py +++ b/src/tests/core/test_views.py @@ -7,11 +7,9 @@ import pytest from django import test from django.contrib.auth.models import User -from django.contrib.contenttypes.models import ContentType from django.core import serializers from django.core.exceptions import ValidationError from django.urls import reverse -from django_comments.models import Comment from kobo.django.xmlrpc.models import XmlRpcLog from pytest_django import asserts @@ -20,7 +18,6 @@ from tcms.management.models import TCMSEnvGroup from tcms.management.models import TCMSEnvProperty from tcms.testcases.forms import CaseAutomatedForm -from tcms.testruns.models import TestCaseRun from tests import BaseCaseRun, BasePlanCase from tests import factories as f @@ -99,67 +96,6 @@ def test_404_when_missing_search_type(self): self.assert404(response) -class TestCommentCaseRuns(BaseCaseRun): - """Test case for ajax.comment_case_runs""" - - auto_login = True - - @classmethod - def setUpTestData(cls): - super().setUpTestData() - cls.many_comments_url = reverse("caserun-comment-caseruns") - - def test_refuse_if_missing_comment(self): - response = self.client.post( - self.many_comments_url, {"run": [self.case_run_1.pk, self.case_run_2.pk]} - ) - self.assertJsonResponse( - response, {"message": "Comments needed"}, status_code=HTTPStatus.BAD_REQUEST - ) - - def test_refuse_if_missing_no_case_run_pk(self): - response = self.client.post(self.many_comments_url, {"comment": "new comment", "run": []}) - self.assertJsonResponse( - response, - {"message": "No runs selected."}, - status_code=HTTPStatus.BAD_REQUEST, - ) - - response = self.client.post(self.many_comments_url, {"comment": "new comment"}) - self.assertJsonResponse( - response, - {"message": "No runs selected."}, - status_code=HTTPStatus.BAD_REQUEST, - ) - - def test_refuse_if_passed_case_run_pks_not_exist(self): - response = self.client.post( - self.many_comments_url, - {"comment": "new comment", "run": [99999998, 1009900]}, - ) - self.assertJsonResponse( - response, - {"message": "No caserun found."}, - status_code=HTTPStatus.BAD_REQUEST, - ) - - def test_add_comment_to_case_runs(self): - new_comment = "new comment" - response = self.client.post( - self.many_comments_url, - {"comment": new_comment, "run": [self.case_run_1.pk, self.case_run_2.pk]}, - ) - self.assertJsonResponse(response, {}) - - # Assert comments are added - case_run_ct = ContentType.objects.get_for_model(TestCaseRun) - - for case_run_pk in (self.case_run_1.pk, self.case_run_2.pk): - comments = Comment.objects.filter(object_pk=case_run_pk, content_type=case_run_ct) - self.assertEqual(new_comment, comments[0].comment) - self.assertEqual(self.tester, comments[0].user) - - class TestGetForm(test.TestCase): """Test case for form""" diff --git a/src/tests/testruns/test_views.py b/src/tests/testruns/test_views.py index dec0bef1..f451320f 100644 --- a/src/tests/testruns/test_views.py +++ b/src/tests/testruns/test_views.py @@ -14,10 +14,12 @@ from xml.etree import ElementTree from bs4 import BeautifulSoup +from django.contrib.contenttypes.models import ContentType from django.db.models import Max, QuerySet from django.utils import formats from django.urls import reverse from django.contrib.auth.models import User +from django_comments.models import Comment from tcms.issuetracker.models import Issue from tcms.linkreference.models import create_link @@ -1504,3 +1506,64 @@ def test_get_the_statistics(self): for item in content: self.assertContains(response, item, html=True) + + +class TestCommentCaseRuns(BaseCaseRun): + """Test case for ajax.comment_case_runs""" + + auto_login = True + + @classmethod + def setUpTestData(cls): + super().setUpTestData() + cls.many_comments_url = reverse("caserun-comment-caseruns") + + def test_refuse_if_missing_comment(self): + response = self.client.post( + self.many_comments_url, {"run": [self.case_run_1.pk, self.case_run_2.pk]} + ) + self.assertJsonResponse( + response, {"message": ["Comment is needed."]}, status_code=HTTPStatus.BAD_REQUEST + ) + + def test_refuse_if_missing_no_case_run_pk(self): + response = self.client.post(self.many_comments_url, {"comment": "new comment", "run": []}) + self.assertJsonResponse( + response, + {"message": ["No test case run id is passed to comment out."]}, + status_code=HTTPStatus.BAD_REQUEST, + ) + + response = self.client.post(self.many_comments_url, {"comment": "new comment"}) + self.assertJsonResponse( + response, + {"message": ["No test case run id is passed to comment out."]}, + status_code=HTTPStatus.BAD_REQUEST, + ) + + def test_refuse_if_passed_case_run_pks_not_exist(self): + response = self.client.post( + self.many_comments_url, + {"comment": "new comment", "run": [99999998]}, + ) + self.assertJsonResponse( + response, + {"message": ["Test case run 99999998 does not exist."]}, + status_code=HTTPStatus.BAD_REQUEST, + ) + + def test_add_comment_to_case_runs(self): + new_comment = "new comment" + response = self.client.post( + self.many_comments_url, + {"comment": new_comment, "run": [self.case_run_1.pk, self.case_run_2.pk]}, + ) + self.assertJsonResponse(response, {}) + + # Assert comments are added + case_run_ct = ContentType.objects.get_for_model(TestCaseRun) + + for case_run_pk in (self.case_run_1.pk, self.case_run_2.pk): + comments = Comment.objects.filter(object_pk=case_run_pk, content_type=case_run_ct) + self.assertEqual(new_comment, comments[0].comment) + self.assertEqual(self.tester, comments[0].user)