Beispiel #1
0
 def test_set_get(self, tmp_path):
     private = PrivateJsonFile(tmp_path)
     private.set("foo", "bar", value=42)
     assert private.get("foo", "bar") == 42
     with private.path.open("r") as f:
         data = json.load(f)
     assert data == {"foo": {"bar": 42}}
Beispiel #2
0
 def test_permissions(self, tmp_path):
     private = PrivateJsonFile(tmp_path)
     assert not private.path.exists()
     private.set("foo", "bar", value=42)
     assert private.path.exists()
     st_mode = private.path.stat().st_mode
     assert st_mode & 0o777 == 0o600
Beispiel #3
0
 def test_wrong_permissions(self, tmp_path):
     private = PrivateJsonFile(tmp_path)
     with private.path.open("w") as f:
         json.dump({"foo": "bar"}, f)
     assert private.path.stat().st_mode & 0o077 > 0
     with pytest.raises(PermissionError,
                        match="readable by others.*expected: 600"):
         private.get("foo")
     with pytest.raises(PermissionError,
                        match="readable by others.*expected: 600"):
         private.set("foo", value="lol")
Beispiel #4
0
 def test_remove(self, tmp_path):
     private = PrivateJsonFile(tmp_path)
     private.set("foo", "bar", value=42)
     assert private.path.exists()
     private.remove()
     assert not private.path.exists()
Beispiel #5
0
 def test_remove_no_file(self, tmp_path):
     private = PrivateJsonFile(tmp_path)
     assert not private.path.exists()
     # Don't complain/fail if it doesn't exist
     private.remove()
Beispiel #6
0
 def test_provide_file_path(self, tmp_path):
     private = PrivateJsonFile(path=tmp_path / "my_data.secret")
     private.set("foo", "bar", value=42)
     assert private.path.exists()
     assert [p.name for p in tmp_path.iterdir()] == ["my_data.secret"]
Beispiel #7
0
 def test_provide_dir_path(self, tmp_path):
     private = PrivateJsonFile(path=tmp_path)
     private.set("foo", "bar", value=42)
     assert private.path.exists()
     assert [p.name for p in tmp_path.iterdir()
             ] == [PrivateJsonFile.DEFAULT_FILENAME]
Beispiel #8
0
 def test_empty(self, tmp_path):
     private = PrivateJsonFile(tmp_path)
     assert private.get("foo") is None
     assert not private.path.exists()