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