mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Refactored specs, mocks, etc.
This commit is contained in:
parent
4aeba71e09
commit
0c53ca992e
8 changed files with 152 additions and 212 deletions
45
lib/fog/aws/requests/storage/hash_to_acl.rb
Normal file
45
lib/fog/aws/requests/storage/hash_to_acl.rb
Normal file
|
@ -0,0 +1,45 @@
|
|||
module Fog
|
||||
module Storage
|
||||
class AWS
|
||||
private
|
||||
def self.hash_to_acl(acl)
|
||||
data =
|
||||
<<-DATA
|
||||
<AccessControlPolicy>
|
||||
<Owner>
|
||||
<ID>#{acl['Owner']['ID']}</ID>
|
||||
<DisplayName>#{acl['Owner']['DisplayName']}</DisplayName>
|
||||
</Owner>
|
||||
<AccessControlList>
|
||||
DATA
|
||||
|
||||
acl['AccessControlList'].each do |grant|
|
||||
data << " <Grant>\n"
|
||||
type = case grant['Grantee'].keys.sort
|
||||
when ['DisplayName', 'ID']
|
||||
'CanonicalUser'
|
||||
when ['EmailAddress']
|
||||
'AmazonCustomerByEmail'
|
||||
when ['URI']
|
||||
'Group'
|
||||
end
|
||||
data << " <Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"#{type}\">\n"
|
||||
for key, value in grant['Grantee']
|
||||
data << " <#{key}>#{value}</#{key}>\n"
|
||||
end
|
||||
data << " </Grantee>\n"
|
||||
data << " <Permission>#{grant['Permission']}</Permission>\n"
|
||||
data << " </Grant>\n"
|
||||
end
|
||||
|
||||
data <<
|
||||
<<-DATA
|
||||
</AccessControlList>
|
||||
</AccessControlPolicy>
|
||||
DATA
|
||||
data
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -3,6 +3,8 @@ module Fog
|
|||
class AWS
|
||||
class Real
|
||||
|
||||
require 'fog/aws/requests/storage/hash_to_acl'
|
||||
|
||||
# Change access control list for an S3 bucket
|
||||
#
|
||||
# ==== Parameters
|
||||
|
@ -30,40 +32,7 @@ module Fog
|
|||
headers = {}
|
||||
|
||||
if acl.is_a?(Hash)
|
||||
data =
|
||||
<<-DATA
|
||||
<AccessControlPolicy>
|
||||
<Owner>
|
||||
<ID>#{acl['Owner']['ID']}</ID>
|
||||
<DisplayName>#{acl['Owner']['DisplayName']}</DisplayName>
|
||||
</Owner>
|
||||
<AccessControlList>
|
||||
DATA
|
||||
|
||||
acl['AccessControlList'].each do |grant|
|
||||
data << " <Grant>\n"
|
||||
type = case grant['Grantee'].keys.sort
|
||||
when ['DisplayName', 'ID']
|
||||
'CanonicalUser'
|
||||
when ['EmailAddress']
|
||||
'AmazonCustomerByEmail'
|
||||
when ['URI']
|
||||
'Group'
|
||||
end
|
||||
data << " <Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"#{type}\">\n"
|
||||
for key, value in grant['Grantee']
|
||||
data << " <#{key}>#{value}</#{key}>\n"
|
||||
end
|
||||
data << " </Grantee>\n"
|
||||
data << " <Permission>#{grant['Permission']}</Permission>\n"
|
||||
data << " </Grant>\n"
|
||||
end
|
||||
|
||||
data <<
|
||||
<<-DATA
|
||||
</AccessControlList>
|
||||
</AccessControlPolicy>
|
||||
DATA
|
||||
data = Fog::Storage::AWS.hash_to_acl(acl)
|
||||
else
|
||||
if !['private', 'public-read', 'public-read-write', 'authenticated-read'].include?(acl)
|
||||
raise Excon::Errors::BadRequest.new('invalid x-amz-acl')
|
||||
|
@ -84,8 +53,21 @@ DATA
|
|||
:query => {'acl' => nil}
|
||||
})
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
def put_bucket_acl(bucket_name, acl)
|
||||
if acl.is_a?(Hash)
|
||||
self.data[:acls][:bucket][bucket_name] = Fog::Storage::AWS.hash_to_acl(acl)
|
||||
else
|
||||
if !['private', 'public-read', 'public-read-write', 'authenticated-read'].include?(acl)
|
||||
raise Excon::Errors::BadRequest.new('invalid x-amz-acl')
|
||||
end
|
||||
self.data[:acls][:bucket][bucket_name] = acl
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,6 +3,8 @@ module Fog
|
|||
class AWS
|
||||
class Real
|
||||
|
||||
require 'fog/aws/requests/storage/hash_to_acl'
|
||||
|
||||
# Change access control list for an S3 object
|
||||
#
|
||||
# ==== Parameters
|
||||
|
@ -38,40 +40,7 @@ module Fog
|
|||
headers = {}
|
||||
|
||||
if acl.is_a?(Hash)
|
||||
data =
|
||||
<<-DATA
|
||||
<AccessControlPolicy>
|
||||
<Owner>
|
||||
<ID>#{acl['Owner']['ID']}</ID>
|
||||
<DisplayName>#{acl['Owner']['DisplayName']}</DisplayName>
|
||||
</Owner>
|
||||
<AccessControlList>
|
||||
DATA
|
||||
|
||||
acl['AccessControlList'].each do |grant|
|
||||
data << " <Grant>\n"
|
||||
type = case grant['Grantee'].keys.sort
|
||||
when ['DisplayName', 'ID']
|
||||
'CanonicalUser'
|
||||
when ['EmailAddress']
|
||||
'AmazonCustomerByEmail'
|
||||
when ['URI']
|
||||
'Group'
|
||||
end
|
||||
data << " <Grantee xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"#{type}\">\n"
|
||||
for key, value in grant['Grantee']
|
||||
data << " <#{key}>#{value}</#{key}>\n"
|
||||
end
|
||||
data << " </Grantee>\n"
|
||||
data << " <Permission>#{grant['Permission']}</Permission>\n"
|
||||
data << " </Grant>\n"
|
||||
end
|
||||
|
||||
data <<
|
||||
<<-DATA
|
||||
</AccessControlList>
|
||||
</AccessControlPolicy>
|
||||
DATA
|
||||
data = Fog::Storage::AWS.hash_to_acl(acl)
|
||||
else
|
||||
if !['private', 'public-read', 'public-read-write', 'authenticated-read'].include?(acl)
|
||||
raise Excon::Errors::BadRequest.new('invalid x-amz-acl')
|
||||
|
@ -93,8 +62,21 @@ DATA
|
|||
:query => query
|
||||
})
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
def put_object_acl(bucket_name, object_name, acl, options = {})
|
||||
if acl.is_a?(Hash)
|
||||
self.data[:acls][:object][bucket_name][object_name] = Fog::Storage::AWS.hash_to_acl(acl)
|
||||
else
|
||||
if !['private', 'public-read', 'public-read-write', 'authenticated-read'].include?(acl)
|
||||
raise Excon::Errors::BadRequest.new('invalid x-amz-acl')
|
||||
end
|
||||
self.data[:acls][:object][bucket_name][object_name] = acl
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
def aws_storage
|
||||
@storage ||= begin
|
||||
storage = Fog::Storage::new({
|
||||
:provider => 'AWS',
|
||||
:aws_access_key_id => '',
|
||||
:aws_secret_access_key => ''})
|
||||
storage.stub(:request) { |hash| hash }
|
||||
storage
|
||||
end
|
||||
end
|
|
@ -1,66 +0,0 @@
|
|||
require 'spec_helper'
|
||||
require 'storage/aws/aws_spec_helper'
|
||||
|
||||
describe Fog::Storage::AWS do
|
||||
|
||||
before(:each) do
|
||||
pending("no support for Fog.mocking") if Fog.mocking?
|
||||
end
|
||||
|
||||
describe "canned ACL" do
|
||||
it "should raise an error with an invalid canned ACL" do
|
||||
lambda {
|
||||
aws_storage.put_bucket_acl('bucket', 'invalid')
|
||||
}.should raise_error(Excon::Errors::BadRequest, "invalid x-amz-acl")
|
||||
end
|
||||
it "should produce a request with an x-amz-acl header" do
|
||||
hash = aws_storage.put_bucket_acl('bucket', 'private')
|
||||
hash[:body].should == ""
|
||||
hash[:expects].should == 200
|
||||
hash[:headers]["x-amz-acl"].should == "private"
|
||||
hash[:headers]["Content-Type"].should == "application/json"
|
||||
hash[:host].should == "bucket.s3.amazonaws.com"
|
||||
hash[:method].should == "PUT"
|
||||
hash[:path].should be_nil
|
||||
hash[:query].should == { "acl" => nil }
|
||||
end
|
||||
end
|
||||
|
||||
describe "xml ACL" do
|
||||
it "sends valid ACL XML" do
|
||||
hash = aws_storage.put_bucket_acl('bucket', {
|
||||
'Owner' => { 'ID' => "8a6925ce4adf5f21c32aa379004fef", 'DisplayName' => "mtd@amazon.com" },
|
||||
'AccessControlList' => [
|
||||
{
|
||||
'Grantee' => { 'ID' => "8a6925ce4adf588a4532142d3f74dd8c71fa124b1ddee97f21c32aa379004fef", 'DisplayName' => "mtd@amazon.com" },
|
||||
'Permission' => "FULL_CONTROL"
|
||||
}
|
||||
]
|
||||
})
|
||||
hash[:body].should == <<-BODY
|
||||
<AccessControlPolicy>
|
||||
<Owner>
|
||||
<ID>8a6925ce4adf5f21c32aa379004fef</ID>
|
||||
<DisplayName>mtd@amazon.com</DisplayName>
|
||||
</Owner>
|
||||
<AccessControlList>
|
||||
<Grant>
|
||||
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
|
||||
<ID>8a6925ce4adf588a4532142d3f74dd8c71fa124b1ddee97f21c32aa379004fef</ID>
|
||||
<DisplayName>mtd@amazon.com</DisplayName>
|
||||
</Grantee>
|
||||
<Permission>FULL_CONTROL</Permission>
|
||||
</Grant>
|
||||
</AccessControlList>
|
||||
</AccessControlPolicy>
|
||||
BODY
|
||||
hash[:expects].should == 200
|
||||
hash[:headers]["Content-Type"].should == "application/json"
|
||||
hash[:host].should == "bucket.s3.amazonaws.com"
|
||||
hash[:method].should == "PUT"
|
||||
hash[:path].should be_nil
|
||||
hash[:query].should == { "acl" => nil }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -1,66 +0,0 @@
|
|||
require 'spec_helper'
|
||||
require 'storage/aws/aws_spec_helper'
|
||||
|
||||
describe Fog::Storage::AWS do
|
||||
|
||||
before(:each) do
|
||||
pending("no support for Fog.mocking") if Fog.mocking?
|
||||
end
|
||||
|
||||
describe "canned ACL" do
|
||||
it "should raise an error with an invalid canned ACL" do
|
||||
lambda {
|
||||
aws_storage.put_object_acl('bucket', 'object', 'invalid')
|
||||
}.should raise_error(Excon::Errors::BadRequest, "invalid x-amz-acl")
|
||||
end
|
||||
it "should produce a request with an x-amz-acl header" do
|
||||
hash = aws_storage.put_object_acl('bucket', 'object', 'private')
|
||||
hash[:body].should == ""
|
||||
hash[:expects].should == 200
|
||||
hash[:headers]["x-amz-acl"].should == "private"
|
||||
hash[:headers]["Content-Type"].should == "application/json"
|
||||
hash[:host].should == "bucket.s3.amazonaws.com"
|
||||
hash[:method].should == "PUT"
|
||||
hash[:path].should == "object"
|
||||
hash[:query].should == { "acl" => nil }
|
||||
end
|
||||
end
|
||||
|
||||
describe "xml ACL" do
|
||||
it "sends valid ACL XML" do
|
||||
hash = aws_storage.put_object_acl('bucket', 'object', {
|
||||
'Owner' => { 'ID' => "8a6925ce4adf5f21c32aa379004fef", 'DisplayName' => "mtd@amazon.com" },
|
||||
'AccessControlList' => [
|
||||
{
|
||||
'Grantee' => { 'ID' => "8a6925ce4adf588a4532142d3f74dd8c71fa124b1ddee97f21c32aa379004fef", 'DisplayName' => "mtd@amazon.com" },
|
||||
'Permission' => "FULL_CONTROL"
|
||||
}
|
||||
]
|
||||
})
|
||||
hash[:body].should == <<-BODY
|
||||
<AccessControlPolicy>
|
||||
<Owner>
|
||||
<ID>8a6925ce4adf5f21c32aa379004fef</ID>
|
||||
<DisplayName>mtd@amazon.com</DisplayName>
|
||||
</Owner>
|
||||
<AccessControlList>
|
||||
<Grant>
|
||||
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
|
||||
<ID>8a6925ce4adf588a4532142d3f74dd8c71fa124b1ddee97f21c32aa379004fef</ID>
|
||||
<DisplayName>mtd@amazon.com</DisplayName>
|
||||
</Grantee>
|
||||
<Permission>FULL_CONTROL</Permission>
|
||||
</Grant>
|
||||
</AccessControlList>
|
||||
</AccessControlPolicy>
|
||||
BODY
|
||||
hash[:expects].should == 200
|
||||
hash[:headers]["Content-Type"].should == "application/json"
|
||||
hash[:host].should == "bucket.s3.amazonaws.com"
|
||||
hash[:method].should == "PUT"
|
||||
hash[:path].should == "object"
|
||||
hash[:query].should == { "acl" => nil }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -128,6 +128,39 @@ Shindo.tests('Fog::Storage[:aws] | bucket requests', [:aws]) do
|
|||
Fog::Storage[:aws].delete_bucket('fogbuckettests')
|
||||
end
|
||||
|
||||
tests("#put_bucket_acl('fogbuckettests', 'private')").succeeds do
|
||||
Fog::Storage[:aws].put_bucket_acl('fogbuckettests', 'private')
|
||||
end
|
||||
|
||||
tests("#put_bucket_acl('fogbuckettests', hash)").returns(true) do
|
||||
Fog::Storage[:aws].put_bucket_acl('fogbuckettests', {
|
||||
'Owner' => { 'ID' => "8a6925ce4adf5f21c32aa379004fef", 'DisplayName' => "mtd@amazon.com" },
|
||||
'AccessControlList' => [
|
||||
{
|
||||
'Grantee' => { 'ID' => "8a6925ce4adf588a4532142d3f74dd8c71fa124b1ddee97f21c32aa379004fef", 'DisplayName' => "mtd@amazon.com" },
|
||||
'Permission' => "FULL_CONTROL"
|
||||
}
|
||||
]
|
||||
})
|
||||
Fog::Storage[:aws].get_bucket_acl('fogbuckettests').body == <<-BODY
|
||||
<AccessControlPolicy>
|
||||
<Owner>
|
||||
<ID>8a6925ce4adf5f21c32aa379004fef</ID>
|
||||
<DisplayName>mtd@amazon.com</DisplayName>
|
||||
</Owner>
|
||||
<AccessControlList>
|
||||
<Grant>
|
||||
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
|
||||
<ID>8a6925ce4adf588a4532142d3f74dd8c71fa124b1ddee97f21c32aa379004fef</ID>
|
||||
<DisplayName>mtd@amazon.com</DisplayName>
|
||||
</Grantee>
|
||||
<Permission>FULL_CONTROL</Permission>
|
||||
</Grant>
|
||||
</AccessControlList>
|
||||
</AccessControlPolicy>
|
||||
BODY
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
tests('failure') do
|
||||
|
@ -162,6 +195,10 @@ Shindo.tests('Fog::Storage[:aws] | bucket requests', [:aws]) do
|
|||
Fog::Storage[:aws].put_request_payment('fognonbucket', 'Requester')
|
||||
end
|
||||
|
||||
tests("#put_bucket_acl('fognonbucket', 'invalid')").raises(Excon::Errors::BadRequest) do
|
||||
Fog::Storage[:aws].put_bucket_acl('fognonbucket', 'invalid')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -30,6 +30,38 @@ Shindo.tests('AWS::Storage | object requests', ['aws']) do
|
|||
Fog::Storage[:aws].head_object(@directory.identity, 'fog_object')
|
||||
end
|
||||
|
||||
tests("#put_object_acl('#{@directory.identity}', 'fog_object', 'private')").succeeds do
|
||||
Fog::Storage[:aws].put_object_acl(@directory.identity, 'fog_object', 'private')
|
||||
end
|
||||
|
||||
tests("#put_object_acl('#{@directory.identity}', 'fog_object', hash)").returns(true) do
|
||||
Fog::Storage[:aws].put_object_acl(@directory.identity, 'fog_object', {
|
||||
'Owner' => { 'ID' => "8a6925ce4adf5f21c32aa379004fef", 'DisplayName' => "mtd@amazon.com" },
|
||||
'AccessControlList' => [
|
||||
{
|
||||
'Grantee' => { 'ID' => "8a6925ce4adf588a4532142d3f74dd8c71fa124b1ddee97f21c32aa379004fef", 'DisplayName' => "mtd@amazon.com" },
|
||||
'Permission' => "FULL_CONTROL"
|
||||
}
|
||||
]})
|
||||
Fog::Storage[:aws].get_object_acl(@directory.identity, 'fog_object').body == <<-BODY
|
||||
<AccessControlPolicy>
|
||||
<Owner>
|
||||
<ID>8a6925ce4adf5f21c32aa379004fef</ID>
|
||||
<DisplayName>mtd@amazon.com</DisplayName>
|
||||
</Owner>
|
||||
<AccessControlList>
|
||||
<Grant>
|
||||
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
|
||||
<ID>8a6925ce4adf588a4532142d3f74dd8c71fa124b1ddee97f21c32aa379004fef</ID>
|
||||
<DisplayName>mtd@amazon.com</DisplayName>
|
||||
</Grantee>
|
||||
<Permission>FULL_CONTROL</Permission>
|
||||
</Grant>
|
||||
</AccessControlList>
|
||||
</AccessControlPolicy>
|
||||
BODY
|
||||
end
|
||||
|
||||
tests("#delete_object('#{@directory.identity}', 'fog_object')").succeeds do
|
||||
Fog::Storage[:aws].delete_object(@directory.identity, 'fog_object')
|
||||
end
|
||||
|
@ -74,6 +106,10 @@ Shindo.tests('AWS::Storage | object requests', ['aws']) do
|
|||
Fog::Storage[:aws].delete_object('fognonbucket', 'fog_non_object')
|
||||
end
|
||||
|
||||
tests("#put_object_acl('#{@directory.identity}', 'fog_object', 'invalid')").raises(Excon::Errors::BadRequest) do
|
||||
Fog::Storage[:aws].put_object_acl('#{@directory.identity}', 'fog_object', 'invalid')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@directory.destroy
|
||||
|
|
Loading…
Add table
Reference in a new issue