File: auth_tests.py

package info (click to toggle)
python-softlayer 6.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,508 kB
  • sloc: python: 57,195; makefile: 133; xml: 97; sh: 59
file content (104 lines) | stat: -rw-r--r-- 3,257 bytes parent folder | download
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
"""
    SoftLayer.tests.auth_tests
    ~~~~~~~~~~~~~~~~~~~~~~~~~~

    :license: MIT, see LICENSE for more details.
"""
from SoftLayer import auth
from SoftLayer import testing
from SoftLayer import transports


class TestAuthenticationBase(testing.TestCase):
    def test_get_request(self):
        auth_base = auth.AuthenticationBase()
        self.assertEqual(auth_base.get_request({}), {})
        self.assertEqual(auth_base.get_headers(), {})


class TestBasicAuthentication(testing.TestCase):
    def set_up(self):
        self.auth = auth.BasicAuthentication('USERNAME', 'APIKEY')

    def test_attribs(self):
        self.assertEqual(self.auth.username, 'USERNAME')
        self.assertEqual(self.auth.api_key, 'APIKEY')

    def test_get_request(self):
        req = transports.Request()
        authed_req = self.auth.get_request(req)
        self.assertEqual(authed_req.headers, {
            'authenticate': {
                'username': 'USERNAME',
                'apiKey': 'APIKEY',
            }
        })

    def test_repr(self):
        s = repr(self.auth)
        self.assertIn('BasicAuthentication', s)
        self.assertIn('USERNAME', s)


class TestTokenAuthentication(testing.TestCase):
    def set_up(self):
        self.auth = auth.TokenAuthentication(12345, 'TOKEN')

    def test_attribs(self):
        self.assertEqual(self.auth.user_id, 12345)
        self.assertEqual(self.auth.auth_token, 'TOKEN')

    def test_get_request(self):
        req = transports.Request()
        authed_req = self.auth.get_request(req)
        self.assertEqual(authed_req.headers, {
            'authenticate': {
                'complexType': 'PortalLoginToken',
                'userId': 12345,
                'authToken': 'TOKEN',
            }
        })

    def test_repr(self):
        s = repr(self.auth)
        self.assertIn('TokenAuthentication', s)
        self.assertIn('12345', s)


class TestBasicHTTPAuthentication(testing.TestCase):
    def set_up(self):
        self.auth = auth.BasicHTTPAuthentication('USERNAME', 'APIKEY')

    def test_attribs(self):
        self.assertEqual(self.auth.username, 'USERNAME')
        self.assertEqual(self.auth.api_key, 'APIKEY')

    def test_get_request(self):
        req = transports.Request()
        authed_req = self.auth.get_request(req)
        self.assertEqual(authed_req.transport_user, 'USERNAME')
        self.assertEqual(authed_req.transport_password, 'APIKEY')

    def test_repr(self):
        s = repr(self.auth)
        self.assertIn('BasicHTTPAuthentication', s)
        self.assertIn('USERNAME', s)


class TestX509AUthentication(testing.TestCase):
    def set_up(self):
        self.auth = auth.X509Authentication('authcert.pm', 'servercert.pm')

    def test_attribs(self):
        self.assertEqual(self.auth.cert, 'authcert.pm')
        self.assertEqual(self.auth.ca_cert, 'servercert.pm')

    def test_get_request(self):
        req = transports.Request()
        authed_req = self.auth.get_request(req)
        self.assertEqual(authed_req.cert, 'authcert.pm')
        self.assertEqual(authed_req.verify, 'servercert.pm')

    def test_repr(self):
        s = repr(self.auth)
        self.assertEqual(s, "X509Authentication(cert=authcert.pm, ca_cert=servercert.pm)")