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