Skip to content

Commit

Permalink
add test for tcms.testplans.views.NewTestPlanView. Fix kiwitcms#1616
Browse files Browse the repository at this point in the history
  • Loading branch information
awalvie committed Nov 17, 2020
1 parent dbf5b2e commit 8ea3a90
Showing 1 changed file with 160 additions and 54 deletions.
214 changes: 160 additions & 54 deletions tcms/testplans/tests/test_new_plan.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,62 @@
from http import HTTPStatus

from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from uuslug import slugify

from tcms.testplans.models import TestPlan
from tcms.testplans.tests.tests import BasePlanTest
from tcms.tests import remove_perm_from_user, user_should_have_perm


class NewPlanViewTest(BasePlanTest):
location = reverse('plans-new')
plan_name = 'plan name'
add_testplan_permission = 'testplans.add_testplan'

def setUp(self):
super().setUp()

user_should_have_perm(self.user, perm=self.add_testplan_permission)

self.user.is_superuser = False
self.user.save()

self.request = {
'author': self.user.pk,
'product': self.product.pk,
'product_version': self.product_version.pk,
'type': self.plan_type.pk,
'name': self.plan_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-plan': '',
'email_settings-0-id': self.test_plan.emailing.pk,
'email_settings-TOTAL_FORMS': '1',
'email_settings-INITIAL_FORMS': '1',
'email_settings-MIN_NUM_FORMS': '0',
'email_settings-MAX_NUM_FORMS': '1',
from tcms.tests import (
remove_perm_from_user,
user_should_have_perm,
LoggedInTestCase,
PermissionsTestCase,
)
from tcms.tests.factories import (
TestPlanFactory,
ProductFactory,
VersionFactory,
PlanTypeFactory,
)


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

cls.product = ProductFactory()
cls.product_version = VersionFactory(product=cls.product)
cls.plan_type = PlanTypeFactory()
cls.plan_name = TestPlanFactory()

user_should_have_perm(cls.tester, perm="testplans.add_testplan")
user_should_have_perm(cls.tester, perm="testplans.view_testplan")

cls.url = reverse("plans-new")

cls.request = {
"author": cls.tester.pk,
"product": cls.product.pk,
"product_version": cls.product_version.pk,
"type": cls.plan_type.pk,
"name": cls.plan_name.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.plan_name.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_plan_new_get(self):
response = self.client.get(self.location)
response = self.client.get(self.url)

self.assertContains(
response,
'<input class="bootstrap-switch" name="is_active" type="checkbox" checked',
html=False)
self.assertContains(
response,
'<input class="bootstrap-switch" name="email_settings-0-auto_to_plan_author" '
Expand All @@ -62,13 +71,29 @@ def test_plan_new_get(self):
'type="checkbox" checked', html=False)
self.assertContains(
response,
'<input class="bootstrap-switch" name="email_settings-0-notify_on_plan_update" '
'<input class="bootstrap-switch" name="email_settings-0-notify_on_case_update" '
'type="checkbox" checked', html=False)
self.assertContains(
response,
'<input class="bootstrap-switch" name="email_settings-0-notify_on_case_update" '
'<input class="bootstrap-switch" name="email_settings-0-notify_on_plan_update" '
'type="checkbox" checked', html=False)

def test_notify_formset_invalid(self):
# Note: Boolean fields are always valid - either False or True
# That's why the only way to make the notify formset invalid is to
# reference a non-existing email_settings ID !!!
data = self.request.copy()
data["email_settings-0-id"] = -1
del data["email_settings-0-auto_to_plan_author"]

response = self.client.post(self.url, data)

self.assertContains(response, _("Create new TestPlan"))
self.assertContains(
response,
'<input class="bootstrap-switch" name="email_settings-0-auto_to_plan_author" '
'type="checkbox"', html=False)

def test_plan_create_new_active(self):
self._test_plan_create_new(is_active=True)

Expand All @@ -78,14 +103,15 @@ def test_plan_create_new_inactive(self):
def _test_plan_create_new(self, is_active):
self.request['is_active'] = is_active

response = self.client.post(self.location, self.request)
response = self.client.post(self.url, self.request)
self.assertEqual(response.status_code, HTTPStatus.FOUND)

plan = TestPlan.objects.get(
name=self.plan_name,
name=self.request["name"],
is_active=is_active,
product=self.request["product"],
)
self.assertEqual(plan.author, self.user)
self.assertEqual(plan.author, self.tester)
self.assertEqual(plan.product, self.product)
self.assertEqual(plan.product_version, self.product_version)
self.assertEqual(plan.type, self.plan_type)
Expand All @@ -96,16 +122,96 @@ def _test_plan_create_new(self, is_active):
self.assertTrue(plan.emailing.notify_on_plan_update)
self.assertTrue(plan.emailing.notify_on_case_update)

def test_get_with_no_perm_redirects_to_login(self):
remove_perm_from_user(self.user, self.add_testplan_permission)
def test_with_invalid_product_shows_error(self):
new_data = self.request.copy()
new_data["product"] = -1
del new_data["email_settings-0-auto_to_plan_author"]

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

self.assertContains(response, _("Create new TestPlan"))
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=False,
)


class TestNewTestPlanViewPermissions(PermissionsTestCase):
add_testplan_permission = "testplans.add_testplan"
permission_label = add_testplan_permission
http_method_names = ["get", "post"]
url = reverse("plans-new")

@classmethod
def setUpTestData(cls):
cls.post_data = {"description": "test"}
super().setUpTestData()

cls.product = ProductFactory()
cls.product_version = VersionFactory(product=cls.product)
cls.plan_type = PlanTypeFactory()
cls.plan_name = TestPlanFactory()

cls.post_data.update(
{
"author": cls.tester.pk,
"product": cls.product.pk,
"product_version": cls.product_version.pk,
"type": cls.plan_type.pk,
"name": cls.plan_name.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.plan_name.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 verify_get_with_permission(self):
response = self.client.get(self.url)

self.assertEqual(response.status_code, 200)
self.assertContains(response, _('Create new TestPlan'))

def verify_post_with_permission(self):
response = self.client.post(self.url, self.post_data, follow=True)
test_plan = TestPlan.objects.get(
name=self.post_data["name"],
product=self.post_data["product"],
)
redirect_url = reverse('test_plan_url', args=[test_plan.pk, slugify(test_plan.name)])

self.assertRedirects(response, redirect_url)
self._assert_test_plan(test_plan)

response = self.client.get(self.location, follow=True)
def verify_get_without_permission(self):
remove_perm_from_user(self.tester, self.add_testplan_permission)
response = self.client.get(self.url, follow=True)

self.assertRedirects(response, reverse('tcms-login') + '?next=' + self.location)
self.assertRedirects(response, reverse("tcms-login") + "?next=" + self.url)

def test_post_with_no_perm_redirects_to_login(self):
remove_perm_from_user(self.user, self.add_testplan_permission)
def verify_post_without_permission(self):
remove_perm_from_user(self.tester, self.add_testplan_permission)
response = self.client.post(self.url, self.post_data, follow=True)

response = self.client.post(self.location, self.request, follow=True)
self.assertRedirects(response, reverse("tcms-login") + "?next=" + self.url)

self.assertRedirects(response, reverse('tcms-login') + '?next=' + self.location)
def _assert_test_plan(self, test_plan):
self.assertEqual(test_plan.author, self.tester)
self.assertEqual(test_plan.product, self.product)
self.assertEqual(test_plan.product_version, self.product_version)
self.assertEqual(test_plan.type, self.plan_type)

0 comments on commit 8ea3a90

Please sign in to comment.