xref: /unit/test/test_static_share.py (revision 1971:3410f9d2a662)
1import os
2from pathlib import Path
3
4import pytest
5from unit.applications.proto import TestApplicationProto
6
7
8class TestStaticShare(TestApplicationProto):
9    prerequisites = {}
10
11    @pytest.fixture(autouse=True)
12    def setup_method_fixture(self, temp_dir):
13        os.makedirs(temp_dir + '/assets/dir')
14        os.makedirs(temp_dir + '/assets/dir2')
15
16        Path(temp_dir + '/assets/dir/file').write_text('1')
17        Path(temp_dir + '/assets/dir2/file2').write_text('2')
18
19        assert 'success' in self.conf(
20            {
21                "listeners": {"*:7080": {"pass": "routes"}},
22                "routes": [{"action": {"share": temp_dir + "/assets$uri"}}],
23                "applications": {},
24            }
25        )
26
27    def action_update(self, conf):
28        assert 'success' in self.conf(conf, 'routes/0/action')
29
30    def test_share_array(self, temp_dir):
31        assert self.get(url='/dir/file')['body'] == '1'
32        assert self.get(url='/dir2/file2')['body'] == '2'
33
34        self.action_update({"share": [temp_dir + "/assets/dir$uri"]})
35
36        assert self.get(url='/file')['body'] == '1'
37        assert self.get(url='/file2')['status'] == 404
38
39        self.action_update(
40            {
41                "share": [
42                    temp_dir + "/assets/dir$uri",
43                    temp_dir + "/assets/dir2$uri",
44                ]
45            }
46        )
47
48        assert self.get(url='/file')['body'] == '1'
49        assert self.get(url='/file2')['body'] == '2'
50
51        self.action_update(
52            {
53                "share": [
54                    temp_dir + "/assets/dir2$uri",
55                    temp_dir + "/assets/dir3$uri",
56                ]
57            }
58        )
59
60        assert self.get(url='/file')['status'] == 404
61        assert self.get(url='/file2')['body'] == '2'
62
63    def test_share_array_fallback(self):
64        self.action_update(
65            {"share": ["/blah", "/blah2"], "fallback": {"return": 201}}
66        )
67
68        assert self.get()['status'] == 201
69
70    def test_share_array_invalid(self):
71        assert 'error' in self.conf({"share": []}, 'routes/0/action')
72        assert 'error' in self.conf({"share": {}}, 'routes/0/action')
73