2.2 KiB · text History b868907
defmodule GitGudWeb.RegistryTokenLiveTest do
  @moduledoc """
  The registry-token page's pull-secret preview: it has to come out under
  the name and namespace the Deployment will look in, or the cluster pulls
  anonymously and gets 401 with the token never used.
  """
  use GitGudWeb.ConnCase, async: false

  import GitGud.ForgeFixtures
  import Phoenix.LiveViewTest

  alias GitGud.Repositories.Storage

  @moduletag :git_required

  setup do
    if System.find_executable("git") == nil do
      {:skip, "git not on PATH"}
    else
      previous = Application.get_env(:git_gud, :registry_host)
      Application.put_env(:git_gud, :registry_host, "ggr.example.com")

      on_exit(fn ->
        if previous,
          do: Application.put_env(:git_gud, :registry_host, previous),
          else: Application.delete_env(:git_gud, :registry_host)
      end)

      {owner, repo} = repository_fixture()
      repo = GitGud.Repo.preload(repo, [:owner, :organization])
      path = "/r/#{Storage.repo_handle(repo)}/#{repo.name}/settings/registry-tokens"
      {:ok, owner: owner, repo: repo, path: path}
    end
  end

  defp create_token(view) do
    view
    |> form("form[phx-submit=create_token]", %{"name" => "argo-pull", "scopes" => ["pull"]})
    |> render_submit()
  end

  test "defaults to <repo>-pull with no namespace, and says to pass -n",
       %{conn: conn, owner: owner, repo: repo, path: path} do
    {:ok, view, _html} = conn |> log_in_user(owner) |> live(path)
    html = create_token(view)

    assert html =~ "name: #{repo.name}-pull"
    refute html =~ ~r/\n\s*namespace: /
    assert html =~ "apply with `kubectl -n"
    assert html =~ ~s(placeholder="#{repo.name}-pull")
  end

  test "uses the name and namespace typed in",
       %{conn: conn, owner: owner, path: path} do
    {:ok, view, _html} = conn |> log_in_user(owner) |> live(path)
    create_token(view)

    html =
      view
      |> form("form[phx-change=secret_meta]", %{
        "secret_name" => "ggr-pull",
        "secret_namespace" => "ws-tower"
      })
      |> render_change()

    assert html =~ "name: ggr-pull"
    assert html =~ ~r/\n\s*namespace: ws-tower\n/
    refute html =~ "apply with `kubectl -n"
    assert html =~ "to <code>ggr-pull</code>"
  end
end