blob: e12c5066b9457d0e4efb6c73973ba602371da68e [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# 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"""Tests for the ban spammer feature."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import json
12import mock
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
21from proto import tracker_pb2
22from 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ínezde942802022-07-15 14:06:55 +020075 'body': 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()
84 ((_parent, called_task), _kwargs) = get_client_mock().create_task.call_args
85 self.assertEqual(called_task, task)
86
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(
105 path=urls.BAN_SPAMMER_TASK + '.do', method='POST',
106 params={'spammer_id': 111, 'reporter_id': 222})
107
108 for i in range(0, 10):
109 issue = fake.MakeTestIssue(
110 1, i, 'issue_summary', 'New', 111, project_name='project-name')
111 self.servlet.services.issue.TestAddIssue(issue)
112
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200113 res = self.servlet.HandleRequest(mr)
114 self.assertEqual(res, json.dumps({'comments': 0, 'issues': 10}))
Copybara854996b2021-09-07 19:36:02 +0000115
116 def testProcessFormData_okSomeCommentsAndIssues(self):
117 mr = testing_helpers.MakeMonorailRequest(
118 path=urls.BAN_SPAMMER_TASK + '.do', method='POST',
119 params={'spammer_id': 111, 'reporter_id': 222})
120
121 for i in range(0, 12):
122 issue = fake.MakeTestIssue(
123 1, i, 'issue_summary', 'New', 111, project_name='project-name')
124 self.servlet.services.issue.TestAddIssue(issue)
125
126 for i in range(10, 20):
127 issue = fake.MakeTestIssue(
128 1, i, 'issue_summary', 'New', 222, project_name='project-name')
129 self.servlet.services.issue.TestAddIssue(issue)
130 for _ in range(0, 5):
131 comment = tracker_pb2.IssueComment()
132 comment.project_id = 1
133 comment.user_id = 111
134 comment.issue_id = issue.issue_id
135 self.servlet.services.issue.TestAddComment(comment, issue.local_id)
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200136 res = self.servlet.HandleRequest(mr)
137 self.assertEqual(res, json.dumps({'comments': 50, 'issues': 10}))