-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_api_client_test.py
552 lines (462 loc) · 19.5 KB
/
google_api_client_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
import sys
import unittest
from unittest.mock import MagicMock, Mock, call
from googleapiclient import errors
import pytest
sys.modules['google_auth_oauthlib'] = Mock()
sys.modules['utils.logger'] = Mock()
sys.modules['utils.credential_store'] = Mock()
from change_client.google_api_client import GoogleApiClient
class TestGoogleApiClient(unittest.TestCase):
def setUp(self):
self.client = GoogleApiClient(
output_path='output',
oa_client_creds='credentials',
is_test_envs=True,
is_dry_run=False,
)
def assert_mock_retries_n_times(self, mock):
mock.assert_has_calls(
[
call.execute(),
call.execute(),
call.execute(),
call.execute(),
call.execute(),
],
any_order=True,
)
def test_get_user_404_error_expect_not_thrown(self):
mock_admin_sdk_client = MagicMock()
mock_users = MagicMock()
mock_get = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.users.return_value = mock_users
mock_users.get.return_value = mock_get
mock_get.execute.side_effect = errors.HttpError(
Mock(status=404), 'User not found'.encode('utf-8')
)
result = self.client.get_user('[email protected]')
self.assertIsNone(result)
def test_get_user_generic_error_retried_failure(self):
mock_admin_sdk_client = MagicMock()
mock_users = MagicMock()
mock_get = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.users.return_value = mock_users
mock_users.get.return_value = mock_get
with pytest.raises(RuntimeError):
mock_get.execute.side_effect = errors.HttpError(
Mock(status=555), 'Some generic error '.encode('utf-8')
)
result = self.client.get_user('[email protected]')
self.assert_mock_retries_n_times(mock_get)
def test_get_group_credential_expired_retry_once_nothrow(self):
mock_admin_sdk_client = MagicMock()
mock_groups = MagicMock()
mock_get = MagicMock()
mock_reauth_and_refresh_clients = MagicMock()
self.client.get_admin_sdk_client = MagicMock(
return_value=mock_admin_sdk_client
)
self.client.reauth_and_refresh_clients = mock_reauth_and_refresh_clients
mock_admin_sdk_client.groups.return_value = mock_groups
mock_groups.get.return_value = mock_get
# mock substution is lost in retry_with_credential_refresh
# instead test that at least reauth_and_refresh_clients invoked
mock_get.execute.side_effect = [
errors.HttpError(Mock(status=401), 'Group not found'.encode('utf-8')),
None,
]
result = self.client.get_group('[email protected]')
mock_reauth_and_refresh_clients.assert_called_once()
def test_group_has_member_found(self):
mock_admin_sdk_client = MagicMock()
mock_members = MagicMock()
mock_get = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.members.return_value = mock_members
mock_members.get.return_value = mock_get
mock_get.execute.return_value = {
'email': '[email protected]',
'active': True,
}
# Assuming group_has_member returns True if member is found and active
result = self.client.group_has_member(
)
self.assertTrue(result)
def test_group_has_member_not_found(self):
# Create a mock for the admin sdk client
mock_admin_sdk_client = MagicMock()
mock_members = MagicMock()
mock_get = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.members.return_value = mock_members
mock_members.get.return_value = mock_get
mock_get.execute.side_effect = errors.HttpError(
Mock(status=404), 'Member not found'.encode('utf-8')
)
result = self.client.group_has_member(
)
self.assertFalse(result)
def test_group_has_member_generic_error(self):
mock_admin_sdk_client = MagicMock()
mock_members = MagicMock()
mock_get = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.members.return_value = mock_members
mock_members.get.return_value = mock_get
with pytest.raises(RuntimeError):
mock_get.execute.side_effect = errors.HttpError(
Mock(status=555), 'Some generic error'.encode('utf-8')
)
self.client.group_has_member('[email protected]', '[email protected]')
self.assert_mock_retries_n_times(mock_get)
def test_get_group_members_success(self):
mock_admin_sdk_client = MagicMock()
mock_members = MagicMock()
mock_list = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.members.return_value = mock_members
mock_members.list.return_value = mock_list
mock_list.execute.return_value = {
'members': [
{'email': '[email protected]'},
{'email': '[email protected]'},
]
}
result = self.client.get_group_members('[email protected]')
self.assertEqual(
result, [{'email': '[email protected]'}, {'email': '[email protected]'}]
)
def test_get_group_members_no_members(self):
# Create a mock for the admin sdk client
mock_admin_sdk_client = MagicMock()
mock_members = MagicMock()
mock_list = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.members.return_value = mock_members
mock_members.list.return_value = mock_list
mock_list.execute.return_value = {}
result = self.client.get_group_members('[email protected]')
self.assertEqual(result, [])
def test_get_group_members_generic_error(self):
mock_admin_sdk_client = MagicMock()
mock_members = MagicMock()
mock_list = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.members.return_value = mock_members
mock_members.list.return_value = mock_list
with pytest.raises(RuntimeError):
mock_list.execute.side_effect = errors.HttpError(
Mock(status=555), 'Some generic error'.encode('utf-8')
)
self.client.get_group_members('[email protected]')
self.assert_mock_retries_n_times(mock_list)
def test_create_group_success(self):
mock_identity_client = MagicMock()
mock_groups = MagicMock()
mock_create = MagicMock()
self.client.get_identity_client = MagicMock(
return_value=mock_identity_client
)
mock_identity_client.groups.return_value = mock_groups
mock_groups.create.return_value = mock_create
mock_create.execute.return_value = None
customer_id = 'some_customer_id'
group_email = '[email protected]'
group_display_name = 'New Group'
group_description = 'This is a new group'
self.client.create_group(
customer_id, group_email, group_display_name, group_description
)
mock_create.execute.assert_called_once()
def test_create_group_group_exists(self):
mock_identity_client = MagicMock()
mock_groups = MagicMock()
mock_create = MagicMock()
self.client.get_identity_client = MagicMock(
return_value=mock_identity_client
)
mock_identity_client.groups.return_value = mock_groups
mock_groups.create.return_value = mock_create
mock_create.execute.side_effect = errors.HttpError(
Mock(status=409), 'Group already exists'.encode('utf-8')
)
customer_id = 'some_customer_id'
group_email = '[email protected]'
group_display_name = 'New Group'
group_description = 'This is a new group'
# Running the method should not raise any exception
self.client.create_group(
customer_id, group_email, group_display_name, group_description
)
def test_create_group_generic_error(self):
mock_identity_client = MagicMock()
mock_groups = MagicMock()
mock_create = MagicMock()
self.client.get_identity_client = MagicMock(
return_value=mock_identity_client
)
mock_identity_client.groups.return_value = mock_groups
mock_groups.create.return_value = mock_create
mock_create.execute.side_effect = errors.HttpError(
Mock(status=555), 'Some generic error'.encode('utf-8')
)
customer_id = 'some_customer_id'
group_email = '[email protected]'
group_display_name = 'New Group'
group_description = 'This is a new group'
with pytest.raises(RuntimeError):
self.client.create_group(
customer_id, group_email, group_display_name, group_description
)
self.assert_mock_retries_n_times(mock_create)
def test_list_roles_single_page(self):
mock_admin_sdk_client = MagicMock()
mock_roles = MagicMock()
roles_response = {'items': [{'id': 'role1', 'name': 'Role 1'}]}
self.client.get_admin_sdk_client = MagicMock(
return_value=mock_admin_sdk_client
)
mock_admin_sdk_client.roles.return_value = mock_roles
mock_roles.list.return_value.execute.return_value = roles_response
roles = self.client.list_roles()
self.assertEqual(roles, roles_response['items'])
def test_list_roles_pagination(self):
mock_admin_sdk_client = MagicMock()
mock_roles = MagicMock()
first_page_response = {
'items': [{'id': 'role1', 'name': 'Role 1'}],
'nextPageToken': 'next_page',
}
second_page_response = {'items': [{'id': 'role2', 'name': 'Role 2'}]}
mock_roles.list.return_value.execute.side_effect = [
first_page_response,
second_page_response,
]
self.client.get_admin_sdk_client = MagicMock(
return_value=mock_admin_sdk_client
)
mock_admin_sdk_client.roles.return_value = mock_roles
roles = self.client.list_roles()
self.assertEqual(
roles,
[{'id': 'role1', 'name': 'Role 1'}, {'id': 'role2', 'name': 'Role 2'}],
)
def test_list_role_assignments_role_and_user_set(self):
mock_admin_sdk_client = MagicMock()
mock_role_assignments = MagicMock()
role_assignments_response = {
'items': [{'roleId': 'role1', 'assignedTo': '[email protected]'}]
}
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.roleAssignments.return_value = mock_role_assignments
mock_role_assignments.list.return_value.execute.return_value = (
role_assignments_response
)
with pytest.raises(AssertionError):
assignments = self.client.list_role_assignments( 'role1','[email protected]',)
self.assertIsNone(assignments)
def test_list_role_assignments_role_set_user_unset(self):
mock_admin_sdk_client = MagicMock()
mock_role_assignments = MagicMock()
role_assignments_response = {'items': [{'roleId': 'role1'}]}
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.roleAssignments.return_value = mock_role_assignments
mock_role_assignments.list.return_value.execute.return_value = (
role_assignments_response
)
assignments = self.client.list_role_assignments(None, 'role1')
self.assertEqual(assignments, role_assignments_response['items'])
def test_list_role_assignments_user_set_role_unset(self):
mock_admin_sdk_client = MagicMock()
mock_role_assignments = MagicMock()
role_assignments_response = {'items': [{'assignedTo': '[email protected]'}]}
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.roleAssignments.return_value = mock_role_assignments
mock_role_assignments.list.return_value.execute.return_value = (
role_assignments_response
)
assignments = self.client.list_role_assignments('[email protected]')
self.assertEqual(assignments, role_assignments_response['items'])
def test_list_role_assignments_neither_set(self):
mock_admin_sdk_client = MagicMock()
mock_role_assignments = MagicMock()
role_assignments_response = {}
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.roleAssignments.return_value = mock_role_assignments
mock_role_assignments.list.return_value.execute.return_value = (
role_assignments_response
)
assignments = self.client.list_role_assignments()
self.assertEqual(assignments, [])
def test_list_role_assignments_forbidden(self):
mock_admin_sdk_client = MagicMock()
mock_role_assignments = MagicMock()
mock_execute = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.roleAssignments.return_value = mock_role_assignments
mock_role_assignments.list.return_value = mock_execute
mock_execute.execute.side_effect = errors.HttpError(
Mock(status=403), 'Forbidden'.encode('utf-8')
)
with pytest.raises(RuntimeError):
self.client.list_role_assignments()
self.assert_mock_retries_n_times(mock_execute)
def test_list_role_assignments_not_found(self):
mock_admin_sdk_client = MagicMock()
mock_role_assignments = MagicMock()
mock_execute = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.roleAssignments.return_value = mock_role_assignments
mock_role_assignments.list.return_value = mock_execute
mock_execute.execute.side_effect = errors.HttpError(
Mock(status=404), 'Not Found'.encode('utf-8')
)
with pytest.raises(RuntimeError):
self.client.list_role_assignments()
self.assert_mock_retries_n_times(mock_execute)
def test_list_role_assignments_paginated_response(self):
mock_admin_sdk_client = MagicMock()
mock_role_assignments = MagicMock()
first_page_response = {
'items': [{'roleId': 'role1', 'assignedTo': '[email protected]'}],
'nextPageToken': 'nextToken',
}
second_page_response = {
'items': [{'roleId': 'role2', 'assignedTo': '[email protected]'}]
}
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.roleAssignments.return_value = mock_role_assignments
mock_role_assignments.list.side_effect = [
Mock(execute=Mock(return_value=first_page_response)),
Mock(execute=Mock(return_value=second_page_response)),
]
assignments = self.client.list_role_assignments()
combined_response = (
first_page_response['items'] + second_page_response['items']
)
self.assertEqual(assignments, combined_response)
def test_delete_role_assignment_success(self):
mock_admin_sdk_client = MagicMock()
mock_role_assignments = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.roleAssignments.return_value = mock_role_assignments
mock_role_assignments.delete.return_value.execute.return_value = None
self.assertTrue(self.client.delete_role_assignment('role_assignment_id_1'))
mock_role_assignments.delete.assert_called_once_with(
customer='my_customer', roleAssignmentId='role_assignment_id_1'
)
def test_delete_role_assignment_dry_run(self):
self.client.is_dry_run = True
with pytest.raises(
AssertionError,
match='GoogleApiClient.delete_role_assignment invoked when dryRun=True',
):
self.client.delete_role_assignment('role_assignment_id_1')
def test_delete_role_assignment_not_found(self):
# HTTP 404 error (not found)
mock_admin_sdk_client = MagicMock()
mock_role_assignments = MagicMock()
mock_execute = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.roleAssignments.return_value = mock_role_assignments
mock_role_assignments.delete.return_value = mock_execute
mock_execute.execute.side_effect = errors.HttpError(
Mock(status=404), 'Not Found'.encode('utf-8')
)
self.assertFalse(self.client.delete_role_assignment('role_assignment_id_1'))
def test_delete_role_assignment_admin_self_revoke(self):
mock_admin_sdk_client = MagicMock()
mock_role_assignments = MagicMock()
mock_execute = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.roleAssignments.return_value = mock_role_assignments
mock_role_assignments.delete.return_value = mock_execute
mock_execute.execute.side_effect = errors.HttpError(
Mock(status=403), 'AdminSelfRevokeNotAllowed'.encode('utf-8')
)
self.assertFalse(self.client.delete_role_assignment('role_assignment_id_1'))
def test_delete_role_assignment_forbidden(self):
mock_admin_sdk_client = MagicMock()
mock_role_assignments = MagicMock()
mock_execute = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.roleAssignments.return_value = mock_role_assignments
mock_role_assignments.delete.return_value = mock_execute
mock_execute.execute.side_effect = errors.HttpError(
Mock(status=403), 'Forbidden Action'.encode('utf-8')
)
with pytest.raises(RuntimeError):
self.client.delete_role_assignment('role_assignment_id_1')
self.assertFalse(self.assert_mock_retries_n_times(mock_execute))
def test_delete_role_assignment_generic_error(self):
mock_admin_sdk_client = MagicMock()
mock_role_assignments = MagicMock()
mock_execute = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.roleAssignments.return_value = mock_role_assignments
mock_role_assignments.delete.return_value = mock_execute
mock_execute.execute.side_effect = errors.HttpError(
Mock(status=500), 'Internal Server Error'.encode('utf-8')
)
with pytest.raises(RuntimeError):
self.client.delete_role_assignment('role_assignment_id_1')
self.assertFalse(self.assert_mock_retries_n_times(mock_execute))
def test_insert_member_into_group_success(self):
mock_admin_sdk_client = MagicMock()
mock_members = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.members.return_value = mock_members
mock_members.insert.return_value.execute.return_value = None
self.client.insert_member_into_group(
'[email protected]', 'user_id_1', '[email protected]'
)
mock_members.insert.assert_called_once_with(
groupKey='[email protected]',
body={'email': '[email protected]', 'role': 'MEMBER'},
)
def test_insert_member_into_group_dry_run(self):
self.client.is_dry_run = True
with pytest.raises(
AssertionError,
match=(
'GoogleApiClient.insert_member_into_group invoked when dryRun=False'
),
):
self.client.insert_member_into_group(
'[email protected]', 'user_id_1', '[email protected]'
)
def test_insert_member_into_group_already_member(self):
mock_admin_sdk_client = MagicMock()
mock_members = MagicMock()
mock_execute = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.members.return_value = mock_members
mock_members.insert.return_value = mock_execute
mock_execute.execute.side_effect = errors.HttpError(
Mock(status=409), 'Conflict'.encode('utf-8')
)
self.client.insert_member_into_group(
'[email protected]', 'user_id_1', '[email protected]'
)
def test_insert_member_into_group_generic_error(self):
mock_admin_sdk_client = MagicMock()
mock_members = MagicMock()
mock_execute = MagicMock()
self.client._adminsdk_client = mock_admin_sdk_client
mock_admin_sdk_client.members.return_value = mock_members
mock_members.insert.return_value = mock_execute
mock_execute.execute.side_effect = errors.HttpError(
Mock(status=500), 'Internal Server Error'.encode('utf-8')
)
with pytest.raises(RuntimeError):
self.client.insert_member_into_group(
'[email protected]', 'user_id_1', '[email protected]'
)
self.assert_mock_retries_n_times(mock_execute)
if __name__ == '__main__':
unittest.main()