12
12
PrivateJsonFile ,
13
13
RefreshTokenStore ,
14
14
get_file_mode ,
15
+ set_file_mode ,
16
+ PRIVATE_PERMISSIONS ,
15
17
)
16
18
17
19
@@ -34,28 +36,26 @@ def test_provide_file_path(self, tmp_path):
34
36
assert private .path .exists ()
35
37
assert [p .name for p in tmp_path .iterdir ()] == ["my_data.secret" ]
36
38
37
- def test_permissions (self , tmp_path ):
39
+ def test_permissions_on_create (self , tmp_path ):
38
40
private = PrivateJsonFile (tmp_path )
39
41
assert not private .path .exists ()
40
42
private .set ("foo" , "bar" , value = 42 )
41
43
assert private .path .exists ()
42
44
st_mode = get_file_mode (private .path )
43
45
assert st_mode & 0o777 == 0o600
44
46
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 :
48
50
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 )
50
54
51
55
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" ):
55
57
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" ):
59
59
private .set ("foo" , value = "lol" )
60
60
else :
61
61
regex = re .compile ("readable by others.*expected: 600" )
@@ -93,6 +93,18 @@ def test_remove(self, tmp_path):
93
93
private .remove ()
94
94
assert not private .path .exists ()
95
95
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
+
96
108
97
109
class TestAuthConfig :
98
110
0 commit comments