2013-05-02 02:49:13 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'imgur'
|
|
|
|
|
|
|
|
describe Imgur do
|
|
|
|
|
2013-06-05 06:34:53 +08:00
|
|
|
describe "store_file" do
|
2013-05-02 02:49:13 +08:00
|
|
|
|
|
|
|
let(:file) { Rails.root.join('app', 'assets', 'images', 'logo.png') }
|
2013-06-05 06:34:53 +08:00
|
|
|
let(:image_info) { FastImage.new(file) }
|
2013-05-02 02:49:13 +08:00
|
|
|
let(:params) { [SiteSetting.imgur_endpoint, { image: Base64.encode64(file.read) }, { 'Authorization' => "Client-ID #{SiteSetting.imgur_client_id}" }] }
|
|
|
|
|
2013-06-05 06:34:53 +08:00
|
|
|
before(:each) do
|
|
|
|
SiteSetting.stubs(:imgur_endpoint).returns("imgur_endpoint")
|
|
|
|
SiteSetting.stubs(:imgur_client_id).returns("imgur_client_id")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the url of the Imgur upload if successful' do
|
2013-05-02 02:49:13 +08:00
|
|
|
json = {
|
|
|
|
data: {
|
|
|
|
id: 'fake',
|
|
|
|
link: 'http://imgur.com/fake.png',
|
|
|
|
deletehash: 'a3kaoad30'
|
|
|
|
},
|
|
|
|
success: true,
|
|
|
|
status: 200
|
|
|
|
}.to_json
|
|
|
|
|
|
|
|
response = mock
|
|
|
|
response.expects(:body).returns(json)
|
|
|
|
RestClient.expects(:post).with(*params).returns(response)
|
|
|
|
|
2013-06-05 06:34:53 +08:00
|
|
|
Imgur.store_file(file, image_info, 1).should == 'http://imgur.com/fake.png'
|
2013-05-02 02:49:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil if the request fails' do
|
|
|
|
json = {
|
|
|
|
success: false,
|
|
|
|
status: 400
|
|
|
|
}.to_json
|
|
|
|
|
|
|
|
response = mock
|
|
|
|
response.expects(:body).returns(json)
|
|
|
|
RestClient.expects(:post).with(*params).returns(response)
|
|
|
|
|
2013-06-05 06:34:53 +08:00
|
|
|
Imgur.store_file(file, image_info, 1).should be_nil
|
2013-05-02 02:49:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|