21
21
import tempfile
22
22
import unittest2
23
23
24
+ import mock
25
+
24
26
from oauth2client import util
25
27
from oauth2client .client import OAuth2Credentials
26
28
from oauth2client .contrib import locked_file
@@ -116,6 +118,18 @@ def test_lock_file_raises_ioerror(self):
116
118
finally :
117
119
os .unlink (filename )
118
120
121
+ def test_lock_file_raise_unexpected_error (self ):
122
+ filehandle , filename = tempfile .mkstemp ()
123
+ os .close (filehandle )
124
+
125
+ try :
126
+ multistore = multistore_file ._MultiStore (filename )
127
+ multistore ._file = _MockLockedFile (filename , errno .EBUSY )
128
+ self .assertRaises (IOError , multistore ._lock )
129
+ self .assertTrue (multistore ._file .open_and_lock_called )
130
+ finally :
131
+ os .unlink (filename )
132
+
119
133
def test_read_only_file_fail_lock (self ):
120
134
credentials = self ._create_test_credentials ()
121
135
@@ -133,6 +147,32 @@ def test_read_only_file_fail_lock(self):
133
147
self .assertTrue (store ._multistore ._read_only )
134
148
os .chmod (FILENAME , 0o600 )
135
149
150
+ def test_read_only_file_fail_lock_no_warning (self ):
151
+ open (FILENAME , 'a+b' ).close ()
152
+ os .chmod (FILENAME , 0o400 )
153
+
154
+ multistore = multistore_file ._MultiStore (FILENAME )
155
+
156
+ with mock .patch .object (multistore_file .logger , 'warn' ) as mock_warn :
157
+ multistore ._warn_on_readonly = False
158
+ multistore ._lock ()
159
+ self .assertFalse (mock_warn .called )
160
+
161
+ def test_lock_skip_refresh (self ):
162
+ with open (FILENAME , 'w' ) as f :
163
+ f .write ('123' )
164
+ os .chmod (FILENAME , 0o400 )
165
+
166
+ multistore = multistore_file ._MultiStore (FILENAME )
167
+
168
+ refresh_patch = mock .patch .object (
169
+ multistore , '_refresh_data_cache' )
170
+
171
+ with refresh_patch as refresh_mock :
172
+ multistore ._data = {}
173
+ multistore ._lock ()
174
+ self .assertFalse (refresh_mock .called )
175
+
136
176
@unittest2 .skipIf (not hasattr (os , 'symlink' ), 'No symlink available' )
137
177
def test_multistore_no_symbolic_link_files (self ):
138
178
SYMFILENAME = FILENAME + 'sym'
@@ -168,12 +208,14 @@ def test_multistore_file(self):
168
208
credentials .user_agent ,
169
209
['some-scope' , 'some-other-scope' ])
170
210
211
+ # Save credentials
171
212
store .put (credentials )
172
213
credentials = store .get ()
173
214
174
215
self .assertNotEquals (None , credentials )
175
216
self .assertEquals ('foo' , credentials .access_token )
176
217
218
+ # Delete credentials
177
219
store .delete ()
178
220
credentials = store .get ()
179
221
@@ -282,6 +324,61 @@ def test_multistore_file_get_all_keys(self):
282
324
keys = multistore_file .get_all_credential_keys (FILENAME )
283
325
self .assertEquals ([], keys )
284
326
327
+ def _refresh_data_cache_helper (self ):
328
+ multistore = multistore_file ._MultiStore (FILENAME )
329
+ json_patch = mock .patch .object (multistore , '_locked_json_read' )
330
+
331
+ return multistore , json_patch
332
+
333
+ def test__refresh_data_cache_bad_json (self ):
334
+ multistore , json_patch = self ._refresh_data_cache_helper ()
335
+
336
+ with json_patch as json_mock :
337
+ json_mock .side_effect = ValueError ('' )
338
+ multistore ._refresh_data_cache ()
339
+ self .assertTrue (json_mock .called )
340
+ self .assertEqual (multistore ._data , {})
341
+
342
+ def test__refresh_data_cache_bad_version (self ):
343
+ multistore , json_patch = self ._refresh_data_cache_helper ()
344
+
345
+ with json_patch as json_mock :
346
+ json_mock .return_value = {}
347
+ multistore ._refresh_data_cache ()
348
+ self .assertTrue (json_mock .called )
349
+ self .assertEqual (multistore ._data , {})
350
+
351
+ def test__refresh_data_cache_newer_version (self ):
352
+ multistore , json_patch = self ._refresh_data_cache_helper ()
353
+
354
+ with json_patch as json_mock :
355
+ json_mock .return_value = {'file_version' : 5 }
356
+ self .assertRaises (
357
+ multistore_file .NewerCredentialStoreError ,
358
+ multistore ._refresh_data_cache )
359
+ self .assertTrue (json_mock .called )
360
+
361
+ def test__refresh_data_cache_bad_credentials (self ):
362
+ multistore , json_patch = self ._refresh_data_cache_helper ()
363
+
364
+ with json_patch as json_mock :
365
+ json_mock .return_value = {
366
+ 'file_version' : 1 ,
367
+ 'data' : [
368
+ {'lol' : 'this is a bad credential object.' }
369
+ ]}
370
+ multistore ._refresh_data_cache ()
371
+ self .assertTrue (json_mock .called )
372
+ self .assertEqual (multistore ._data , {})
373
+
374
+ def test__delete_credential_nonexistent (self ):
375
+ multistore = multistore_file ._MultiStore (FILENAME )
376
+
377
+ with mock .patch .object (multistore , '_write' ) as write_mock :
378
+ multistore ._data = {}
379
+ multistore ._delete_credential ('nonexistent_key' )
380
+ self .assertTrue (write_mock .called )
381
+
285
382
286
383
if __name__ == '__main__' : # pragma: NO COVER
287
384
unittest2 .main ()
0 commit comments