discourse/spec/lib/onebox/engine/github_commit_onebox_spec.rb
Martin Brennan 97e2b353f6
FEATURE: Allow for multiple GitHub onebox tokens (#27887)
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.
2024-07-15 13:07:36 +10:00

137 lines
4.0 KiB
Ruby

# frozen_string_literal: true
RSpec.describe Onebox::Engine::GithubCommitOnebox do
describe "regular commit url" do
before do
stub_request(
:get,
"https://api.github.com/repos/discourse/discourse/commits/803d023e2307309f8b776ab3b8b7e38ba91c0919",
).to_return(status: 200, body: onebox_response("githubcommit"))
end
include_context "with engines" do
let(:link) do
"https://github.com/discourse/discourse/commit/803d023e2307309f8b776ab3b8b7e38ba91c0919"
end
end
it_behaves_like "an engine"
describe "#to_html" do
it "includes repository name" do
expect(html).to include("discourse/discourse")
end
it "includes commit sha" do
expect(html).to include("803d023e2307309f8b776ab3b8b7e38ba91c0919")
end
it "includes commit author gravatar" do
expect(html).to include("2F7d3010c11d08cf990b7614d2c2ca9098.png")
end
it "includes commit message" do
expect(html).to include("Fixed GitHub auth")
end
it "includes commit author" do
expect(html).to include("SamSaffron")
end
it "includes commit time and date" do
expect(html).to include("02:16AM - 02 Aug 13 UTC")
end
it "includes number of files changed" do
expect(html).to include("1 file")
end
it "includes number of additions" do
expect(html).to include("18 additions")
end
it "includes number of deletions" do
expect(html).to include("2 deletions")
end
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,
"https://api.github.com/repos/discourse/discourse/commits/803d023e2307309f8b776ab3b8b7e38ba91c0919",
).with(headers: { "Authorization" => "Bearer github_pat_1234" })
end
end
end
describe "PR with commit URL" do
before do
stub_request(
:get,
"https://api.github.com/repos/discourse/discourse/commits/803d023e2307309f8b776ab3b8b7e38ba91c0919",
).to_return(status: 200, body: onebox_response("githubcommit"))
end
include_context "with engines" do
let(:link) do
"https://github.com/discourse/discourse/pull/4662/commit/803d023e2307309f8b776ab3b8b7e38ba91c0919"
end
end
# TODO: fix test to make sure it's not failing when matching object
# it_behaves_like "an engine"
describe "#to_html" do
it "includes repository name" do
expect(html).to include("discourse/discourse")
end
it "includes commit sha" do
expect(html).to include("803d023e2307309f8b776ab3b8b7e38ba91c0919")
end
it "includes commit author gravatar" do
expect(html).to include("2F7d3010c11d08cf990b7614d2c2ca9098.png")
end
it "includes commit message" do
expect(html).to include("Fixed GitHub auth")
end
it "includes commit author" do
expect(html).to include("SamSaffron")
end
it "includes commit time and date" do
expect(html).to include("02:16AM - 02 Aug 13 UTC")
end
it "includes number of files changed" do
expect(html).to include("1 file")
end
it "includes number of additions" do
expect(html).to include("18 additions")
end
it "includes number of deletions" do
expect(html).to include("2 deletions")
end
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,
"https://api.github.com/repos/discourse/discourse/commits/803d023e2307309f8b776ab3b8b7e38ba91c0919",
).with(headers: { "Authorization" => "Bearer github_pat_1234" })
end
end
end
end