blob: 6dbe990007f926e4474bd2787b20d9994bbd4402 [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 projectexport 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 project import projectexport
17from proto import tracker_pb2
18from services import service_manager
19from services.template_svc import TemplateService
20from testing import fake
21from testing import testing_helpers
22
23
24class ProjectExportTest(unittest.TestCase):
25
26 def setUp(self):
27 self.services = service_manager.Services()
28 self.servlet = projectexport.ProjectExport(
29 'req', 'res', services=self.services)
30
31 def testAssertBasePermission(self):
32 mr = testing_helpers.MakeMonorailRequest(
33 perms=permissions.OWNER_ACTIVE_PERMISSIONSET)
34 self.assertRaises(permissions.PermissionException,
35 self.servlet.AssertBasePermission, mr)
36 mr.auth.user_pb.is_site_admin = True
37 self.servlet.AssertBasePermission(mr)
38
39
40class ProjectExportJSONTest(unittest.TestCase):
41
42 def setUp(self):
43 self.services = service_manager.Services(
44 config=fake.ConfigService(),
45 project=fake.ProjectService(),
46 user=fake.UserService(),
47 template=Mock(spec=TemplateService))
48 self.services.user.TestAddUser('user1@example.com', 111)
49 self.servlet = projectexport.ProjectExportJSON(
50 'req', 'res', services=self.services)
51 self.project = fake.Project(project_id=789)
52 self.mr = testing_helpers.MakeMonorailRequest(
53 perms=permissions.OWNER_ACTIVE_PERMISSIONSET)
54 self.mr.auth.user_pb.is_site_admin = True
55 self.mr.project = self.project
56
57 @patch('time.time')
58 def testHandleRequest_Normal(self, mockTime):
59 mockTime.return_value = 123456789
60 self.services.project.GetProject = Mock(return_value=self.project)
61 test_config = fake.MakeTestConfig(project_id=789, labels=[], statuses=[])
62 self.services.config.GetProjectConfig = Mock(return_value=test_config)
63 test_templates = testing_helpers.DefaultTemplates()
64 self.services.template.GetProjectTemplates = Mock(
65 return_value=test_templates)
66 self.services.config.UsersInvolvedInConfig = Mock(return_value=[111])
67
68 json_data = self.servlet.HandleRequest(self.mr)
69
70 expected = {
71 'project': {
72 'committers': [],
73 'owners': [],
74 'recent_activity': 0,
75 'name': 'proj',
76 'contributors': [],
77 'perms': [],
78 'attachment_quota': None,
79 'process_inbound_email': False,
80 'revision_url_format': None,
81 'summary': '',
82 'access': 'ANYONE',
83 'state': 'LIVE',
84 'read_only_reason': None,
85 'only_owners_remove_restrictions': False,
86 'only_owners_see_contributors': False,
87 'attachment_bytes': 0,
88 'issue_notify_address': None,
89 'description': ''
90 },
91 'config': {
92 'templates': [{
93 'status': 'Accepted',
94 'members_only': True,
95 'labels': [],
96 'summary_must_be_edited': True,
97 'owner': None,
98 'owner_defaults_to_member': True,
99 'component_required': False,
100 'name': 'Defect report from developer',
101 'summary': 'Enter one-line summary',
102 'content': 'What steps will reproduce the problem?\n1. \n2. \n3. \n'
103 '\n'
104 'What is the expected output?\n\n\nWhat do you see instead?\n'
105 '\n\n'
106 'Please use labels and text to provide additional information.\n',
107 'admins': []
108 }, {
109 'status': 'New',
110 'members_only': False,
111 'labels': [],
112 'summary_must_be_edited': True,
113 'owner': None,
114 'owner_defaults_to_member': True,
115 'component_required': False,
116 'name': 'Defect report from user',
117 'summary': 'Enter one-line summary', 'content': 'What steps will '
118 'reproduce the problem?\n1. \n2. \n3. \n\nWhat is the expected '
119 'output?\n\n\nWhat do you see instead?\n\n\nWhat version of the '
120 'product are you using? On what operating system?\n\n\nPlease '
121 'provide any additional information below.\n',
122 'admins': []
123 }],
124 'labels': [],
125 'statuses_offer_merge': ['Duplicate'],
126 'exclusive_label_prefixes': ['Type', 'Priority', 'Milestone'],
127 'only_known_values': False,
128 'statuses': [],
129 'list_spec': '',
130 'developer_template': 0,
131 'user_template': 0,
132 'grid_y': '',
133 'grid_x': '',
134 'components': [],
135 'list_cols': 'ID Type Status Priority Milestone Owner Summary'
136 },
137 'emails': ['user1@example.com'],
138 'metadata': {
139 'version': 1,
140 'when': 123456789,
141 'who': None,
142 }
143 }
144 self.assertDictEqual(expected, json_data)
145 self.services.template.GetProjectTemplates.assert_called_once_with(
146 self.mr.cnxn, 789)
147 self.services.config.UsersInvolvedInConfig.assert_called_once_with(
148 test_config, test_templates)