spec/aws/s3_spec.rb in heirloom-0.8.1 vs spec/aws/s3_spec.rb in heirloom-0.8.2

- old
+ new

@@ -1,35 +1,147 @@ require 'spec_helper' describe Heirloom do before do + @directories_mock = mock 'directories' + @bucket_mock = mock 'bucket' + @logger_stub = stub 'logger', :debug => true, + :info => true, + :warn => true @config_mock = mock 'config' - @config_mock.should_receive(:access_key).and_return 'the-key' - @config_mock.should_receive(:secret_key).and_return 'the-secret' + @config_mock.stub :access_key => 'the-key', + :secret_key => 'the-secret', + :logger => @logger_stub @fog_mock = mock 'fog' + @fog_mock.stub :directories => @directories_mock Fog::Storage.should_receive(:new).and_return @fog_mock @s3 = Heirloom::AWS::S3.new :config => @config_mock, :region => 'us-west-1' + end + context "bucket_exists?" do + it "should return true if the bucket exists" do + @directories_mock.should_receive(:get). + with('bucket').and_return @bucket_mock + @s3.bucket_exists?('bucket').should be_true + end + it "should return false if the bucket does not exist" do + @directories_mock.should_receive(:get). + with('bucket').and_return nil + @s3.bucket_exists?('bucket').should be_false + end + + it "should return false if bucket owned by another account" do + @directories_mock.should_receive(:get). + with('bucket'). + and_raise Excon::Errors::Forbidden.new('msg') + @s3.bucket_exists?('bucket').should be_false + end end + context "bucket_exists_in_another_region?" do + it "should return true if the bucket exists in another region" do + @bucket_mock.stub :location => 'us-east-1' + @directories_mock.should_receive(:get). + with('bucket').at_least(:once). + and_return @bucket_mock + @s3.bucket_exists_in_another_region?('bucket').should be_true + end + + it "should return false if the bucket exists in the curren region" do + @bucket_mock.stub :location => 'us-west-1' + @directories_mock.should_receive(:get). + with('bucket').at_least(:once). + and_return @bucket_mock + @s3.bucket_exists_in_another_region?('bucket').should be_false + end + + it "should return false if bucket owned by another account" do + @directories_mock.should_receive(:get). + with('bucket'). + and_raise Excon::Errors::Forbidden.new('msg') + @s3.bucket_exists_in_another_region?('bucket').should be_false + end + end + + context "bucket_owned_by_another_account?" do + it "should return false if bucket owned by this account" do + @directories_mock.should_receive(:get). + with('bucket'). + and_return @bucket_mock + @s3.bucket_owned_by_another_account?('bucket').should be_false + end + + it "should return false if bucket does not exist" do + @directories_mock.should_receive(:get). + with('bucket'). + and_return nil + @s3.bucket_owned_by_another_account?('bucket').should be_false + end + + it "should return true if bucket is not owned by another account" do + @directories_mock.should_receive(:get). + with('bucket'). + and_raise Excon::Errors::Forbidden.new('msg') + @s3.bucket_owned_by_another_account?('bucket').should be_true + end + end + it "should delete an object from s3" do @fog_mock.should_receive(:delete_object). with('bucket', 'object', { :option => 'test' }) @s3.delete_object('bucket', 'object', { :option => 'test' }) end it "should get a bucket from s3" do - directories_mock = mock 'directories' - @fog_mock.should_receive(:directories). - and_return directories_mock - directories_mock.should_receive(:get).with 'bucket' + @directories_mock.should_receive(:get).with 'bucket' @s3.get_bucket 'bucket' end + context "testing bucket availability" do + + it "should return false if the bucket is forbidden" do + @directories_mock.should_receive(:get). + with('bucket'). + and_raise Excon::Errors::Forbidden.new('msg') + @s3.bucket_name_available?('bucket').should be_false + end + + it "should return false if bucket in different region" do + @directories_mock.should_receive(:get). + with('bucket').at_least(:once). + and_return @bucket_mock + @bucket_mock.stub :location => 'us-east-1' + @s3.bucket_name_available?('bucket').should be_false + end + + it "should return true if the bucket is in this account / region" do + @directories_mock.should_receive(:get). + with('bucket').at_least(:once). + and_return @bucket_mock + @bucket_mock.stub :location => 'us-west-1' + @s3.bucket_name_available?('bucket').should be_true + end + + it "should return true if the bucket is not found" do + @directories_mock.should_receive(:get). + with('bucket').at_least(:once). + and_return nil + @s3.bucket_name_available?('bucket').should be_true + end + + end + it "should delete a bucket from s3" do @fog_mock.should_receive(:delete_bucket).with 'bucket' + @s3.delete_bucket 'bucket' + end + + it "should return true if Excon::Errors::NotFound raised when deleting bucket" do + @fog_mock.should_receive(:delete_bucket). + with('bucket'). + and_raise Excon::Errors::NotFound.new 'Bucket does not exist.' @s3.delete_bucket 'bucket' end it "should get an object from s3" do body_mock = mock 'body'