diff --git a/lib/fog/aws/requests/iam/create_access_key.rb b/lib/fog/aws/requests/iam/create_access_key.rb index 0716f9339..3eb112e35 100644 --- a/lib/fog/aws/requests/iam/create_access_key.rb +++ b/lib/fog/aws/requests/iam/create_access_key.rb @@ -37,18 +37,20 @@ module Fog #FIXME: Not 100% correct as AWS will use the signing credentials when there is no 'UserName' in the options hash # Also doesn't raise an error when there are too many keys user_name = options['UserName'] - if data[:users].keys.include? user_name - response = Excon::Response.new - key = { 'SecretAccessKey' => '123', + if data[:users].has_key? user_name + key = { 'SecretAccessKey' => Fog::Mock.random_base64(40), 'Status' => 'Active', - 'AccessKeyId' => '456', + 'AccessKeyId' => Fog::AWS::Mock.key_id(20), 'UserName' => user_name } + data[:users][user_name][:access_keys] << key - response.status = 200 - response.body = { 'AccessKey' => key, - 'RequestId' => Fog::AWS::Mock.request_id } - response + + Excon::Response.new.tap do |response| + response.status = 200 + response.body = { 'AccessKey' => key, + 'RequestId' => Fog::AWS::Mock.request_id } + end else raise Fog::AWS::IAM::NotFound.new('The user with name booboboboob cannot be found.') end diff --git a/lib/fog/aws/requests/iam/create_user.rb b/lib/fog/aws/requests/iam/create_user.rb index 16467ebda..97f44a6ae 100644 --- a/lib/fog/aws/requests/iam/create_user.rb +++ b/lib/fog/aws/requests/iam/create_user.rb @@ -37,23 +37,21 @@ module Fog class Mock def create_user(user_name, path='/') - if data[:users].keys.include? user_name + if data[:users].has_key? user_name raise Fog::AWS::IAM::EntityAlreadyExists.new "User with name #{user_name} already exists." else - response = Excon::Response.new data[:users][user_name][:path] = path - user = { - "UserId" => data[:users][user_name][:user_id], - "Path" => data[:users][user_name][:path], - "UserName" => user_name, - "Arn" => data[:users][user_name][:arn] - } - - response.status = 200 - response.body = { 'User' => user, - 'RequestId' => Fog::AWS::Mock.request_id - } - response + Excon::Response.new.tap do |response| + response.status = 200 + response.body = { 'User' => { + "UserId" => data[:users][user_name][:user_id], + "Path" => path, + "UserName" => user_name, + "Arn" => data[:users][user_name][:arn] + }, + 'RequestId' => Fog::AWS::Mock.request_id + } + end end end end diff --git a/lib/fog/aws/requests/iam/delete_user_policy.rb b/lib/fog/aws/requests/iam/delete_user_policy.rb index 2fbbfad62..dd022e24d 100644 --- a/lib/fog/aws/requests/iam/delete_user_policy.rb +++ b/lib/fog/aws/requests/iam/delete_user_policy.rb @@ -33,12 +33,12 @@ module Fog class Mock def delete_user_policy(user_name, policy_name) - if data[:users].keys.include?(user_name) && data[:users][user_name][:policies].keys.include?(policy_name) + if data[:users].has_key?(user_name) && data[:users][user_name][:policies].has_key?(policy_name) data[:users][user_name][:policies].delete policy_name - response = Excon::Response.new - response.body = { 'RequestId' => Fog::AWS::Mock.request_id } - response.status = 200 - response + Excon::Response.new.tap do |response| + response.body = { 'RequestId' => Fog::AWS::Mock.request_id } + response.status = 200 + end else raise Fog::AWS::IAM::NotFound.new("The user policy with name #{policy_name} cannot be found.") end diff --git a/lib/fog/aws/requests/iam/list_users.rb b/lib/fog/aws/requests/iam/list_users.rb index 5b88b80d3..1c18d1b0d 100644 --- a/lib/fog/aws/requests/iam/list_users.rb +++ b/lib/fog/aws/requests/iam/list_users.rb @@ -41,17 +41,17 @@ module Fog class Mock def list_users(options = {}) #FIXME: none of the options are currently supported - response = Excon::Response.new - response.body = {'Users' => data[:users].map do |user, data| - { 'UserId' => data[:user_id], - 'Path' => data[:path], - 'UserName' => user, - 'Arn' => data[:arn] } - end, - 'IsTruncated' => false, - 'RequestId' => Fog::AWS::Mock.request_id } - response.status = 200 - response + Excon::Response.new.tap do |response| + response.body = {'Users' => data[:users].map do |user, data| + { 'UserId' => data[:user_id], + 'Path' => data[:path], + 'UserName' => user, + 'Arn' => data[:arn] } + end, + 'IsTruncated' => false, + 'RequestId' => Fog::AWS::Mock.request_id } + response.status = 200 + end end end end diff --git a/lib/fog/aws/requests/iam/put_user_policy.rb b/lib/fog/aws/requests/iam/put_user_policy.rb index f5a6aa4f3..76d98b2e7 100644 --- a/lib/fog/aws/requests/iam/put_user_policy.rb +++ b/lib/fog/aws/requests/iam/put_user_policy.rb @@ -37,12 +37,13 @@ module Fog #FIXME: You can't actually use the credentials for anything elsewhere in Fog #FIXME: Doesn't do any validation on the policy def put_user_policy(user_name, policy_name, policy_document) - if data[:users].keys.include? user_name - response = Excon::Response.new - response.body = { 'RequestId' => Fog::AWS::Mock.request_id } - response.status = 200 + if data[:users].has_key? user_name data[:users][user_name][:policies][policy_name] = policy_document - response + + Excon::Response.new.tap do |response| + response.body = { 'RequestId' => Fog::AWS::Mock.request_id } + response.status = 200 + end else raise Fog::AWS::IAM::NotFound.new("The user with name #{user_name} cannot be found.") end