Completed
Push — master ( 897349...4f09bf )
by Alexander
109:51 queued 107:08
created

TestDashboard.test_dashboard_shows_testruns_for_manager()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# -*- coding: utf-8 -*-
2
3
from http import HTTPStatus
4
5
from django import test
6
from django.conf import settings
7
from django.contrib.contenttypes.models import ContentType
8
from django.urls import reverse
9
from django_comments.models import Comment
10
from django.utils.translation import ugettext_lazy as _
11
12
from tcms.testruns.models import TestExecution
13
from tcms.tests import BaseCaseRun
14
from tcms.tests.factories import UserFactory
15
from tcms.tests.factories import TestPlanFactory
16
from tcms.tests.factories import TestRunFactory
17
from tcms.tests.factories import TestExecutionFactory
18
19
20
class TestNavigation(test.TestCase):
21
    @classmethod
22
    def setUpTestData(cls):
23
        super(TestNavigation, cls).setUpTestData()
24
        cls.user = UserFactory(email='[email protected]')
25
        cls.user.set_password('testing')
26
        cls.user.save()
27
28
    def test_navigation_displays_currently_logged_user(self):
29
        self.client.login(  # nosec:B106:hardcoded_password_funcarg
30
            username=self.user.username,
31
            password='testing')
32
        response = self.client.get(reverse('iframe-navigation'))
33
34
        self.assertContains(response, self.user.username)
35
        self.assertContains(response, _('My profile'))
36
37
38
class TestDashboard(BaseCaseRun):
39
    @classmethod
40
    def setUpTestData(cls):
41
        super().setUpTestData()
42
        # used to reproduce Sentry #KIWI-TCMS-38 where rendering fails
43
        # with that particular value
44
        cls.chinese_tp = TestPlanFactory(name="缺货反馈测试需求",
45
                                         author=cls.tester)
46
47
    def test_when_not_logged_in_redirects_to_login(self):
48
        self.client.logout()
49
        response = self.client.get(reverse('core-views-index'))
50
        self.assertRedirects(
51
            response,
52
            reverse('tcms-login')+'?next=/',
53
            target_status_code=HTTPStatus.OK)
54
55
    def test_when_logged_in_renders_dashboard(self):
56
        response = self.client.get(reverse('core-views-index'))
57
58
        self.assertContains(response, _('Test executions'))
59
        self.assertContains(response, _('Dashboard'))
60
        self.assertContains(response, _('Your Test plans'))
61
62
    def test_dashboard_shows_testruns_for_manager(self):
63
        test_run = TestRunFactory(manager=self.tester)
64
65
        response = self.client.get(reverse('core-views-index'))
66
        self.assertContains(response, test_run.summary)
67
68
    def test_dashboard_shows_testruns_for_default_tester(self):
69
        test_run = TestRunFactory(default_tester=self.tester)
70
71
        response = self.client.get(reverse('core-views-index'))
72
        self.assertContains(response, test_run.summary)
73
74
    def test_dashboard_shows_testruns_for_execution_assignee(self):
75
        execution = TestExecutionFactory(assignee=self.tester)
76
77
        response = self.client.get(reverse('core-views-index'))
78
        self.assertContains(response, execution.run.summary)
79
80
81
class TestCommentCaseRuns(BaseCaseRun):
82
    """Test case for ajax.comment_case_runs"""
83
84
    @classmethod
85
    def setUpTestData(cls):
86
        super(TestCommentCaseRuns, cls).setUpTestData()
87
        cls.many_comments_url = reverse('ajax-comment_case_runs')
88
89
    def test_refuse_if_missing_comment(self):
90
        self.client.login(  # nosec:B106:hardcoded_password_funcarg
91
            username=self.tester.username,
92
            password='password')
93
94
        response = self.client.post(self.many_comments_url,
95
                                    {'run': [self.execution_1.pk, self.execution_2.pk]})
96
        self.assertJSONEqual(
97
            str(response.content, encoding=settings.DEFAULT_CHARSET),
98
            {'rc': 1, 'response': 'Comments needed'})
99
100
    def test_refuse_if_missing_no_case_run_pk(self):
101
        self.client.login(  # nosec:B106:hardcoded_password_funcarg
102
            username=self.tester.username,
103
            password='password')
104
105
        response = self.client.post(self.many_comments_url,
106
                                    {'comment': 'new comment', 'run': []})
107
        self.assertJSONEqual(
108
            str(response.content, encoding=settings.DEFAULT_CHARSET),
109
            {'rc': 1, 'response': 'No runs selected.'})
110
111
        response = self.client.post(self.many_comments_url,
112
                                    {'comment': 'new comment'})
113
        self.assertJSONEqual(
114
            str(response.content, encoding=settings.DEFAULT_CHARSET),
115
            {'rc': 1, 'response': 'No runs selected.'})
116
117
    def test_refuse_if_passed_case_run_pks_not_exist(self):
118
        self.client.login(  # nosec:B106:hardcoded_password_funcarg
119
            username=self.tester.username,
120
            password='password')
121
122
        response = self.client.post(self.many_comments_url,
123
                                    {'comment': 'new comment',
124
                                     'run': '99999998,1009900'})
125
        self.assertJSONEqual(
126
            str(response.content, encoding=settings.DEFAULT_CHARSET),
127
            {'rc': 1, 'response': 'No caserun found.'})
128
129
    def test_add_comment_to_case_runs(self):
130
        self.client.login(  # nosec:B106:hardcoded_password_funcarg
131
            username=self.tester.username,
132
            password='password')
133
134
        new_comment = 'new comment'
135
        response = self.client.post(
136
            self.many_comments_url,
137
            {'comment': new_comment,
138
             'run': ','.join([str(self.execution_1.pk),
139
                              str(self.execution_2.pk)])})
140
        self.assertJSONEqual(
141
            str(response.content, encoding=settings.DEFAULT_CHARSET),
142
            {'rc': 0, 'response': 'ok'})
143
144
        # Assert comments are added
145
        case_run_ct = ContentType.objects.get_for_model(TestExecution)
146
147
        for case_run_pk in (self.execution_1.pk, self.execution_2.pk):
148
            comments = Comment.objects.filter(object_pk=case_run_pk,
149
                                              content_type=case_run_ct)
150
            self.assertEqual(new_comment, comments[0].comment)
151
            self.assertEqual(self.tester, comments[0].user)
152