2022-04-12 13:25:18 +00:00
|
|
|
"""Tests for path handling
|
|
|
|
"""
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2022-05-05 06:33:03 +00:00
|
|
|
from earthsnake.exc import ValidationError
|
2022-04-12 13:25:18 +00:00
|
|
|
from earthsnake.identity import Identity
|
|
|
|
from earthsnake.path import Path
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'path,error_msg',
|
|
|
|
[
|
|
|
|
pytest.param('/', 'Path length must be between 2 and 512', id='short'),
|
|
|
|
pytest.param('/' + 'a' * 512, 'Path length must be between 2 and 512', id='long'),
|
|
|
|
pytest.param('badstart', 'Paths must start with a /', id='no_slash_start'),
|
|
|
|
pytest.param('/@atstart', 'Paths must not start with /@', id='slashat_start'),
|
|
|
|
pytest.param('/double//slash', 'Paths must not contain //', id='double_slash'),
|
|
|
|
pytest.param('/multiple!excl!', 'Only one ! is allowed in paths', id='multi_excl'),
|
|
|
|
pytest.param('/test/', 'Paths must not end with a /', id='trailing_slash'),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_invalid(path: str, error_msg: str) -> None:
|
|
|
|
"""Test if validation fails for invalid paths"""
|
|
|
|
|
2022-05-05 06:33:03 +00:00
|
|
|
with pytest.raises(ValidationError) as ctx:
|
2022-04-12 13:25:18 +00:00
|
|
|
Path.validate(path)
|
|
|
|
|
|
|
|
assert error_msg in str(ctx.value)
|
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_non_ephemeral() -> None:
|
|
|
|
"""Test validating ephemeral paths when ephemeral is not allowed"""
|
|
|
|
|
2022-05-05 06:33:03 +00:00
|
|
|
with pytest.raises(ValidationError) as ctx:
|
2022-04-12 13:25:18 +00:00
|
|
|
Path.validate('/ephemeral!')
|
|
|
|
|
|
|
|
assert 'Only ephemeral paths may contain !' in str(ctx.value)
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_ephemeral() -> None:
|
|
|
|
"""Test validation of ephemeral paths"""
|
|
|
|
|
|
|
|
assert Path.validate('/ephemeral!', allow_ephemeral=True) is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_is_ephemeral() -> None:
|
|
|
|
"""Test the is_ephemeral property"""
|
|
|
|
|
|
|
|
assert Path('/ephemeral!').is_ephemeral
|
|
|
|
assert not Path('/non-ephemeral').is_ephemeral
|
|
|
|
|
|
|
|
|
|
|
|
def test_is_shared() -> None:
|
|
|
|
"""Test the is_shared property"""
|
|
|
|
|
|
|
|
assert Path('/test').is_shared
|
|
|
|
assert not Path('/test/~').is_shared
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'author,path,allowed',
|
|
|
|
[
|
|
|
|
pytest.param(
|
|
|
|
'@test.bcz76z52y5dlpohtkmpuj3jsdcvfmebzpcgfmtmhu4u7hlexzreya',
|
|
|
|
'/test',
|
|
|
|
True,
|
|
|
|
id='shared',
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
'@test.bcz76z52y5dlpohtkmpuj3jsdcvfmebzpcgfmtmhu4u7hlexzreya',
|
|
|
|
'/test/~@test.bcz76z52y5dlpohtkmpuj3jsdcvfmebzpcgfmtmhu4u7hlexzreya',
|
|
|
|
True,
|
|
|
|
id='allowed',
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
'@test.bcz76z52y5dlpohtkmpuj3jsdcvfmebzpcgfmtmhu4u7hlexzreya',
|
|
|
|
'/test/~@abob.bcz76z52y5dlpohtkmpuj3jsdcvfmebzpcgfmtmhu4u7hlexzreya',
|
|
|
|
False,
|
|
|
|
id='someone_else',
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
'@test.bcz76z52y5dlpohtkmpuj3jsdcvfmebzpcgfmtmhu4u7hlexzreya',
|
|
|
|
'/test/~',
|
|
|
|
False,
|
|
|
|
id='strictly_forbidden',
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_can_write(author: str, path: str, allowed: bool) -> None:
|
|
|
|
"""Test the can_write method"""
|
|
|
|
|
|
|
|
path_obj = Path(path)
|
|
|
|
identity = Identity.from_address(author)
|
|
|
|
|
|
|
|
assert path_obj.can_write(identity) is allowed
|
2022-05-05 11:38:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_str() -> None:
|
|
|
|
"""Test the __str__ method"""
|
|
|
|
|
|
|
|
assert str(Path('/test.txt')) == '/test.txt'
|
|
|
|
|
|
|
|
|
|
|
|
def test_repr() -> None:
|
|
|
|
"""Test the __repr__ method"""
|
|
|
|
|
|
|
|
assert repr(Path('/test.txt')) == '<Path /test.txt>'
|