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