Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2020 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 | """Tests for the Monorail SPA pages, as served by EZT.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import mock |
| 11 | import unittest |
| 12 | |
| 13 | import ezt |
| 14 | |
| 15 | import settings |
| 16 | from framework import permissions |
| 17 | from proto import project_pb2 |
| 18 | from proto import site_pb2 |
| 19 | from services import service_manager |
| 20 | from tracker import webcomponentspage |
| 21 | from testing import fake |
| 22 | from testing import testing_helpers |
| 23 | |
| 24 | |
| 25 | class WebComponentsPageTest(unittest.TestCase): |
| 26 | |
| 27 | def setUp(self): |
| 28 | self.services = service_manager.Services( |
| 29 | user=fake.UserService(), |
| 30 | project=fake.ProjectService(), |
| 31 | features=fake.FeaturesService()) |
| 32 | |
| 33 | self.user = self.services.user.TestAddUser('user@example.com', 111) |
| 34 | self.project = self.services.project.TestAddProject('proj', project_id=789) |
| 35 | self.hotlist = self.services.features.TestAddHotlist( |
| 36 | 'HotlistName', summary='summary', owner_ids=[111], hotlist_id=1236) |
| 37 | |
| 38 | self.servlet = webcomponentspage.WebComponentsPage( |
| 39 | 'req', 'res', services=self.services) |
| 40 | |
| 41 | def testHotlistPage_OldUiUrl(self): |
| 42 | mr = testing_helpers.MakeMonorailRequest( |
| 43 | user_info={'user_id': 111}, |
| 44 | path='/hotlists/1236', |
| 45 | services=self.services) |
| 46 | |
| 47 | page_data = self.servlet.GatherPageData(mr) |
| 48 | self.assertEqual('/u/111/hotlists/HotlistName', page_data['old_ui_url']) |
| 49 | |
| 50 | def testHotlistPage_OldUiUrl_People(self): |
| 51 | mr = testing_helpers.MakeMonorailRequest( |
| 52 | user_info={'user_id': 111}, |
| 53 | path='/hotlists/1236/people', |
| 54 | services=self.services) |
| 55 | |
| 56 | page_data = self.servlet.GatherPageData(mr) |
| 57 | self.assertEqual( |
| 58 | '/u/111/hotlists/HotlistName/people', page_data['old_ui_url']) |
| 59 | |
| 60 | def testHotlistPage_OldUiUrl_Settings(self): |
| 61 | mr = testing_helpers.MakeMonorailRequest( |
| 62 | user_info={'user_id': 111}, |
| 63 | path='/hotlists/1236/settings', |
| 64 | services=self.services) |
| 65 | |
| 66 | page_data = self.servlet.GatherPageData(mr) |
| 67 | self.assertEqual( |
| 68 | '/u/111/hotlists/HotlistName/details', page_data['old_ui_url']) |
| 69 | |
| 70 | |
| 71 | class ProjectListPageTest(unittest.TestCase): |
| 72 | |
| 73 | def setUp(self): |
| 74 | self.services = service_manager.Services(project=fake.ProjectService()) |
| 75 | |
| 76 | self.project_a = self.services.project.TestAddProject('a', project_id=1) |
| 77 | self.project_b = self.services.project.TestAddProject('b', project_id=2) |
| 78 | |
| 79 | self.servlet = webcomponentspage.ProjectListPage( |
| 80 | 'req', 'res', services=self.services) |
| 81 | |
| 82 | @mock.patch('settings.domain_to_default_project', {}) |
| 83 | def testMaybeRedirectToDomainDefaultProject_NoMatch(self): |
| 84 | """No redirect if the user is not accessing via a configured domain.""" |
| 85 | mr = testing_helpers.MakeMonorailRequest() |
| 86 | mr.request.host = 'example.com' |
| 87 | msg = self.servlet._MaybeRedirectToDomainDefaultProject(mr) |
| 88 | print('msg: ' + msg) |
| 89 | self.assertTrue(msg.startswith('No configured')) |
| 90 | |
| 91 | @mock.patch('settings.domain_to_default_project', {'example.com': 'huh'}) |
| 92 | def testMaybeRedirectToDomainDefaultProject_NoSuchProject(self): |
| 93 | """No redirect if the configured project does not exist.""" |
| 94 | mr = testing_helpers.MakeMonorailRequest() |
| 95 | mr.request.host = 'example.com' |
| 96 | print('host is %r' % mr.request.host) |
| 97 | msg = self.servlet._MaybeRedirectToDomainDefaultProject(mr) |
| 98 | print('msg: ' + msg) |
| 99 | self.assertTrue(msg.endswith('not found')) |
| 100 | |
| 101 | @mock.patch('settings.domain_to_default_project', {'example.com': 'a'}) |
| 102 | def testMaybeRedirectToDomainDefaultProject_CantView(self): |
| 103 | """No redirect if the user can't view the configured project.""" |
| 104 | self.project_a.access = project_pb2.ProjectAccess.MEMBERS_ONLY |
| 105 | mr = testing_helpers.MakeMonorailRequest() |
| 106 | mr.request.host = 'example.com' |
| 107 | msg = self.servlet._MaybeRedirectToDomainDefaultProject(mr) |
| 108 | print('msg: ' + msg) |
| 109 | self.assertTrue(msg.startswith('User cannot')) |
| 110 | |
| 111 | @mock.patch('settings.domain_to_default_project', {'example.com': 'a'}) |
| 112 | def testMaybeRedirectToDomainDefaultProject_Redirect(self): |
| 113 | """We redirect if there's a configured project that the user can view.""" |
| 114 | mr = testing_helpers.MakeMonorailRequest() |
| 115 | mr.request.host = 'example.com' |
| 116 | self.servlet.redirect = mock.Mock() |
| 117 | msg = self.servlet._MaybeRedirectToDomainDefaultProject(mr) |
| 118 | print('msg: ' + msg) |
| 119 | self.assertTrue(msg.startswith('Redirected')) |
| 120 | self.servlet.redirect.assert_called_once() |