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