summaryrefslogtreecommitdiff
path: root/barbicanclient/test/common/test_auth.py
blob: eb7eb2530788c4a08bc5f24715e908265c3f8720 (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
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
# Copyright (c) 2013 Rackspace, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json

import mock
import requests
import unittest2 as unittest

from barbicanclient.common import auth


class WhenTestingKeystoneAuthentication(unittest.TestCase):

    def setUp(self):
        self.keystone_client = mock.MagicMock()

        self.auth_url = 'https://www.yada.com'
        self.username = 'user'
        self.password = 'pw'
        self.tenant_name = 'tenant'
        self.tenant_id = '1234'

        self.keystone_auth = auth.KeystoneAuthV2(auth_url=self.auth_url,
                                                 username=self.username,
                                                 password=self.password,
                                                 tenant_name=self.tenant_name,
                                                 tenant_id=self.tenant_id,
                                                 keystone=
                                                 self.keystone_client)

    def test_endpoint_username_password_tenant_are_required(self):
        with self.assertRaises(ValueError):
            keystone = auth.KeystoneAuthV2()

    def test_nothing_is_required_if_keystone_is_present(self):
        fake_keystone = mock.Mock(tenant_name='foo', tenant_id='bar')
        keystone_auth = auth.KeystoneAuthV2(keystone=fake_keystone)
        self.assertEqual('foo', keystone_auth.tenant_name)
        self.assertEqual('bar', keystone_auth.tenant_id)

    def test_get_barbican_url(self):
        barbican_url = 'https://www.barbican.com'
        self.keystone_auth._barbican_url = barbican_url
        self.assertEquals(barbican_url, self.keystone_auth.barbican_url)


class WhenTestingRackspaceAuthentication(unittest.TestCase):

    def setUp(self):
        self._auth_url = 'https://auth.url.com'
        self._username = 'username'
        self._api_key = 'api_key'
        self._auth_token = '078a50dcdc984a639bb287c8d4adf541'
        self._tenant_id = '123456'

        self._response = requests.Response()
        self._response._content = json.dumps({
            'access': {
                'token': {
                    'id': self._auth_token,
                    'expires': '2013-12-19T23:06:17.047Z',
                    'tenant': {
                        'id': self._tenant_id,
                        'name': '123456'
                    }
                }
            }
        })

        patcher = mock.patch('barbicanclient.common.auth.requests.post')
        self._mock_post = patcher.start()
        self._mock_post.return_value = self._response
        self.addCleanup(patcher.stop)

    def test_auth_url_username_and_api_key_are_required(self):
        with self.assertRaises(ValueError):
            identity = auth.RackspaceAuthV2()
        with self.assertRaises(ValueError):
            identity = auth.RackspaceAuthV2(self._auth_url)
        with self.assertRaises(ValueError):
            identity = auth.RackspaceAuthV2(self._auth_url,
                                            self._username)
        with self.assertRaises(ValueError):
            identity = auth.RackspaceAuthV2(self._auth_url,
                                            api_key=self._api_key)

    def test_tokens_is_appended_to_auth_url(self):
        identity = auth.RackspaceAuthV2(self._auth_url,
                                        self._username,
                                        api_key=self._api_key)
        self._mock_post.assert_called_with(
            'https://auth.url.com/tokens',
            data=mock.ANY,
            headers=mock.ANY)

    def test_authenticate_with_api_key(self):
        with mock.patch(
            'barbicanclient.common.auth.RackspaceAuthV2.'
            '_authenticate_with_api_key'
        ) as mock_authenticate:
            mock_authenticate.return_value = {}
            identity = auth.RackspaceAuthV2(self._auth_url,
                                            self._username,
                                            api_key=self._api_key)
            mock_authenticate.assert_called_once_with()

    def test_authenticate_with_password(self):
        with mock.patch(
            'barbicanclient.common.auth.RackspaceAuthV2.'
            '_authenticate_with_password'
        ) as mock_authenticate:
            mock_authenticate.return_value = {}
            identity = auth.RackspaceAuthV2(self._auth_url,
                                            self._username,
                                            password='password')
            mock_authenticate.assert_called_once_with()

    def test_auth_exception_thrown_for_bad_status(self):
        self._response.status_code = 400
        with self.assertRaises(auth.AuthException):
            identity = auth.RackspaceAuthV2(self._auth_url,
                                            self._username,
                                            api_key=self._api_key)

    def test_error_raised_for_bad_response_from_server(self):
        self._response._content = 'Not JSON'
        with self.assertRaises(auth.AuthException):
            identity = auth.RackspaceAuthV2(self._auth_url,
                                            self._username,
                                            api_key=self._api_key)

    def test_auth_token_is_set(self):
        identity = auth.RackspaceAuthV2(self._auth_url,
                                        self._username,
                                        api_key=self._api_key)
        self.assertEqual(identity.auth_token, self._auth_token)

    def test_tenant_id_is_set(self):
        identity = auth.RackspaceAuthV2(self._auth_url,
                                        self._username,
                                        api_key=self._api_key)
        self.assertEqual(identity.tenant_id, self._tenant_id)