diff options
| author | 2023-03-10 02:03:01 +0200 | |
|---|---|---|
| committer | 2023-03-10 02:03:01 +0200 | |
| commit | 8a85b86174067618891e2530dd56b5025fd2f28b (patch) | |
| tree | cc85ad898f492c0b799917cda05537c88bea83c6 /tests/test_snekio.py | |
| parent | Merge pull request #167 from python-discord/deployment-update (diff) | |
| parent | Merge branch 'main' into bytes-output (diff) | |
Merge pull request #159 from python-discord/bytes-output
File system and Binary file sending
Diffstat (limited to 'tests/test_snekio.py')
| -rw-r--r-- | tests/test_snekio.py | 58 | 
1 files changed, 58 insertions, 0 deletions
| diff --git a/tests/test_snekio.py b/tests/test_snekio.py new file mode 100644 index 0000000..8f04429 --- /dev/null +++ b/tests/test_snekio.py @@ -0,0 +1,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) | 
