xref: /unit/test/test_python_environment.py (revision 2330:4b1f175f9c88)
1from unit.applications.lang.python import TestApplicationPython
2
3
4class TestPythonEnvironment(TestApplicationPython):
5    prerequisites = {'modules': {'python': 'any'}}
6
7    def test_python_environment_name_null(self):
8        self.load('environment')
9
10        assert 'error' in self.conf(
11            {"va\0r": "val1"}, 'applications/environment/environment'
12        ), 'name null'
13
14    def test_python_environment_name_equals(self):
15        self.load('environment')
16
17        assert 'error' in self.conf(
18            {"var=": "val1"}, 'applications/environment/environment'
19        ), 'name equals'
20
21    def test_python_environment_value_null(self):
22        self.load('environment')
23
24        assert 'error' in self.conf(
25            {"var": "\0val"}, 'applications/environment/environment'
26        ), 'value null'
27
28    def test_python_environment_update(self):
29        self.load('environment')
30
31        self.conf({"var": "val1"}, 'applications/environment/environment')
32
33        assert (
34            self.get(
35                headers={
36                    'Host': 'localhost',
37                    'X-Variables': 'var',
38                    'Connection': 'close',
39                }
40            )['body']
41            == 'val1'
42        ), 'set'
43
44        self.conf({"var": "val2"}, 'applications/environment/environment')
45
46        assert (
47            self.get(
48                headers={
49                    'Host': 'localhost',
50                    'X-Variables': 'var',
51                    'Connection': 'close',
52                }
53            )['body']
54            == 'val2'
55        ), 'update'
56
57    def test_python_environment_replace(self):
58        self.load('environment')
59
60        self.conf({"var1": "val1"}, 'applications/environment/environment')
61
62        assert (
63            self.get(
64                headers={
65                    'Host': 'localhost',
66                    'X-Variables': 'var1',
67                    'Connection': 'close',
68                }
69            )['body']
70            == 'val1'
71        ), 'set'
72
73        self.conf({"var2": "val2"}, 'applications/environment/environment')
74
75        assert (
76            self.get(
77                headers={
78                    'Host': 'localhost',
79                    'X-Variables': 'var1,var2',
80                    'Connection': 'close',
81                }
82            )['body']
83            == 'val2'
84        ), 'replace'
85
86    def test_python_environment_clear(self):
87        self.load('environment')
88
89        self.conf(
90            {"var1": "val1", "var2": "val2"},
91            'applications/environment/environment',
92        )
93
94        assert (
95            self.get(
96                headers={
97                    'Host': 'localhost',
98                    'X-Variables': 'var1,var2',
99                    'Connection': 'close',
100                }
101            )['body']
102            == 'val1,val2'
103        ), 'set'
104
105        self.conf({}, 'applications/environment/environment')
106
107        assert (
108            self.get(
109                headers={
110                    'Host': 'localhost',
111                    'X-Variables': 'var1,var2',
112                    'Connection': 'close',
113                }
114            )['body']
115            == ''
116        ), 'clear'
117
118    def test_python_environment_replace_default(self):
119        self.load('environment')
120
121        home_default = self.get(
122            headers={
123                'Host': 'localhost',
124                'X-Variables': 'HOME',
125                'Connection': 'close',
126            }
127        )['body']
128
129        assert len(home_default) > 1, 'get default'
130
131        self.conf({"HOME": "/"}, 'applications/environment/environment')
132
133        assert (
134            self.get(
135                headers={
136                    'Host': 'localhost',
137                    'X-Variables': 'HOME',
138                    'Connection': 'close',
139                }
140            )['body']
141            == '/'
142        ), 'replace default'
143
144        self.conf({}, 'applications/environment/environment')
145
146        assert (
147            self.get(
148                headers={
149                    'Host': 'localhost',
150                    'X-Variables': 'HOME',
151                    'Connection': 'close',
152                }
153            )['body']
154            == home_default
155        ), 'restore default'
156