2011-01-25 14:01:22 -06:00
|
|
|
import mock
|
|
|
|
import httplib2
|
|
|
|
from nose.tools import assert_raises, assert_equal
|
|
|
|
|
2011-08-03 16:36:03 -04:00
|
|
|
import novaclient.v1_0
|
|
|
|
from novaclient.v1_0 import exceptions
|
|
|
|
|
2011-01-25 14:01:22 -06:00
|
|
|
|
|
|
|
def test_authenticate_success():
|
2011-08-03 16:36:03 -04:00
|
|
|
cs = novaclient.v1_0.Client("username", "apikey", "project_id")
|
2011-01-25 14:01:22 -06:00
|
|
|
auth_response = httplib2.Response({
|
|
|
|
'status': 204,
|
|
|
|
'x-server-management-url':
|
|
|
|
'https://servers.api.rackspacecloud.com/v1.0/443470',
|
|
|
|
'x-auth-token': '1b751d74-de0c-46ae-84f0-915744b582d1',
|
|
|
|
})
|
|
|
|
mock_request = mock.Mock(return_value=(auth_response, None))
|
|
|
|
|
|
|
|
@mock.patch.object(httplib2.Http, "request", mock_request)
|
|
|
|
def test_auth_call():
|
|
|
|
cs.client.authenticate()
|
|
|
|
mock_request.assert_called_with(cs.client.auth_url, 'GET',
|
|
|
|
headers={
|
|
|
|
'X-Auth-User': 'username',
|
|
|
|
'X-Auth-Key': 'apikey',
|
2011-06-20 18:09:52 -07:00
|
|
|
'X-Auth-Project-Id': 'project_id',
|
2011-01-25 14:01:22 -06:00
|
|
|
'User-Agent': cs.client.USER_AGENT
|
|
|
|
})
|
|
|
|
assert_equal(cs.client.management_url,
|
|
|
|
auth_response['x-server-management-url'])
|
|
|
|
assert_equal(cs.client.auth_token, auth_response['x-auth-token'])
|
|
|
|
|
|
|
|
test_auth_call()
|
|
|
|
|
|
|
|
|
|
|
|
def test_authenticate_failure():
|
2011-08-03 16:36:03 -04:00
|
|
|
cs = novaclient.v1_0.Client("username", "apikey", "project_id")
|
2011-01-25 14:01:22 -06:00
|
|
|
auth_response = httplib2.Response({'status': 401})
|
|
|
|
mock_request = mock.Mock(return_value=(auth_response, None))
|
|
|
|
|
|
|
|
@mock.patch.object(httplib2.Http, "request", mock_request)
|
|
|
|
def test_auth_call():
|
2011-08-03 16:36:03 -04:00
|
|
|
assert_raises(exceptions.Unauthorized, cs.client.authenticate)
|
2011-01-25 14:01:22 -06:00
|
|
|
|
|
|
|
test_auth_call()
|
|
|
|
|
|
|
|
|
|
|
|
def test_auth_automatic():
|
2011-08-03 16:36:03 -04:00
|
|
|
client = novaclient.v1_0.Client("username", "apikey", "project_id").client
|
2011-01-25 14:01:22 -06:00
|
|
|
client.management_url = ''
|
|
|
|
mock_request = mock.Mock(return_value=(None, None))
|
|
|
|
|
|
|
|
@mock.patch.object(client, 'request', mock_request)
|
|
|
|
@mock.patch.object(client, 'authenticate')
|
|
|
|
def test_auth_call(m):
|
|
|
|
client.get('/')
|
|
|
|
m.assert_called()
|
|
|
|
mock_request.assert_called()
|
|
|
|
|
|
|
|
test_auth_call()
|
|
|
|
|
|
|
|
|
|
|
|
def test_auth_manual():
|
2011-08-03 16:36:03 -04:00
|
|
|
cs = novaclient.v1_0.Client("username", "apikey", "project_id")
|
2011-01-25 14:01:22 -06:00
|
|
|
|
|
|
|
@mock.patch.object(cs.client, 'authenticate')
|
|
|
|
def test_auth_call(m):
|
|
|
|
cs.authenticate()
|
|
|
|
m.assert_called()
|
|
|
|
|
|
|
|
test_auth_call()
|