discourse/spec/lib/onebox/engine/github_blob_onebox_spec.rb
Martin Brennan 560e8aff75
FEATURE: Allow oneboxing private GitHub URLs (#27705)
This commit adds the ability to onebox private GitHub
commits, pull requests, issues, blobs, and actions using
a new `github_onebox_access_token` site setting. The token
must be set up in correctly to have access to the repos needed.

To do this successfully with the Oneboxer, we need to skip
redirects on the github.com host, otherwise we get a 404
on the URL before it is translated into a GitHub API URL
and has the appropriate headers added.
2024-07-10 09:39:31 +10:00

58 lines
1.7 KiB
Ruby

# frozen_string_literal: true
RSpec.describe Onebox::Engine::GithubBlobOnebox do
before do
@link =
"https://github.com/discourse/onebox/blob/master/lib/onebox/engine/github_blob_onebox.rb"
@uri = URI.parse(@link)
@raw_uri =
"https://raw.githubusercontent.com/discourse/onebox/master/lib/onebox/engine/github_blob_onebox.rb"
stub_request(:get, @raw_uri).to_return(
status: 200,
body: onebox_response(described_class.onebox_name),
)
end
include_context "with engines"
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
it "does not include blob contents if it is binary" do
# stub_request if the response must be binary (ASCII-8BIT)
uri = mock("object")
uri.stubs(:open).returns(File.open("#{Rails.root}/spec/fixtures/pdf/small.pdf", "rb"))
URI.stubs(:parse).with(@link).returns(@uri)
URI
.stubs(:parse)
.with(
"https://raw.githubusercontent.com/discourse/onebox/master/lib/onebox/engine/github_blob_onebox.rb",
)
.returns(uri)
expect(html).not_to include("/Pages")
expect(html).to include("This file is binary.")
end
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
expect(WebMock).to have_requested(:get, @raw_uri).with(
headers: {
"Authorization" => "Bearer #{SiteSetting.github_onebox_access_token}",
},
)
end
end
end
end