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 | """Tests for the ban spammer feature.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import json |
| 12 | import mock |
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 |
| 21 | from proto import tracker_pb2 |
| 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 | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 75 | 'body': 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() |
| 84 | ((_parent, called_task), _kwargs) = get_client_mock().create_task.call_args |
| 85 | self.assertEqual(called_task, task) |
| 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( |
| 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 113 | res = self.servlet.HandleRequest(mr) |
| 114 | self.assertEqual(res, json.dumps({'comments': 0, 'issues': 10})) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 115 | |
| 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 136 | res = self.servlet.HandleRequest(mr) |
| 137 | self.assertEqual(res, json.dumps({'comments': 50, 'issues': 10})) |