Skip to content

Commit

Permalink
Test for tcms.testplans.views.Edit. Refs kiwitcms#1617
Browse files Browse the repository at this point in the history
  • Loading branch information
cmbahadir committed Jun 9, 2020
1 parent ebea9b0 commit 758ade5
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 3 deletions.
131 changes: 129 additions & 2 deletions tcms/testplans/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from tcms.tests import BasePlanCase, remove_perm_from_user
from tcms.tests.factories import TagFactory, UserFactory
from tcms.tests import BasePlanCase, LoggedInTestCase, remove_perm_from_user, user_should_have_perm
from tcms.tests.factories import (TagFactory, UserFactory, TestPlanFactory,
ProductFactory, VersionFactory, PlanTypeFactory)
from tcms.utils.permissions import initiate_user_with_default_setups


Expand Down Expand Up @@ -54,3 +55,129 @@ def test_view_tags_without_permissions(self):
# assert tag actions are shown
self.assertNotContains(response, 'Add Tag')
self.assertContains(response, '<span class="disabled grey">%s</span>' % _('Remove'))


class EditPlanViewTest(LoggedInTestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()

user_should_have_perm(cls.tester, perm='testplans.change_testplan')
cls.product = ProductFactory()
cls.product_version = VersionFactory(product=cls.product)
cls.test_plan_type = PlanTypeFactory()
cls.test_plan_1 = TestPlanFactory()
cls.testplan_edit_url = reverse('plan-edit', args=[cls.test_plan_1.pk])

cls.testplan_edit_data = {
'author': cls.tester.pk,
'product': cls.product.pk,
'product_version': cls.product_version.pk,
'type': cls.test_plan_type.pk,
'name': cls.test_plan_1.name,

'email_settings-0-auto_to_plan_author': 'on',
'email_settings-0-auto_to_case_owner': 'on',
'email_settings-0-auto_to_case_default_tester': 'on',
'email_settings-0-notify_on_case_update': 'on',
'email_settings-0-notify_on_plan_update': 'on',

'email_settings-0-id': cls.test_plan_1.emailing.pk,
'email_settings-TOTAL_FORMS': '1',
'email_settings-INITIAL_FORMS': '1',
'email_settings-MIN_NUM_FORMS': '0',
'email_settings-MAX_NUM_FORMS': '1',
'is_active': True,
}

def test_show_testplan_edit_page(self):
response = self.client.get(self.testplan_edit_url)
self.assertContains(response, _('Edit TestPlan'))
self.assert_notify_form(response)
self.assertContains(
response,
'<input class="bootstrap-switch" name="is_active" type="checkbox" checked>',
html=True)

def test_edit_testplan_text_field(self):
edit_data = self.testplan_edit_data.copy()
new_text = 'Edited: {0}'.format(self.test_plan_1.text)
edit_data['text'] = new_text

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

self.assertContains(response, new_text, html=True)
self.test_plan_1.refresh_from_db()
self.assertEqual(new_text, self.test_plan_1.text)

# Issue: When there is an invalid value on notify_formset,
# returned response is always the default (ON).
def test_with_invalid_notify_form_value(self):
test_plan = TestPlanFactory(
author=self.tester)

testplan_edit_url = reverse('plan-edit', args=[test_plan.pk])
edit_data = self.testplan_edit_data.copy()
edit_data['email_settings-0-auto_to_plan_author'] = 'invalid_value'

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

self.assert_notify_form(response)

def test_with_invalid_product_shows_error(self):
edit_data = self.testplan_edit_data.copy()
edit_data['product'] = -1
del edit_data['email_settings-0-auto_to_plan_author']

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

self.assertContains(
response,
_('Select a valid choice. That choice is not one of the available choices.'))
self.assertContains(
response,
'<input class="bootstrap-switch" name="email_settings-0-auto_to_plan_author" '
'type="checkbox">', html=True)

def test_with_invalid_type_shows_error(self):
edit_data = self.testplan_edit_data.copy()
edit_data['type'] = -1

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

self.assertContains(
response,
_('Select a valid choice. That choice is not one of the available choices.'))

def test_with_invalid_version_shows_error(self):
edit_data = self.testplan_edit_data.copy()
version = VersionFactory()
edit_data['product_version'] = version.pk

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

self.assertContains(
response,
_('Select a valid choice. That choice is not one of the available choices.'))

def assert_notify_form(self, response):
self.assertContains(
response,
'<input class="bootstrap-switch" name="email_settings-0-auto_to_plan_author" '
'type="checkbox" checked>', html=True)
self.assertContains(
response,
'<input class="bootstrap-switch" name="email_settings-0-auto_to_case_owner" '
'type="checkbox" checked>', html=True)
self.assertContains(
response,
'<input class="bootstrap-switch" name="email_settings-0-auto_to_case_default_tester" '
'type="checkbox" checked>', html=True)
self.assertContains(
response,
'<input class="bootstrap-switch" name="email_settings-0-notify_on_case_update" '
'type="checkbox" checked>', html=True)
self.assertContains(
response,
'<input class="bootstrap-switch" name="email_settings-0-notify_on_plan_update" '
'type="checkbox" checked>', html=True)
8 changes: 7 additions & 1 deletion tcms/testplans/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ def form_valid(self, form):
return super().form_valid(form)

# taken from FormMixin.form_invalid()
return self.render_to_response(self.get_context_data(notify_formset=notify_formset))
context_data = self.get_context_data(form=form, notify_formset=notify_formset)
return self.render_to_response(context_data)

def form_invalid(self, form):
notify_formset = PlanNotifyFormSet(self.request.POST, instance=self.object)
context_data = self.get_context_data(form=form, notify_formset=notify_formset)
return self.render_to_response(context_data)


class SearchTestPlanView(TemplateView): # pylint: disable=missing-permission-required
Expand Down

0 comments on commit 758ade5

Please sign in to comment.