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 | """Tests for the Monorail home page.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import mock |
| 12 | import unittest |
| 13 | |
| 14 | import ezt |
| 15 | |
| 16 | import settings |
| 17 | from framework import permissions |
| 18 | from proto import project_pb2 |
| 19 | from proto import site_pb2 |
| 20 | from services import service_manager |
| 21 | from sitewide import hostinghome |
| 22 | from sitewide import projectsearch |
| 23 | from testing import fake |
| 24 | from testing import testing_helpers |
| 25 | |
| 26 | |
| 27 | class MockProjectSearchPipeline(object): |
| 28 | |
| 29 | def __init__(self, _mr, services): |
| 30 | self.visible_results = services.mock_visible_results |
| 31 | self.pagination = None |
| 32 | |
| 33 | def SearchForIDs(self, domain=None): |
| 34 | pass |
| 35 | |
| 36 | def GetProjectsAndPaginate(self, cnxn, list_page_url): |
| 37 | pass |
| 38 | |
| 39 | |
| 40 | class HostingHomeTest(unittest.TestCase): |
| 41 | |
| 42 | def setUp(self): |
| 43 | self.services = service_manager.Services( |
| 44 | project=fake.ProjectService(), |
| 45 | project_star=fake.ProjectStarService()) |
| 46 | self.services.mock_visible_results = [] |
| 47 | self.project_a = self.services.project.TestAddProject('a', project_id=1) |
| 48 | self.project_b = self.services.project.TestAddProject('b', project_id=2) |
| 49 | |
Adrià Vilanova MartÃnez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame^] | 50 | self.servlet = hostinghome.HostingHome(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 51 | self.mr = testing_helpers.MakeMonorailRequest(user_info={'user_id': 111}) |
| 52 | |
| 53 | self.orig_pipeline_class = projectsearch.ProjectSearchPipeline |
| 54 | projectsearch.ProjectSearchPipeline = MockProjectSearchPipeline |
| 55 | |
| 56 | def tearDown(self): |
| 57 | projectsearch.ProjectSearchPipeline = self.orig_pipeline_class |
| 58 | |
| 59 | def testSearch_ZeroResults(self): |
| 60 | self.services.mock_visible_results = [] |
| 61 | page_data = self.servlet.GatherPageData(self.mr) |
| 62 | self.assertEqual([], page_data['projects']) |
| 63 | |
| 64 | def testSearch_NonzeroResults(self): |
| 65 | self.services.mock_visible_results = [self.project_a, self.project_b] |
| 66 | page_data = self.servlet.GatherPageData(self.mr) |
| 67 | self.assertEqual(['a', 'b'], |
| 68 | [pv.project_name for pv in page_data['projects']]) |
| 69 | |
| 70 | def testStarCounts(self): |
| 71 | """Test the display of star counts on each displayed project.""" |
| 72 | self.services.mock_visible_results = [self.project_a, self.project_b] |
| 73 | # We go straight to the services layer because this is a test set up |
| 74 | # rather than an actual user request. |
| 75 | self.services.project_star.SetStar('fake cnxn', 1, 111, True) |
| 76 | self.services.project_star.SetStar('fake cnxn', 1, 222, True) |
| 77 | page_data = self.servlet.GatherPageData(self.mr) |
| 78 | project_view_a, project_view_b = page_data['projects'] |
| 79 | self.assertEqual(2, project_view_a.num_stars) |
| 80 | self.assertEqual(0, project_view_b.num_stars) |
| 81 | |
| 82 | def testStarredProjects(self): |
| 83 | self.services.mock_visible_results = [self.project_a, self.project_b] |
| 84 | self.services.project_star.SetStar('fake cnxn', 1, 111, True) |
| 85 | page_data = self.servlet.GatherPageData(self.mr) |
| 86 | project_view_a, project_view_b = page_data['projects'] |
| 87 | self.assertTrue(project_view_a.starred) |
| 88 | self.assertFalse(project_view_b.starred) |
| 89 | |
| 90 | def testGatherPageData(self): |
| 91 | mr = testing_helpers.MakeMonorailRequest() |
| 92 | page_data = self.servlet.GatherPageData(mr) |
| 93 | self.assertEqual(settings.learn_more_link, page_data['learn_more_link']) |
| 94 | |
| 95 | def testGatherPageData_CanCreateProject(self): |
| 96 | mr = testing_helpers.MakeMonorailRequest() |
| 97 | mr.perms = permissions.PermissionSet([permissions.CREATE_PROJECT]) |
| 98 | page_data = self.servlet.GatherPageData(mr) |
| 99 | self.assertEqual( |
| 100 | ezt.boolean(settings.project_creation_restriction == |
| 101 | site_pb2.UserTypeRestriction.ANYONE), |
| 102 | page_data['can_create_project']) |
| 103 | |
| 104 | mr.perms = permissions.PermissionSet([]) |
| 105 | page_data = self.servlet.GatherPageData(mr) |
| 106 | self.assertEqual(ezt.boolean(False), page_data['can_create_project']) |
| 107 | |
| 108 | @mock.patch('settings.domain_to_default_project', {}) |
| 109 | def testMaybeRedirectToDomainDefaultProject_NoMatch(self): |
| 110 | """No redirect if the user is not accessing via a configured domain.""" |
| 111 | mr = testing_helpers.MakeMonorailRequest() |
| 112 | mr.request.host = 'example.com' |
| 113 | msg = self.servlet._MaybeRedirectToDomainDefaultProject(mr) |
| 114 | print('msg: ' + msg) |
| 115 | self.assertTrue(msg.startswith('No configured')) |
| 116 | |
| 117 | @mock.patch('settings.domain_to_default_project', {'example.com': 'huh'}) |
| 118 | def testMaybeRedirectToDomainDefaultProject_NoSuchProject(self): |
| 119 | """No redirect if the configured project does not exist.""" |
| 120 | mr = testing_helpers.MakeMonorailRequest() |
| 121 | mr.request.host = 'example.com' |
| 122 | print('host is %r' % mr.request.host) |
| 123 | msg = self.servlet._MaybeRedirectToDomainDefaultProject(mr) |
| 124 | print('msg: ' + msg) |
| 125 | self.assertTrue(msg.endswith('not found')) |
| 126 | |
| 127 | @mock.patch('settings.domain_to_default_project', {'example.com': 'a'}) |
| 128 | def testMaybeRedirectToDomainDefaultProject_CantView(self): |
| 129 | """No redirect if the user can't view the configured project.""" |
| 130 | self.project_a.access = project_pb2.ProjectAccess.MEMBERS_ONLY |
| 131 | mr = testing_helpers.MakeMonorailRequest() |
| 132 | mr.request.host = 'example.com' |
| 133 | msg = self.servlet._MaybeRedirectToDomainDefaultProject(mr) |
| 134 | print('msg: ' + msg) |
| 135 | self.assertTrue(msg.startswith('User cannot')) |
| 136 | |
| 137 | @mock.patch('settings.domain_to_default_project', {'example.com': 'a'}) |
| 138 | def testMaybeRedirectToDomainDefaultProject_Redirect(self): |
| 139 | """We redirect if there's a configured project that the user can view.""" |
| 140 | mr = testing_helpers.MakeMonorailRequest() |
| 141 | mr.request.host = 'example.com' |
| 142 | self.servlet.redirect = mock.Mock() |
| 143 | msg = self.servlet._MaybeRedirectToDomainDefaultProject(mr) |
| 144 | print('msg: ' + msg) |
| 145 | self.assertTrue(msg.startswith('Redirected')) |
| 146 | self.servlet.redirect.assert_called_once() |