/
google_directory_service_test.py
384 lines (324 loc) · 15.1 KB
/
google_directory_service_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
"""Test google directory service module functionality."""
from config import PATHS
from mock import MagicMock
from mock import patch
import sys
import unittest
# Need to mock the call to get an XSRF token at function definition time, i.e.
# when the module is loaded. http://stackoverflow.com/a/7667621/2830207
def MockToken():
"""Mock token generator that returns empty."""
return ''
MOCK_XSRF = MagicMock()
MOCK_XSRF.XSRFToken = MockToken
sys.modules['xsrf'] = MOCK_XSRF
from appengine_config import JINJA_ENVIRONMENT
import google_directory_service
from google_directory_service import GoogleDirectoryService
from google_directory_service import MY_CUSTOMER_ALIAS
from google_directory_service import NUM_RETRIES
from google_directory_service import VALID_WATCH_EVENTS
def MockHttpFunction():
"""Mock http function to return a mocked object."""
return MOCK_HTTP
MOCK_HTTP = MagicMock()
MOCK_OAUTH_DECORATOR = MagicMock()
MOCK_OAUTH_DECORATOR.http = MockHttpFunction
MOCK_SERVICE = MagicMock()
FAKE_EMAIL_1 = 'foo@mybusiness.com'
FAKE_EMAIL_2 = 'bar@mybusiness.com'
FAKE_USER_1 = {}
FAKE_USER_1['primaryEmail'] = FAKE_EMAIL_1
FAKE_USER_1['isAdmin'] = True
FAKE_USER_2 = {}
FAKE_USER_2['primaryEmail'] = FAKE_EMAIL_2
FAKE_USER_2['isAdmin'] = False
FAKE_USERS = [FAKE_USER_1, FAKE_USER_2]
FAKE_ID_1 = 'some id 1' # Also doubles as a user key
FAKE_ID_2 = 'some id 2'
FAKE_GROUP_MEMBER_USER_1 = {}
FAKE_GROUP_MEMBER_USER_1['type'] = 'USER'
FAKE_GROUP_MEMBER_USER_1['id'] = FAKE_ID_1
FAKE_GROUP_MEMBER_USER_2 = {}
FAKE_GROUP_MEMBER_USER_2['type'] = 'USER'
FAKE_GROUP_MEMBER_USER_2['id'] = FAKE_ID_2
FAKE_GROUP_MEMBER_GROUP = {}
FAKE_GROUP_MEMBER_GROUP['type'] = 'GROUP'
FAKE_GROUP = [FAKE_GROUP_MEMBER_USER_1, FAKE_GROUP_MEMBER_USER_2,
FAKE_GROUP_MEMBER_GROUP]
FAKE_PAGE_TOKEN = 'I am a fake page token'
FAKE_GROUP_KEY = 'my_group@mybusiness.com'
class GoogleDirectoryServiceTest(unittest.TestCase):
"""Test google directory service class functionality."""
@patch('google_directory_service.build')
def setUp(self, mock_build):
"""Setup test object on which to call methods later on."""
# pylint: disable=arguments-differ
mock_build.return_value = MOCK_SERVICE
self.directory_service = GoogleDirectoryService(MOCK_OAUTH_DECORATOR)
@patch('google_directory_service.build')
def testInit(self, mock_build):
"""Test that init passes the correct parameters and creates an object."""
fake_service = MagicMock()
mock_build.return_value = fake_service
google_directory_service = GoogleDirectoryService(MOCK_OAUTH_DECORATOR)
mock_build.assert_called_once_with(serviceName='admin',
version='directory_v1',
http=MOCK_HTTP)
self.assertEqual(google_directory_service.service, fake_service)
def testConstantDefinitions(self):
"""Test the constants set in GoogleDirectoryService are as expected."""
self.assertEqual(MY_CUSTOMER_ALIAS, 'my_customer')
self.assertEqual(NUM_RETRIES, 3)
allowed_watch_events = ['add', 'delete', 'makeAdmin', 'undelete', 'update']
self.assertEqual(VALID_WATCH_EVENTS, allowed_watch_events)
@patch.object(MOCK_SERVICE.users.list, 'execute')
@patch.object(MOCK_SERVICE.users, 'list')
@patch.object(MOCK_SERVICE, 'users')
def testGetUsers(self, mock_users, mock_list, mock_execute):
"""Test the get users request handles a valid response correctly."""
fake_dictionary = {}
fake_dictionary['users'] = FAKE_USERS
mock_execute.return_value = fake_dictionary
mock_list.return_value.execute = mock_execute
mock_users.return_value.list = mock_list
self.directory_service.users = mock_users
users_returned = self.directory_service.GetUsers()
mock_users.assert_called_once_with()
mock_list.assert_called_once_with(customer=MY_CUSTOMER_ALIAS,
maxResults=500, pageToken='',
projection='full', orderBy='email')
mock_execute.assert_called_once_with(num_retries=NUM_RETRIES)
self.assertEqual(users_returned, FAKE_USERS)
@patch.object(MOCK_SERVICE.users.list, 'execute')
@patch.object(MOCK_SERVICE.users, 'list')
@patch.object(MOCK_SERVICE, 'users')
def testGetUsersPaged(self, mock_users, mock_list, mock_execute):
"""Test the get users request handles a long valid response correctly."""
fake_dictionary_1 = {}
fake_dictionary_1['users'] = FAKE_USERS
fake_dictionary_1['nextPageToken'] = FAKE_PAGE_TOKEN
fake_extra_user = MagicMock()
fake_dictionary_2 = {}
fake_dictionary_2['users'] = [fake_extra_user]
expected_list = []
expected_list += FAKE_USERS
expected_list += [fake_extra_user]
def SideEffect(customer, maxResults, pageToken, projection, orderBy):
"""Mock list function to return different mock execute calls."""
# pylint: disable=unused-argument
# pylint: disable=invalid-name
if pageToken == '':
mock_execute.return_value = fake_dictionary_1
else:
mock_execute.return_value = fake_dictionary_2
some_object = MagicMock()
some_object.execute = mock_execute
return some_object
mock_list.side_effect = SideEffect
mock_users.return_value.list = mock_list
self.directory_service.users = mock_users
users_returned = self.directory_service.GetUsers()
mock_users.assert_any_call()
mock_list.assert_any_call(customer=MY_CUSTOMER_ALIAS,
maxResults=500, pageToken='',
projection='full', orderBy='email')
mock_list.assert_any_call(customer=MY_CUSTOMER_ALIAS,
maxResults=500, pageToken=FAKE_PAGE_TOKEN,
projection='full', orderBy='email')
mock_execute.assert_any_call(num_retries=NUM_RETRIES)
self.assertEqual(users_returned, expected_list)
@patch.object(GoogleDirectoryService, 'GetUser')
@patch.object(MOCK_SERVICE.members.list, 'execute')
@patch.object(MOCK_SERVICE.members, 'list')
@patch.object(MOCK_SERVICE, 'members')
def testGetUsersByGroupKey(self, mock_members, mock_list, mock_execute,
mock_get_user):
"""Test get users by group key handles a valid response correctly."""
fake_dictionary = {}
fake_dictionary['members'] = FAKE_GROUP
mock_execute.return_value = fake_dictionary
mock_list.return_value.execute = mock_execute
mock_members.return_value.list = mock_list
self.directory_service.users = mock_members
expected_list = [FAKE_GROUP_MEMBER_USER_1, FAKE_GROUP_MEMBER_USER_2]
def SideEffect(user_key):
"""Mock get user function to return different users after group get."""
if user_key == FAKE_ID_1:
return FAKE_GROUP_MEMBER_USER_1
else:
return FAKE_GROUP_MEMBER_USER_2
mock_get_user.side_effect = SideEffect
users_returned = self.directory_service.GetUsersByGroupKey(FAKE_GROUP_KEY)
mock_members.assert_called_once_with()
mock_list.assert_called_once_with(groupKey=FAKE_GROUP_KEY)
mock_execute.assert_called_once_with(num_retries=NUM_RETRIES)
self.assertEqual(users_returned, expected_list)
@patch.object(GoogleDirectoryService, 'GetUser')
@patch.object(MOCK_SERVICE.members.list, 'execute')
@patch.object(MOCK_SERVICE.members, 'list')
@patch.object(MOCK_SERVICE, 'members')
def testGetUsersByGroupKeyPaged(self, mock_members, mock_list, mock_execute,
mock_get_user):
"""Test get users by group key handles a long valid response correctly."""
fake_dictionary_1 = {}
fake_dictionary_1['members'] = FAKE_GROUP
fake_dictionary_1['nextPageToken'] = FAKE_PAGE_TOKEN
fake_dictionary_2 = {}
fake_dictionary_2['members'] = FAKE_GROUP
expected_list = [FAKE_GROUP_MEMBER_USER_1, FAKE_GROUP_MEMBER_USER_2,
FAKE_GROUP_MEMBER_USER_1, FAKE_GROUP_MEMBER_USER_2]
def SideEffect1(groupKey, pageToken=''):
"""Mock list function to return different mock execute calls."""
# pylint: disable=unused-argument
# pylint: disable=invalid-name
if pageToken == '':
mock_execute.return_value = fake_dictionary_1
else:
mock_execute.return_value = fake_dictionary_2
some_object = MagicMock()
some_object.execute = mock_execute
return some_object
mock_list.side_effect = SideEffect1
mock_members.return_value.list = mock_list
self.directory_service.users = mock_members
def SideEffect2(user_key):
"""Mock get user function to return different users after group get."""
if user_key == FAKE_ID_1:
return FAKE_GROUP_MEMBER_USER_1
else:
return FAKE_GROUP_MEMBER_USER_2
mock_get_user.side_effect = SideEffect2
users_returned = self.directory_service.GetUsersByGroupKey(FAKE_GROUP_KEY)
mock_members.assert_any_call()
mock_list.assert_any_call(groupKey=FAKE_GROUP_KEY)
mock_list.assert_any_call(groupKey=FAKE_GROUP_KEY,
pageToken=FAKE_PAGE_TOKEN)
mock_execute.assert_any_call(num_retries=NUM_RETRIES)
self.assertEqual(users_returned, expected_list)
@patch.object(MOCK_SERVICE.users.get, 'execute')
@patch.object(MOCK_SERVICE.users, 'get')
@patch.object(MOCK_SERVICE, 'users')
def testGetUser(self, mock_users, mock_get, mock_execute):
"""Test the get user request handles a valid response correctly."""
mock_execute.return_value = FAKE_USER_1
mock_get.return_value.execute = mock_execute
mock_users.return_value.get = mock_get
self.directory_service.users = mock_users
user_returned = self.directory_service.GetUser(FAKE_ID_1)
mock_users.assert_called_once_with()
mock_get.assert_called_once_with(userKey=FAKE_ID_1, projection='full')
mock_execute.assert_called_once_with(num_retries=NUM_RETRIES)
self.assertEqual(user_returned, FAKE_USER_1)
@patch.object(GoogleDirectoryService, 'GetUser')
def testGetUserAsList(self, mock_get_user):
"""Test get user as list turns a valid response into a list."""
mock_get_user.return_value = FAKE_USER_1
user_list_returned = self.directory_service.GetUserAsList(FAKE_ID_1)
mock_get_user.assert_called_once_with(FAKE_ID_1)
self.assertEqual(user_list_returned, [FAKE_USER_1])
@patch.object(GoogleDirectoryService, 'GetUser')
def testIsAdminUser(self, mock_get_user):
"""Test is admin user returns whether a user is an admin."""
def SideEffect(user_key):
"""Mock get user function to return different users based on key."""
if user_key == FAKE_ID_1:
return FAKE_USER_1
else:
return FAKE_USER_2
mock_get_user.side_effect = SideEffect
boolean_returned = self.directory_service.IsAdminUser(FAKE_ID_1)
mock_get_user.assert_called_with(FAKE_ID_1)
self.assertEqual(boolean_returned, True)
boolean_returned = self.directory_service.IsAdminUser(FAKE_ID_2)
mock_get_user.assert_called_with(FAKE_ID_2)
self.assertEqual(boolean_returned, False)
@patch('datastore.NotificationChannel.Insert')
@patch.object(MOCK_SERVICE.users.watch, 'execute')
@patch.object(MOCK_SERVICE.users, 'watch')
@patch.object(MOCK_SERVICE, 'users')
@patch('google_directory_service.time')
@patch('datastore.NotificationChannel.GetAll')
def testWatchUsers(self, mock_get_all, mock_time, mock_users, mock_watch,
mock_execute, mock_insert):
"""Test watch users requests a channel then inserts into the datastore."""
# pylint: disable=too-many-locals
# pylint: disable=too-many-arguments
fake_resource_id = 'some resource id'
fake_watch_result = {}
fake_watch_result['resourceId'] = fake_resource_id
mock_execute.return_value = fake_watch_result
mock_watch.return_value.execute = mock_execute
mock_users.return_value.watch = mock_watch
self.directory_service.users = mock_users
fake_time = 1.001
fake_time_in_millis = '1001'
mock_time.return_value = fake_time
invalid_event = 'foo'
fake_event_already_watched = 'add'
fake_event_not_watched = 'delete'
fake_channel = MagicMock(event=fake_event_already_watched)
mock_get_all.return_value = [fake_channel]
# Test with an invalid event, which should just return.
self.directory_service.WatchUsers(invalid_event)
mock_get_all.assert_not_called()
mock_time.assert_not_called()
mock_users.assert_not_called()
mock_watch.assert_not_called()
mock_execute.assert_not_called()
mock_insert.assert_not_called()
# Test with an event already in the datastore, which should just return.
self.directory_service.WatchUsers(fake_event_already_watched)
mock_get_all.assert_any_call()
mock_time.assert_not_called()
mock_users.assert_not_called()
mock_watch.assert_not_called()
mock_execute.assert_not_called()
mock_insert.assert_not_called()
# Test with an event not in the datastore, which should flow through.
self.directory_service.WatchUsers(fake_event_not_watched)
fake_body = {}
fake_body['id'] = (MY_CUSTOMER_ALIAS + '_' + fake_event_not_watched + '_' +
fake_time_in_millis)
fake_body['type'] = 'web_hook'
fake_address = (JINJA_ENVIRONMENT.globals['BASE_URL'] +
PATHS['receive_push_notifications'])
fake_body['address'] = fake_address
mock_get_all.assert_any_call()
mock_time.assert_called_once_with()
mock_users.assert_called_once_with()
mock_watch.assert_called_once_with(customer=MY_CUSTOMER_ALIAS,
event=fake_event_not_watched,
projection='full', orderBy='email',
body=fake_body)
mock_execute.assert_called_once_with(num_retries=NUM_RETRIES)
mock_insert.assert_called_once_with(event=fake_event_not_watched,
channel_id=fake_body['id'],
resource_id=fake_resource_id)
@patch('datastore.NotificationChannel.Delete')
@patch.object(MOCK_SERVICE.channels.stop, 'execute')
@patch.object(MOCK_SERVICE.channels, 'stop')
@patch.object(MOCK_SERVICE, 'channels')
def testStopNotifications(self, mock_channels, mock_stop, mock_execute,
mock_delete):
"""Test stop notifications requests with stop then deletes the channel."""
fake_id = 'foobarbaz'
fake_key = MagicMock(id=fake_id)
fake_channel_id = 'some channel id'
fake_resource_id = 'some resource id'
fake_notification_channel = MagicMock(channel_id=fake_channel_id,
resource_id=fake_resource_id,
key=fake_key)
fake_body = {}
fake_body['id'] = fake_channel_id
fake_body['resourceId'] = fake_resource_id
mock_stop.return_value.execute = mock_execute
mock_channels.return_value.stop = mock_stop
self.directory_service.channels = mock_channels
self.directory_service.StopNotifications(fake_notification_channel)
mock_channels.assert_called_once_with()
mock_stop.assert_called_once_with(body=fake_body)
mock_execute.assert_called_once_with(num_retries=NUM_RETRIES)
mock_delete.assert_called_once_with(fake_id)
if __name__ == '__main__':
unittest.main()