Skip to content

A platform for community discussion. Free, open, simple.

5.7 KB
Raw Blame
# frozen_string_literal: true

RSpec.describe Onebox::Engine::GithubRepoOnebox do
  let(:gh_link) { "https://github.com/discourse/discourse" }
  let(:api_uri) { "https://api.github.com/repos/discourse/discourse" }
  let(:response) { onebox_response(described_class.onebox_name) }

  before { stub_request(:get, api_uri).to_return(status: 200, body: response) }

  include_context "with engines" do
    let(:link) { gh_link }
  end
  it_behaves_like "an engine"

  describe "#to_html" do
    it "shows the preview type and repository in the header" do
      header = Nokogiri::HTML5.fragment(html).at_css("aside > header")

      expect(header.at_css(".github-onebox__label").text.strip).to eq("Repository")
      expect(header.at_css("a").text.strip).to eq("discourse/discourse")
      expect(header.at_css("a")["href"]).to eq(link)
    end

    it "includes the description of the repo" do
      expect(html).to include("A platform for community discussion. Free, open, simple.")
    end

    it "includes the name of the repo and truncated description for the title" do
      expect(html).to include(
        "GitHub - discourse/discourse: A platform for community discussion. Free, open,...",
      )
    end

    it "includes a thumbnail url" do
      SecureRandom.stubs(:hex).returns("1234")
      expect(html).to include("https://opengraph.githubassets.com/1234/discourse/discourse")
    end

    it "sets the data-github-private-repo attr to false" do
      expect(html).to include("data-github-private-repo=\"false\"")
    end

    it "respects a customized repository header label" do
      TranslationOverride.upsert!(I18n.locale, "onebox.github.header.repository", "Project")

      expect(Nokogiri::HTML5.fragment(html).at_css(".github-onebox__label").text.strip).to eq(
        "Project",
      )
    end

    context "when header labels are only partially translated" do
      before do
        I18n.backend.store_translations(
          :de,
          onebox: {
            github: {
              header: {
                file: "Datei",
                repository: nil,
              },
            },
          },
        )
      end

      after { I18n.backend.reload! }

      it "falls back for the untranslated repository label" do
        I18n.with_locale(:de) do
          expect(Nokogiri::HTML5.fragment(html).at_css(".github-onebox__label").text.strip).to eq(
            "Repository",
          )
        end
      end
    end

    it "keeps query and fragment parameters out of the displayed repository name" do
      repository_link = "https://github.com/discourse/discourse?tab=readme-ov-file#readme"
      stub_request(:get, %r{\Ahttps://api.github.com/repos/discourse/discourse}).to_return(
        body: response,
      )

      preview = Nokogiri::HTML5.fragment(Onebox::Preview.new(repository_link).to_s)

      expect(preview.at_css(".github-onebox__repo").text.strip).to eq("discourse/discourse")
    end

    context "when the PR is in a private repo" do
      let(:response) do
        resp = MultiJson.load(onebox_response(described_class.onebox_name))
        resp["private"] = true
        MultiJson.dump(resp)
      end

      it "sets the data-github-private-repo attr to true" do
        expect(html).to include("data-github-private-repo=\"true\"")
      end
    end

    context "when the repo has no description" do
      let(:response) do
        resp = MultiJson.load(onebox_response(described_class.onebox_name))
        resp["description"] = ""
        MultiJson.dump(resp)
      end

      it "includes a message about contributing to the repo" do
        expect(html).to include(I18n.t("onebox.github.no_description", repo: "discourse/discourse"))
      end
    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, api_uri).with(
        headers: {
          "Authorization" => "Bearer github_pat_1234",
        },
      )
    end
  end

  describe "#inline_data" do
    it "returns nil when no access token is configured" do
      expect(described_class.new(gh_link).inline_data).to be_nil
    end

    it "returns the repo title from the API when an access token is configured" do
      SiteSetting.github_onebox_access_tokens = "discourse|github_pat_1234"
      expect(described_class.new(gh_link).inline_data).to eq(
        title: "GitHub - discourse/discourse - A platform for community discussion. Free, open,...",
      )
    end
  end

  describe ".===" do
    it "matches valid GitHub repository URL" do
      valid_url = URI("https://github.com/username/repository/")
      expect(described_class === valid_url).to eq(true)
    end

    it "matches valid GitHub repository URL without trailing slash" do
      valid_url_without_slash = URI("https://github.com/username/repository")
      expect(described_class === valid_url_without_slash).to eq(true)
    end

    it "matches valid GitHub repository URL with www" do
      valid_url_with_www = URI("https://www.github.com/username/repository/")
      expect(described_class === valid_url_with_www).to eq(true)
    end

    it "does not match Gist URL" do
      gist_url = URI("https://gist.github.com/username/123456")
      expect(described_class === gist_url).to eq(false)
    end

    it "does not match URL with valid domain as part of another domain" do
      malicious_url = URI("https://github.com.malicious.com/username/repository/")
      expect(described_class === malicious_url).to eq(false)
    end

    it "does not match invalid path" do
      invalid_path_url = URI("https://github.com/username/repository/invalid")
      expect(described_class === invalid_path_url).to eq(false)
    end
  end
end