spec/service/s3_spec.rb in alephant-harness-0.2.2 vs spec/service/s3_spec.rb in alephant-harness-1.0.0

- old
+ new

@@ -1,120 +1,72 @@ require 'spec_helper' describe Alephant::Harness::Service::S3 do let(:id) { 'my-bucket' } - let(:buckets) { double("AWS::S3::BucketCollection") } - let(:bucket) { double("AWS::S3::Bucket") } - let(:s3_object) { double("AWS::S3::S3Object") } - let(:s3_object_collection) { double('AWS::S3::ObjectCollection') } + let(:fake_client) { Aws::S3::Client.new(stub_responses: true) } + before do + allow(subject).to receive(:client).and_return(fake_client) + end describe ".create" do it "creates a bucket" do - allow(buckets).to receive(:create).with(id) - expect_any_instance_of(AWS::S3).to receive(:buckets).and_return(buckets) - subject.create id + expect(subject.create(id).data).to be_a(Aws::S3::Types::CreateBucketOutput) end end describe ".delete(id)" do it "deletes a bucket" do - bucket = double('AWS::S3::Bucket', :delete => nil) - s3_object = double('AWS::S3::S3Object', :delete => nil) - - allow(buckets).to receive(:[]).with(id).and_return(bucket) - allow(bucket).to receive(:objects).and_return([s3_object]) - - expect_any_instance_of(AWS::S3).to receive(:buckets).and_return(buckets) - subject.delete id + expect(subject.delete(id)).to be_a(Aws::EmptyStructure) end end describe ".add_object(bucket_id, object_id, data)" do it "adds an object to the bucket" do object_id = 'foo/bar' - data = { :some => 'data' } + data = 'Some data' - allow(buckets).to receive(:[]).with(id).and_return(bucket) - allow(bucket).to receive(:objects).and_return(s3_object_collection) - allow(s3_object_collection).to receive(:[]).with(object_id).and_return(s3_object) - allow(s3_object).to receive(:write).with(data) - - expect_any_instance_of(AWS::S3).to receive(:buckets).and_return(buckets) - subject.add_object(id, object_id, data) + expect(subject.add_object(id, object_id, data).data).to be_a(Aws::S3::Types::PutObjectOutput) end end describe ".get_object(bucket_id, object_id)" do it "gets an object from the specified bucket" do object_id = 'foo/bar' - allow(buckets).to receive(:[]).with(id).and_return(bucket) - allow(bucket).to receive(:objects).and_return(s3_object_collection) - allow(s3_object_collection).to receive(:[]).with(object_id).and_return(s3_object) - - expect_any_instance_of(AWS::S3).to receive(:buckets).and_return(buckets) - expect(subject.get_object(id, object_id)).to eq(s3_object) + expect(subject.get_object(id, object_id).data).to be_a(Aws::S3::Types::GetObjectOutput) end end - describe ".delete_object(bucket_id, object_id)" do - it "deletes an object from the specified bucket" do - object_id = 'foo/bar' - - allow(buckets).to receive(:[]).with(id).and_return(bucket) - allow(bucket).to receive(:objects).and_return(s3_object_collection) - allow(s3_object_collection).to receive(:[]).with(object_id).and_return(s3_object) - allow(s3_object).to receive(:delete) - - expect_any_instance_of(AWS::S3).to receive(:buckets).and_return(buckets) - subject.delete_object(id, object_id) - end - end - describe ".bucket_exists?" do - before(:each) do - allow_any_instance_of(AWS::S3).to receive(:buckets).and_return(buckets) - allow(buckets).to receive(:[]).with(id).and_return(bucket) - end - context "when bucket exists" do - it "yields control" do - allow(bucket).to receive(:exists?).and_return(true) - expect { |b| subject.bucket_exists?(id, &b) }.to yield_control + context 'with block' do + it 'should call block' do + expect { |b| subject.bucket_exists?(id, &b) }.to yield_control + end end - end - context "when bucket does not exist" do - it "does not yield control" do - allow(bucket).to receive(:exists?).and_return(false) - expect { |b| subject.bucket_exists?(id, &b) }.to_not yield_control + context 'with no block' do + it 'should return true' do + fake_client.stub_data(:head_bucket, {}) + expect(subject.bucket_exists?(id)).to eq(true) + end end end - end - describe ".exists?" do - context "when queue exists" do - it "yields control" do - allow_any_instance_of(AWS::S3).to receive(:buckets).and_return(buckets) - - allow(buckets).to receive(:[]).with(id).and_return(bucket) - allow(bucket).to receive(:objects).and_return(s3_object_collection) - allow(s3_object_collection).to receive(:[]).with(object_id).and_return(s3_object) - - expect { |b| subject.exists?(id, object_id, &b) }.to yield_control + context "when bucket does not exist" do + context 'with block' do + it 'should not call block' do + fake_client.stub_responses(:head_bucket, Aws::EmptyStructure) + expect { |b| subject.bucket_exists?(id, &b) }.to_not yield_control + end end - end - context "when queue does not exist" do - it "does not yield control" do - allow_any_instance_of(AWS::S3).to receive(:buckets).and_return(buckets) - - allow(buckets).to receive(:[]).with(id).and_return(bucket) - allow(bucket).to receive(:objects).and_return(s3_object_collection) - allow(s3_object_collection).to receive(:[]).with(object_id).and_return(nil) - - expect { |b| subject.exists?(id, object_id, &b) }.to_not yield_control + context 'with no block' do + it 'should return false' do + fake_client.stub_responses(:head_bucket, Aws::EmptyStructure) + expect(subject.bucket_exists?(id)).to eq(false) + end end end end end