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