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