2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe OneboxController do
|
|
|
|
|
2013-03-05 14:03:50 -05:00
|
|
|
let(:url) { "http://google.com" }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
it "requires the user to be logged in" do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
get :show, params: { url: url }, format: :json
|
|
|
|
end.to raise_error(Discourse::NotLoggedIn)
|
2013-03-05 14:03:50 -05:00
|
|
|
end
|
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
describe "logged in" do
|
2013-03-05 14:03:50 -05:00
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
before { @user = log_in(:admin) }
|
2013-03-05 14:03:50 -05:00
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
it 'invalidates the cache if refresh is passed' do
|
|
|
|
Oneboxer.expects(:preview).with(url, invalidate_oneboxes: true)
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { url: url, refresh: 'true', user_id: @user.id }, format: :json
|
2013-03-05 14:03:50 -05:00
|
|
|
end
|
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
describe "cached onebox" do
|
|
|
|
|
|
|
|
let(:body) { "This is a cached onebox body" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Oneboxer.expects(:cached_preview).with(url).returns(body)
|
|
|
|
Oneboxer.expects(:preview).never
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { url: url, user_id: @user.id }, format: :json
|
2016-12-19 18:31:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the cached onebox response in the body" do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect(response).to be_success
|
2016-12-19 18:31:10 -05:00
|
|
|
expect(response.body).to eq(body)
|
|
|
|
end
|
|
|
|
|
2013-03-05 14:03:50 -05:00
|
|
|
end
|
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
describe "only 1 outgoing preview per user" do
|
|
|
|
|
|
|
|
it "returns 429" do
|
|
|
|
Oneboxer.expects(:is_previewing?).returns(true)
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { url: url, user_id: @user.id }, format: :json
|
2016-12-19 18:31:10 -05:00
|
|
|
expect(response.status).to eq(429)
|
|
|
|
end
|
|
|
|
|
2013-03-05 14:03:50 -05:00
|
|
|
end
|
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
describe "found onebox" do
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
let(:body) { "this is the onebox body" }
|
2016-12-19 18:31:10 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
Oneboxer.expects(:preview).with(url, invalidate_oneboxes: false).returns(body)
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { url: url, user_id: @user.id }, format: :json
|
2016-12-19 18:31:10 -05:00
|
|
|
end
|
2013-03-05 14:03:50 -05:00
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
it 'returns the onebox response in the body' do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect(response).to be_success
|
2016-12-19 18:31:10 -05:00
|
|
|
expect(response.body).to eq(body)
|
|
|
|
end
|
2013-03-05 14:03:50 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-12-19 18:31:10 -05:00
|
|
|
describe "missing onebox" do
|
|
|
|
|
|
|
|
it "returns 404 if the onebox is nil" do
|
|
|
|
Oneboxer.expects(:preview).with(url, invalidate_oneboxes: false).returns(nil)
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { url: url, user_id: @user.id }, format: :json
|
2016-12-19 18:31:10 -05:00
|
|
|
expect(response.response_code).to eq(404)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404 if the onebox is an empty string" do
|
|
|
|
Oneboxer.expects(:preview).with(url, invalidate_oneboxes: false).returns(" \t ")
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { url: url, user_id: @user.id }, format: :json
|
2016-12-19 18:31:10 -05:00
|
|
|
expect(response.response_code).to eq(404)
|
|
|
|
end
|
|
|
|
|
2013-03-05 14:03:50 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|