mirror of
https://github.com/discourse/discourse.git
synced 2025-06-03 19:39:30 +08:00
Update to Imgur API v3
Version 2 of Imgur's API is deprecated. Their documentation for v2 is no longer online, and applications can only be registered under version 3. Version 3 of their API has a slightly different endpoint but, more importantly, uses a Client ID/Secret pair instead of an API Key. This PR updates Discourse to use the new version of Imgur's API. Signed-off-by: David Celis <me@davidcel.is>
This commit is contained in:
55
spec/components/imgur_spec.rb
Normal file
55
spec/components/imgur_spec.rb
Normal file
@ -0,0 +1,55 @@
|
||||
require 'spec_helper'
|
||||
require 'imgur'
|
||||
|
||||
describe Imgur do
|
||||
|
||||
describe "upload_file" do
|
||||
|
||||
let(:file) { Rails.root.join('app', 'assets', 'images', 'logo.png') }
|
||||
let(:params) { [SiteSetting.imgur_endpoint, { image: Base64.encode64(file.read) }, { 'Authorization' => "Client-ID #{SiteSetting.imgur_client_id}" }] }
|
||||
|
||||
it 'returns JSON of the Imgur upload if successful' do
|
||||
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)
|
||||
|
||||
image_info = {
|
||||
url: 'http://imgur.com/fake.png',
|
||||
filesize: File.size(file)
|
||||
}
|
||||
|
||||
RestClient.expects(:post).with(*params).returns(response)
|
||||
result = Imgur.upload_file(file)
|
||||
|
||||
# Not testing what width/height actually are because ImageSizer is already tested
|
||||
result[:url].should eq(image_info[:url])
|
||||
result[:filesize].should eq(image_info[:filesize])
|
||||
result[:width].should_not be_nil
|
||||
result[:height].should_not be_nil
|
||||
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)
|
||||
|
||||
Imgur.upload_file(file).should be_nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
Reference in New Issue
Block a user