Debian Codegraph @

tests/test_helpers.py

raw symbols SCIP
1 import string
2
3 import pytest
4
5 from pyxs.exceptions import InvalidPath, InvalidPermission
6 from pyxs.helpers import check_path, check_watch_path, check_perms
7
8
9 def test_check_path():
10 # a) max length is bounded by 3072 for absolute path and 2048 for
11 # relative ones.
12 with pytest.raises(InvalidPath):
13 check_path(b"/foo/bar" * 3072)
14
15 with pytest.raises(InvalidPath):
16 check_path(b"foo/bar" * 2048)
17
18 # b) ASCII alphanumerics and -/_@ only!
19 for char in string.punctuation:
20 if char in "-/_@":
21 continue
22
23 with pytest.raises(InvalidPath):
24 check_path(b"/foo" + char.encode())
25
26 # c) no trailing / -- except for root path.
27 with pytest.raises(InvalidPath):
28 check_path(b"/foo/")
29
30 # d) no //'s!.
31 with pytest.raises(InvalidPath):
32 check_path(b"/foo//bar")
33
34 # e) OK-case.
35 check_path(b"/")
36
37
38 def test_check_watch_path():
39 # a) ordinary path should be checked with `check_path()`
40 with pytest.raises(InvalidPath):
41 check_watch_path(b"/foo/")
42
43 with pytest.raises(InvalidPath):
44 check_watch_path(b"/fo\x07o")
45
46 with pytest.raises(InvalidPath):
47 check_watch_path(b"/$/foo")
48
49 # b) special path options are limited to `@introduceDomain` and
50 # `@releaseDomain`.
51 with pytest.raises(InvalidPath):
52 check_watch_path(b"@foo")
53
54 # c) OK-case.
55 check_watch_path(b"@introduceDomain")
56 check_watch_path(b"@releaseDomain")
57
58
59 def test_check_perms():
60 # A valid permission has a form `[wrbn]:digits:`.
61 with pytest.raises(InvalidPermission):
62 check_perms([b"foo"])
63
64 with pytest.raises(InvalidPermission):
65 check_perms([b"f20"])
66
67 with pytest.raises(InvalidPermission):
68 check_perms([b"r-20"])
69
70 # OK-case
71 check_perms(b"w0 r0 b0 n0".split())
72 check_perms([b"w999999"]) # valid, even though it overflows int32.