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 | """Unit tests for servlet base class module.""" |
| 5 | from __future__ import print_function |
| 6 | from __future__ import division |
| 7 | from __future__ import absolute_import |
| 8 | |
| 9 | import time |
| 10 | import mock |
| 11 | import unittest |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 12 | import logging |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 13 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 14 | from google.appengine.ext import testbed |
| 15 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 16 | from framework import framework_constants |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 17 | from framework import servlet |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 18 | from framework import servlet_helpers |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 19 | from framework import xsrf |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 20 | from mrproto import project_pb2 |
| 21 | from mrproto import tracker_pb2 |
| 22 | from mrproto import user_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 23 | from services import service_manager |
| 24 | from testing import fake |
| 25 | from testing import testing_helpers |
| 26 | |
| 27 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 28 | class _TestableServlet(servlet.Servlet): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 29 | """A tiny concrete subclass of abstract class Servlet.""" |
| 30 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 31 | def __init__(self, services=None, do_post_redirect=True): |
| 32 | super(_TestableServlet, self).__init__(services=services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 33 | self.do_post_redirect = do_post_redirect |
| 34 | self.seen_post_data = None |
| 35 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 36 | |
| 37 | class ServletTest(unittest.TestCase): |
| 38 | |
| 39 | def setUp(self): |
| 40 | services = service_manager.Services( |
| 41 | project=fake.ProjectService(), |
| 42 | project_star=fake.ProjectStarService(), |
| 43 | user=fake.UserService(), |
| 44 | usergroup=fake.UserGroupService()) |
| 45 | services.user.TestAddUser('user@example.com', 111) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 46 | self.page_class = servlet.Servlet(services=services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 47 | self.testbed = testbed.Testbed() |
| 48 | self.testbed.activate() |
| 49 | self.testbed.init_user_stub() |
| 50 | self.testbed.init_memcache_stub() |
| 51 | self.testbed.init_datastore_v3_stub() |
| 52 | |
| 53 | def tearDown(self): |
| 54 | self.testbed.deactivate() |
| 55 | |
| 56 | def testDefaultValues(self): |
| 57 | self.assertEqual(None, self.page_class._MAIN_TAB_MODE) |
| 58 | self.assertTrue(self.page_class._TEMPLATE_PATH.endswith('/templates/')) |
| 59 | self.assertEqual(None, self.page_class._PAGE_TEMPLATE) |
| 60 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 61 | @mock.patch('flask.abort') |
| 62 | def testCheckForMovedProject_NoRedirect(self, mock_abort): |
| 63 | project = fake.Project( |
| 64 | project_name='proj', state=project_pb2.ProjectState.LIVE) |
| 65 | request, mr = testing_helpers.GetRequestObjects( |
| 66 | path='/p/proj', project=project) |
| 67 | self.page_class._CheckForMovedProject(mr, request) |
| 68 | mock_abort.assert_not_called() |
| 69 | |
| 70 | request, mr = testing_helpers.GetRequestObjects( |
| 71 | path='/p/proj/source/browse/p/adminAdvanced', project=project) |
| 72 | self.page_class._CheckForMovedProject(mr, request) |
| 73 | mock_abort.assert_not_called() |
| 74 | |
| 75 | @mock.patch('flask.redirect') |
| 76 | def testCheckForMovedProject_Redirect(self, mock_redirect): |
| 77 | project = fake.Project(project_name='proj', moved_to='http://example.com') |
| 78 | request, mr = testing_helpers.GetRequestObjects( |
| 79 | path='/p/proj', project=project) |
| 80 | self.page_class.request_path = '/p/test' |
| 81 | self.page_class._CheckForMovedProject(mr, request) |
| 82 | mock_redirect.assert_called_once_with( |
| 83 | 'http://127.0.0.1/hosting/moved?project=proj', code=302) |
| 84 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 85 | def testGatherBaseData(self): |
| 86 | project = self.page_class.services.project.TestAddProject( |
| 87 | 'testproj', state=project_pb2.ProjectState.LIVE) |
| 88 | project.cached_content_timestamp = 12345 |
| 89 | |
| 90 | (_request, mr) = testing_helpers.GetRequestObjects( |
| 91 | path='/p/testproj/feeds', project=project) |
| 92 | nonce = '1a2b3c4d5e6f7g' |
| 93 | |
| 94 | base_data = self.page_class.GatherBaseData(mr, nonce) |
| 95 | |
| 96 | self.assertEqual(base_data['nonce'], nonce) |
| 97 | self.assertEqual(base_data['projectname'], 'testproj') |
| 98 | self.assertEqual(base_data['project'].cached_content_timestamp, 12345) |
| 99 | self.assertEqual(base_data['project_alert'], None) |
| 100 | |
| 101 | self.assertTrue(base_data['currentPageURL'].endswith('/p/testproj/feeds')) |
| 102 | self.assertTrue( |
| 103 | base_data['currentPageURLEncoded'].endswith('%2Fp%2Ftestproj%2Ffeeds')) |
| 104 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 105 | def testGatherHelpData_Normal(self): |
| 106 | project = fake.Project(project_name='proj') |
| 107 | _request, mr = testing_helpers.GetRequestObjects( |
| 108 | path='/p/proj', project=project) |
| 109 | help_data = self.page_class.GatherHelpData(mr, {}) |
| 110 | self.assertEqual(None, help_data['cue']) |
| 111 | self.assertEqual(None, help_data['account_cue']) |