blob: 4e70ab74b38ff39bf9cbc542c48ac6349f9067e9 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Unittests for the issueexport servlet."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
12
13from mock import Mock, patch
14
15from framework import permissions
16from proto import tracker_pb2
17from services import service_manager
18from testing import testing_helpers
19from testing import fake
20from tracker import issueexport
21
22
23class IssueExportTest(unittest.TestCase):
24
25 def setUp(self):
26 self.services = service_manager.Services(
27 config=fake.ConfigService(),
28 project=fake.ProjectService(),
29 issue=fake.IssueService(),
30 user=fake.UserService(),
31 issue_star=fake.IssueStarService(),
32 )
33 self.cnxn = 'fake connection'
34 self.project = self.services.project.TestAddProject('proj', project_id=789)
35 self.servlet = issueexport.IssueExport(
36 'req', 'res', services=self.services)
37 self.jsonfeed = issueexport.IssueExportJSON(
38 'req', 'res', services=self.services)
39 self.mr = testing_helpers.MakeMonorailRequest(
40 project=self.project, perms=permissions.OWNER_ACTIVE_PERMISSIONSET)
41 self.mr.can = 1
42
43 def testAssertBasePermission(self):
44 self.assertRaises(permissions.PermissionException,
45 self.servlet.AssertBasePermission, self.mr)
46 self.mr.auth.user_pb.is_site_admin = True
47 self.servlet.AssertBasePermission(self.mr)
48
49 @patch('time.time')
50 def testHandleRequest(self, mockTime):
51 mockTime.return_value = 1234
52 self.services.issue.GetAllIssuesInProject = Mock(return_value=[])
53 self.services.issue.GetCommentsForIssues = Mock(return_value={})
54 self.services.issue_star.LookupItemsStarrers = Mock(return_value={})
55 self.services.user.LookupUserEmails = Mock(
56 return_value={111: 'user1@test.com', 222: 'user2@test.com'})
57
58 self.mr.project_name = self.project.project_name
59 json_data = self.jsonfeed.HandleRequest(self.mr)
60
61 self.assertEqual(json_data['metadata'],
62 {'version': 1, 'who': None, 'when': 1234,
63 'project': 'proj', 'start': 0, 'num': 100})
64 self.assertEqual(json_data['issues'], [])
65 self.assertItemsEqual(
66 json_data['emails'], ['user1@test.com', 'user2@test.com'])
67
68 # TODO(jojwang): test attachments, amendments, comment details
69 def testMakeIssueJSON(self):
70 config = self.services.config.GetProjectConfig(
71 self.cnxn, 789)
72 config.field_defs.extend(
73 [tracker_pb2.FieldDef(
74 field_id=1, field_name='UXReview',
75 field_type=tracker_pb2.FieldTypes.APPROVAL_TYPE),
76 tracker_pb2.FieldDef(
77 field_id=2, field_name='approvalsubfield',
78 field_type=tracker_pb2.FieldTypes.STR_TYPE, approval_id=1),
79 tracker_pb2.FieldDef(
80 field_id=3, field_name='phasefield',
81 field_type=tracker_pb2.FieldTypes.INT_TYPE, is_phase_field=True),
82 tracker_pb2.FieldDef(
83 field_id=4, field_name='normalfield',
84 field_type=tracker_pb2.FieldTypes.STR_TYPE)
85 ])
86 self.services.config.StoreConfig(self.cnxn, config)
87
88 phases = [tracker_pb2.Phase(phase_id=1, name='Phase1', rank=1),
89 tracker_pb2.Phase(phase_id=2, name='Phase2', rank=2)]
90 avs = [tracker_pb2.ApprovalValue(
91 approval_id=1, status=tracker_pb2.ApprovalStatus.APPROVED,
92 setter_id=111, set_on=7, approver_ids=[333, 444], phase_id=1)]
93 fvs = [tracker_pb2.FieldValue(field_id=2, str_value='two'),
94 tracker_pb2.FieldValue(field_id=3, int_value=3, phase_id=2),
95 tracker_pb2.FieldValue(field_id=4, str_value='four')]
96 labels = ['test', 'Type-FLT-Launch']
97
98 issue = fake.MakeTestIssue(
99 self.project.project_id, 1, 'summary', 'Open', 111, labels=labels,
100 issue_id=78901, reporter_id=222, opened_timestamp=1,
101 closed_timestamp=2, modified_timestamp=3, project_name='project',
102 field_values=fvs, phases=phases, approval_values=avs)
103
104 email_dict = {111: 'user1@test.com', 222: 'user2@test.com',
105 333: 'user3@test.com', 444: 'user4@test.com'}
106 comment_list = [
107 tracker_pb2.IssueComment(content='simple'),
108 tracker_pb2.IssueComment(
109 content='issue desc', is_description=True)]
110 starrer_id_list = [222, 333]
111
112 issue_JSON = self.jsonfeed._MakeIssueJSON(
113 self.mr, issue, email_dict, comment_list, starrer_id_list)
114 expected_JSON = {
115 'local_id': 1,
116 'reporter': 'user2@test.com',
117 'summary': 'summary',
118 'owner': 'user1@test.com',
119 'status': 'Open',
120 'cc': [],
121 'labels': labels,
122 'phases': [{'id': 1, 'name': 'Phase1', 'rank': 1},
123 {'id': 2, 'name': 'Phase2', 'rank': 2}],
124 'fields': [
125 {'field': 'approvalsubfield',
126 'phase': None,
127 'approval': 'UXReview',
128 'str_value': 'two'},
129 {'field': 'phasefield',
130 'phase': 'Phase2',
131 'int_value': 3},
132 {'field': 'normalfield',
133 'phase': None,
134 'str_value': 'four'}],
135 'approvals': [
136 {'approval': 'UXReview',
137 'status': 'APPROVED',
138 'setter': 'user1@test.com',
139 'set_on': 7,
140 'approvers': ['user3@test.com', 'user4@test.com'],
141 'phase': 'Phase1'}
142 ],
143 'starrers': ['user2@test.com', 'user3@test.com'],
144 'comments': [
145 {'content': 'simple',
146 'timestamp': None,
147 'amendments': [],
148 'commenter': None,
149 'attachments': [],
150 'description_num': None},
151 {'content': 'issue desc',
152 'timestamp': None,
153 'amendments': [],
154 'commenter': None,
155 'attachments': [],
156 'description_num': '1'},
157 ],
158 'opened': 1,
159 'modified': 3,
160 'closed': 2,
161 }
162
163 self.assertEqual(expected_JSON, issue_JSON)