2.9 KiB · text History b868907
defmodule GitGudWeb.StatsLicenseTest do
  @moduledoc """
  The detected licence beside the stats tab strip, on every page that
  carries it.

  It is labelled with the default branch rather than with whichever ref
  the stats are for: detection follows the default branch's tip, and
  the two can differ.
  """
  use GitGudWeb.ConnCase, async: false

  import Ecto.Query, only: [from: 2]
  import Phoenix.LiveViewTest
  import GitGud.ForgeFixtures

  alias GitGud.Repositories
  alias GitGud.Repositories.Repository

  setup %{conn: conn} do
    {user, repo} = repository_fixture(%{visibility: "public"})
    seed_main_branch(repo, user)
    %{conn: log_in_user(conn, user), user: user, repo: repo, handle: user.handle}
  end

  test "shows the licence and links to the file it came from", %{
    conn: conn,
    user: user,
    repo: repo,
    handle: handle
  } do
    sha = seed_file(repo, "main", "LICENSE.md", File.read!("LICENSE.md"), user)
    assert {:ok, "AGPL-3.0"} = Repositories.detect_and_store_license(repo, sha)

    {:ok, _view, html} = live(conn, ~p"/r/#{handle}/#{repo.name}/stats/languages")

    assert html =~ "AGPL-3.0"
    assert html =~ "LICENSE.md"
    assert html =~ "/r/#{handle}/#{repo.name}/blob/main/LICENSE.md"
  end

  test "shows on the committers tab too, being the repository's", %{
    conn: conn,
    user: user,
    repo: repo,
    handle: handle
  } do
    sha = seed_file(repo, "main", "LICENSE.md", File.read!("LICENSE.md"), user)
    assert {:ok, "AGPL-3.0"} = Repositories.detect_and_store_license(repo, sha)

    {:ok, _view, html} = live(conn, ~p"/r/#{handle}/#{repo.name}/stats/committers")

    assert html =~ "AGPL-3.0"
    assert html =~ "/r/#{handle}/#{repo.name}/blob/main/LICENSE.md"
  end

  test "says so when nothing was detected", %{conn: conn, repo: repo, handle: handle} do
    {:ok, _view, html} = live(conn, ~p"/r/#{handle}/#{repo.name}/stats/languages")

    assert html =~ "No licence detected"
    refute html =~ "badge-outline\">AGPL"
  end

  test "an unrecognised licence file reads as undetected, not as a guess", %{
    conn: conn,
    user: user,
    repo: repo,
    handle: handle
  } do
    sha = seed_file(repo, "main", "LICENSE", "Be excellent to each other.\n", user)
    assert {:ok, nil} = Repositories.detect_and_store_license(repo, sha)

    {:ok, _view, html} = live(conn, ~p"/r/#{handle}/#{repo.name}/stats/languages")

    assert html =~ "No licence detected"
  end

  test "renders a licence with no recorded file", %{conn: conn, repo: repo, handle: handle} do
    # Possible for a row written before `license_file` existed: the
    # badge still shows, with nothing to link to.
    {1, _} =
      GitGud.Repo.update_all(
        from(r in Repository, where: r.id == ^repo.id),
        set: [license: "MIT", license_file: nil]
      )

    {:ok, _view, html} = live(conn, ~p"/r/#{handle}/#{repo.name}/stats/languages")

    assert html =~ "MIT"
    refute html =~ "/blob/main/"
  end
end