Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2019 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 | """Unit tests for deleteusers classes.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import logging |
| 12 | import mock |
| 13 | import unittest |
| 14 | import urllib |
| 15 | |
| 16 | from framework import cloud_tasks_helpers |
| 17 | from framework import deleteusers |
| 18 | from framework import framework_constants |
| 19 | from framework import urls |
| 20 | from services import service_manager |
| 21 | from testing import fake |
| 22 | from testing import testing_helpers |
| 23 | |
| 24 | class TestWipeoutSyncCron(unittest.TestCase): |
| 25 | |
| 26 | def setUp(self): |
| 27 | self.services = service_manager.Services(user=fake.UserService()) |
| 28 | self.task = deleteusers.WipeoutSyncCron( |
| 29 | request=None, response=None, services=self.services) |
| 30 | self.user_1 = self.services.user.TestAddUser('user1@example.com', 111) |
| 31 | self.user_2 = self.services.user.TestAddUser('user2@example.com', 222) |
| 32 | self.user_3 = self.services.user.TestAddUser('user3@example.com', 333) |
| 33 | |
| 34 | def generate_simple_task(self, url, body): |
| 35 | return { |
| 36 | 'app_engine_http_request': |
| 37 | { |
| 38 | 'relative_uri': url, |
| 39 | 'body': body, |
| 40 | 'headers': { |
| 41 | 'Content-type': 'application/x-www-form-urlencoded' |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | @mock.patch('framework.cloud_tasks_helpers._get_client') |
| 47 | def testHandleRequest(self, get_client_mock): |
| 48 | mr = testing_helpers.MakeMonorailRequest( |
| 49 | path='url/url?batchsize=2', |
| 50 | services=self.services) |
| 51 | self.task.HandleRequest(mr) |
| 52 | |
| 53 | self.assertEqual(get_client_mock().create_task.call_count, 3) |
| 54 | |
| 55 | expected_task = self.generate_simple_task( |
| 56 | urls.SEND_WIPEOUT_USER_LISTS_TASK + '.do', 'limit=2&offset=0') |
| 57 | get_client_mock().create_task.assert_any_call( |
| 58 | get_client_mock().queue_path(), |
| 59 | expected_task, |
| 60 | retry=cloud_tasks_helpers._DEFAULT_RETRY) |
| 61 | |
| 62 | expected_task = self.generate_simple_task( |
| 63 | urls.SEND_WIPEOUT_USER_LISTS_TASK + '.do', 'limit=2&offset=2') |
| 64 | get_client_mock().create_task.assert_any_call( |
| 65 | get_client_mock().queue_path(), |
| 66 | expected_task, |
| 67 | retry=cloud_tasks_helpers._DEFAULT_RETRY) |
| 68 | |
| 69 | expected_task = self.generate_simple_task( |
| 70 | urls.DELETE_WIPEOUT_USERS_TASK + '.do', '') |
| 71 | get_client_mock().create_task.assert_any_call( |
| 72 | get_client_mock().queue_path(), |
| 73 | expected_task, |
| 74 | retry=cloud_tasks_helpers._DEFAULT_RETRY) |
| 75 | |
| 76 | @mock.patch('framework.cloud_tasks_helpers._get_client') |
| 77 | def testHandleRequest_NoBatchSizeParam(self, get_client_mock): |
| 78 | mr = testing_helpers.MakeMonorailRequest(services=self.services) |
| 79 | self.task.HandleRequest(mr) |
| 80 | |
| 81 | expected_task = self.generate_simple_task( |
| 82 | urls.SEND_WIPEOUT_USER_LISTS_TASK + '.do', |
| 83 | 'limit={}&offset=0'.format(deleteusers.MAX_BATCH_SIZE)) |
| 84 | get_client_mock().create_task.assert_any_call( |
| 85 | get_client_mock().queue_path(), |
| 86 | expected_task, |
| 87 | retry=cloud_tasks_helpers._DEFAULT_RETRY) |
| 88 | |
| 89 | @mock.patch('framework.cloud_tasks_helpers._get_client') |
| 90 | def testHandleRequest_NoUsers(self, get_client_mock): |
| 91 | mr = testing_helpers.MakeMonorailRequest() |
| 92 | self.services.user.users_by_id = {} |
| 93 | self.task.HandleRequest(mr) |
| 94 | |
| 95 | calls = get_client_mock().create_task.call_args_list |
| 96 | self.assertEqual(len(calls), 0) |
| 97 | |
| 98 | |
| 99 | class SendWipeoutUserListsTaskTest(unittest.TestCase): |
| 100 | |
| 101 | def setUp(self): |
| 102 | self.services = service_manager.Services(user=fake.UserService()) |
| 103 | self.task = deleteusers.SendWipeoutUserListsTask( |
| 104 | request=None, response=None, services=self.services) |
| 105 | self.task.sendUserLists = mock.Mock() |
| 106 | deleteusers.authorize = mock.Mock(return_value='service') |
| 107 | self.user_1 = self.services.user.TestAddUser('user1@example.com', 111) |
| 108 | self.user_2 = self.services.user.TestAddUser('user2@example.com', 222) |
| 109 | self.user_3 = self.services.user.TestAddUser('user3@example.com', 333) |
| 110 | |
| 111 | def testHandleRequest_NoBatchSizeParam(self): |
| 112 | mr = testing_helpers.MakeMonorailRequest(path='url/url?limit=2&offset=1') |
| 113 | self.task.HandleRequest(mr) |
| 114 | deleteusers.authorize.assert_called_once_with() |
| 115 | self.task.sendUserLists.assert_called_once_with( |
| 116 | 'service', [ |
| 117 | {'id': self.user_2.email}, |
| 118 | {'id': self.user_3.email}]) |
| 119 | |
| 120 | def testHandleRequest_NoLimit(self): |
| 121 | mr = testing_helpers.MakeMonorailRequest() |
| 122 | self.services.user.users_by_id = {} |
| 123 | with self.assertRaisesRegexp(AssertionError, 'Missing param limit'): |
| 124 | self.task.HandleRequest(mr) |
| 125 | |
| 126 | def testHandleRequest_NoOffset(self): |
| 127 | mr = testing_helpers.MakeMonorailRequest(path='url/url?limit=3') |
| 128 | self.services.user.users_by_id = {} |
| 129 | with self.assertRaisesRegexp(AssertionError, 'Missing param offset'): |
| 130 | self.task.HandleRequest(mr) |
| 131 | |
| 132 | def testHandleRequest_ZeroOffset(self): |
| 133 | mr = testing_helpers.MakeMonorailRequest(path='url/url?limit=2&offset=0') |
| 134 | self.task.HandleRequest(mr) |
| 135 | self.task.sendUserLists.assert_called_once_with( |
| 136 | 'service', [ |
| 137 | {'id': self.user_1.email}, |
| 138 | {'id': self.user_2.email}]) |
| 139 | |
| 140 | |
| 141 | class DeleteWipeoutUsersTaskTest(unittest.TestCase): |
| 142 | |
| 143 | def setUp(self): |
| 144 | self.services = service_manager.Services() |
| 145 | deleteusers.authorize = mock.Mock(return_value='service') |
| 146 | self.task = deleteusers.DeleteWipeoutUsersTask( |
| 147 | request=None, response=None, services=self.services) |
| 148 | deleted_users = [ |
| 149 | {'id': 'user1@gmail.com'}, {'id': 'user2@gmail.com'}, |
| 150 | {'id': 'user3@gmail.com'}, {'id': 'user4@gmail.com'}] |
| 151 | self.task.fetchDeletedUsers = mock.Mock(return_value=deleted_users) |
| 152 | |
| 153 | def generate_simple_task(self, url, body): |
| 154 | return { |
| 155 | 'app_engine_http_request': |
| 156 | { |
| 157 | 'relative_uri': url, |
| 158 | 'body': body, |
| 159 | 'headers': { |
| 160 | 'Content-type': 'application/x-www-form-urlencoded' |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | @mock.patch('framework.cloud_tasks_helpers._get_client') |
| 166 | def testHandleRequest(self, get_client_mock): |
| 167 | mr = testing_helpers.MakeMonorailRequest(path='url/url?limit=3') |
| 168 | self.task.HandleRequest(mr) |
| 169 | |
| 170 | deleteusers.authorize.assert_called_once_with() |
| 171 | self.task.fetchDeletedUsers.assert_called_once_with('service') |
| 172 | ((_app_id, _region, queue), |
| 173 | _kwargs) = get_client_mock().queue_path.call_args |
| 174 | self.assertEqual(queue, framework_constants.QUEUE_DELETE_USERS) |
| 175 | |
| 176 | self.assertEqual(get_client_mock().create_task.call_count, 2) |
| 177 | |
| 178 | query = urllib.urlencode( |
| 179 | {'emails': 'user1@gmail.com,user2@gmail.com,user3@gmail.com'}) |
| 180 | expected_task = self.generate_simple_task( |
| 181 | urls.DELETE_USERS_TASK + '.do', query) |
| 182 | |
| 183 | get_client_mock().create_task.assert_any_call( |
| 184 | get_client_mock().queue_path(), |
| 185 | expected_task, |
| 186 | retry=cloud_tasks_helpers._DEFAULT_RETRY) |
| 187 | |
| 188 | query = urllib.urlencode({'emails': 'user4@gmail.com'}) |
| 189 | expected_task = self.generate_simple_task( |
| 190 | urls.DELETE_USERS_TASK + '.do', query) |
| 191 | |
| 192 | get_client_mock().create_task.assert_any_call( |
| 193 | get_client_mock().queue_path(), |
| 194 | expected_task, |
| 195 | retry=cloud_tasks_helpers._DEFAULT_RETRY) |
| 196 | |
| 197 | @mock.patch('framework.cloud_tasks_helpers._get_client') |
| 198 | def testHandleRequest_DefaultMax(self, get_client_mock): |
| 199 | mr = testing_helpers.MakeMonorailRequest(path='url/url') |
| 200 | self.task.HandleRequest(mr) |
| 201 | |
| 202 | deleteusers.authorize.assert_called_once_with() |
| 203 | self.task.fetchDeletedUsers.assert_called_once_with('service') |
| 204 | self.assertEqual(get_client_mock().create_task.call_count, 1) |
| 205 | |
| 206 | emails = 'user1@gmail.com,user2@gmail.com,user3@gmail.com,user4@gmail.com' |
| 207 | query = urllib.urlencode({'emails': emails}) |
| 208 | expected_task = self.generate_simple_task( |
| 209 | urls.DELETE_USERS_TASK + '.do', query) |
| 210 | |
| 211 | get_client_mock().create_task.assert_any_call( |
| 212 | get_client_mock().queue_path(), |
| 213 | expected_task, |
| 214 | retry=cloud_tasks_helpers._DEFAULT_RETRY) |