blob: f899ce886bcc3779e7613c301dc8ce7124a6238a [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Unit tests for issuelistcsv module."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010010import six
Copybara854996b2021-09-07 19:36:02 +000011import unittest
12
13from google.appengine.ext import testbed
14
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010015import flask
Copybara854996b2021-09-07 19:36:02 +000016
17from framework import permissions
18from framework import sorting
19from framework import xsrf
20from services import service_manager
21from testing import fake
22from testing import testing_helpers
23from features import hotlistissuescsv
24
25
26class 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())
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010041 self.servlet = hotlistissuescsv.HotlistIssuesCsv(services=self.services)
42 self.servlet.response = flask.Response()
Copybara854996b2021-09-07 19:36:02 +000043 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'
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010083 self._MakeMR(path + '?token=%s' % six.ensure_str(token))
Copybara854996b2021-09-07 19:36:02 +000084 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)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010094 self._MakeMR(path + '?token=%s' % six.ensure_str(token))
Copybara854996b2021-09-07 19:36:02 +000095 self.mr.auth.email = self.user1.email
96 self.mr.auth.user_id = self.user1.user_id
97 self.servlet.GatherPageData(self.mr)