3.2 KiB · text History 3208487
defmodule VigilWeb.SentryCliProjectsTest do
  @moduledoc """
  The real sentry-cli (on PATH, or `SENTRY_CLI`) against a running server:
  `projects list`, `issues list`, `issues resolve/mute/unresolve`. Skipped
  when there is no binary.
  """
  use VigilWeb.ConnCase, async: false

  import Vigil.AccountsFixtures
  import Vigil.OrganizationsFixtures
  import Vigil.ReleasesFixtures

  alias Vigil.{ApiTokens, Issues}

  @cli System.get_env("SENTRY_CLI") || System.find_executable("sentry-cli")

  setup do
    owner = user_fixture()
    org = organization_fixture(owner)
    scope = org_scope(owner, org)
    team = team_fixture(org, %{name: "Backend"})
    project = project_fixture(scope, %{"name" => "web", "team_id" => team.id})

    {:ok, token} =
      ApiTokens.create_token(owner, %{
        label: "cli",
        scopes: ["project:read", "org:read", "event:read", "event:write"]
      })

    server = start_supervised!({Bandit, plug: VigilWeb.Endpoint, port: 0, ip: :loopback})
    {:ok, {_ip, port}} = ThousandIsland.listener_info(server)

    env = [
      {"SENTRY_URL", "http://127.0.0.1:#{port}"},
      {"SENTRY_AUTH_TOKEN", token.token},
      {"SENTRY_ORG", org.slug},
      {"SENTRY_PROJECT", project.slug},
      {"SENTRY_LOG_LEVEL", "warn"},
      {"SENTRY_DISABLE_UPDATE_CHECK", "true"},
      {"SENTRY_LOAD_DOTENV", "0"}
    ]

    %{env: env, project: project}
  end

  defp ok!(env, args) do
    {out, status} =
      System.cmd(@cli, args, env: env, stderr_to_stdout: true, cd: System.tmp_dir!())

    assert status == 0, "sentry-cli #{Enum.join(args, " ")} failed:\n#{out}"
    out
  end

  @tag :sdk
  test "projects list, issues list and the bulk status changes", %{env: env, project: project} do
    if @cli do
      list = ok!(env, ["projects", "list"])
      assert list =~ "web"
      assert list =~ "Backend"

      a = issue_fixture(project, title: "TypeError: x is undefined")
      b = issue_fixture(project, title: "Timeout talking to db")

      issues = ok!(env, ["issues", "list"])
      assert issues =~ "WEB-#{a.short_id}"
      assert issues =~ "Timeout talking to db"

      assert ok!(env, ["issues", "list", "--query", "timeout"]) =~ "WEB-#{b.short_id}"
      refute ok!(env, ["issues", "list", "--query", "timeout"]) =~ "WEB-#{a.short_id}"

      ok!(env, ["issues", "resolve", "--id", to_string(a.short_id)])
      assert %{status: :resolved} = Issues.get_issue(project.id, a.id)
      assert %{status: :unresolved} = Issues.get_issue(project.id, b.id)

      ok!(env, ["issues", "mute", "--status", "unresolved"])
      assert %{status: :ignored} = Issues.get_issue(project.id, b.id)

      ok!(env, ["issues", "unresolve", "--all"])
      # `--all` is every issue the default query matches: the unresolved
      # ones, so the resolved and ignored stay as they are.
      assert %{status: :resolved} = Issues.get_issue(project.id, a.id)

      ok!(env, [
        "issues",
        "unresolve",
        "--id",
        to_string(a.short_id),
        "--id",
        to_string(b.short_id)
      ])

      assert %{status: :unresolved} = Issues.get_issue(project.id, a.id)
      assert %{status: :unresolved} = Issues.get_issue(project.id, b.id)
    else
      IO.puts("skipping sentry-cli projects: no sentry-cli on PATH")
    end
  end
end