3.2 KiB · text History 18b6032
defmodule VigilWeb.ShareLiveTest do
  use VigilWeb.ConnCase, async: true

  import Phoenix.LiveViewTest
  import Vigil.AccountsFixtures
  import Vigil.OrganizationsFixtures

  alias Vigil.Ingest
  alias Vigil.Ingest.{Drain, Envelope}
  alias Vigil.Issues

  setup do
    owner = user_fixture()
    org = organization_fixture(owner)
    scope = org_scope(owner, org)
    project = project_fixture(scope)

    target = %Vigil.Ingest.Target{
      project_id: project.id,
      dsn_id: project.dsn_id,
      organization_id: org.id,
      key_id: Ecto.UUID.generate()
    }

    event = %{
      "event_id" => Vigil.Schema.uuid7() |> String.replace("-", ""),
      "platform" => "python",
      "level" => "error",
      "exception" => %{
        "values" => [
          %{
            "type" => "KeyError",
            "value" => "'sku'",
            "stacktrace" => %{
              "frames" => [
                %{
                  "filename" => "shop/cart.py",
                  "function" => "add",
                  "lineno" => 12,
                  "in_app" => true,
                  "context_line" => "    sku = item['sku']",
                  "vars" => %{"secret_token" => "s3cr3t"}
                }
              ]
            }
          }
        ]
      },
      "logentry" => %{"formatted" => "adding to cart failed"},
      "request" => %{"url" => "https://shop.example/cart", "headers" => [["Cookie", "sid=abc"]]},
      "user" => %{"email" => "buyer@example.test"},
      "breadcrumbs" => %{"values" => [%{"message" => "clicked add"}]},
      "tags" => %{"customer" => "acme"}
    }

    item = %Envelope.Item{type: "event", headers: %{}, payload: Jason.encode!(event)}
    Ingest.accept(target, %{headers: %{}, items: [item]}, %{})
    Drain.drain(project.id)
    [issue] = Issues.list_issues(project.id)

    %{scope: scope, issue: issue}
  end

  test "a public issue shows its exception, not its personal data", %{
    conn: conn,
    scope: scope,
    issue: issue
  } do
    {:ok, _} = Issues.set_public(scope, issue, true)
    {:ok, view, html} = live(conn, ~p"/share/issues/#{issue.id}")

    assert has_element?(view, "#share-title", "KeyError")
    assert has_element?(view, "#share-stats")
    assert has_element?(view, "#share-event-exceptions", "'sku'")
    assert has_element?(view, "#share-event-exceptions", "shop/cart.py")
    assert has_element?(view, "#share-event-message", "adding to cart failed")

    refute has_element?(view, "#share-event-request")
    refute has_element?(view, "#share-event-user")
    refute has_element?(view, "#share-event-breadcrumbs")
    refute has_element?(view, "#share-event-tags")
    refute has_element?(view, "#share-event-json")
    refute html =~ "buyer@example.test"
    refute html =~ "s3cr3t"
    refute html =~ "sid=abc"
    refute html =~ "acme"
  end

  test "a private issue is not found", %{conn: conn, issue: issue} do
    {:ok, view, _} = live(conn, ~p"/share/issues/#{issue.id}")
    assert has_element?(view, "#share-not-found")
    refute has_element?(view, "#share-title")
  end

  test "a bad id is not found", %{conn: conn} do
    {:ok, view, _} = live(conn, ~p"/share/issues/nope")
    assert has_element?(view, "#share-not-found")
  end
end