Skip to content

Commit

Permalink
Merge pull request #49 from tobowers/dev/acl-cleanup
Browse files Browse the repository at this point in the history
Clean up return value and metadata state management for set_*_acl methods
  • Loading branch information
minter committed Aug 10, 2011
2 parents ea9aa70 + d3d718a commit 4d634be
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 21 deletions.
6 changes: 5 additions & 1 deletion lib/cloudfiles/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,18 @@ def make_public(options = {:ttl => 86400})

# Only to be used with openstack swift
def set_write_acl(write_string)
refresh
headers = {"X-Container-Write" => write_string}
post_with_headers(headers)
true
end

# Only to be used with openstack swift
def set_read_acl(read_string)
refresh
headers = {"X-Container-Read" => read_string}
post_with_headers(headers)
true
end

def post_with_headers(headers = {})
Expand All @@ -363,7 +367,7 @@ def post_with_headers(headers = {})
else
response = self.connection.storage_request("POST", escaped_name, headers)
end
raise CloudFiles::Exception::NoSuchContainer, "Container #{@name} does not exist" unless (response.code =~ /^20/)
raise CloudFiles::Exception::NoSuchContainer, "Container #{@name} does not exist (response code: #{response.code})" unless (response.code =~ /^20/)
response
end

Expand Down
77 changes: 57 additions & 20 deletions test/cloudfiles_container_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,68 @@ def test_empty_is_true
@container = CloudFiles::Container.new(connection, 'test_container')
assert_equal @container.empty?, true
end

def test_read_acl_is_set_from_headers
connection = stub()
response = {

def build_acl_test_state(opts={})
@connection = stub(:storagehost => 'test.storage.example', :storagepath => '/dummy/path', :storageport => 443, :storagescheme => 'https', :cdn_available? => false)

@response = {
'x-container-bytes-used' => '0',
'x-container-object-count' => '0',
'x-container-read' => ".r:*"
}
response.stubs(:code).returns('204')
connection.stubs(:storage_request => response)
@container = CloudFiles::Container.new(connection, 'test_container')
assert_equal response["x-container-read"], @container.read_acl
}.merge(opts.fetch(:response, {}))

@response.stubs(:code).returns(opts.fetch(:code, '204'))
@connection.stubs(:storage_request => @response)
@container = CloudFiles::Container.new(@connection, 'test_container')
end

def test_read_acl_is_set_from_headers
build_acl_test_state :response => { 'x-container-read' => '.r:*' }, :code => '204'
assert_equal @response["x-container-read"], @container.read_acl
end

def test_set_read_acl_fails
build_acl_test_state

response = stub(:code => '999')

@connection.stubs(:storage_request => response)

assert_raises(CloudFiles::Exception::NoSuchContainer) do
@container.set_read_acl('.r:*')
end
end

def test_set_read_acl_succeeds
build_acl_test_state

@connection.stubs(:storage_request => stub(:code => '204'))

assert_equal @container.set_read_acl('.r:*'), true
end

def test_write_acl_is_set_from_headers
connection = stub()
response = {
'x-container-bytes-used' => '0',
'x-container-object-count' => '0',
'x-container-write' => ".r:*"
}
response.stubs(:code).returns('204')
connection.stubs(:storage_request => response)
@container = CloudFiles::Container.new(connection, 'test_container')
assert_equal response["x-container-write"], @container.write_acl
build_acl_test_state :response => { 'x-container-write' => '.r:*' }, :code => '204'
assert_equal @response["x-container-write"], @container.write_acl
end

def test_set_write_acl_fails
build_acl_test_state

response = stub(:code => '999')

@connection.stubs(:storage_request => response)

assert_raises(CloudFiles::Exception::NoSuchContainer) do
@container.set_write_acl('.r:*')
end
end

def test_set_write_acl_succeeds
build_acl_test_state

@connection.stubs(:storage_request => stub(:code => '204'))

assert_equal @container.set_write_acl('.r:*'), true
end

def test_log_retention_is_true
Expand Down

0 comments on commit 4d634be

Please sign in to comment.