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 | """Unittests for monorail.framework.banned.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
| 11 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 12 | from framework import banned |
| 13 | from framework import monorailrequest |
| 14 | from services import service_manager |
| 15 | from testing import testing_helpers |
| 16 | |
| 17 | |
| 18 | class BannedTest(unittest.TestCase): |
| 19 | |
| 20 | def setUp(self): |
| 21 | self.services = service_manager.Services() |
| 22 | |
| 23 | def testAssertBasePermission(self): |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 24 | servlet = banned.Banned(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 25 | |
| 26 | mr = monorailrequest.MonorailRequest(self.services) |
| 27 | mr.auth.user_id = 0 # Anon user cannot see banned page. |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 28 | with self.assertRaises(Exception) as cm: |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 29 | servlet.AssertBasePermission(mr) |
| 30 | self.assertEqual(404, cm.exception.code) |
| 31 | |
| 32 | mr.auth.user_id = 111 # User who is not banned cannot view banned page. |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 33 | with self.assertRaises(Exception) as cm: |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 34 | servlet.AssertBasePermission(mr) |
| 35 | self.assertEqual(404, cm.exception.code) |
| 36 | |
| 37 | # This should not throw exception. |
| 38 | mr.auth.user_pb.banned = 'spammer' |
| 39 | servlet.AssertBasePermission(mr) |
| 40 | |
| 41 | def testGatherPageData(self): |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 42 | servlet = banned.Banned(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 43 | self.assertNotEqual(servlet.template, None) |
| 44 | |
| 45 | _request, mr = testing_helpers.GetRequestObjects() |
| 46 | page_data = servlet.GatherPageData(mr) |
| 47 | |
| 48 | self.assertFalse(page_data['is_plus_address']) |
| 49 | self.assertEqual(None, page_data['currentPageURLEncoded']) |
| 50 | |
| 51 | mr.auth.user_pb.email = 'user+shadystuff@example.com' |
| 52 | page_data = servlet.GatherPageData(mr) |
| 53 | |
| 54 | self.assertTrue(page_data['is_plus_address']) |
| 55 | self.assertEqual(None, page_data['currentPageURLEncoded']) |