Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [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 | |
| 6 | """Tests for the user settings page.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import unittest |
| 12 | |
| 13 | import mox |
| 14 | |
| 15 | from framework import framework_helpers |
| 16 | from framework import permissions |
| 17 | from framework import template_helpers |
| 18 | from proto import user_pb2 |
| 19 | from services import service_manager |
| 20 | from sitewide import usersettings |
| 21 | from testing import fake |
| 22 | from testing import testing_helpers |
| 23 | |
| 24 | |
| 25 | class UserSettingsTest(unittest.TestCase): |
| 26 | |
| 27 | def setUp(self): |
| 28 | self.mox = mox.Mox() |
| 29 | self.services = service_manager.Services(user=fake.UserService()) |
| 30 | self.servlet = usersettings.UserSettings( |
| 31 | 'req', 'res', services=self.services) |
| 32 | |
| 33 | def tearDown(self): |
| 34 | self.mox.UnsetStubs() |
| 35 | |
| 36 | def testAssertBasePermission(self): |
| 37 | mr = testing_helpers.MakeMonorailRequest() |
| 38 | mr.auth.user_id = 111 |
| 39 | |
| 40 | # The following should return without exception. |
| 41 | self.servlet.AssertBasePermission(mr) |
| 42 | |
| 43 | # No logged in user means anonymous access, should raise error. |
| 44 | mr.auth.user_id = 0 |
| 45 | self.assertRaises(permissions.PermissionException, |
| 46 | self.servlet.AssertBasePermission, mr) |
| 47 | |
| 48 | def testGatherPageData(self): |
| 49 | self.mox.StubOutWithMock( |
| 50 | framework_helpers.UserSettings, 'GatherUnifiedSettingsPageData') |
| 51 | framework_helpers.UserSettings.GatherUnifiedSettingsPageData( |
| 52 | 0, None, mox.IsA(user_pb2.User), mox.IsA(user_pb2.UserPrefs) |
| 53 | ).AndReturn({'unified': None}) |
| 54 | self.mox.ReplayAll() |
| 55 | |
| 56 | mr = testing_helpers.MakeMonorailRequest() |
| 57 | page_data = self.servlet.GatherPageData(mr) |
| 58 | |
| 59 | self.assertItemsEqual( |
| 60 | ['logged_in_user_pb', 'unified', 'user_tab_mode', |
| 61 | 'viewed_user', 'offer_saved_queries_subtab', 'viewing_self'], |
| 62 | list(page_data.keys())) |
| 63 | self.assertEqual(template_helpers.PBProxy(mr.auth.user_pb), |
| 64 | page_data['logged_in_user_pb']) |
| 65 | |
| 66 | self.mox.VerifyAll() |