2021-05-26 17:41:35 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe Onebox::Engine::GithubBlobOnebox do
|
2024-07-11 18:15:48 +08:00
|
|
|
let(:raw_uri) do
|
|
|
|
"https://raw.githubusercontent.com/discourse/onebox/master/lib/onebox/engine/github_blob_onebox.rb"
|
|
|
|
end
|
|
|
|
|
2021-05-26 17:41:35 +08:00
|
|
|
before do
|
2024-07-11 18:15:48 +08:00
|
|
|
stub_request(:get, raw_uri).to_return(
|
2024-07-10 07:39:31 +08:00
|
|
|
status: 200,
|
|
|
|
body: onebox_response(described_class.onebox_name),
|
|
|
|
)
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
2024-07-11 18:15:48 +08:00
|
|
|
include_context "with engines" do
|
|
|
|
let(:link) do
|
|
|
|
"https://github.com/discourse/onebox/blob/master/lib/onebox/engine/github_blob_onebox.rb"
|
|
|
|
end
|
|
|
|
let(:uri) { URI.parse(link) }
|
|
|
|
end
|
2021-05-26 17:41:35 +08:00
|
|
|
it_behaves_like "an engine"
|
|
|
|
|
|
|
|
describe "#to_html" do
|
|
|
|
it "includes file name" do
|
|
|
|
expect(html).to include("github_blob_onebox.rb")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes blob contents" do
|
|
|
|
expect(html).to include("module Oneboxer")
|
|
|
|
end
|
2022-01-19 20:45:36 +08:00
|
|
|
|
|
|
|
it "does not include blob contents if it is binary" do
|
|
|
|
# stub_request if the response must be binary (ASCII-8BIT)
|
2024-07-11 18:15:48 +08:00
|
|
|
uri_mock = mock("object")
|
|
|
|
uri_mock.stubs(:open).returns(File.open("#{Rails.root}/spec/fixtures/pdf/small.pdf", "rb"))
|
|
|
|
URI.stubs(:parse).with(link).returns(uri)
|
2022-01-19 20:45:36 +08:00
|
|
|
URI
|
|
|
|
.stubs(:parse)
|
|
|
|
.with(
|
|
|
|
"https://raw.githubusercontent.com/discourse/onebox/master/lib/onebox/engine/github_blob_onebox.rb",
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
2024-07-11 18:15:48 +08:00
|
|
|
.returns(uri_mock)
|
2022-01-19 20:45:36 +08:00
|
|
|
|
|
|
|
expect(html).not_to include("/Pages")
|
|
|
|
expect(html).to include("This file is binary.")
|
|
|
|
end
|
2024-07-10 07:39:31 +08:00
|
|
|
|
|
|
|
context "when github_onebox_access_token is configured" do
|
|
|
|
before { SiteSetting.github_onebox_access_token = "1234" }
|
|
|
|
|
|
|
|
it "sends it as part of the request" do
|
|
|
|
html
|
2024-07-11 18:15:48 +08:00
|
|
|
expect(WebMock).to have_requested(:get, raw_uri).with(
|
2024-07-10 07:39:31 +08:00
|
|
|
headers: {
|
|
|
|
"Authorization" => "Bearer #{SiteSetting.github_onebox_access_token}",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
end
|