blob: f96358ac4dac5c51da6d9299ff2d24e65396b4d0 [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"""Tests for the ban spammer feature."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import json
11import mock
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010012import six
Copybara854996b2021-09-07 19:36:02 +000013import unittest
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020014from six.moves import urllib
Copybara854996b2021-09-07 19:36:02 +000015
16import settings
17from features import banspammer
18from framework import framework_views
19from framework import permissions
20from framework import urls
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010021from mrproto import tracker_pb2
Copybara854996b2021-09-07 19:36:02 +000022from services import service_manager
23from testing import fake
24from testing import testing_helpers
25
26class BanSpammerTest(unittest.TestCase):
27
28 def setUp(self):
29 self.cnxn = 'fake cnxn'
30 self.mr = testing_helpers.MakeMonorailRequest()
31 self.services = service_manager.Services(
32 issue=fake.IssueService(),
33 project=fake.ProjectService(),
34 spam=fake.SpamService(),
35 user=fake.UserService())
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020036 self.servlet = banspammer.BanSpammer(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000037
38 @mock.patch('framework.cloud_tasks_helpers._get_client')
39 def testProcessFormData_noPermission(self, get_client_mock):
40 self.servlet.services.user.TestAddUser('member', 222)
41 self.servlet.services.user.TestAddUser('spammer@domain.com', 111)
42 mr = testing_helpers.MakeMonorailRequest(
43 path='/u/spammer@domain.com/banSpammer.do',
44 perms=permissions.GetPermissions(None, {}, None))
45 mr.viewed_user_auth.user_view = framework_views.MakeUserView(mr.cnxn,
46 self.servlet.services.user, 111)
47 mr.auth.user_id = 222
48 self.assertRaises(permissions.PermissionException,
49 self.servlet.AssertBasePermission, mr)
50 try:
51 self.servlet.ProcessFormData(mr, {})
52 except permissions.PermissionException:
53 pass
54 self.assertEqual(get_client_mock().queue_path.call_count, 0)
55 self.assertEqual(get_client_mock().create_task.call_count, 0)
56
57 @mock.patch('framework.cloud_tasks_helpers._get_client')
58 def testProcessFormData_ok(self, get_client_mock):
59 self.servlet.services.user.TestAddUser('owner', 222)
60 self.servlet.services.user.TestAddUser('spammer@domain.com', 111)
61 mr = testing_helpers.MakeMonorailRequest(
62 path='/u/spammer@domain.com/banSpammer.do',
63 perms=permissions.ADMIN_PERMISSIONSET)
64 mr.viewed_user_auth.user_view = framework_views.MakeUserView(mr.cnxn,
65 self.servlet.services.user, 111)
66 mr.viewed_user_auth.user_pb.user_id = 111
67 mr.auth.user_id = 222
68 self.servlet.ProcessFormData(mr, {'banned': 'non-empty'})
69
70 params = {'spammer_id': 111, 'reporter_id': 222, 'is_spammer': True}
71 task = {
72 'app_engine_http_request':
73 {
74 'relative_uri': urls.BAN_SPAMMER_TASK + '.do',
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010075 'body': six.ensure_binary(urllib.parse.urlencode(params)),
Copybara854996b2021-09-07 19:36:02 +000076 'headers': {
77 'Content-type': 'application/x-www-form-urlencoded'
78 }
79 }
80 }
81 get_client_mock().queue_path.assert_called_with(
82 settings.app_id, settings.CLOUD_TASKS_REGION, 'default')
83 get_client_mock().create_task.assert_called_once()
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010084 _, kwargs = get_client_mock().create_task.call_args
85 self.assertEqual(kwargs['task'], task)
Copybara854996b2021-09-07 19:36:02 +000086
87
88class BanSpammerTaskTest(unittest.TestCase):
89 def setUp(self):
90 self.services = service_manager.Services(
91 issue=fake.IssueService(),
92 spam=fake.SpamService())
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020093 self.servlet = banspammer.BanSpammerTask(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000094
95 def testProcessFormData_okNoIssues(self):
96 mr = testing_helpers.MakeMonorailRequest(
97 path=urls.BAN_SPAMMER_TASK + '.do', method='POST',
98 params={'spammer_id': 111, 'reporter_id': 222})
99
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200100 res = self.servlet.HandleRequest(mr)
101 self.assertEqual(res, json.dumps({'comments': 0, 'issues': 0}))
Copybara854996b2021-09-07 19:36:02 +0000102
103 def testProcessFormData_okSomeIssues(self):
104 mr = testing_helpers.MakeMonorailRequest(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100105 path=urls.BAN_SPAMMER_TASK + '.do',
106 method='POST',
107 params={
108 'spammer_id': 111,
109 'reporter_id': 222
110 })
Copybara854996b2021-09-07 19:36:02 +0000111 for i in range(0, 10):
112 issue = fake.MakeTestIssue(
113 1, i, 'issue_summary', 'New', 111, project_name='project-name')
114 self.servlet.services.issue.TestAddIssue(issue)
115
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200116 res = self.servlet.HandleRequest(mr)
117 self.assertEqual(res, json.dumps({'comments': 0, 'issues': 10}))
Copybara854996b2021-09-07 19:36:02 +0000118
119 def testProcessFormData_okSomeCommentsAndIssues(self):
120 mr = testing_helpers.MakeMonorailRequest(
121 path=urls.BAN_SPAMMER_TASK + '.do', method='POST',
122 params={'spammer_id': 111, 'reporter_id': 222})
123
124 for i in range(0, 12):
125 issue = fake.MakeTestIssue(
126 1, i, 'issue_summary', 'New', 111, project_name='project-name')
127 self.servlet.services.issue.TestAddIssue(issue)
128
129 for i in range(10, 20):
130 issue = fake.MakeTestIssue(
131 1, i, 'issue_summary', 'New', 222, project_name='project-name')
132 self.servlet.services.issue.TestAddIssue(issue)
133 for _ in range(0, 5):
134 comment = tracker_pb2.IssueComment()
135 comment.project_id = 1
136 comment.user_id = 111
137 comment.issue_id = issue.issue_id
138 self.servlet.services.issue.TestAddComment(comment, issue.local_id)
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200139 res = self.servlet.HandleRequest(mr)
140 self.assertEqual(res, json.dumps({'comments': 50, 'issues': 10}))