1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[rackspace|identity] Add requests and request tests.

This commit is contained in:
Brad Gignac 2012-06-23 14:58:19 -04:00
parent 874003eadc
commit 2ad93f330f
14 changed files with 385 additions and 0 deletions

View file

@ -0,0 +1,25 @@
module Fog
module Rackspace
class Identity
class Real
def create_token(username, api_key)
data = {
'auth' => {
'RAX-KSKEY:apiKeyCredentials' => {
'username' => username,
'apiKey' => api_key
}
}
}
request(
:body => Fog::JSON.encode(data),
:expects => [200, 203],
:method => 'POST',
:path => 'tokens'
)
end
end
end
end
end

View file

@ -0,0 +1,25 @@
module Fog
module Rackspace
class Identity
class Real
def create_user(username, email, enabled, options = {})
data = {
'user' => {
'username' => username,
'email' => email,
'enabled' => enabled
}
}
data['user']['OS-KSADM:password'] = options[:password] unless options[:password].nil?
request(
:body => Fog::JSON.encode(data),
:expects => [201],
:method => 'POST',
:path => 'users'
)
end
end
end
end
end

View file

@ -0,0 +1,15 @@
module Fog
module Rackspace
class Identity
class Real
def delete_user(user_id)
request(
:expects => [204],
:method => 'DELETE',
:path => "users/#{user_id}"
)
end
end
end
end
end

View file

@ -0,0 +1,15 @@
module Fog
module Rackspace
class Identity
class Real
def get_credentials(user_id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "users/#{user_id}/OS-KSADM/credentials/RAX-KSKEY:apiKeyCredentials"
)
end
end
end
end
end

View file

@ -0,0 +1,15 @@
module Fog
module Rackspace
class Identity
class Real
def get_user_by_id(user_id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "users/#{user_id}"
)
end
end
end
end
end

View file

@ -0,0 +1,15 @@
module Fog
module Rackspace
class Identity
class Real
def get_user_by_name(username)
request(
:expects => [200, 203],
:method => 'GET',
:path => "users?name=#{username}"
)
end
end
end
end
end

View file

@ -0,0 +1,15 @@
module Fog
module Rackspace
class Identity
class Real
def list_credentials(user_id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "users/#{user_id}/OS-KSADM/credentials"
)
end
end
end
end
end

View file

@ -0,0 +1,15 @@
module Fog
module Rackspace
class Identity
class Real
def list_tenants()
request(
:expects => [200, 203],
:method => 'GET',
:path => 'tenants'
)
end
end
end
end
end

View file

@ -0,0 +1,15 @@
module Fog
module Rackspace
class Identity
class Real
def list_user_roles(user_id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "users/#{user_id}/roles"
)
end
end
end
end
end

View file

@ -0,0 +1,15 @@
module Fog
module Rackspace
class Identity
class Real
def list_users()
request(
:expects => [200, 203],
:method => 'GET',
:path => 'users'
)
end
end
end
end
end

View file

@ -0,0 +1,24 @@
module Fog
module Rackspace
class Identity
class Real
def update_user(user_id, username, email, enabled, options = {})
data = {
'user' => {
'username' => username,
'email' => email,
'enabled' => enabled
}
}
request(
:body => Fog::JSON.encode(data),
:expects => [200, 203],
:method => 'POST',
:path => "users/#{user_id}"
)
end
end
end
end
end

View file

@ -0,0 +1,21 @@
Shindo.tests('Fog::Rackspace::Identity | tenants', ['rackspace']) do
pending if Fog.mock?
TENANTS_FORMATS = {
'tenants' => [{
'id' => String,
'name' => String,
'description' => Fog::Nullable::String,
'enabled' => Fog::Nullable::Boolean
}]
}
service = Fog::Rackspace::Identity.new
tests('success') do
tests('#list_tenants').formats(TENANTS_FORMATS) do
service.list_tenants().body
end
end
end

View file

@ -0,0 +1,63 @@
Shindo.tests('Fog::Rackspace::Identity | tokens', ['rackspace']) do
pending if Fog.mock?
ROLE_FORMAT = {
'id' => String,
'name' => String,
'description' => String
}
ENDPOINT_FORMAT = {
'tenantId' => String,
'publicURL' => Fog::Nullable::String,
'internalURL' => Fog::Nullable::String,
'region' => Fog::Nullable::String,
'versionId' => Fog::Nullable::String,
'versionInfo' => Fog::Nullable::String,
'versionList' => Fog::Nullable::String
}
SERVICE_FORMAT = {
'name' => String,
'type' => String,
'endpoints' => [ENDPOINT_FORMAT]
}
ACCESS_FORMAT = {
'access' => {
'token' => {
'id' => String,
'expires' => String,
'tenant' => {
'id' => String,
'name' => String
}
},
'user' => {
'id' => String,
'name' => String,
'roles' => [ROLE_FORMAT]
},
'serviceCatalog' => [SERVICE_FORMAT]
}
}
service = Fog::Rackspace::Identity.new
tests('success') do
credentials = Fog.credentials
username = credentials[:rackspace_username]
api_key = credentials[:rackspace_api_key]
tests('#create_token').formats(ACCESS_FORMAT) do
service.create_token(username, api_key).body
end
end
tests('failure') do
tests('#create_token(invalidname, invalidkey').raises(Excon::Errors::HTTPStatusError) do
service.create_token('baduser', 'badkey')
end
end
end

View file

@ -0,0 +1,107 @@
Shindo.tests('Fog::Rackspace::Identity | users', ['rackspace']) do
pending if Fog.mock?
USER_INFO = {
'id' => String,
'username' => String,
'email' => Fog::Nullable::String,
'enabled' => Fog::Boolean,
'OS-KSADM:password' => Fog::Nullable::String,
'created' => Fog::Nullable::String,
'updated' => Fog::Nullable::String
}
USER_FORMAT = {
'user' => USER_INFO
}
USERS_FORMAT = {
'users' => [USER_INFO]
}
CREDENTIAL_FORMAT = {
'RAX-KSKEY:apiKeyCredentials' => {
'username' => String,
'apiKey' => String
}
}
CREDENTIALS_FORMAT = {
'credentials' => [CREDENTIAL_FORMAT]
}
ROLES_FORMAT = {
'roles' => [{
'id' => String,
'name' => String,
'description' => String
}]
}
service = Fog::Rackspace::Identity.new
id = nil
username = 'fog_user'
email = 'fog_user@example.com'
enabled = true
password = 'Fog_password1'
tests('success') do
tests('#create_user').formats(USER_FORMAT) do
data = service.create_user(username, email, enabled).body
id = data['user']['id']
data
end
tests('#delete_user').succeeds do
service.delete_user(id)
end
tests('#create_user with password').succeeds do
data = service.create_user(username, email, enabled, :password => password ).body
id = data['user']['id']
data
end
tests('#get_user_by_name').formats(USER_FORMAT) do
data = service.get_user_by_name(username).body
id = data['user']['id']
data
end
tests('#get_user_by_id').formats(USER_FORMAT) do
service.get_user_by_id(id).body
end
tests('#list_users').formats(USERS_FORMAT) do
service.list_users().body
end
tests('#update_user').formats(USER_FORMAT) do
service.update_user(id, username, 'updated_user@example.com', enabled).body
end
tests('#update_user with password').succeeds do
service.update_user(id, username, email, enabled, :password => password).body
end
tests('#list_user_roles').formats(ROLES_FORMAT) do
service.list_user_roles(id).body
end
service.delete_user(id)
# Users are only authorized to request their own credentials,
# so perform credential tests with the ID of the user running tests.
credential_username = Fog.credentials[:rackspace_username]
credential_id = service.get_user_by_name(credential_username).body['user']['id']
tests('#list_credentials').formats(CREDENTIALS_FORMAT) do
service.list_credentials(credential_id).body
end
tests('#get_credentials').formats(CREDENTIAL_FORMAT) do
service.get_credentials(credential_id).body
end
end
end