From 4c8ca82b6cadceb48ccf118c0b8bb8c7b44922f7 Mon Sep 17 00:00:00 2001 From: Edward Muller Date: Mon, 6 Feb 2012 17:55:44 -0800 Subject: [PATCH] Additional mocks --- lib/fog/aws/requests/iam/add_user_to_group.rb | 24 +++++++++++++++++ lib/fog/aws/requests/iam/create_group.rb | 21 +++++++++++++++ lib/fog/aws/requests/iam/delete_access_key.rb | 17 ++++++++++++ lib/fog/aws/requests/iam/delete_group.rb | 19 +++++++++++++ lib/fog/aws/requests/iam/delete_user.rb | 17 ++++++++++++ lib/fog/aws/requests/iam/list_access_keys.rb | 21 +++++++++++++++ lib/fog/aws/requests/iam/list_groups.rb | 18 +++++++++++++ .../aws/requests/iam/list_groups_for_user.rb | 24 +++++++++++++++++ .../aws/requests/iam/list_user_policies.rb | 18 +++++++++++++ .../requests/iam/remove_user_from_group.rb | 19 +++++++++++++ tests/aws/requests/iam/access_key_tests.rb | 2 -- tests/aws/requests/iam/group_tests.rb | 5 +--- tests/aws/requests/iam/user_policy_tests.rb | 7 ++--- tests/aws/requests/iam/user_tests.rb | 27 ++++++++++++------- 14 files changed, 219 insertions(+), 20 deletions(-) diff --git a/lib/fog/aws/requests/iam/add_user_to_group.rb b/lib/fog/aws/requests/iam/add_user_to_group.rb index 35b5e869f..fe92a9463 100644 --- a/lib/fog/aws/requests/iam/add_user_to_group.rb +++ b/lib/fog/aws/requests/iam/add_user_to_group.rb @@ -29,6 +29,30 @@ module Fog end end + + class Mock + + def add_user_to_group(group_name, user_name) + if data[:groups].has_key? group_name + if data[:users].has_key? user_name + + unless data[:groups][group_name][:members].include?(user_name) + data[:groups][group_name][:members] << user_name + end + + Excon::Response.new.tap do |response| + response.status = 200 + response.body = { 'RequestId' => Fog::AWS::Mock.request_id } + end + else + raise Fog::AWS::IAM::NotFound.new("The user with name #{user_name} cannot be found.") + end + else + raise Fog::AWS::IAM::NotFound.new("The group with name #{group_name} cannot be found.") + end + end + + end end end end diff --git a/lib/fog/aws/requests/iam/create_group.rb b/lib/fog/aws/requests/iam/create_group.rb index 6049ee50d..4e941e462 100644 --- a/lib/fog/aws/requests/iam/create_group.rb +++ b/lib/fog/aws/requests/iam/create_group.rb @@ -34,6 +34,27 @@ module Fog end end + + class Mock + + def create_group(group_name, path = '/') + if data[:groups].has_key? group_name + raise Fog::AWS::IAM::EntityAlreadyExists.new("Group with name #{group_name} already exists.") + else + data[:groups][group_name][:path] = path + Excon::Response.new.tap do |response| + response.body = { 'Group' => { + 'GroupId' => data[:groups][group_name][:group_id], + 'GroupName' => group_name, + 'Path' => path, + 'Arn' => data[:groups][group_name][:arn] }, + 'RequestId' => Fog::AWS::Mock.request_id } + response.status = 200 + end + end + + end + end end end end diff --git a/lib/fog/aws/requests/iam/delete_access_key.rb b/lib/fog/aws/requests/iam/delete_access_key.rb index 7ecb5042e..b5bd6157c 100644 --- a/lib/fog/aws/requests/iam/delete_access_key.rb +++ b/lib/fog/aws/requests/iam/delete_access_key.rb @@ -29,6 +29,23 @@ module Fog end end + + class Mock + + def delete_access_key(access_key_id, options = {}) + user_name = options['UserName'] + if user_name && data[:users].has_key?(user_name) && data[:users][user_name][:access_keys].any? { |akey| akey['AccessKeyId'] == access_key_id } + data[:users][user_name][:access_keys].delete_if { |akey| akey['AccessKeyId'] == access_key_id } + 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 Access Key with id #{access_key_id} cannot be found.") + end + end + + end end end end diff --git a/lib/fog/aws/requests/iam/delete_group.rb b/lib/fog/aws/requests/iam/delete_group.rb index 7f7957d5a..2d9b40868 100644 --- a/lib/fog/aws/requests/iam/delete_group.rb +++ b/lib/fog/aws/requests/iam/delete_group.rb @@ -27,6 +27,25 @@ module Fog end end + + class Mock + + def delete_group(group_name) + if data[:groups].has_key? group_name + if data[:groups][group_name][:members].empty? + data[:groups].delete group_name + Excon::Response.new.tap do |response| + response.status = 200 + response.body = { 'RequestId' => Fog::AWS::Mock.request_id } + end + else + raise Fog::AWS::IAM::Error.new("DeleteConflict => Cannot delete entity, must delete users in group first.") + end + else + raise Fog::AWS::IAM::NotFound.new("The group with name #{group_name} cannot be found.") + end + end + end end end end diff --git a/lib/fog/aws/requests/iam/delete_user.rb b/lib/fog/aws/requests/iam/delete_user.rb index 6dc7cd9fd..26e2d15b0 100644 --- a/lib/fog/aws/requests/iam/delete_user.rb +++ b/lib/fog/aws/requests/iam/delete_user.rb @@ -27,6 +27,23 @@ module Fog end end + + class Mock + + def delete_user(user_name) + if data[:users].has_key? user_name + data[:users].delete user_name + 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 + + end + + end end end end diff --git a/lib/fog/aws/requests/iam/list_access_keys.rb b/lib/fog/aws/requests/iam/list_access_keys.rb index 8ea3d2737..e1872cf75 100644 --- a/lib/fog/aws/requests/iam/list_access_keys.rb +++ b/lib/fog/aws/requests/iam/list_access_keys.rb @@ -35,6 +35,27 @@ module Fog end end + + class Mock + + def list_access_keys(options = {}) + #FIXME: Doesn't do anything with options, aside from UserName + user = options['UserName'] + + if data[:users].has_key? user + Excon::Response.new.tap do |response| + response.body = { 'AccessKeys' => data[:users][user][:access_keys].map do |akey| + {'Status' => akey['Status'], 'AccessKeyId' => akey['AccessKeyId']} + end, + 'IsTruncated' => false, + 'RequestId' => Fog::AWS::Mock.request_id } + response.status = 200 + end + else + Fog::AWS::IAM::NotFound.new("The user with name #{user} cannot be found.") + end + end + end end end end diff --git a/lib/fog/aws/requests/iam/list_groups.rb b/lib/fog/aws/requests/iam/list_groups.rb index 7a90ca9d9..3936ed16d 100644 --- a/lib/fog/aws/requests/iam/list_groups.rb +++ b/lib/fog/aws/requests/iam/list_groups.rb @@ -37,6 +37,24 @@ module Fog end end + + class Mock + + def list_groups(options = {} ) + #FIXME: Doesn't observe options + Excon::Response.new.tap do |response| + response.status = 200 + response.body = { 'Groups' => data[:groups].map do |name, group| + { 'GroupId' => group[:group_id], + 'GroupName' => name, + 'Path' => group[:path], + 'Arn' => group[:arn] } + end, + 'IsTruncated' => false, + 'RequestId' => Fog::AWS::Mock.request_id } + end + end + end end end end diff --git a/lib/fog/aws/requests/iam/list_groups_for_user.rb b/lib/fog/aws/requests/iam/list_groups_for_user.rb index 0826221af..42f54eacb 100644 --- a/lib/fog/aws/requests/iam/list_groups_for_user.rb +++ b/lib/fog/aws/requests/iam/list_groups_for_user.rb @@ -38,6 +38,30 @@ module Fog end end + + class Mock + def list_groups_for_user(user_name, options = {}) + #FIXME: Does not consider options + if data[:users].has_key? user_name + Excon::Response.new.tap do |response| + response.status = 200 + response.body = { 'GroupsForUser' => data[:groups].select do |name, group| + group[:members].include? user_name + end.map do |name, group| + { 'GroupId' => group[:group_id], + 'GroupName' => name, + 'Path' => group[:path], + 'Arn' => group[:arn] } + end, + 'IsTruncated' => false, + 'RequestId' => Fog::AWS::Mock.request_id + } + end + else + raise Fog::AWS::IAM::NotFound.new("The user with name #{user_name} cannot be found.") + end + end + end end end end diff --git a/lib/fog/aws/requests/iam/list_user_policies.rb b/lib/fog/aws/requests/iam/list_user_policies.rb index 048eea8cf..45b974a97 100644 --- a/lib/fog/aws/requests/iam/list_user_policies.rb +++ b/lib/fog/aws/requests/iam/list_user_policies.rb @@ -34,6 +34,24 @@ module Fog end end + + class Mock + + def list_user_policies(user_name, options = {}) + #FIXME: doesn't use options atm + if data[:users].has_key? user_name + Excon::Response.new.tap do |response| + response.body = { 'PolicyNames' => data[:users][user_name][:policies].keys, + 'IsTruncated' => false, + '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 + end + + end end end end diff --git a/lib/fog/aws/requests/iam/remove_user_from_group.rb b/lib/fog/aws/requests/iam/remove_user_from_group.rb index 3a8917632..16e847a13 100644 --- a/lib/fog/aws/requests/iam/remove_user_from_group.rb +++ b/lib/fog/aws/requests/iam/remove_user_from_group.rb @@ -29,6 +29,25 @@ module Fog end end + + class Mock + + def remove_user_from_group(group_name, user_name) + if data[:groups].has_key? group_name + if data[:users].has_key? user_name + data[:groups][group_name][:members].delete_if { |item| item == user_name } + Excon::Response.new.tap do |response| + response.status = 200 + response.body = { 'RequestId' => Fog::AWS::Mock.request_id } + end + else + raise Fog::AWS::IAM::NotFound.new("The user with name #{user_name} cannot be found.") + end + else + raise Fog::AWS::IAM::NotFound.new("The group with name #{group_name} cannot be found.") + end + end + end end end end diff --git a/tests/aws/requests/iam/access_key_tests.rb b/tests/aws/requests/iam/access_key_tests.rb index 958c71c78..931bd0dda 100644 --- a/tests/aws/requests/iam/access_key_tests.rb +++ b/tests/aws/requests/iam/access_key_tests.rb @@ -30,7 +30,6 @@ Shindo.tests('AWS::IAM | access key requests', ['aws']) do } tests("#list_access_keys('Username' => 'fog_access_key_tests')").formats(@access_keys_format) do - pending if Fog.mocking? Fog::AWS[:iam].list_access_keys('UserName' => 'fog_access_key_tests').body end @@ -40,7 +39,6 @@ Shindo.tests('AWS::IAM | access key requests', ['aws']) do end tests("#delete_access_key('#{@access_key_id}', 'UserName' => 'fog_access_key_tests)").formats(AWS::IAM::Formats::BASIC) do - pending if Fog.mocking? Fog::AWS[:iam].delete_access_key(@access_key_id, 'UserName' => 'fog_access_key_tests').body end diff --git a/tests/aws/requests/iam/group_tests.rb b/tests/aws/requests/iam/group_tests.rb index 6947b0de2..ee4f9520c 100644 --- a/tests/aws/requests/iam/group_tests.rb +++ b/tests/aws/requests/iam/group_tests.rb @@ -13,7 +13,6 @@ Shindo.tests('AWS::IAM | group requests', ['aws']) do } tests("#create_group('fog_group')").formats(@group_format) do - pending if Fog.mocking? Fog::AWS[:iam].create_group('fog_group').body end @@ -29,12 +28,10 @@ Shindo.tests('AWS::IAM | group requests', ['aws']) do } tests("#list_groups").formats(@groups_format) do - pending if Fog.mocking? Fog::AWS[:iam].list_groups.body end tests("#delete_group('fog_group')").formats(AWS::IAM::Formats::BASIC) do - pending if Fog.mocking? Fog::AWS[:iam].delete_group('fog_group').body end @@ -44,4 +41,4 @@ Shindo.tests('AWS::IAM | group requests', ['aws']) do test('failing conditions') end -end \ No newline at end of file +end diff --git a/tests/aws/requests/iam/user_policy_tests.rb b/tests/aws/requests/iam/user_policy_tests.rb index d8f00e844..fa63848ee 100644 --- a/tests/aws/requests/iam/user_policy_tests.rb +++ b/tests/aws/requests/iam/user_policy_tests.rb @@ -16,8 +16,7 @@ Shindo.tests('AWS::IAM | user policy requests', ['aws']) do 'RequestId' => String } - tests("list_user_policies('fog_user_policy_tests')").formats(@user_policies_format) do - pending if Fog.mocking? + tests("#list_user_policies('fog_user_policy_tests')").formats(@user_policies_format) do Fog::AWS[:iam].list_user_policies('fog_user_policy_tests').body end @@ -31,8 +30,6 @@ Shindo.tests('AWS::IAM | user policy requests', ['aws']) do test('failing conditions') end - unless Fog.mocking? - Fog::AWS[:iam].delete_user('fog_user_policy_tests') - end + Fog::AWS[:iam].delete_user('fog_user_policy_tests') end diff --git a/tests/aws/requests/iam/user_tests.rb b/tests/aws/requests/iam/user_tests.rb index 7412a6d6f..5f102a917 100644 --- a/tests/aws/requests/iam/user_tests.rb +++ b/tests/aws/requests/iam/user_tests.rb @@ -1,8 +1,6 @@ Shindo.tests('AWS::IAM | user requests', ['aws']) do - unless Fog.mocking? - Fog::AWS[:iam].create_group('fog_user_tests') - end + Fog::AWS[:iam].create_group('fog_user_tests') tests('success') do @@ -36,28 +34,39 @@ Shindo.tests('AWS::IAM | user requests', ['aws']) do end tests("#add_user_to_group('fog_user_tests', 'fog_user')").formats(AWS::IAM::Formats::BASIC) do - pending if Fog.mocking? Fog::AWS[:iam].add_user_to_group('fog_user_tests', 'fog_user').body end + @groups_format = { + 'GroupsForUser' => [{ + 'Arn' => String, + 'GroupId' => String, + 'GroupName' => String, + 'Path' => String + }], + 'IsTruncated' => Fog::Boolean, + 'RequestId' => String + } + + tests("#list_groups_for_user('fog_user')").formats(@groups_format) do + Fog::AWS[:iam].list_groups_for_user('fog_user').body + end + tests("#remove_user_from_group('fog_user_tests', 'fog_user')").formats(AWS::IAM::Formats::BASIC) do - pending if Fog.mocking? Fog::AWS[:iam].remove_user_from_group('fog_user_tests', 'fog_user').body end tests("#delete_user('fog_user')").formats(AWS::IAM::Formats::BASIC) do - pending if Fog.mocking? Fog::AWS[:iam].delete_user('fog_user').body end + end tests('failure') do test('failing conditions') end - unless Fog.mocking? - Fog::AWS[:iam].delete_group('fog_user_tests') - end + Fog::AWS[:iam].delete_group('fog_user_tests') end