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