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 7, 2020
1 parent 7d9fa21 commit 926c754
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 6 deletions.
39 changes: 33 additions & 6 deletions tcms/bugs/tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

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 @@ -58,6 +59,32 @@ 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):
bug = BugFactory()

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

super().setUpTestData()

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

self.assertContains(response, _('Edit bug'))
self.assertContains(response, 'bugs/js/mutable.js')

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


class TestM2MPermissionsExist(TestCase):
def test_permissions_exist(self):
ctype = ContentType.objects.get_for_model(Bug.tags.through)
Expand Down
59 changes: 59 additions & 0 deletions tcms/bugs/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=too-many-ancestors,wrong-import-position

import unittest
from http import HTTPStatus

from django.conf import settings

Expand Down Expand Up @@ -139,3 +140,61 @@ 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')

cls.bug = BugFactory()

cls.url = reverse('bugs-edit', args=(cls.bug.pk,))

cls.summary_edit = 'An edited summary'
cls.reporter_edit = cls.tester
cls.assignee_edit = UserFactory()
cls.product_edit = ProductFactory()
cls.version_edit = VersionFactory()
cls.build_edit = BuildFactory()

cls.edit_data = {
'summary': cls.summary_edit,
'reporter': cls.reporter_edit.pk,
'assignee': cls.assignee_edit.pk,
'product': cls.product_edit.pk,
'version': cls.version_edit.pk,
'build': cls.build_edit.pk
}

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

self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['page_title'], _('Edit bug'))
self.assertEqual(response.context['form_post_url'], self.url)
self.assertTemplateUsed(response, 'bugs/mutable.html')

def test_edit_bug(self):
response = self.client.post(self.url, self.edit_data)

bug = Bug.objects.get(pk=self.bug.pk)
self.assertRedirects(
response,
reverse('bugs-get', args=(bug.pk,)),
status_code=302,
target_status_code=200
)
self.assertEqual(bug.summary, self.summary_edit)
self.assertEqual(bug.reporter, self.reporter_edit)
self.assertEqual(bug.assignee, self.assignee_edit)
self.assertEqual(bug.product, self.product_edit)
self.assertEqual(bug.version, self.version_edit)
self.assertEqual(bug.build, self.build_edit)

def test_404_if_bug_not_exist(self):
url = reverse('bugs-edit', args=(99999,))
response = self.client.post(url, self.edit_data)
self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)

0 comments on commit 926c754

Please sign in to comment.