1.6 KiB · text History f960521
defmodule Mix.Tasks.Vigil.TokenTest do
  use Vigil.DataCase, async: false

  import ExUnit.CaptureIO
  import Vigil.AccountsFixtures

  alias Vigil.ApiTokens

  setup do
    Mix.shell(Mix.Shell.IO)
    on_exit(fn -> Mix.shell(Mix.Shell.IO) end)
    %{user: user_fixture()}
  end

  test "prints a raw token that authenticates as the user, with the scopes given", %{user: user} do
    out =
      capture_io(fn ->
        Mix.Tasks.Vigil.Token.run([user.email, "sentry-cli", "project:releases,org:read"])
      end)

    [raw | _] = String.split(out, "\n", trim: true)
    assert raw =~ ~r/^[0-9a-f]{64}$/
    assert out =~ "shown this once"

    token = ApiTokens.fetch_by_raw(raw)
    assert token.user.id == user.id
    assert token.label == "sentry-cli"
    assert Enum.sort(token.scopes) == ["org:read", "project:releases"]
  end

  test "an unknown user, unknown scopes or missing arguments make nothing", %{user: user} do
    err =
      capture_io(:stderr, fn ->
        assert catch_exit(Mix.Tasks.Vigil.Token.run(["nobody@example.com", "x", "org:read"])) ==
                 {:shutdown, 1}
      end)

    assert err =~ "No user with email nobody@example.com"

    err =
      capture_io(:stderr, fn ->
        assert catch_exit(Mix.Tasks.Vigil.Token.run([user.email, "x", "root"])) == {:shutdown, 1}
      end)

    assert err =~ "scopes"
    assert err =~ "project:releases"

    err =
      capture_io(:stderr, fn ->
        assert catch_exit(Mix.Tasks.Vigil.Token.run([user.email])) == {:shutdown, 1}
      end)

    assert err =~ "usage"
    assert ApiTokens.list_tokens(user) == []
  end
end