1212 PrivateJsonFile ,
1313 RefreshTokenStore ,
1414 get_file_mode ,
15+ set_file_mode ,
16+ PRIVATE_PERMISSIONS ,
1517)
1618
1719
@@ -34,28 +36,26 @@ def test_provide_file_path(self, tmp_path):
3436 assert private .path .exists ()
3537 assert [p .name for p in tmp_path .iterdir ()] == ["my_data.secret" ]
3638
37- def test_permissions (self , tmp_path ):
39+ def test_permissions_on_create (self , tmp_path ):
3840 private = PrivateJsonFile (tmp_path )
3941 assert not private .path .exists ()
4042 private .set ("foo" , "bar" , value = 42 )
4143 assert private .path .exists ()
4244 st_mode = get_file_mode (private .path )
4345 assert st_mode & 0o777 == 0o600
4446
45- def test_wrong_permissions (self , tmp_path , caplog ):
46- private = PrivateJsonFile ( tmp_path )
47- with private . path .open ("w" ) as f :
47+ def test_wrong_permissions_on_load (self , tmp_path , caplog ):
48+ path = tmp_path / "my_data.secret"
49+ with path .open ("w" ) as f :
4850 json .dump ({"foo" : "bar" }, f )
49- assert private .path .stat ().st_mode & 0o077 > 0
51+ assert path .stat ().st_mode & 0o077 > 0
52+
53+ private = PrivateJsonFile (path )
5054
5155 if platform .system () != "Windows" :
52- with pytest .raises (
53- PermissionError , match = "readable by others.*expected: 600"
54- ):
56+ with pytest .raises (PermissionError , match = "readable by others.*expected: 600" ):
5557 private .get ("foo" )
56- with pytest .raises (
57- PermissionError , match = "readable by others.*expected: 600"
58- ):
58+ with pytest .raises (PermissionError , match = "readable by others.*expected: 600" ):
5959 private .set ("foo" , value = "lol" )
6060 else :
6161 regex = re .compile ("readable by others.*expected: 600" )
@@ -93,6 +93,18 @@ def test_remove(self, tmp_path):
9393 private .remove ()
9494 assert not private .path .exists ()
9595
96+ def test_load_corrupt_content (self , tmp_path ):
97+ path = tmp_path / "my_data.secret"
98+ path .write_text ("{\n Invalid JSON here!" )
99+ set_file_mode (path , mode = PRIVATE_PERMISSIONS )
100+
101+ private = PrivateJsonFile (path = path )
102+ with pytest .raises (
103+ RuntimeError , match = r"Failed to load PrivateJsonFile from .*my_data\.secret.*JSONDecodeError.*line 2"
104+ ):
105+ private .load ()
106+
107+
96108
97109class TestAuthConfig :
98110
0 commit comments