Sha256: e0e8337107e7b4a91a46908930f3a1f820c17779d031c24bd21beb428313c24e

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require 'json'
require 'spec_helper'
require 'typhoeus'
require 'wgif/uploader'

describe WGif::Uploader do
  let(:api_key) { 'api-key' }
  let(:uploader) { WGif::Uploader.new(api_key) }
  let(:success) { Typhoeus::Response.new(
    response_code: 200,
    return_code: :ok,
    body: {data: {link: 'foo'}}.to_json)
  }

  let(:failure) { Typhoeus::Response.new(
    response_code: 400,
    return_code: :error,
    body: {data: {error: 'You should be ashamed of yourself.'}}.to_json )
  }
  let(:tempfile) { Tempfile.new('whatever') }
  let(:request_params) { {body: {image: tempfile},
                          headers: {Authorization: "Client-ID #{api_key}"}} }

  it 'sends an authorized POST request to Imgur with image file data' do
    File.stub(:open).and_yield(tempfile)
    expect(Typhoeus).to receive(:post).
      with('https://api.imgur.com/3/image', request_params).and_return(success)
    uploader.upload(tempfile.path)
  end

  it 'raises an exception if request is not successful' do
    Typhoeus.stub(/http/).and_return(failure)
    expect{ uploader.upload(tempfile.path) }.
      to raise_error(WGif::ImgurException, 'You should be ashamed of yourself.')
  end

  it 'returns the url when successful' do
    File.stub(:open).and_yield(tempfile)
    expect(Typhoeus).to receive(:post).
      with('https://api.imgur.com/3/image', request_params).and_return(success)
    expect(uploader.upload(tempfile.path)).to eq("foo")
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wgif-0.2.0 spec/wgif/uploader_spec.rb