Skip to content

Commit

Permalink
test: Add tests for tcms.bugs.views.Edit -- Closes kiwitcms#1599
Browse files Browse the repository at this point in the history
  • Loading branch information
mfonism committed Aug 24, 2020
1 parent 250e2b0 commit 5ed564c
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 7 deletions.
58 changes: 51 additions & 7 deletions tcms/bugs/tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
if 'tcms.bugs.apps.AppConfig' not in settings.INSTALLED_APPS:
raise unittest.SkipTest('tcms.bugs is disabled')


from django.contrib.auth.models import Permission # noqa: E402
from django.contrib.contenttypes.models import ContentType # noqa: E402
from django.test import TestCase # noqa: E402
from django.urls import reverse # noqa: E402
from django.utils.translation import gettext_lazy as _ # noqa: E402
from django.test import TestCase # noqa: E402
from django.urls import reverse # noqa: E402
from django.utils.translation import gettext_lazy as _ # noqa: E402

from tcms import tests # noqa: E402
from tcms.bugs.models import Bug # noqa: E402
from tcms.tests import factories # noqa: E402
from tcms import tests # noqa: E402
from tcms.bugs.models import Bug # noqa: E402
from tcms.bugs.tests.factory import BugFactory # noqa: E402
from tcms.tests import factories # noqa: E402


class TestNew(tests.PermissionsTestCase):
Expand Down Expand Up @@ -59,6 +59,50 @@ def verify_post_with_permission(self):
self.assertContains(response, 'BUG-%d' % last_bug.pk)


class TestEdit(tests.PermissionsTestCase):
permission_label = 'bugs.change_bug'
http_method_names = ['get', 'post']

@classmethod
def setUpTestData(cls):
cls.bug = BugFactory()

cls.url = reverse('bugs-edit', args=(cls.bug.pk,))
cls.post_data = {
'summary': 'An edited summary',
'reporter': cls.bug.reporter.pk,
'assignee': cls.bug.assignee.pk,
'product': cls.bug.product.pk,
'version': cls.bug.version.pk,
'build': cls.bug.build.pk,
}

super().setUpTestData()
tests.user_should_have_perm(cls.tester, 'bugs.view_bug')

def verify_get_with_permission(self):
response = self.client.get(self.url)

self.assertContains(response, _('Edit bug'))
self.assertContains(response, self.url)
self.assertContains(response, self.bug.summary)
self.assertTemplateUsed(response, 'bugs/mutable.html')

def verify_post_with_permission(self):
response = self.client.post(self.url, self.post_data, follow=True)

self.assertRedirects(
response,
reverse('bugs-get', args=(self.bug.pk,)),
status_code=302,
target_status_code=200
)

self.bug.refresh_from_db()
self.assertContains(response, 'BUG-%d: %s' % (self.bug.pk, _(self.bug.summary)))
self.assertTemplateUsed(response, 'bugs/get.html')


class TestM2MPermissionsExist(TestCase):
def test_permissions_exist(self):
ctype = ContentType.objects.get_for_model(Bug.tags.through)
Expand Down
69 changes: 69 additions & 0 deletions tcms/bugs/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.urls import reverse # noqa: E402
from django.utils.translation import gettext_lazy as _ # noqa: E402

from tcms.core.helpers.comments import get_comments # noqa: E402
from tcms.core.templatetags.extra_filters import markdown2html # noqa: E402
from tcms.bugs.models import Bug # noqa: E402
from tcms.bugs.tests.factory import BugFactory # noqa: E402
Expand Down Expand Up @@ -140,3 +141,71 @@ def test_new_bug_assignee_inferred_from_components(self):
bug_created = Bug.objects.last()
self.assertEqual(bug_created.summary, self.summary)
self.assertEqual(bug_created.assignee, comp.initial_owner)


class TestEditBug(LoggedInTestCase):

@classmethod
def setUpTestData(cls):
super().setUpTestData()
user_should_have_perm(cls.tester, 'bugs.change_bug')
user_should_have_perm(cls.tester, 'bugs.view_bug')

def setUp(self):
super().setUp()
self.bug = BugFactory()
self.created_at = self.bug.created_at
self.url = reverse('bugs-edit', args=(self.bug.pk,))

def test_edit_bug(self):
summary_edit = 'An edited summary'
version_edit = VersionFactory()
build_edit = BuildFactory()

edit_data = {
'summary': summary_edit,
'version': version_edit.pk,
'build': build_edit.pk,
'reporter': self.bug.reporter.pk,
'assignee': self.bug.assignee.pk,
'product': self.bug.product.pk
}

response = self.client.post(self.url, edit_data, follow=True)

self.assertRedirects(
response,
reverse('bugs-get', args=(self.bug.pk,)),
status_code=302,
target_status_code=200
)

self.bug.refresh_from_db()
self.assertEqual(self.bug.summary, summary_edit)
self.assertEqual(self.bug.version, version_edit)
self.assertEqual(self.bug.build, build_edit)
self.assertEqual(self.bug.created_at, self.created_at)

def test_record_changes(self):
old_summary = self.bug.summary
new_summary = 'An edited summary'
old_comment_count = get_comments(self.bug).count()

edit_data = {
'summary': new_summary,
'version': self.bug.version.pk,
'build': self.bug.build.pk,
'reporter': self.bug.reporter.pk,
'assignee': self.bug.assignee.pk,
'product': self.bug.product.pk
}

self.client.post(self.url, edit_data, follow=True)
self.bug.refresh_from_db()
comments = get_comments(self.bug)

self.assertEqual(comments.count(), old_comment_count + 1)
self.assertEqual(
comments.last().comment,
"Summary: %s -> %s\n" % (old_summary, new_summary)
)

0 comments on commit 5ed564c

Please sign in to comment.