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 |
| 13 | import os |
| 14 | import unittest |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame^] | 15 | from six.moves import urllib |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 16 | import webapp2 |
| 17 | |
| 18 | import settings |
| 19 | from features import banspammer |
| 20 | from framework import framework_views |
| 21 | from framework import permissions |
| 22 | from framework import urls |
| 23 | from proto import tracker_pb2 |
| 24 | from services import service_manager |
| 25 | from testing import fake |
| 26 | from testing import testing_helpers |
| 27 | |
| 28 | class BanSpammerTest(unittest.TestCase): |
| 29 | |
| 30 | def setUp(self): |
| 31 | self.cnxn = 'fake cnxn' |
| 32 | self.mr = testing_helpers.MakeMonorailRequest() |
| 33 | self.services = service_manager.Services( |
| 34 | issue=fake.IssueService(), |
| 35 | project=fake.ProjectService(), |
| 36 | spam=fake.SpamService(), |
| 37 | user=fake.UserService()) |
| 38 | self.servlet = banspammer.BanSpammer('req', 'res', services=self.services) |
| 39 | |
| 40 | @mock.patch('framework.cloud_tasks_helpers._get_client') |
| 41 | def testProcessFormData_noPermission(self, get_client_mock): |
| 42 | self.servlet.services.user.TestAddUser('member', 222) |
| 43 | self.servlet.services.user.TestAddUser('spammer@domain.com', 111) |
| 44 | mr = testing_helpers.MakeMonorailRequest( |
| 45 | path='/u/spammer@domain.com/banSpammer.do', |
| 46 | perms=permissions.GetPermissions(None, {}, None)) |
| 47 | mr.viewed_user_auth.user_view = framework_views.MakeUserView(mr.cnxn, |
| 48 | self.servlet.services.user, 111) |
| 49 | mr.auth.user_id = 222 |
| 50 | self.assertRaises(permissions.PermissionException, |
| 51 | self.servlet.AssertBasePermission, mr) |
| 52 | try: |
| 53 | self.servlet.ProcessFormData(mr, {}) |
| 54 | except permissions.PermissionException: |
| 55 | pass |
| 56 | self.assertEqual(get_client_mock().queue_path.call_count, 0) |
| 57 | self.assertEqual(get_client_mock().create_task.call_count, 0) |
| 58 | |
| 59 | @mock.patch('framework.cloud_tasks_helpers._get_client') |
| 60 | def testProcessFormData_ok(self, get_client_mock): |
| 61 | self.servlet.services.user.TestAddUser('owner', 222) |
| 62 | self.servlet.services.user.TestAddUser('spammer@domain.com', 111) |
| 63 | mr = testing_helpers.MakeMonorailRequest( |
| 64 | path='/u/spammer@domain.com/banSpammer.do', |
| 65 | perms=permissions.ADMIN_PERMISSIONSET) |
| 66 | mr.viewed_user_auth.user_view = framework_views.MakeUserView(mr.cnxn, |
| 67 | self.servlet.services.user, 111) |
| 68 | mr.viewed_user_auth.user_pb.user_id = 111 |
| 69 | mr.auth.user_id = 222 |
| 70 | self.servlet.ProcessFormData(mr, {'banned': 'non-empty'}) |
| 71 | |
| 72 | params = {'spammer_id': 111, 'reporter_id': 222, 'is_spammer': True} |
| 73 | task = { |
| 74 | 'app_engine_http_request': |
| 75 | { |
| 76 | 'relative_uri': urls.BAN_SPAMMER_TASK + '.do', |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame^] | 77 | 'body': urllib.parse.urlencode(params), |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 78 | 'headers': { |
| 79 | 'Content-type': 'application/x-www-form-urlencoded' |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | get_client_mock().queue_path.assert_called_with( |
| 84 | settings.app_id, settings.CLOUD_TASKS_REGION, 'default') |
| 85 | get_client_mock().create_task.assert_called_once() |
| 86 | ((_parent, called_task), _kwargs) = get_client_mock().create_task.call_args |
| 87 | self.assertEqual(called_task, task) |
| 88 | |
| 89 | |
| 90 | class BanSpammerTaskTest(unittest.TestCase): |
| 91 | def setUp(self): |
| 92 | self.services = service_manager.Services( |
| 93 | issue=fake.IssueService(), |
| 94 | spam=fake.SpamService()) |
| 95 | self.res = webapp2.Response() |
| 96 | self.servlet = banspammer.BanSpammerTask('req', self.res, |
| 97 | services=self.services) |
| 98 | |
| 99 | def testProcessFormData_okNoIssues(self): |
| 100 | mr = testing_helpers.MakeMonorailRequest( |
| 101 | path=urls.BAN_SPAMMER_TASK + '.do', method='POST', |
| 102 | params={'spammer_id': 111, 'reporter_id': 222}) |
| 103 | |
| 104 | self.servlet.HandleRequest(mr) |
| 105 | self.assertEqual(self.res.body, json.dumps({'comments': 0, 'issues': 0})) |
| 106 | |
| 107 | def testProcessFormData_okSomeIssues(self): |
| 108 | mr = testing_helpers.MakeMonorailRequest( |
| 109 | path=urls.BAN_SPAMMER_TASK + '.do', method='POST', |
| 110 | params={'spammer_id': 111, 'reporter_id': 222}) |
| 111 | |
| 112 | for i in range(0, 10): |
| 113 | issue = fake.MakeTestIssue( |
| 114 | 1, i, 'issue_summary', 'New', 111, project_name='project-name') |
| 115 | self.servlet.services.issue.TestAddIssue(issue) |
| 116 | |
| 117 | self.servlet.HandleRequest(mr) |
| 118 | self.assertEqual(self.res.body, json.dumps({'comments': 0, 'issues': 10})) |
| 119 | |
| 120 | def testProcessFormData_okSomeCommentsAndIssues(self): |
| 121 | mr = testing_helpers.MakeMonorailRequest( |
| 122 | path=urls.BAN_SPAMMER_TASK + '.do', method='POST', |
| 123 | params={'spammer_id': 111, 'reporter_id': 222}) |
| 124 | |
| 125 | for i in range(0, 12): |
| 126 | issue = fake.MakeTestIssue( |
| 127 | 1, i, 'issue_summary', 'New', 111, project_name='project-name') |
| 128 | self.servlet.services.issue.TestAddIssue(issue) |
| 129 | |
| 130 | for i in range(10, 20): |
| 131 | issue = fake.MakeTestIssue( |
| 132 | 1, i, 'issue_summary', 'New', 222, project_name='project-name') |
| 133 | self.servlet.services.issue.TestAddIssue(issue) |
| 134 | for _ in range(0, 5): |
| 135 | comment = tracker_pb2.IssueComment() |
| 136 | comment.project_id = 1 |
| 137 | comment.user_id = 111 |
| 138 | comment.issue_id = issue.issue_id |
| 139 | self.servlet.services.issue.TestAddComment(comment, issue.local_id) |
| 140 | self.servlet.HandleRequest(mr) |
| 141 | self.assertEqual(self.res.body, json.dumps({'comments': 50, 'issues': 10})) |