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 | """Unit tests for issuelistcsv module.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import unittest |
| 12 | |
| 13 | from google.appengine.ext import testbed |
| 14 | |
| 15 | import webapp2 |
| 16 | |
| 17 | from framework import permissions |
| 18 | from framework import sorting |
| 19 | from framework import xsrf |
| 20 | from services import service_manager |
| 21 | from testing import fake |
| 22 | from testing import testing_helpers |
| 23 | from features import hotlistissuescsv |
| 24 | |
| 25 | |
| 26 | class HotlistIssuesCsvTest(unittest.TestCase): |
| 27 | |
| 28 | def setUp(self): |
| 29 | self.testbed = testbed.Testbed() |
| 30 | self.testbed.activate() |
| 31 | self.testbed.init_memcache_stub() |
| 32 | self.testbed.init_datastore_v3_stub() |
| 33 | self.services = service_manager.Services( |
| 34 | issue_star=fake.IssueStarService(), |
| 35 | config=fake.ConfigService(), |
| 36 | user=fake.UserService(), |
| 37 | issue=fake.IssueService(), |
| 38 | project=fake.ProjectService(), |
| 39 | cache_manager=fake.CacheManager(), |
| 40 | features=fake.FeaturesService()) |
| 41 | self.servlet = hotlistissuescsv.HotlistIssuesCsv( |
| 42 | 'req', webapp2.Response(), services=self.services) |
| 43 | self.user1 = self.services.user.TestAddUser('testuser@gmail.com', 111) |
| 44 | self.user2 = self.services.user.TestAddUser('testuser2@gmail.com', 222) |
| 45 | self.services.project.TestAddProject('project-name', project_id=1) |
| 46 | self.issue1 = fake.MakeTestIssue( |
| 47 | 1, 1, 'issue_summary', 'New', 111, project_name='project-name') |
| 48 | self.services.issue.TestAddIssue(self.issue1) |
| 49 | self.issues = [self.issue1] |
| 50 | self.hotlist_item_fields = [ |
| 51 | (issue.issue_id, rank, 111, 1205079300, '') for |
| 52 | rank, issue in enumerate(self.issues)] |
| 53 | self.hotlist = self.services.features.TestAddHotlist( |
| 54 | 'MyHotlist', hotlist_id=123, owner_ids=[222], editor_ids=[111], |
| 55 | hotlist_item_fields=self.hotlist_item_fields) |
| 56 | self._MakeMR('/u/222/hotlists/MyHotlist') |
| 57 | sorting.InitializeArtValues(self.services) |
| 58 | |
| 59 | def _MakeMR(self, path): |
| 60 | self.mr = testing_helpers.MakeMonorailRequest( |
| 61 | hotlist=self.hotlist, path=path, services=self.services) |
| 62 | self.mr.hotlist_id = self.hotlist.hotlist_id |
| 63 | self.mr.hotlist = self.hotlist |
| 64 | |
| 65 | def testGatherPageData_AnonUsers(self): |
| 66 | """Anonymous users cannot download the issue list.""" |
| 67 | self.mr.auth.user_id = 0 |
| 68 | self.assertRaises(permissions.PermissionException, |
| 69 | self.servlet.GatherPageData, self.mr) |
| 70 | |
| 71 | def testGatherPageData_NoXSRF(self): |
| 72 | """Users need a valid XSRF token to download the issue list.""" |
| 73 | # Note no token query-string parameter is set. |
| 74 | self.mr.auth.user_id = self.user2.user_id |
| 75 | self.assertRaises(xsrf.TokenIncorrect, |
| 76 | self.servlet.GatherPageData, self.mr) |
| 77 | |
| 78 | def testGatherPageData_BadXSRF(self): |
| 79 | """Users need a valid XSRF token to download the issue list.""" |
| 80 | for path in ('/u/222/hotlists/MyHotlist', |
| 81 | '/u/testuser2@gmail.com/hotlists/MyHotlist'): |
| 82 | token = 'bad' |
| 83 | self._MakeMR(path + '?token=%s' % token) |
| 84 | self.mr.auth.user_id = self.user2.user_id |
| 85 | self.assertRaises(xsrf.TokenIncorrect, |
| 86 | self.servlet.GatherPageData, self.mr) |
| 87 | |
| 88 | def testGatherPageData_Normal(self): |
| 89 | """Users can get the hotlist issue list.""" |
| 90 | for path in ('/u/222/hotlists/MyHotlist', |
| 91 | '/u/testuser2@gmail.com/hotlists/MyHotlist'): |
| 92 | form_token_path = self.servlet._FormHandlerURL(path) |
| 93 | token = xsrf.GenerateToken(self.user1.user_id, form_token_path) |
| 94 | self._MakeMR(path + '?token=%s' % token) |
| 95 | self.mr.auth.email = self.user1.email |
| 96 | self.mr.auth.user_id = self.user1.user_id |
| 97 | self.servlet.GatherPageData(self.mr) |