2018-10-09 02:01:08 -04:00
|
|
|
# encoding: utf-8
|
2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
2018-10-09 02:01:08 -04:00
|
|
|
|
|
|
|
require "theme_store/git_importer"
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe ThemeStore::GitImporter do
|
2022-07-27 06:21:10 -04:00
|
|
|
describe "#import" do
|
2018-10-09 02:01:08 -04:00
|
|
|
let(:url) { "https://github.com/example/example.git" }
|
2022-11-30 15:21:09 -05:00
|
|
|
let(:first_fetch_url) do
|
|
|
|
"https://github.com/example/example.git/info/refs?service=git-upload-pack"
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2019-06-04 05:28:36 -04:00
|
|
|
let(:trailing_slash_url) { "https://github.com/example/example/" }
|
2018-10-09 02:01:08 -04:00
|
|
|
let(:ssh_url) { "git@github.com:example/example.git" }
|
|
|
|
let(:branch) { "dev" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
hex = "xxx"
|
|
|
|
SecureRandom.stubs(:hex).returns(hex)
|
2022-11-30 15:21:09 -05:00
|
|
|
|
|
|
|
FinalDestination::SSRFDetector
|
|
|
|
.stubs(:lookup_and_filter_ips)
|
|
|
|
.with("github.com")
|
|
|
|
.returns(["192.0.2.100"])
|
|
|
|
|
2018-10-09 02:01:08 -04:00
|
|
|
@temp_folder = "#{Pathname.new(Dir.tmpdir).realpath}/discourse_theme_#{hex}"
|
|
|
|
@ssh_folder = "#{Pathname.new(Dir.tmpdir).realpath}/discourse_theme_ssh_#{hex}"
|
|
|
|
end
|
|
|
|
|
2022-11-01 12:33:17 -04:00
|
|
|
it "imports ssh urls" do
|
|
|
|
Discourse::Utils.expects(:execute_command).with(
|
|
|
|
{
|
|
|
|
"GIT_SSH_COMMAND" =>
|
|
|
|
"ssh -i #{@ssh_folder}/id_rsa -o IdentitiesOnly=yes -o IdentityFile=#{@ssh_folder}/id_rsa -o StrictHostKeyChecking=no",
|
|
|
|
},
|
2022-11-02 05:47:59 -04:00
|
|
|
"git",
|
|
|
|
"clone",
|
|
|
|
"ssh://git@github.com/example/example.git",
|
|
|
|
@temp_folder,
|
|
|
|
timeout: 20,
|
2022-11-01 12:33:17 -04:00
|
|
|
)
|
2018-10-09 02:01:08 -04:00
|
|
|
|
|
|
|
importer = ThemeStore::GitImporter.new(ssh_url, private_key: "private_key")
|
|
|
|
importer.import!
|
|
|
|
end
|
|
|
|
|
2022-11-01 12:33:17 -04:00
|
|
|
it "imports ssh urls with a particular branch" do
|
|
|
|
Discourse::Utils.expects(:execute_command).with(
|
|
|
|
{
|
|
|
|
"GIT_SSH_COMMAND" =>
|
|
|
|
"ssh -i #{@ssh_folder}/id_rsa -o IdentitiesOnly=yes -o IdentityFile=#{@ssh_folder}/id_rsa -o StrictHostKeyChecking=no",
|
|
|
|
},
|
2022-11-15 14:23:57 -05:00
|
|
|
"git",
|
|
|
|
"clone",
|
|
|
|
"--single-branch",
|
|
|
|
"-b",
|
|
|
|
branch,
|
|
|
|
"ssh://git@github.com/example/example.git",
|
|
|
|
@temp_folder,
|
|
|
|
timeout: 20,
|
2022-11-01 12:33:17 -04:00
|
|
|
)
|
2018-10-09 02:01:08 -04:00
|
|
|
|
|
|
|
importer = ThemeStore::GitImporter.new(ssh_url, private_key: "private_key", branch: branch)
|
|
|
|
importer.import!
|
|
|
|
end
|
2022-12-01 11:50:06 -05:00
|
|
|
|
|
|
|
context "with a redirect" do
|
|
|
|
before do
|
|
|
|
FinalDestination
|
|
|
|
.stubs(:resolve)
|
|
|
|
.with(first_fetch_url, http_verb: :get)
|
|
|
|
.returns(
|
|
|
|
URI.parse(
|
|
|
|
"https://github.com/redirected/example.git/info/refs?service=git-upload-pack",
|
2023-01-09 06:18:21 -05:00
|
|
|
),
|
2022-12-01 11:50:06 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "imports http urls" do
|
|
|
|
Discourse::Utils.expects(:execute_command).with(
|
|
|
|
{ "GIT_TERMINAL_PROMPT" => "0" },
|
|
|
|
"git",
|
|
|
|
"-c",
|
|
|
|
"http.followRedirects=false",
|
|
|
|
"-c",
|
|
|
|
"http.curloptResolve=github.com:443:192.0.2.100",
|
|
|
|
"clone",
|
|
|
|
"https://github.com/redirected/example.git",
|
|
|
|
@temp_folder,
|
|
|
|
timeout: 20,
|
|
|
|
)
|
|
|
|
|
|
|
|
importer = ThemeStore::GitImporter.new(url)
|
|
|
|
importer.import!
|
|
|
|
|
|
|
|
expect(importer.url).to eq(url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without a redirect" do
|
|
|
|
before do
|
|
|
|
FinalDestination
|
|
|
|
.stubs(:resolve)
|
|
|
|
.with(first_fetch_url, http_verb: :get)
|
|
|
|
.returns(URI.parse(first_fetch_url))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "imports http urls" do
|
|
|
|
Discourse::Utils.expects(:execute_command).with(
|
|
|
|
{ "GIT_TERMINAL_PROMPT" => "0" },
|
|
|
|
"git",
|
|
|
|
"-c",
|
|
|
|
"http.followRedirects=false",
|
|
|
|
"-c",
|
|
|
|
"http.curloptResolve=github.com:443:192.0.2.100",
|
|
|
|
"clone",
|
|
|
|
"https://github.com/example/example.git",
|
|
|
|
@temp_folder,
|
|
|
|
timeout: 20,
|
|
|
|
)
|
|
|
|
|
|
|
|
importer = ThemeStore::GitImporter.new(url)
|
|
|
|
importer.import!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "imports when the url has a trailing slash" do
|
|
|
|
Discourse::Utils.expects(:execute_command).with(
|
|
|
|
{ "GIT_TERMINAL_PROMPT" => "0" },
|
|
|
|
"git",
|
|
|
|
"-c",
|
|
|
|
"http.followRedirects=false",
|
|
|
|
"-c",
|
|
|
|
"http.curloptResolve=github.com:443:192.0.2.100",
|
|
|
|
"clone",
|
|
|
|
"https://github.com/example/example.git",
|
|
|
|
@temp_folder,
|
|
|
|
timeout: 20,
|
|
|
|
)
|
|
|
|
|
|
|
|
importer = ThemeStore::GitImporter.new(trailing_slash_url)
|
|
|
|
importer.import!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "imports http urls with a particular branch" do
|
|
|
|
Discourse::Utils.expects(:execute_command).with(
|
|
|
|
{ "GIT_TERMINAL_PROMPT" => "0" },
|
|
|
|
"git",
|
|
|
|
"-c",
|
|
|
|
"http.followRedirects=false",
|
|
|
|
"-c",
|
|
|
|
"http.curloptResolve=github.com:443:192.0.2.100",
|
|
|
|
"clone",
|
|
|
|
"--single-branch",
|
|
|
|
"-b",
|
|
|
|
branch,
|
|
|
|
"https://github.com/example/example.git",
|
|
|
|
@temp_folder,
|
|
|
|
timeout: 20,
|
|
|
|
)
|
|
|
|
|
|
|
|
importer = ThemeStore::GitImporter.new(url, branch: branch)
|
|
|
|
importer.import!
|
|
|
|
end
|
|
|
|
end
|
2018-10-09 02:01:08 -04:00
|
|
|
end
|
|
|
|
end
|