-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest_auth.py
326 lines (244 loc) · 11.5 KB
/
test_auth.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python3
#
# Copyright (c) 2024 YunoHost Contributors
#
# This file is part of YunoHost (see https://yunohost.org)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import pytest
from moulinette import MoulinetteError
class TestAuthAPI:
def login(self, webapi, csrf=False, profile=None, status=200, password=None):
if password is None:
password = "dummy"
data = {"credentials": password}
if profile:
data["profile"] = profile
return webapi.post(
"/login",
data,
status=status,
headers=None if csrf else {"X-Requested-With": ""},
)
def test_request_no_auth_needed(self, moulinette_webapi):
assert (
moulinette_webapi.get("/test-auth/none", status=200).text
== '"some_data_from_none"'
)
def test_request_no_auth_needed_subcategories(self, moulinette_webapi):
assert (
moulinette_webapi.get("/test-auth/subcat/none", status=200).text
== '"some_data_from_subcat_none"'
)
def test_request_with_auth_but_not_logged(self, moulinette_webapi):
assert (
moulinette_webapi.get("/test-auth/default", status=401).text
== "Authentication required"
)
def test_request_with_auth_subcategories_but_not_logged(self, moulinette_webapi):
assert (
moulinette_webapi.get("/test-auth/subcat/default", status=401).text
== "Authentication required"
)
def test_request_not_logged_only_api(self, moulinette_webapi):
assert (
moulinette_webapi.get("/test-auth/only-api", status=401).text
== "Authentication required"
)
def test_request_only_api(self, moulinette_webapi):
self.login(moulinette_webapi)
assert (
moulinette_webapi.get("/test-auth/only-api", status=200).text
== '"some_data_from_only_api"'
)
def test_request_not_logged_only_cli(self, moulinette_webapi):
assert (
moulinette_webapi.get("/test-auth/only-cli", status=200).text
== '"some_data_from_only_cli"'
)
def test_login(self, moulinette_webapi):
assert self.login(moulinette_webapi).text == "Logged in"
assert "moulitest" in moulinette_webapi.cookies
def test_login_bad_password(self, moulinette_webapi):
assert (
self.login(moulinette_webapi, password="Bad Password", status=401).text
== "invalid_password"
)
assert "moulitest" not in moulinette_webapi.cookies
def test_login_csrf_attempt(self, moulinette_webapi):
# CSRF should not block the login endpoint
assert (
"CSRF protection"
not in self.login(moulinette_webapi, csrf=True, status=200).text
)
assert "moulitest" in moulinette_webapi.cookies
def test_login_then_legit_request_without_cookies(self, moulinette_webapi):
self.login(moulinette_webapi)
moulinette_webapi.cookiejar.clear()
moulinette_webapi.get("/test-auth/default", status=401)
def test_login_then_legit_request(self, moulinette_webapi):
self.login(moulinette_webapi)
assert "moulitest" in moulinette_webapi.cookies
assert (
moulinette_webapi.get("/test-auth/default", status=200).text
== '"some_data_from_default"'
)
assert (
moulinette_webapi.get("/test-auth/subcat/default", status=200).text
== '"some_data_from_subcat_default"'
)
def test_login_then_logout(self, moulinette_webapi):
self.login(moulinette_webapi)
moulinette_webapi.get("/logout", status=200)
assert (
moulinette_webapi.get("/test-auth/default", status=401).text
== "Authentication required"
)
def test_login_other_profile(self, moulinette_webapi):
self.login(moulinette_webapi, profile="yoloswag", password="yoloswag")
assert "moulitest" in moulinette_webapi.cookies
def test_login_wrong_profile(self, moulinette_webapi):
self.login(moulinette_webapi)
assert (
moulinette_webapi.get("/test-auth/other-profile", status=401).text
== "Authentication required"
)
moulinette_webapi.get("/logout", status=200)
self.login(moulinette_webapi, profile="yoloswag", password="yoloswag")
assert (
moulinette_webapi.get("/test-auth/default", status=401).text
== "Authentication required"
)
def test_request_with_arg(self, moulinette_webapi, capsys):
self.login(moulinette_webapi)
assert (
moulinette_webapi.get("/test-auth/with_arg/yoloswag", status=200).text
== '"yoloswag"'
)
def test_request_arg_with_extra(self, moulinette_webapi, caplog, mocker):
self.login(moulinette_webapi)
assert (
moulinette_webapi.get(
"/test-auth/with_extra_str_only/YoLoSwAg", status=200
).text
== '"YoLoSwAg"'
)
error = "error_message"
mocker.patch("moulinette.Moulinette18n.n", return_value=error)
moulinette_webapi.get("/test-auth/with_extra_str_only/12345", status=400)
assert any("doesn't match pattern" in message for message in caplog.messages)
def test_request_arg_with_type(self, moulinette_webapi, caplog, mocker):
self.login(moulinette_webapi)
assert (
moulinette_webapi.get("/test-auth/with_type_int/12345", status=200).text
== "12345"
)
error = "error_message"
mocker.patch("moulinette.Moulinette18n.g", return_value=error)
moulinette_webapi.get("/test-auth/with_type_int/yoloswag", status=400)
def test_request_arg_without_action(self, moulinette_webapi, caplog, mocker):
self.login(moulinette_webapi)
moulinette_webapi.get("/test-auth", status=405)
class TestAuthCLI:
def test_login(self, moulinette_cli, capsys, mocker):
mocker.patch("os.isatty", return_value=True)
mocker.patch("prompt_toolkit.prompt", return_value="dummy")
moulinette_cli.run(["testauth", "default"], output_as="plain")
message = capsys.readouterr()
assert "some_data_from_default" in message.out
moulinette_cli.run(["testauth", "default"], output_as="plain")
message = capsys.readouterr()
assert "some_data_from_default" in message.out
def test_login_bad_password(self, moulinette_cli, capsys, mocker):
mocker.patch("os.isatty", return_value=True)
mocker.patch("prompt_toolkit.prompt", return_value="Bad Password")
with pytest.raises(MoulinetteError):
moulinette_cli.run(["testauth", "default"], output_as="plain")
mocker.patch("os.isatty", return_value=True)
mocker.patch("prompt_toolkit.prompt", return_value="Bad Password")
with pytest.raises(MoulinetteError):
moulinette_cli.run(["testauth", "default"], output_as="plain")
def test_login_wrong_profile(self, moulinette_cli, mocker):
mocker.patch("os.isatty", return_value=True)
mocker.patch("prompt_toolkit.prompt", return_value="dummy")
with pytest.raises(MoulinetteError) as exception:
moulinette_cli.run(["testauth", "other-profile"], output_as="none")
assert "invalid_password" in str(exception)
mocker.patch("os.isatty", return_value=True)
mocker.patch("prompt_toolkit.prompt", return_value="yoloswag")
with pytest.raises(MoulinetteError) as exception:
moulinette_cli.run(["testauth", "default"], output_as="none")
assert "invalid_password" in str(exception)
def test_request_no_auth_needed(self, capsys, moulinette_cli):
moulinette_cli.run(["testauth", "none"], output_as="plain")
message = capsys.readouterr()
assert "some_data_from_none" in message.out
def test_request_not_logged_only_api(self, capsys, moulinette_cli):
moulinette_cli.run(["testauth", "only-api"], output_as="plain")
message = capsys.readouterr()
assert "some_data_from_only_api" in message.out
def test_request_only_cli(self, capsys, moulinette_cli, mocker):
mocker.patch("os.isatty", return_value=True)
mocker.patch("prompt_toolkit.prompt", return_value="dummy")
moulinette_cli.run(["testauth", "only-cli"], output_as="plain")
message = capsys.readouterr()
assert "some_data_from_only_cli" in message.out
def test_request_not_logged_only_cli(self, capsys, moulinette_cli, mocker):
mocker.patch("os.isatty", return_value=True)
mocker.patch("prompt_toolkit.prompt")
with pytest.raises(MoulinetteError) as exception:
moulinette_cli.run(["testauth", "only-cli"], output_as="plain")
message = capsys.readouterr()
assert "some_data_from_only_cli" not in message.out
assert "invalid_password" in str(exception)
def test_request_with_arg(self, moulinette_cli, capsys, mocker):
mocker.patch("os.isatty", return_value=True)
mocker.patch("prompt_toolkit.prompt", return_value="dummy")
moulinette_cli.run(["testauth", "with_arg", "yoloswag"], output_as="plain")
message = capsys.readouterr()
assert "yoloswag" in message.out
def test_request_arg_with_extra(self, moulinette_cli, capsys, mocker):
mocker.patch("os.isatty", return_value=True)
mocker.patch("prompt_toolkit.prompt", return_value="dummy")
moulinette_cli.run(
["testauth", "with_extra_str_only", "YoLoSwAg"], output_as="plain"
)
message = capsys.readouterr()
assert "YoLoSwAg" in message.out
error = "error_message"
mocker.patch("moulinette.Moulinette18n.n", return_value=error)
with pytest.raises(MoulinetteError):
moulinette_cli.run(
["testauth", "with_extra_str_only", "12345"], output_as="plain"
)
message = capsys.readouterr()
assert "doesn't match pattern" in message.err
def test_request_arg_with_type(self, moulinette_cli, capsys, mocker):
mocker.patch("os.isatty", return_value=True)
mocker.patch("prompt_toolkit.prompt", return_value="dummy")
moulinette_cli.run(["testauth", "with_type_int", "12345"], output_as="plain")
message = capsys.readouterr()
assert "12345" in message.out
with pytest.raises(SystemExit):
moulinette_cli.run(
["testauth", "with_type_int", "yoloswag"], output_as="plain"
)
message = capsys.readouterr()
assert "invalid int value" in message.err
def test_request_arg_without_action(self, moulinette_cli, capsys, mocker):
with pytest.raises(SystemExit):
moulinette_cli.run(["testauth"], output_as="plain")
message = capsys.readouterr()
assert "error: the following arguments are required:" in message.err