OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered 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 |
| 8 import email |
| 9 |
| 10 from framework import profiler |
| 11 from proto import user_pb2 |
| 12 from services import service_manager |
| 13 from testing import fake |
| 14 import webapp2 |
| 15 |
| 16 DEFAULT_HOST = '127.0.0.1' |
| 17 |
| 18 |
| 19 MINIMAL_HEADER_LINES = [ |
| 20 ('From', 'user@example.com'), |
| 21 ('To', 'proj@monorail.example.com'), |
| 22 ('Cc', 'ningerso@chromium.org'), |
| 23 ('Subject', 'Issue 123 in proj: broken link'), |
| 24 ] |
| 25 |
| 26 # Add one more (long) line for In-Reply-To |
| 27 HEADER_LINES = MINIMAL_HEADER_LINES + [ |
| 28 ('In-Reply-To', '<0=969704940193871313=13442892928193434663=' |
| 29 'proj@monorail.example.com>'), |
| 30 ] |
| 31 |
| 32 |
| 33 def MakeMessage(header_list, body): |
| 34 """Convenience function to make an email.message.Message.""" |
| 35 msg = email.message.Message() |
| 36 for key, value in header_list: |
| 37 msg[key] = value |
| 38 msg.set_payload(body) |
| 39 return msg |
| 40 |
| 41 |
| 42 def MakeMonorailRequest(*args, **kwargs): |
| 43 """Get just the monorailrequest.MonorailRequest() from GetRequestObjects.""" |
| 44 _request, mr = GetRequestObjects(*args, **kwargs) |
| 45 return mr |
| 46 |
| 47 |
| 48 def GetRequestObjects( |
| 49 headers=None, path='/', params=None, user_info=None, project=None, |
| 50 method='GET', perms=None, services=None): |
| 51 """Make fake request and MonorailRequest objects for testing. |
| 52 |
| 53 Host param will override the 'Host' header, and has a default value of |
| 54 '127.0.0.1'. |
| 55 |
| 56 Args: |
| 57 headers: Dict of HTTP header strings. |
| 58 path: Path part of the URL in the request. |
| 59 params: Dict of query-string parameters. |
| 60 user_info: Dict of user attributes to set on a MonorailRequest object. |
| 61 For example, "user_id: 5" causes self.auth.user_id=5. |
| 62 project: optional Project object for the current request. |
| 63 method: 'GET' or 'POST'. |
| 64 perms: PermissionSet to use for this request. |
| 65 services: Connections to backends. |
| 66 |
| 67 Returns: |
| 68 A tuple of (http Request, monorailrequest.MonorailRequest()). |
| 69 """ |
| 70 headers = headers or {} |
| 71 params = params or {} |
| 72 |
| 73 headers.setdefault('Host', DEFAULT_HOST) |
| 74 post_items = params if method == 'POST' else None |
| 75 |
| 76 if not services: |
| 77 services = service_manager.Services( |
| 78 project=fake.ProjectService(), |
| 79 user=fake.UserService(), |
| 80 usergroup=fake.UserGroupService()) |
| 81 services.project.TestAddProject('proj') |
| 82 |
| 83 request = webapp2.Request.blank(path, headers=headers, POST=post_items) |
| 84 mr = fake.MonorailRequest( |
| 85 user_info=user_info, project=project, perms=perms, params=params) |
| 86 mr.ParseRequest( |
| 87 request, services, profiler.Profiler(), do_user_lookups=False) |
| 88 mr.auth.user_pb = user_pb2.MakeUser() |
| 89 |
| 90 return request, mr |
| 91 |
| 92 |
| 93 class Blank(object): |
| 94 """Simple class that assigns all named args to attributes. |
| 95 |
| 96 Tip: supply a lambda to define a method. |
| 97 """ |
| 98 |
| 99 def __init__(self, **kwargs): |
| 100 vars(self).update(kwargs) |
| 101 |
| 102 def __repr__(self): |
| 103 return '%s(%s)' % (self.__class__.__name__, str(vars(self))) |
| 104 |
| 105 def __eq__(self, other): |
| 106 if other is None: |
| 107 return False |
| 108 return vars(self) == vars(other) |
OLD | NEW |