Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # 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 | """Helpers for testing.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import email |
| 12 | |
| 13 | from framework import emailfmt |
| 14 | from framework import framework_bizobj |
| 15 | from proto import user_pb2 |
| 16 | from services import service_manager |
| 17 | from services import template_svc |
| 18 | from testing import fake |
| 19 | from tracker import tracker_constants |
| 20 | import webapp2 |
| 21 | |
| 22 | DEFAULT_HOST = '127.0.0.1' |
| 23 | |
| 24 | MINIMAL_HEADER_LINES = [ |
| 25 | ('From', 'user@example.com'), |
| 26 | ('To', 'proj@monorail.example.com'), |
| 27 | ('Cc', 'ningerso@chromium.org'), |
| 28 | ('Subject', 'Issue 123 in proj: broken link'), |
| 29 | ] |
| 30 | |
| 31 | # Add one more (long) line for In-Reply-To |
| 32 | HEADER_LINES = MINIMAL_HEADER_LINES + [ |
| 33 | ('In-Reply-To', '<0=969704940193871313=13442892928193434663=' |
| 34 | 'proj@monorail.example.com>'), |
| 35 | ] |
| 36 | |
| 37 | AlertEmailHeader = emailfmt.AlertEmailHeader |
| 38 | ALERT_EMAIL_HEADER_LINES = HEADER_LINES + [ |
| 39 | (AlertEmailHeader.INCIDENT_ID, '1234567890123456789'), |
| 40 | (AlertEmailHeader.OWNER, 'owner@example.com'), |
| 41 | (AlertEmailHeader.CC, 'cc1@example.com,cc2@example.com'), |
| 42 | (AlertEmailHeader.PRIORITY, '0'), |
| 43 | (AlertEmailHeader.STATUS, 'Unconfirmed'), |
| 44 | (AlertEmailHeader.COMPONENT, 'Component'), |
| 45 | (AlertEmailHeader.TYPE, 'Bug'), |
| 46 | (AlertEmailHeader.OS, 'Android,Windows'), |
| 47 | (AlertEmailHeader.LABEL, ''), |
| 48 | ] |
| 49 | |
| 50 | |
| 51 | # TODO(crbug/monorail/7238): this should be moved to framework_bizobj |
| 52 | # as this is no longer only used for testing. |
| 53 | def ObscuredEmail(address): |
| 54 | (_username, _domain, _obs_username, |
| 55 | obs_email) = framework_bizobj.ParseAndObscureAddress(address) |
| 56 | return obs_email |
| 57 | |
| 58 | def MakeMessage(header_list, body): |
| 59 | """Convenience function to make an email.message.Message.""" |
| 60 | msg = email.message.Message() |
| 61 | for key, value in header_list: |
| 62 | msg[key] = value |
| 63 | msg.set_payload(body) |
| 64 | return msg |
| 65 | |
| 66 | |
| 67 | def MakeMonorailRequest(*args, **kwargs): |
| 68 | """Get just the monorailrequest.MonorailRequest() from GetRequestObjects.""" |
| 69 | _request, mr = GetRequestObjects(*args, **kwargs) |
| 70 | return mr |
| 71 | |
| 72 | |
| 73 | def GetRequestObjects( |
| 74 | headers=None, path='/', params=None, payload=None, user_info=None, |
| 75 | project=None, method='GET', perms=None, services=None, hotlist=None): |
| 76 | """Make fake request and MonorailRequest objects for testing. |
| 77 | |
| 78 | Host param will override the 'Host' header, and has a default value of |
| 79 | '127.0.0.1'. |
| 80 | |
| 81 | Args: |
| 82 | headers: Dict of HTTP header strings. |
| 83 | path: Path part of the URL in the request. |
| 84 | params: Dict of query-string parameters. |
| 85 | user_info: Dict of user attributes to set on a MonorailRequest object. |
| 86 | For example, "user_id: 5" causes self.auth.user_id=5. |
| 87 | project: optional Project object for the current request. |
| 88 | method: 'GET' or 'POST'. |
| 89 | perms: PermissionSet to use for this request. |
| 90 | services: Connections to backends. |
| 91 | hotlist: optional Hotlist object for the current request |
| 92 | |
| 93 | Returns: |
| 94 | A tuple of (http Request, monorailrequest.MonorailRequest()). |
| 95 | """ |
| 96 | headers = headers or {} |
| 97 | params = params or {} |
| 98 | |
| 99 | headers.setdefault('Host', DEFAULT_HOST) |
| 100 | post_items=None |
| 101 | if method == 'POST' and payload: |
| 102 | post_items = payload |
| 103 | elif method == 'POST' and params: |
| 104 | post_items = params |
| 105 | |
| 106 | if not services: |
| 107 | services = service_manager.Services( |
| 108 | project=fake.ProjectService(), |
| 109 | user=fake.UserService(), |
| 110 | usergroup=fake.UserGroupService(), |
| 111 | features=fake.FeaturesService()) |
| 112 | services.project.TestAddProject('proj') |
| 113 | services.features.TestAddHotlist('hotlist') |
| 114 | |
| 115 | request = webapp2.Request.blank(path, headers=headers, POST=post_items) |
| 116 | mr = fake.MonorailRequest( |
| 117 | services, user_info=user_info, project=project, perms=perms, |
| 118 | params=params, hotlist=hotlist) |
| 119 | mr.ParseRequest( |
| 120 | request, services, do_user_lookups=False) |
| 121 | mr.auth.user_pb = user_pb2.MakeUser(0) |
| 122 | return request, mr |
| 123 | |
| 124 | |
| 125 | class Blank(object): |
| 126 | """Simple class that assigns all named args to attributes. |
| 127 | |
| 128 | Tip: supply a lambda to define a method. |
| 129 | """ |
| 130 | |
| 131 | def __init__(self, **kwargs): |
| 132 | vars(self).update(kwargs) |
| 133 | |
| 134 | def __repr__(self): |
| 135 | return '%s(%s)' % (self.__class__.__name__, str(vars(self))) |
| 136 | |
| 137 | def __eq__(self, other): |
| 138 | if other is None: |
| 139 | return False |
| 140 | return vars(self) == vars(other) |
| 141 | |
| 142 | |
| 143 | def DefaultTemplateRows(): |
| 144 | return [( |
| 145 | None, |
| 146 | 789, |
| 147 | template_dict['name'], |
| 148 | template_dict['content'], |
| 149 | template_dict['summary'], |
| 150 | template_dict.get('summary_must_be_edited'), |
| 151 | None, |
| 152 | template_dict['status'], |
| 153 | template_dict.get('members_only', False), |
| 154 | template_dict.get('owner_defaults_to_member', True), |
| 155 | template_dict.get('component_required', False), |
| 156 | ) for template_dict in tracker_constants.DEFAULT_TEMPLATES] |
| 157 | |
| 158 | |
| 159 | def DefaultTemplates(): |
| 160 | return [template_svc.UnpackTemplate(t) for t in DefaultTemplateRows()] |