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 | """Tests for the ban spammer feature.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import json |
| 11 | import mock |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 12 | import six |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 13 | import unittest |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 14 | from six.moves import urllib |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 15 | |
| 16 | import settings |
| 17 | from features import banspammer |
| 18 | from framework import framework_views |
| 19 | from framework import permissions |
| 20 | from framework import urls |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 21 | from mrproto import tracker_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 22 | from services import service_manager |
| 23 | from testing import fake |
| 24 | from testing import testing_helpers |
| 25 | |
| 26 | class 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 36 | self.servlet = banspammer.BanSpammer(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 37 | |
| 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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 75 | 'body': six.ensure_binary(urllib.parse.urlencode(params)), |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 76 | '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ínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 84 | _, kwargs = get_client_mock().create_task.call_args |
| 85 | self.assertEqual(kwargs['task'], task) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | class BanSpammerTaskTest(unittest.TestCase): |
| 89 | def setUp(self): |
| 90 | self.services = service_manager.Services( |
| 91 | issue=fake.IssueService(), |
| 92 | spam=fake.SpamService()) |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 93 | self.servlet = banspammer.BanSpammerTask(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 94 | |
| 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 100 | res = self.servlet.HandleRequest(mr) |
| 101 | self.assertEqual(res, json.dumps({'comments': 0, 'issues': 0})) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 102 | |
| 103 | def testProcessFormData_okSomeIssues(self): |
| 104 | mr = testing_helpers.MakeMonorailRequest( |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 105 | path=urls.BAN_SPAMMER_TASK + '.do', |
| 106 | method='POST', |
| 107 | params={ |
| 108 | 'spammer_id': 111, |
| 109 | 'reporter_id': 222 |
| 110 | }) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 111 | 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 116 | res = self.servlet.HandleRequest(mr) |
| 117 | self.assertEqual(res, json.dumps({'comments': 0, 'issues': 10})) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 118 | |
| 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 139 | res = self.servlet.HandleRequest(mr) |
| 140 | self.assertEqual(res, json.dumps({'comments': 50, 'issues': 10})) |