1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
from unittest import TestCase
from snekbox import snekio
from snekbox.snekio import FileAttachment, IllegalPathError, ParsingError
class SnekIOTests(TestCase):
def test_safe_path(self) -> None:
cases = [
("", ""),
("foo", "foo"),
("foo/bar", "foo/bar"),
("foo/bar.ext", "foo/bar.ext"),
]
for path, expected in cases:
self.assertEqual(snekio.safe_path(path), expected)
def test_safe_path_raise(self):
cases = [
("../foo", IllegalPathError, "File path '../foo' may not traverse beyond root"),
("/foo", IllegalPathError, "File path '/foo' must be relative"),
]
for path, error, msg in cases:
with self.assertRaises(error) as cm:
snekio.safe_path(path)
self.assertEqual(str(cm.exception), msg)
def test_file_from_dict(self):
cases = [
({"path": "foo", "content": ""}, FileAttachment("foo", b"")),
({"path": "foo"}, FileAttachment("foo", b"")),
({"path": "foo", "content": "Zm9v"}, FileAttachment("foo", b"foo")),
({"path": "foo/bar.ext", "content": "Zm9v"}, FileAttachment("foo/bar.ext", b"foo")),
]
for data, expected in cases:
self.assertEqual(FileAttachment.from_dict(data), expected)
def test_file_from_dict_error(self):
cases = [
(
{"path": "foo", "content": "9"},
ParsingError,
"Invalid base64 encoding for file 'foo'",
),
(
{"path": "var/a.txt", "content": "1="},
ParsingError,
"Invalid base64 encoding for file 'var/a.txt'",
),
]
for data, error, msg in cases:
with self.assertRaises(error) as cm:
FileAttachment.from_dict(data)
self.assertEqual(str(cm.exception), msg)
|