1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# pylint: disable=too-many-ancestors |
3
|
|
|
import os |
4
|
|
|
import unittest |
5
|
|
|
from http import HTTPStatus |
6
|
|
|
|
7
|
|
|
from django import test |
8
|
|
|
from django.conf import settings |
9
|
|
|
from django.contrib.sites.models import Site |
10
|
|
|
from django.core.management import call_command |
11
|
|
|
from django.urls import include, path, reverse |
12
|
|
|
from django.utils.translation import gettext_lazy as _ |
13
|
|
|
|
14
|
|
|
from tcms import urls |
15
|
|
|
from tcms.tests import LoggedInTestCase |
16
|
|
|
from tcms.tests.factories import ( |
17
|
|
|
TestExecutionFactory, |
18
|
|
|
TestPlanFactory, |
19
|
|
|
TestRunFactory, |
20
|
|
|
UserFactory, |
21
|
|
|
) |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class TestDashboard(LoggedInTestCase): |
25
|
|
|
@classmethod |
26
|
|
|
def setUpTestData(cls): |
27
|
|
|
super().setUpTestData() |
28
|
|
|
# used to reproduce Sentry #KIWI-TCMS-38 where rendering fails |
29
|
|
|
# with that particular value |
30
|
|
|
cls.chinese_tp = TestPlanFactory(name="缺货反馈测试需求", author=cls.tester) |
31
|
|
|
doc_url = "https://kiwitcms.readthedocs.io/en/latest/admin.html#configure-kiwi-s-base-url" |
32
|
|
|
cls.base_url_error_message = _( |
33
|
|
|
"Base URL is not configured! " |
34
|
|
|
'See <a href="%(doc_url)s">documentation</a> and ' |
35
|
|
|
'<a href="%(admin_url)s">change it</a>' |
36
|
|
|
) % { |
37
|
|
|
"doc_url": doc_url, |
38
|
|
|
"admin_url": reverse("admin:sites_site_change", args=[settings.SITE_ID]), |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
def test_when_not_logged_in_redirects_to_login(self): |
42
|
|
|
self.client.logout() |
43
|
|
|
response = self.client.get(reverse("core-views-index")) |
44
|
|
|
self.assertRedirects( |
45
|
|
|
response, |
46
|
|
|
reverse("tcms-login") + "?next=/", |
47
|
|
|
target_status_code=HTTPStatus.OK, |
48
|
|
|
) |
49
|
|
|
|
50
|
|
|
def test_when_logged_in_renders_dashboard(self): |
51
|
|
|
response = self.client.get(reverse("core-views-index")) |
52
|
|
|
|
53
|
|
|
self.assertContains(response, _("Test executions")) |
54
|
|
|
self.assertContains(response, _("Dashboard")) |
55
|
|
|
self.assertContains(response, _("Your Test plans")) |
56
|
|
|
|
57
|
|
|
def test_dashboard_shows_testruns_for_manager(self): |
58
|
|
|
test_run = TestRunFactory(manager=self.tester) |
59
|
|
|
|
60
|
|
|
response = self.client.get(reverse("core-views-index")) |
61
|
|
|
self.assertContains(response, test_run.summary) |
62
|
|
|
|
63
|
|
|
def test_dashboard_shows_testruns_for_default_tester(self): |
64
|
|
|
test_run = TestRunFactory(default_tester=self.tester) |
65
|
|
|
|
66
|
|
|
response = self.client.get(reverse("core-views-index")) |
67
|
|
|
self.assertContains(response, test_run.summary) |
68
|
|
|
|
69
|
|
|
def test_dashboard_shows_testruns_for_execution_assignee(self): |
70
|
|
|
execution = TestExecutionFactory(assignee=self.tester) |
71
|
|
|
|
72
|
|
|
response = self.client.get(reverse("core-views-index")) |
73
|
|
|
self.assertContains(response, execution.run.summary) |
74
|
|
|
|
75
|
|
|
def test_check_base_url_not_configured(self): |
76
|
|
|
response = self.client.get("/", follow=True) |
77
|
|
|
self.assertContains(response, self.base_url_error_message) |
78
|
|
|
|
79
|
|
|
def test_check_base_url_configured(self): |
80
|
|
|
site = Site.objects.create(domain="example.com", name="example") |
81
|
|
|
with test.override_settings(SITE_ID=site.pk): |
82
|
|
|
response = self.client.get("/", follow=True) |
83
|
|
|
self.assertNotContains(response, self.base_url_error_message) |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
@unittest.skipUnless( |
87
|
|
|
os.getenv("TEST_DASHBOARD_CHECK_UNAPPLIED_MIGRATIONS"), |
88
|
|
|
"Check for missing migrations testing is not enabled", |
89
|
|
|
) |
90
|
|
|
class TestDashboardCheckMigrations(test.TransactionTestCase): |
91
|
|
|
unapplied_migration_message = _( |
92
|
|
|
"unapplied migration(s). See " |
93
|
|
|
'<a href="https://kiwitcms.readthedocs.io/en/latest/' |
94
|
|
|
"installing_docker.html#initial-configuration-of-running-" |
95
|
|
|
'container">documentation</a>' |
96
|
|
|
) |
97
|
|
|
|
98
|
|
|
def test_check_unapplied_migrations(self): |
99
|
|
|
call_command("migrate", "bugs", "zero", verbosity=2, interactive=False) |
100
|
|
|
tester = UserFactory() |
101
|
|
|
tester.set_password("password") |
102
|
|
|
tester.save() |
103
|
|
|
self.client.login( # nosec:B106:hardcoded_password_funcarg |
104
|
|
|
username=tester.username, |
105
|
|
|
password="password", |
106
|
|
|
) |
107
|
|
|
response = self.client.get("/", follow=True) |
108
|
|
|
self.assertContains(response, self.unapplied_migration_message) |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
def exception_view(request): |
112
|
|
|
raise Exception |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
urlpatterns = [ |
116
|
|
|
path("will-trigger-500/", exception_view), |
117
|
|
|
path("", include(urls)), |
118
|
|
|
] |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
handler500 = "tcms.core.views.server_error" |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
@test.override_settings(ROOT_URLCONF=__name__) |
125
|
|
|
class TestServerError(test.TestCase): |
126
|
|
|
def test_custom_server_error_view(self): |
127
|
|
|
client = test.Client(raise_request_exception=False) |
128
|
|
|
response = client.get("/will-trigger-500/") |
129
|
|
|
|
130
|
|
|
self.assertEqual(response.status_code, 500) |
131
|
|
|
self.assertTemplateUsed(response, "500.html") |
132
|
|
|
|