forked from AspenWeb/pando.py
/
test_httpbasic.py
59 lines (44 loc) · 2.21 KB
/
test_httpbasic.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
from aspen.http.response import Response
from aspen.testing import assert_raises, StubRequest
from aspen.auth.httpbasic import inbound_responder
import base64
# convenience functions
def _auth_header(username, password):
"""return the value part of an Authorization: header for basic auth with the specified username and password"""
return "Basic " + base64.b64encode(username + ":" + password)
# tests
def _request_with(authfunc, auth_header):
request = StubRequest()
if auth_header is not None:
request.headers['Authorization'] = auth_header
hook = inbound_responder(authfunc)
return hook(request)
def test_good_works():
request = _request_with(lambda u, p: u == "username" and p == "password", _auth_header("username", "password"))
success = request.auth.authorized()
assert success
assert request.auth.username() == "username", request.auth.username()
def test_hard_passwords():
for password in [ 'pass', 'username', ':password', ':password:','::::::' ]:
request = _request_with(lambda u, p: u == "username" and p == password, _auth_header("username", password))
success = request.auth.authorized()
assert success
assert request.auth.username() == "username", request.auth.username()
def test_no_auth():
auth = lambda u, p: u == "username" and p == "password"
response = assert_raises(Response, _request_with, auth, None)
assert response.code == 401, response
def test_bad_fails():
auth = lambda u, p: u == "username" and p == "password"
response = assert_raises(Response, _request_with, auth, _auth_header("username", "wrong password"))
assert response.code == 401, response
def test_wrong_auth():
auth = lambda u, p: u == "username" and p == "password"
response = assert_raises(Response, _request_with, auth, "Wacky xxx")
assert response.code == 400, response
def test_malformed_password():
auth = lambda u, p: u == "username" and p == "password"
response = assert_raises(Response, _request_with, auth, "Basic " + base64.b64encode("usernamepassword"))
assert response.code == 400, response
response = assert_raises(Response, _request_with, auth, "Basic xxx")
assert response.code == 400, response