Sha256: 4456c1b14c72ce25dee54f7beae588d4a64355cecc4e6b87a1d04e5ca6a7355c

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

require "spec_helper"

describe NightcrawlerSwift::List do

  let(:connection) { NightcrawlerSwift::Connection.new }
  let(:token) { "token" }
  let(:expires_at) { (DateTime.now + 60).to_time }
  let(:upload_url) { "server-url" }

  subject do
    NightcrawlerSwift::List.new
  end

  before do
    allow(NightcrawlerSwift).to receive(:connection).and_return(connection)
    allow(connection).to receive(:token_id).and_return(token)
    allow(connection).to receive(:expires_at).and_return(expires_at)
    allow(connection).to receive(:upload_url).and_return(upload_url)
  end

  describe "#execute" do
    context "success" do
      let :json_response do
        [{"name" => "file"}]
      end

      let :response do
        double(:response, code: 200, body: json_response.to_json)
      end

      before do
        allow(subject).to receive(:get).and_return(response)
      end

      it "gets upload url" do
        subject.execute
        expect(subject).to have_received(:get).with(connection.upload_url, headers: {accept: :json})
      end

      it "returns the parsed json" do
        result = subject.execute
        expect(result).to eql json_response
      end
    end

    context "when the bucket does not exist" do
      let :response do
        double(:response, code: 400)
      end

      before do
        allow(subject).to receive(:get).and_raise(RestClient::ResourceNotFound.new(response))
      end

      it "raises NightcrawlerSwift::Exceptions::NotFoundError" do
        expect { subject.execute }.to raise_error NightcrawlerSwift::Exceptions::NotFoundError
      end
    end

    context "when another error happens" do
      let :response do
        double(:response, code: 500)
      end

      before do
        allow(subject).to receive(:get).and_raise(RuntimeError.new(response))
      end

      it "raises NightcrawlerSwift::Exceptions::ConnectionError" do
        expect { subject.execute }.to raise_error NightcrawlerSwift::Exceptions::ConnectionError
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nightcrawler_swift-0.4.0 spec/lib/nightcrawler_swift/commands/list_spec.rb