This repository was archived by the owner on Nov 29, 2023. It is now read-only.
forked from openstack/python-neutronclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_auth.py
More file actions
129 lines (107 loc) · 4.52 KB
/
Copy pathtest_auth.py
File metadata and controls
129 lines (107 loc) · 4.52 KB
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
# Copyright 2012 NEC Corporation
# All Rights Reserved
#
# 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.
#
# vim: tabstop=4 shiftwidth=4 softtabstop=4
import httplib2
import json
import unittest
import uuid
import mox
from mox import ContainsKeyValue, IsA, StrContains
import testtools
from quantumclient.client import HTTPClient
from quantumclient.common import exceptions
USERNAME = 'testuser'
TENANT_NAME = 'testtenant'
PASSWORD = 'password'
AUTH_URL = 'authurl'
ENDPOINT_URL = 'localurl'
TOKEN = 'tokentoken'
REGION = 'RegionTest'
KS_TOKEN_RESULT = {
'access': {
'token': {'id': TOKEN,
'expires': '2012-08-11T07:49:01Z',
'tenant': {'id': str(uuid.uuid1())}},
'user': {'id': str(uuid.uuid1())},
'serviceCatalog': [
{'endpoints_links': [],
'endpoints': [{'adminURL': ENDPOINT_URL,
'internalURL': ENDPOINT_URL,
'publicURL': ENDPOINT_URL,
'region': REGION}],
'type': 'network',
'name': 'Quantum Service'}
]
}
}
class CLITestAuthKeystone(testtools.TestCase):
def setUp(self):
"""Prepare the test environment"""
super(CLITestAuthKeystone, self).setUp()
self.mox = mox.Mox()
self.client = HTTPClient(username=USERNAME, tenant_name=TENANT_NAME,
password=PASSWORD, auth_url=AUTH_URL,
region_name=REGION)
self.addCleanup(self.mox.VerifyAll)
self.addCleanup(self.mox.UnsetStubs)
def test_get_token(self):
self.mox.StubOutWithMock(self.client, "request")
res200 = self.mox.CreateMock(httplib2.Response)
res200.status = 200
self.client.request(AUTH_URL + '/tokens', 'POST',
body=IsA(str), headers=IsA(dict)).\
AndReturn((res200, json.dumps(KS_TOKEN_RESULT)))
self.client.request(StrContains(ENDPOINT_URL + '/resource'), 'GET',
headers=ContainsKeyValue('X-Auth-Token', TOKEN)).\
AndReturn((res200, ''))
self.mox.ReplayAll()
self.client.do_request('/resource', 'GET')
self.assertEqual(self.client.endpoint_url, ENDPOINT_URL)
self.assertEqual(self.client.auth_token, TOKEN)
self.assertEqual(self.client.token_retrieved, True)
def test_already_token_retrieved(self):
self.mox.StubOutWithMock(self.client, "request")
self.client.auth_token = TOKEN
self.client.endpoint_url = ENDPOINT_URL
self.client.token_retrieved = True
res200 = self.mox.CreateMock(httplib2.Response)
res200.status = 200
self.client.request(StrContains(ENDPOINT_URL + '/resource'), 'GET',
headers=ContainsKeyValue('X-Auth-Token', TOKEN)).\
AndReturn((res200, ''))
self.mox.ReplayAll()
self.client.do_request('/resource', 'GET')
def test_refresh_token(self):
self.mox.StubOutWithMock(self.client, "request")
self.client.auth_token = TOKEN
self.client.endpoint_url = ENDPOINT_URL
self.client.token_retrieved = True
res200 = self.mox.CreateMock(httplib2.Response)
res200.status = 200
res401 = self.mox.CreateMock(httplib2.Response)
res401.status = 401
# If a token is expired, quantum server retruns 401
self.client.request(StrContains(ENDPOINT_URL + '/resource'), 'GET',
headers=ContainsKeyValue('X-Auth-Token', TOKEN)).\
AndReturn((res401, ''))
self.client.request(AUTH_URL + '/tokens', 'POST',
body=IsA(str), headers=IsA(dict)).\
AndReturn((res200, json.dumps(KS_TOKEN_RESULT)))
self.client.request(StrContains(ENDPOINT_URL + '/resource'), 'GET',
headers=ContainsKeyValue('X-Auth-Token', TOKEN)).\
AndReturn((res200, ''))
self.mox.ReplayAll()
self.client.do_request('/resource', 'GET')