blob: 13c6922cc1bcc9f6b25d7c8c8691379830a89dfd [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2019 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Unit tests for deleteusers classes."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import logging
11import mock
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010012import six
Copybara854996b2021-09-07 19:36:02 +000013import unittest
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020014from six.moves import urllib
Copybara854996b2021-09-07 19:36:02 +000015
16from framework import cloud_tasks_helpers
17from framework import deleteusers
18from framework import framework_constants
19from framework import urls
20from services import service_manager
21from testing import fake
22from testing import testing_helpers
23
24class TestWipeoutSyncCron(unittest.TestCase):
25
26 def setUp(self):
27 self.services = service_manager.Services(user=fake.UserService())
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020028 self.task = deleteusers.WipeoutSyncCron(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000029 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(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010055 urls.SEND_WIPEOUT_USER_LISTS_TASK + '.do', b'limit=2&offset=0')
Copybara854996b2021-09-07 19:36:02 +000056 get_client_mock().create_task.assert_any_call(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010057 parent=get_client_mock().queue_path(),
58 task=expected_task,
Copybara854996b2021-09-07 19:36:02 +000059 retry=cloud_tasks_helpers._DEFAULT_RETRY)
60
61 expected_task = self.generate_simple_task(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010062 urls.SEND_WIPEOUT_USER_LISTS_TASK + '.do', b'limit=2&offset=2')
Copybara854996b2021-09-07 19:36:02 +000063 get_client_mock().create_task.assert_any_call(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010064 parent=get_client_mock().queue_path(),
65 task=expected_task,
Copybara854996b2021-09-07 19:36:02 +000066 retry=cloud_tasks_helpers._DEFAULT_RETRY)
67
68 expected_task = self.generate_simple_task(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010069 urls.DELETE_WIPEOUT_USERS_TASK + '.do', b'')
Copybara854996b2021-09-07 19:36:02 +000070 get_client_mock().create_task.assert_any_call(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010071 parent=get_client_mock().queue_path(),
72 task=expected_task,
Copybara854996b2021-09-07 19:36:02 +000073 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',
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010082 b'limit=%d&offset=0' % deleteusers.MAX_BATCH_SIZE)
Copybara854996b2021-09-07 19:36:02 +000083 get_client_mock().create_task.assert_any_call(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010084 parent=get_client_mock().queue_path(),
85 task=expected_task,
Copybara854996b2021-09-07 19:36:02 +000086 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
98class SendWipeoutUserListsTaskTest(unittest.TestCase):
99
100 def setUp(self):
101 self.services = service_manager.Services(user=fake.UserService())
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200102 self.task = deleteusers.SendWipeoutUserListsTask(services=self.services)
Copybara854996b2021-09-07 19:36:02 +0000103 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 = {}
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100121 with self.assertRaisesRegex(AssertionError, 'Missing param limit'):
Copybara854996b2021-09-07 19:36:02 +0000122 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 = {}
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100127 with self.assertRaisesRegex(AssertionError, 'Missing param offset'):
Copybara854996b2021-09-07 19:36:02 +0000128 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
139class 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ínez9f9ade52022-10-10 23:20:11 +0200144 self.task = deleteusers.DeleteWipeoutUsersTask(services=self.services)
Copybara854996b2021-09-07 19:36:02 +0000145 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,
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100155 'body': six.ensure_binary(body),
Copybara854996b2021-09-07 19:36:02 +0000156 '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ínezde942802022-07-15 14:06:55 +0200175 query = urllib.parse.urlencode(
Copybara854996b2021-09-07 19:36:02 +0000176 {'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(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100181 parent=get_client_mock().queue_path(),
182 task=expected_task,
Copybara854996b2021-09-07 19:36:02 +0000183 retry=cloud_tasks_helpers._DEFAULT_RETRY)
184
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200185 query = urllib.parse.urlencode({'emails': 'user4@gmail.com'})
Copybara854996b2021-09-07 19:36:02 +0000186 expected_task = self.generate_simple_task(
187 urls.DELETE_USERS_TASK + '.do', query)
188
189 get_client_mock().create_task.assert_any_call(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100190 parent=get_client_mock().queue_path(),
191 task=expected_task,
Copybara854996b2021-09-07 19:36:02 +0000192 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ínezde942802022-07-15 14:06:55 +0200204 query = urllib.parse.urlencode({'emails': emails})
Copybara854996b2021-09-07 19:36:02 +0000205 expected_task = self.generate_simple_task(
206 urls.DELETE_USERS_TASK + '.do', query)
207
208 get_client_mock().create_task.assert_any_call(
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100209 parent=get_client_mock().queue_path(),
210 task=expected_task,
Copybara854996b2021-09-07 19:36:02 +0000211 retry=cloud_tasks_helpers._DEFAULT_RETRY)