/
test_page_login.py
332 lines (279 loc) · 11.6 KB
/
test_page_login.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
327
328
329
330
331
332
# -*- coding: utf-8 -*-
# rdiffweb, A web interface to rdiff-backup repositories
# Copyright (C) 2012-2021 rdiffweb contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Created on Dec 26, 2015
@author: Patrik Dufresne
"""
from base64 import b64encode
import rdiffweb.test
class LoginPageTest(rdiffweb.test.WebCase):
def test_getpage(self):
"""
Make sure the login page can be rendered without error.
"""
self.getPage('/')
self.assertStatus('303 See Other')
self.assertHeaderItemValue('Location', self.baseurl + '/login/?redirect=%2F')
def test_cookie_http_only(self):
# Given a request made to rdiffweb
# When receiving the response
self.getPage('/')
# Then the header contains Set-Cookie with HttpOnly
cookie = self.assertHeader('Set-Cookie')
self.assertIn('HttpOnly', cookie)
def test_getpage_with_plaintext(self):
"""
Requesting plain text without being authenticated should show the login form.
"""
self.getPage('/', headers=[("Accept", "text/plain")])
self.assertStatus('303 See Other')
self.assertHeaderItemValue('Location', self.baseurl + '/login/?redirect=%2F')
def test_getpage_with_redirect_get(self):
"""
Check encoding of redirect url when send using GET method.
"""
# Query the page without login-in
self.getPage('/browse/' + self.USERNAME + "/" + self.REPO + '/DIR%EF%BF%BD/')
self.assertStatus('303 See Other')
self.assertHeaderItemValue(
'Location', self.baseurl + '/login/?redirect=%2Fbrowse%2Fadmin%2Ftestcases%2FDIR%C3%AF%C2%BF%C2%BD%2F'
)
def test_getpage_with_open_redirect(self):
# Given a user browsing a URL with open redirect
# When the user visit the login page
self.getPage('/login/?redirect=https://attacker.com')
# The URL is sanitize.
self.assertNotInBody('https://attacker.com')
def test_getpage_with_broken_encoding(self):
"""
Check encoding of redirect url when send using GET method.
"""
# Query the page without login-in
self.getPage(
'/restore/'
+ self.USERNAME
+ "/"
+ self.REPO
+ '/Fichier%20avec%20non%20asci%20char%20%C9velyne%20M%E8re.txt'
)
self.assertStatus('303 See Other')
self.assertHeaderItemValue(
'Location',
self.baseurl
+ '/login/?redirect=%2Frestore%2Fadmin%2Ftestcases%2FFichier+avec+non+asci+char+%C3%89velyne+M%C3%A8re.txt',
)
def test_getpage_with_redirect_post(self):
"""
Check encoding of redirect url when send using POST method.
"""
b = {'login': 'admin', 'password': 'invalid', 'redirect': '/browse/' + self.REPO + '/DIR%EF%BF%BD/'}
self.getPage('/login/', method='POST', body=b)
self.assertStatus('200 OK')
self.assertInBody('id="form-login"')
self.assertInBody('/browse/' + self.REPO + '/DIR%EF%BF%BD/"')
def test_getpage_with_querystring_redirect_get(self):
"""
Check if unauthenticated users are redirect properly to login page.
"""
self.getPage('/browse/' + self.REPO + '/?restore=T')
self.assertStatus('303 See Other')
self.assertHeaderItemValue('Location', self.baseurl + '/login/?redirect=%2Fbrowse%2Ftestcases%2F%3Frestore%3DT')
def test_getpage_with_multiple_querystring_redirect_get(self):
self.getPage('/restore/' + self.REPO + '?date=1414871387&kind=zip')
self.assertStatus('303 See Other')
self.assertHeaderItemValue(
'Location', self.baseurl + '/login/?redirect=%2Frestore%2Ftestcases%3Fdate%3D1414871387%26kind%3Dzip'
)
def test_getpage_with_redirection(self):
"""
Check if redirect url is properly rendered in HTML.
"""
b = {
'login': 'admin',
'password': 'admin123',
'redirect': '/restore/' + self.REPO + '?date=1414871387&kind=zip',
}
self.getPage('/login/', method='POST', body=b)
self.assertStatus('303 See Other')
self.assertHeaderItemValue('Location', self.baseurl + '/restore/' + self.REPO + '?date=1414871387&kind=zip')
def test_getpage_without_username(self):
"""
Check if error is raised when requesting /login without a username.
"""
self.getPage('/login/', method='GET')
self.assertStatus('200 OK')
def test_getpage_with_empty_password(self):
"""
Check if authentication is failing without a password.
"""
b = {'login': 'admin', 'password': ''}
self.getPage('/login/', method='POST', body=b)
self.assertStatus('200 OK')
self.assertInBody('This field is required.')
def test_getpage_with_invalid_url(self):
self.getPage('/login/kefuxian.mvc', method='GET')
self.assertStatus('303 See Other')
def test_post_with_invalid_url(self):
self.getPage('/login/kefuxian.mvc', method='POST')
self.assertStatus('303 See Other')
def test_getpage_admin(self):
"""
Access to admin area without session should redirect to login page.
"""
self.getPage('/admin/')
self.assertStatus('303 See Other')
def test_getapi_without_authorization(self):
"""
Check if 401 is return when authorization is not provided.
"""
self.getPage('/api/')
self.assertStatus('401 Unauthorized')
def test_getapi_without_username(self):
"""
Check if error 401 is raised when requesting /login without a username.
"""
self.getPage('/api/', headers=[("Authorization", "Basic " + b64encode(b":admin123").decode('ascii'))])
self.assertStatus('401 Unauthorized')
def test_getapi_with_empty_password(self):
"""
Check if 401 is return when authorization is not provided.
"""
self.getPage('/api/', headers=[("Authorization", "Basic " + b64encode(b"admin:").decode('ascii'))])
self.assertStatus('401 Unauthorized')
def test_getapi_with_invalid_password(self):
"""
Check if 401 is return when authorization is not provided.
"""
self.getPage('/api/', headers=[("Authorization", "Basic " + b64encode(b"admin:invalid").decode('ascii'))])
self.assertStatus('401 Unauthorized')
def test_getapi_with_authorization(self):
"""
Check if 200 is return when authorization is not provided.
"""
self.getPage('/api/', headers=[("Authorization", "Basic " + b64encode(b"admin:admin123").decode('ascii'))])
self.assertStatus('200 OK')
def test_getapi_with_session(self):
"""
Check if 200 is return when authorization is not provided.
"""
b = {'login': 'admin', 'password': 'admin123'}
self.getPage('/login/', method='POST', body=b)
self.assertStatus('303 See Other')
self.getPage('/')
self.assertStatus('200 OK')
# Get api using the same session.
self.getPage('/api/')
self.assertStatus('200 OK')
class LoginPageWithWelcomeMsgTest(rdiffweb.test.WebCase):
default_config = {'welcomemsg': 'default message', 'welcomemsg[fr]': 'french message'}
def test_getpage_default(self):
"""
Make sure the login page can be rendered without error.
"""
self.getPage('/login/', headers=[("Accept-Language", "it")])
self.assertStatus('200 OK')
self.assertInBody('default message')
def test_getpage_french(self):
"""
Make sure the login page can be rendered without error.
"""
self.getPage('/login/', headers=[("Accept-Language", "fr")])
self.assertStatus('200 OK')
self.assertInBody('french message')
class LoginPageWithSessionDirTest(rdiffweb.test.WebCase):
default_config = {
'session-dir': '/tmp',
}
def test_login(self):
# Query a page
self.getPage('/login/')
self.assertStatus('200 OK')
self.assertInBody('Enter your username and password to log in.')
# Login
self.getPage("/login/", method='POST', body={'login': self.USERNAME, 'password': self.PASSWORD})
self.assertStatus('303 See Other')
# Query page again
self.getPage('/')
self.assertStatus('200 OK')
self.assertNotInBody('Enter your username and password to log in.')
class LoginPageRateLimitTest(rdiffweb.test.WebCase):
default_config = {
'rate-limit': 5,
}
def test_login_ratelimit(self):
# Given an unauthenticate
# When requesting multple time the login page
for i in range(0, 6):
self.getPage('/login/')
# Then a 429 error (too many request) is return
self.assertStatus(429)
class LoginPageRateLimitWithSessionDirTest(rdiffweb.test.WebCase):
default_config = {
'session-dir': '/tmp',
'rate-limit': 5,
}
def test_login_ratelimit(self):
# Given an unauthenticate
# When requesting multple time the login page
for i in range(0, 6):
self.getPage('/login/')
# Then a 429 error (too many request) is return
self.assertStatus(429)
class LoginPageRateLimitTestWithXForwardedFor(rdiffweb.test.WebCase):
default_config = {
'rate-limit': 5,
}
def test_login_ratelimit(self):
# Given an unauthenticate
# When requesting multple time the login page
for i in range(0, 6):
self.getPage('/login/', headers=[('X-Forwarded-For', '127.0.0.%s' % i)])
# Then a 429 error (too many request) is return
self.assertStatus(429)
class LoginPageRateLimitTestWithXRealIP(rdiffweb.test.WebCase):
default_config = {
'rate-limit': 5,
}
def test_login_ratelimit(self):
# Given an unauthenticate
# When requesting multple time the login page
for i in range(0, 6):
self.getPage('/login/', headers=[('X-Real-IP', '127.0.0.%s' % i)])
# Then a 200 is return.
self.assertStatus(200)
class LogoutPageTest(rdiffweb.test.WebCase):
def test_getpage_without_login(self):
# Accessing logout page directly will redirect to "/".
self.getPage('/logout/')
self.assertStatus('303 See Other')
self.assertHeaderItemValue('Location', self.baseurl + '/')
def test_getpage_with_login(self):
# Login
b = {'login': 'admin', 'password': 'admin123'}
self.getPage('/login/', method='POST', body=b)
self.assertStatus('303 See Other')
# Get content of a page.
self.getPage("/prefs/")
self.assertStatus('200 OK')
# Then logout
self.getPage('/logout/')
self.assertStatus('303 See Other')
self.assertHeaderItemValue('Location', self.baseurl + '/')
# Get content of a page.
self.getPage("/prefs/")
self.assertStatus('303 See Other')
self.assertHeaderItemValue('Location', self.baseurl + '/login/?redirect=%2Fprefs%2F')