blob: eccb195f5cdea95c0626c1e0da93edd8a9769bea [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# 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"""Unit tests for the moved project notification page servlet."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
12
13import webapp2
14
15from framework import exceptions
16from services import service_manager
17from sitewide import moved
18from testing import fake
19from testing import testing_helpers
20
21
22class MovedTest(unittest.TestCase):
23
24 def setUp(self):
25 self.services = service_manager.Services(
26 project=fake.ProjectService())
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020027 self.servlet = moved.ProjectMoved(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000028 self.old_project = 'old-project'
29
30 def testGatherPageData_NoProjectSpecified(self):
31 # Project was not included in URL, so raise exception, will cause 400.
32 _, mr = testing_helpers.GetRequestObjects(
33 path='/hosting/moved')
34
35 with self.assertRaises(exceptions.InputException):
36 self.servlet.GatherPageData(mr)
37
38 def testGatherPageData_NoSuchProject(self):
39 # Project doesn't exist, so 404 NOT FOUND.
40 _, mr = testing_helpers.GetRequestObjects(
41 path='/hosting/moved?project=nonexistent')
42
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020043 with self.assertRaises(Exception) as cm:
Copybara854996b2021-09-07 19:36:02 +000044 self.servlet.GatherPageData(mr)
45 self.assertEqual(404, cm.exception.code)
46
47 def testGatherPageData_NotMoved(self):
48 # Project exists but has not been moved, so 400 BAD_REQUEST.
49 self.services.project.TestAddProject(self.old_project)
50 _, mr = testing_helpers.GetRequestObjects(
51 path='/hosting/moved?project=%s' % self.old_project)
52
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020053 with self.assertRaises(Exception) as cm:
Copybara854996b2021-09-07 19:36:02 +000054 self.servlet.GatherPageData(mr)
55 self.assertEqual(400, cm.exception.code)
56
57 def testGatherPageData_URL(self):
58 # Display the moved_to url if it is valid.
59 project = self.services.project.TestAddProject(self.old_project)
60 project.moved_to = 'https://other-tracker.bugs'
61 _, mr = testing_helpers.GetRequestObjects(
62 path='/hosting/moved?project=%s' % self.old_project)
63
64 page_data = self.servlet.GatherPageData(mr)
65 self.assertItemsEqual(
66 ['project_name', 'moved_to_url'],
67 list(page_data.keys()))
68 self.assertEqual(self.old_project, page_data['project_name'])
69 self.assertEqual('https://other-tracker.bugs', page_data['moved_to_url'])
70
71 def testGatherPageData_ProjectName(self):
72 # Construct the moved-to url from just the project name.
73 project = self.services.project.TestAddProject(self.old_project)
74 project.moved_to = 'new-project'
75 _, mr = testing_helpers.GetRequestObjects(
76 path='/hosting/moved?project=%s' % self.old_project)
77
78 page_data = self.servlet.GatherPageData(mr)
79 self.assertItemsEqual(
80 ['project_name', 'moved_to_url'],
81 list(page_data.keys()))
82 self.assertEqual(self.old_project, page_data['project_name'])
83 self.assertEqual('http://127.0.0.1/p/new-project/',
84 page_data['moved_to_url'])
85
86 def testGatherPageData_HttpProjectName(self):
87 # A project named "http-foo" gets treated as a project, not a url.
88 project = self.services.project.TestAddProject(self.old_project)
89 project.moved_to = 'http-project'
90 _, mr = testing_helpers.GetRequestObjects(
91 path='/hosting/moved?project=%s' % self.old_project)
92
93 page_data = self.servlet.GatherPageData(mr)
94 self.assertItemsEqual(
95 ['project_name', 'moved_to_url'],
96 list(page_data.keys()))
97 self.assertEqual(self.old_project, page_data['project_name'])
98 self.assertEqual('http://127.0.0.1/p/http-project/',
99 page_data['moved_to_url'])
100
101 def testGatherPageData_BadScheme(self):
102 # We only display URLs that start with 'http(s)://'.
103 project = self.services.project.TestAddProject(self.old_project)
104 project.moved_to = 'javascript:alert(1)'
105 _, mr = testing_helpers.GetRequestObjects(
106 path='/hosting/moved?project=%s' % self.old_project)
107
108 page_data = self.servlet.GatherPageData(mr)
109 self.assertItemsEqual(
110 ['project_name', 'moved_to_url'],
111 list(page_data.keys()))
112 self.assertEqual(self.old_project, page_data['project_name'])
113 self.assertEqual('#invalid-destination-url', page_data['moved_to_url'])