14
14
import os
15
15
import platform
16
16
import shutil
17
+ import stat
17
18
import tempfile
18
19
19
20
import pytest
@@ -319,15 +320,29 @@ def setUp(self, request):
319
320
os .unlink (self .db_path )
320
321
except OSError :
321
322
pass
322
- self .home = tempfile .mkdtemp ()
323
+ self .test_home = tempfile .mkdtemp ()
323
324
324
325
yield
325
- for cleanup_method in [lambda : os .unlink (self .db_path ), lambda : shutil .rmtree (self .home )]:
326
+ for cleanup_method in [
327
+ lambda : os .unlink (self .db_path ), lambda : shutil .rmtree (self .test_home )
328
+ ]:
326
329
try :
327
- cleanup_method
330
+ cleanup_method ()
328
331
except OSError :
329
332
pass
330
333
334
+ @pytest .mark .skipif (
335
+ platform .system () == 'Windows' ,
336
+ reason = 'different permission system on Windows'
337
+ )
338
+ def test_permissions (self ):
339
+ """
340
+ Test that a new database won't be readable by just any user
341
+ """
342
+ s = SqliteAccountInfo (file_name = self .db_path ,)
343
+ mode = os .stat (self .db_path ).st_mode
344
+ assert stat .filemode (mode ) == '-rw-------'
345
+
331
346
def test_corrupted (self ):
332
347
"""
333
348
Test that a corrupted file will be replaced with a blank file.
@@ -371,7 +386,7 @@ def _make_sqlite_account_info(self, env=None, last_upgrade_to_run=None):
371
386
:param dict env: Override Environment variables.
372
387
"""
373
388
# Override HOME to ensure hermetic tests
374
- with mock .patch ('os.environ' , env or {'HOME' : self .home }):
389
+ with mock .patch ('os.environ' , env or {'HOME' : self .test_home }):
375
390
return SqliteAccountInfo (
376
391
file_name = self .db_path if not env else None ,
377
392
last_upgrade_to_run = last_upgrade_to_run ,
@@ -380,24 +395,24 @@ def _make_sqlite_account_info(self, env=None, last_upgrade_to_run=None):
380
395
def test_uses_default (self ):
381
396
account_info = self ._make_sqlite_account_info (
382
397
env = {
383
- 'HOME' : self .home ,
384
- 'USERPROFILE' : self .home ,
398
+ 'HOME' : self .test_home ,
399
+ 'USERPROFILE' : self .test_home ,
385
400
}
386
401
)
387
402
actual_path = os .path .abspath (account_info .filename )
388
- assert os .path .join (self .home , '.b2_account_info' ) == actual_path
403
+ assert os .path .join (self .test_home , '.b2_account_info' ) == actual_path
389
404
390
405
def test_uses_xdg_config_home (self , apiver ):
391
406
with WindowsSafeTempDir () as d :
392
407
account_info = self ._make_sqlite_account_info (
393
408
env = {
394
- 'HOME' : self .home ,
395
- 'USERPROFILE' : self .home ,
409
+ 'HOME' : self .test_home ,
410
+ 'USERPROFILE' : self .test_home ,
396
411
XDG_CONFIG_HOME_ENV_VAR : d ,
397
412
}
398
413
)
399
414
if apiver in ['v0' , 'v1' ]:
400
- expected_path = os .path .abspath (os .path .join (self .home , '.b2_account_info' ))
415
+ expected_path = os .path .abspath (os .path .join (self .test_home , '.b2_account_info' ))
401
416
else :
402
417
assert os .path .exists (os .path .join (d , 'b2' ))
403
418
expected_path = os .path .abspath (os .path .join (d , 'b2' , 'account_info' ))
@@ -406,12 +421,12 @@ def test_uses_xdg_config_home(self, apiver):
406
421
407
422
def test_uses_existing_file_and_ignores_xdg (self ):
408
423
with WindowsSafeTempDir () as d :
409
- default_db_file_location = os .path .join (self .home , '.b2_account_info' )
424
+ default_db_file_location = os .path .join (self .test_home , '.b2_account_info' )
410
425
open (default_db_file_location , 'a' ).close ()
411
426
account_info = self ._make_sqlite_account_info (
412
427
env = {
413
- 'HOME' : self .home ,
414
- 'USERPROFILE' : self .home ,
428
+ 'HOME' : self .test_home ,
429
+ 'USERPROFILE' : self .test_home ,
415
430
XDG_CONFIG_HOME_ENV_VAR : d ,
416
431
}
417
432
)
@@ -423,8 +438,8 @@ def test_account_info_env_var_overrides_xdg_config_home(self):
423
438
with WindowsSafeTempDir () as d :
424
439
account_info = self ._make_sqlite_account_info (
425
440
env = {
426
- 'HOME' : self .home ,
427
- 'USERPROFILE' : self .home ,
441
+ 'HOME' : self .test_home ,
442
+ 'USERPROFILE' : self .test_home ,
428
443
XDG_CONFIG_HOME_ENV_VAR : d ,
429
444
B2_ACCOUNT_INFO_ENV_VAR : os .path .join (d , 'b2_account_info' ),
430
445
}
0 commit comments