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