Skip to content

Commit

Permalink
Move comment_case_runs view to testruns app (#913)
Browse files Browse the repository at this point in the history
Signed-off-by: Chenxiong Qi <qcxhome@gmail.com>
  • Loading branch information
tkdchen committed Jul 10, 2021
1 parent 4427d97 commit 1542063
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 100 deletions.
2 changes: 1 addition & 1 deletion src/static/js/testrun_actions.js
Expand Up @@ -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,
});
Expand Down
28 changes: 0 additions & 28 deletions src/tcms/core/ajax.py
Expand Up @@ -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
Expand Down Expand Up @@ -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({})
16 changes: 14 additions & 2 deletions src/tcms/testruns/forms.py
Expand Up @@ -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"))

Expand Down Expand Up @@ -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 ==============


Expand Down
1 change: 1 addition & 0 deletions src/tcms/testruns/urls/runs_urls.py
Expand Up @@ -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"),
]
18 changes: 18 additions & 0 deletions src/tcms/testruns/views.py
Expand Up @@ -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

Expand All @@ -60,6 +61,7 @@
PlanFilterRunForm,
RunAndEnvValueForm,
ChangeRunEnvValueForm,
CommentCaseRunsForm,
)
from tcms.testruns.forms import NewRunForm, SearchRunForm, EditRunForm, RunCloneForm
from tcms.testruns.helpers.serializer import TCR2File
Expand Down Expand Up @@ -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({})
5 changes: 0 additions & 5 deletions src/tcms/urls.py
Expand Up @@ -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")),
Expand Down
64 changes: 0 additions & 64 deletions src/tests/core/test_views.py
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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"""

Expand Down
63 changes: 63 additions & 0 deletions src/tests/testruns/test_views.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 1542063

Please sign in to comment.