blob: 2f4ee4fb10f220165f13ec943693d88c2f8d9598 [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"""Unit tests for the moved project notification page servlet."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010010import six
Copybara854996b2021-09-07 19:36:02 +000011import unittest
12
Copybara854996b2021-09-07 19:36:02 +000013from framework import exceptions
14from services import service_manager
15from sitewide import moved
16from testing import fake
17from testing import testing_helpers
18
19
20class MovedTest(unittest.TestCase):
21
22 def setUp(self):
23 self.services = service_manager.Services(
24 project=fake.ProjectService())
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020025 self.servlet = moved.ProjectMoved(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000026 self.old_project = 'old-project'
27
28 def testGatherPageData_NoProjectSpecified(self):
29 # Project was not included in URL, so raise exception, will cause 400.
30 _, mr = testing_helpers.GetRequestObjects(
31 path='/hosting/moved')
32
33 with self.assertRaises(exceptions.InputException):
34 self.servlet.GatherPageData(mr)
35
36 def testGatherPageData_NoSuchProject(self):
37 # Project doesn't exist, so 404 NOT FOUND.
38 _, mr = testing_helpers.GetRequestObjects(
39 path='/hosting/moved?project=nonexistent')
40
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020041 with self.assertRaises(Exception) as cm:
Copybara854996b2021-09-07 19:36:02 +000042 self.servlet.GatherPageData(mr)
43 self.assertEqual(404, cm.exception.code)
44
45 def testGatherPageData_NotMoved(self):
46 # Project exists but has not been moved, so 400 BAD_REQUEST.
47 self.services.project.TestAddProject(self.old_project)
48 _, mr = testing_helpers.GetRequestObjects(
49 path='/hosting/moved?project=%s' % self.old_project)
50
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020051 with self.assertRaises(Exception) as cm:
Copybara854996b2021-09-07 19:36:02 +000052 self.servlet.GatherPageData(mr)
53 self.assertEqual(400, cm.exception.code)
54
55 def testGatherPageData_URL(self):
56 # Display the moved_to url if it is valid.
57 project = self.services.project.TestAddProject(self.old_project)
58 project.moved_to = 'https://other-tracker.bugs'
59 _, mr = testing_helpers.GetRequestObjects(
60 path='/hosting/moved?project=%s' % self.old_project)
61
62 page_data = self.servlet.GatherPageData(mr)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010063 six.assertCountEqual(
64 self, ['project_name', 'moved_to_url'], list(page_data.keys()))
Copybara854996b2021-09-07 19:36:02 +000065 self.assertEqual(self.old_project, page_data['project_name'])
66 self.assertEqual('https://other-tracker.bugs', page_data['moved_to_url'])
67
68 def testGatherPageData_ProjectName(self):
69 # Construct the moved-to url from just the project name.
70 project = self.services.project.TestAddProject(self.old_project)
71 project.moved_to = 'new-project'
72 _, mr = testing_helpers.GetRequestObjects(
73 path='/hosting/moved?project=%s' % self.old_project)
74
75 page_data = self.servlet.GatherPageData(mr)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010076 six.assertCountEqual(
77 self, ['project_name', 'moved_to_url'], list(page_data.keys()))
Copybara854996b2021-09-07 19:36:02 +000078 self.assertEqual(self.old_project, page_data['project_name'])
79 self.assertEqual('http://127.0.0.1/p/new-project/',
80 page_data['moved_to_url'])
81
82 def testGatherPageData_HttpProjectName(self):
83 # A project named "http-foo" gets treated as a project, not a url.
84 project = self.services.project.TestAddProject(self.old_project)
85 project.moved_to = 'http-project'
86 _, mr = testing_helpers.GetRequestObjects(
87 path='/hosting/moved?project=%s' % self.old_project)
88
89 page_data = self.servlet.GatherPageData(mr)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010090 six.assertCountEqual(
91 self, ['project_name', 'moved_to_url'], list(page_data.keys()))
Copybara854996b2021-09-07 19:36:02 +000092 self.assertEqual(self.old_project, page_data['project_name'])
93 self.assertEqual('http://127.0.0.1/p/http-project/',
94 page_data['moved_to_url'])
95
96 def testGatherPageData_BadScheme(self):
97 # We only display URLs that start with 'http(s)://'.
98 project = self.services.project.TestAddProject(self.old_project)
99 project.moved_to = 'javascript:alert(1)'
100 _, mr = testing_helpers.GetRequestObjects(
101 path='/hosting/moved?project=%s' % self.old_project)
102
103 page_data = self.servlet.GatherPageData(mr)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100104 six.assertCountEqual(
105 self, ['project_name', 'moved_to_url'], list(page_data.keys()))
Copybara854996b2021-09-07 19:36:02 +0000106 self.assertEqual(self.old_project, page_data['project_name'])
107 self.assertEqual('#invalid-destination-url', page_data['moved_to_url'])