Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # 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. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Unit tests for the moved project notification page servlet.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 10 | import six |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 11 | import unittest |
| 12 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 13 | from framework import exceptions |
| 14 | from services import service_manager |
| 15 | from sitewide import moved |
| 16 | from testing import fake |
| 17 | from testing import testing_helpers |
| 18 | |
| 19 | |
| 20 | class MovedTest(unittest.TestCase): |
| 21 | |
| 22 | def setUp(self): |
| 23 | self.services = service_manager.Services( |
| 24 | project=fake.ProjectService()) |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 25 | self.servlet = moved.ProjectMoved(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 26 | 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 41 | with self.assertRaises(Exception) as cm: |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 42 | 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 51 | with self.assertRaises(Exception) as cm: |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 52 | 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 63 | six.assertCountEqual( |
| 64 | self, ['project_name', 'moved_to_url'], list(page_data.keys())) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 65 | 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 76 | six.assertCountEqual( |
| 77 | self, ['project_name', 'moved_to_url'], list(page_data.keys())) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 78 | 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 90 | six.assertCountEqual( |
| 91 | self, ['project_name', 'moved_to_url'], list(page_data.keys())) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 92 | 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 104 | six.assertCountEqual( |
| 105 | self, ['project_name', 'moved_to_url'], list(page_data.keys())) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 106 | self.assertEqual(self.old_project, page_data['project_name']) |
| 107 | self.assertEqual('#invalid-destination-url', page_data['moved_to_url']) |