blob: 33f3644916e17614a54745d507715d30b04c227f [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"""Unit tests for servlet base class module."""
5from __future__ import print_function
6from __future__ import division
7from __future__ import absolute_import
8
9import time
10import mock
11import unittest
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010012import logging
Copybara854996b2021-09-07 19:36:02 +000013
Copybara854996b2021-09-07 19:36:02 +000014from google.appengine.ext import testbed
15
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010016from framework import framework_constants
Copybara854996b2021-09-07 19:36:02 +000017from framework import servlet
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010018from framework import servlet_helpers
Copybara854996b2021-09-07 19:36:02 +000019from framework import xsrf
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010020from mrproto import project_pb2
21from mrproto import tracker_pb2
22from mrproto import user_pb2
Copybara854996b2021-09-07 19:36:02 +000023from services import service_manager
24from testing import fake
25from testing import testing_helpers
26
27
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010028class _TestableServlet(servlet.Servlet):
Copybara854996b2021-09-07 19:36:02 +000029 """A tiny concrete subclass of abstract class Servlet."""
30
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010031 def __init__(self, services=None, do_post_redirect=True):
32 super(_TestableServlet, self).__init__(services=services)
Copybara854996b2021-09-07 19:36:02 +000033 self.do_post_redirect = do_post_redirect
34 self.seen_post_data = None
35
Copybara854996b2021-09-07 19:36:02 +000036
37class 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ínezf19ea432024-01-23 20:20:52 +010046 self.page_class = servlet.Servlet(services=services)
Copybara854996b2021-09-07 19:36:02 +000047 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ínezf19ea432024-01-23 20:20:52 +010061 @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
Copybara854996b2021-09-07 19:36:02 +000085 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
Copybara854996b2021-09-07 19:36:02 +0000105 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'])