blob: 063b40006ff86912e24e336b13cf9c40f6688aad (
plain)
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
|
import base64
import unittest
import distixapi
class GetCredentialsTests(unittest.TestCase):
def test_raises_error_if_no_Authentication_header(self):
request = DummyRequest()
with self.assertRaises(distixapi.AuthenticationError):
distixapi.get_credentials(request)
def test_raises_error_if_empty_Authentication_header(self):
request = DummyRequest()
request.add_header('Authorization', '')
with self.assertRaises(distixapi.AuthenticationError):
distixapi.get_credentials(request)
def test_raises_error_if_not_BasicAuth_header(self):
request = DummyRequest()
request.add_header('Authorization', 'Bearer token')
with self.assertRaises(distixapi.AuthenticationError):
distixapi.get_credentials(request)
def test_raises_error_if_no_BasicAuth_value(self):
request = DummyRequest()
request.add_header('Authorization', 'Basic')
with self.assertRaises(distixapi.AuthenticationError):
distixapi.get_credentials(request)
def test_returns_username_password(self):
request = DummyRequest()
username = 'fooser'
password = 'secret'
value = base64.b64encode('{}:{}'.format(username, password))
request.add_header('Authorization', 'Basic {}'.format(value))
u, p = distixapi.get_credentials(request)
self.assertEqual(username, u)
self.assertEqual(password, p)
class DummyRequest(object):
def __init__(self):
self._headers = {}
def add_header(self, header, value):
self._headers[header] = value
def get_header(self, header):
return self._headers.get(header)
|