blob: 443f919b80ac594761ff5815f592db236adb73f3 [file] [log] [blame]
Adrià Vilanova Martínezac4a6442022-05-15 19:05:13 +02001# 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."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import time
11import mock
12import unittest
13import logging
14
15from google.appengine.ext import testbed
16
17from framework import flaskservlet, framework_constants, servlet_helpers
18from framework import xsrf
19from proto import project_pb2
20from proto import tracker_pb2
21from proto import user_pb2
22from services import service_manager
23from testing import fake
24from testing import testing_helpers
25
26
27class 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
36class 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
74 @mock.patch('flask.abort')
75 def testCheckForMovedProject_Redirect(self, mock_abort):
76 project = fake.Project(project_name='proj', moved_to='http://example.com')
77 request, mr = testing_helpers.GetRequestObjects(
78 path='/p/proj', project=project)
79 self.page_class._CheckForMovedProject(mr, request)
80 mock_abort.assert_called_once_with(302)
81
82 def testGatherBaseData(self):
83 project = self.page_class.services.project.TestAddProject(
84 'testproj', state=project_pb2.ProjectState.LIVE)
85 project.cached_content_timestamp = 12345
86
87 (_request, mr) = testing_helpers.GetRequestObjects(
88 path='/p/testproj/feeds', project=project)
89 nonce = '1a2b3c4d5e6f7g'
90
91 base_data = self.page_class.GatherBaseData(mr, nonce)
92
93 self.assertEqual(base_data['nonce'], nonce)
94 self.assertEqual(base_data['projectname'], 'testproj')
95 self.assertEqual(base_data['project'].cached_content_timestamp, 12345)
96 self.assertEqual(base_data['project_alert'], None)
97
98 self.assertTrue(base_data['currentPageURL'].endswith('/p/testproj/feeds'))
99 self.assertTrue(
100 base_data['currentPageURLEncoded'].endswith('%2Fp%2Ftestproj%2Ffeeds'))
101
102 def testGatherHelpData_Normal(self):
103 project = fake.Project(project_name='proj')
104 _request, mr = testing_helpers.GetRequestObjects(
105 path='/p/proj', project=project)
106 help_data = self.page_class.GatherHelpData(mr, {})
107 self.assertEqual(None, help_data['cue'])
108 self.assertEqual(None, help_data['account_cue'])