mirror of
https://github.com/discourse/discourse.git
synced 2024-12-24 04:23:43 +08:00
97e2b353f6
Followup 560e8aff75
GitHub auth tokens cannot be made with permissions to
access multiple organisations. This is quite limiting.
This commit changes the site setting to be a "secret list"
type, which allows for a key/value mapping where the value
is treated like a password in the UI.
Now when a GitHub URL is requested for oneboxing, the
org name from the URL is used to determine which token
to use for the request.
Just in case anyone used the old site setting already,
there is a migration to create a `default` entry
with that token in the new list setting, and for
a period of time we will consider that token valid to
use for all GitHub oneboxes as well.
62 lines
1.8 KiB
Ruby
62 lines
1.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe Onebox::Engine::GithubBlobOnebox do
|
|
let(:raw_uri) do
|
|
"https://raw.githubusercontent.com/discourse/onebox/master/lib/onebox/engine/github_blob_onebox.rb"
|
|
end
|
|
|
|
before do
|
|
stub_request(:get, raw_uri).to_return(
|
|
status: 200,
|
|
body: onebox_response(described_class.onebox_name),
|
|
)
|
|
end
|
|
|
|
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
|
|
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 = mock("object")
|
|
uri_mock.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_mock)
|
|
|
|
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_tokens = "discourse|github_pat_1234" }
|
|
|
|
it "sends it as part of the request" do
|
|
html
|
|
expect(WebMock).to have_requested(:get, raw_uri).with(
|
|
headers: {
|
|
"Authorization" => "Bearer github_pat_1234",
|
|
},
|
|
)
|
|
end
|
|
end
|
|
end
|
|
end
|