12.6 KiB · text History bbb2a48
defmodule VigilWeb.ProjectApiTest do
  use VigilWeb.ConnCase, async: true

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

  alias Vigil.{ApiTokens, Issues, Organizations, Projects, Repo, Uptime}
  alias Vigil.Organizations.Roles

  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})

    %{owner: owner, org: org, scope: scope, team: team, project: project}
  end

  defp token(user, scopes \\ Roles.all_scopes()) do
    {:ok, t} = ApiTokens.create_token(user, %{label: "t", scopes: scopes})
    t.token
  end

  defp api(token), do: build_conn() |> put_req_header("authorization", "Bearer " <> token)

  # A member (no global view) on no team, in an org without open membership.
  defp outsider(org, role \\ :member) do
    org |> Ecto.Changeset.change(open_membership: false) |> Repo.update!()
    member_fixture(org, nil, role).user
  end

  describe "teams" do
    test "lists the organization's teams with membership and visible projects", ctx do
      body = ctx.owner |> token() |> api() |> get(~p"/api/0/organizations/#{ctx.org.slug}/teams/")

      assert [%{"slug" => "backend", "name" => "Backend", "isMember" => false} = t] =
               json_response(body, 200)

      assert t["hasAccess"]
      assert [%{"slug" => "web"}] = t["projects"]
    end

    test "creating a team puts its maker on it", ctx do
      t = token(ctx.owner)

      assert %{"slug" => "ops", "isMember" => true, "memberCount" => 1} =
               t
               |> api()
               |> post(~p"/api/0/organizations/#{ctx.org.slug}/teams/", %{name: "Ops"})
               |> json_response(201)

      assert %{"slug" => ["is taken"]} =
               t
               |> api()
               |> post(~p"/api/0/organizations/#{ctx.org.slug}/teams/", %{name: "Ops"})
               |> json_response(400)
    end

    test "403 without the scopes, 404 for another organization", ctx do
      t = token(ctx.owner, ["project:read"])

      assert t
             |> api()
             |> get(~p"/api/0/organizations/#{ctx.org.slug}/teams/")
             |> json_response(403)

      member = outsider(ctx.org)

      assert member
             |> token()
             |> api()
             |> post(~p"/api/0/organizations/#{ctx.org.slug}/teams/", %{name: "X"})
             |> json_response(403)

      other = organization_fixture()

      assert ctx.owner
             |> token()
             |> api()
             |> get(~p"/api/0/organizations/#{other.slug}/teams/")
             |> json_response(404)
    end
  end

  describe "creating projects on a team" do
    test "201 with the project, on the team, with a key", ctx do
      body =
        ctx.owner
        |> token()
        |> api()
        |> post(~p"/api/0/teams/#{ctx.org.slug}/#{ctx.team.slug}/projects/", %{
          name: "Mobile App",
          platform: "java"
        })
        |> json_response(201)

      assert %{
               "slug" => "mobile-app",
               "name" => "Mobile App",
               "platform" => "java",
               "team" => %{"slug" => "backend"},
               "organization" => %{"slug" => slug}
             } = body

      assert slug == ctx.org.slug
      project = Projects.get_visible(ctx.scope, "mobile-app")
      assert [_key] = Projects.list_keys(project)
      assert body["id"] == to_string(project.dsn_id)
    end

    test "an explicit slug, and a bad platform is a 400", ctx do
      t = token(ctx.owner)
      path = ~p"/api/0/teams/#{ctx.org.slug}/#{ctx.team.slug}/projects/"

      assert %{"slug" => "api"} =
               t |> api() |> post(path, %{name: "API server", slug: "api"}) |> json_response(201)

      assert %{"platform" => _} =
               t |> api() |> post(path, %{name: "x", platform: "cobol"}) |> json_response(400)
    end

    test "403 without project:write or off the team; 404 for no such team", ctx do
      path = ~p"/api/0/teams/#{ctx.org.slug}/#{ctx.team.slug}/projects/"

      assert ctx.owner
             |> token(["project:read"])
             |> api()
             |> post(path, %{name: "x"})
             |> json_response(403)

      # An admin may write projects, but not on a team they aren't on.
      admin = outsider(ctx.org, :admin)
      assert admin |> token() |> api() |> post(path, %{name: "x"}) |> json_response(403)

      assert ctx.owner
             |> token()
             |> api()
             |> post(~p"/api/0/teams/#{ctx.org.slug}/nope/projects/", %{name: "x"})
             |> json_response(404)
    end
  end

  describe "project details" do
    test "show, rename, re-slug and delete", ctx do
      t = token(ctx.owner)
      path = ~p"/api/0/projects/#{ctx.org.slug}/web/"

      assert %{"slug" => "web", "teams" => [%{"slug" => "backend"}]} =
               t |> api() |> get(path) |> json_response(200)

      assert %{"slug" => "website", "name" => "Website", "platform" => "javascript"} =
               t
               |> api()
               |> put(path, %{name: "Website", slug: "website", platform: "javascript"})
               |> json_response(200)

      assert t |> api() |> get(path) |> json_response(404)
      new_path = ~p"/api/0/projects/#{ctx.org.slug}/website/"

      assert %{"slug" => ["use lowercase letters, numbers and single dashes"]} =
               t |> api() |> put(new_path, %{slug: "Bad Slug!"}) |> json_response(400)

      assert t |> api() |> delete(new_path) |> response(204)
      assert t |> api() |> get(new_path) |> json_response(404)
      assert Projects.get_visible(ctx.scope, "website") == nil
    end

    test "a project off the token's teams is a 404; scopes are checked", ctx do
      path = ~p"/api/0/projects/#{ctx.org.slug}/web/"
      member = outsider(ctx.org)

      assert member |> token() |> api() |> get(path) |> json_response(404)

      # On the team: it can read, not change or delete (members lack
      # project:write and project:admin).
      {_org, m} = Organizations.get_membership(member, ctx.org.slug)
      :ok = Organizations.put_team_member(ctx.team.id, m.id)
      t = token(member)

      assert %{"slug" => "web"} = t |> api() |> get(path) |> json_response(200)
      assert t |> api() |> put(path, %{name: "x"}) |> json_response(403)
      assert t |> api() |> delete(path) |> json_response(403)

      # A token without project:admin can't delete, whoever holds it.
      assert ctx.owner
             |> token(["project:read", "project:write"])
             |> api()
             |> delete(path)
             |> json_response(403)
    end
  end

  describe "keys" do
    test "list, create, rename, disable, rate-limit and delete", ctx do
      t = token(ctx.owner)
      path = ~p"/api/0/projects/#{ctx.org.slug}/web/keys/"

      assert [%{"id" => id, "name" => "Default", "isActive" => true, "dsn" => dsn}] =
               t |> api() |> get(path) |> json_response(200)

      assert dsn["public"] =~ "://#{id}@"
      assert dsn["public"] =~ ~r"/#{ctx.project.dsn_id}$"
      assert dsn["security"] =~ "/api/#{ctx.project.dsn_id}/security/?sentry_key=#{id}"
      assert dsn["minidump"] =~ "/api/#{ctx.project.dsn_id}/minidump/?sentry_key=#{id}"
      assert dsn["secret"] == dsn["public"]

      assert %{"id" => new_id, "name" => "CI"} =
               t |> api() |> post(path, %{name: "CI"}) |> json_response(201)

      assert %{"name" => "Builds", "isActive" => false, "rateLimit" => %{"count" => 10}} =
               t
               |> api()
               |> put(path <> "#{new_id}/", %{
                 name: "Builds",
                 isActive: false,
                 rateLimit: %{window: 60, count: 10}
               })
               |> json_response(200)

      assert t |> api() |> delete(path <> "#{new_id}/") |> response(204)
      assert t |> api() |> delete(path <> "#{new_id}/") |> json_response(404)
      assert [%{"id" => ^id}] = t |> api() |> get(path) |> json_response(200)
    end

    test "scopes and visibility", ctx do
      path = ~p"/api/0/projects/#{ctx.org.slug}/web/keys/"
      [key] = Projects.list_keys(ctx.project)

      assert ctx.owner
             |> token(["project:read"])
             |> api()
             |> post(path, %{name: "x"})
             |> json_response(403)

      assert ctx.owner
             |> token(["project:read", "project:write"])
             |> api()
             |> delete(path <> "#{key.public_key}/")
             |> json_response(403)

      assert ctx.org |> outsider() |> token() |> api() |> get(path) |> json_response(404)
    end
  end

  describe "issues" do
    setup ctx do
      older = DateTime.add(DateTime.utc_now(), -60)
      a = issue_fixture(ctx.project, title: "TypeError: x is undefined", level: "error")
      b = issue_fixture(ctx.project, title: "Timeout talking to db", last_seen: older)
      c = issue_fixture(ctx.project, title: "Old one", status: :resolved)
      %{a: a, b: b, c: c}
    end

    test "lists unresolved issues in sentry-cli's shape, with query and cursors", ctx do
      t = token(ctx.owner)
      path = ~p"/api/0/projects/#{ctx.org.slug}/web/issues/"

      conn = t |> api() |> get(path)

      assert [%{"id" => id, "shortId" => short, "status" => "unresolved", "level" => "error"}, _] =
               json_response(conn, 200)

      assert id == to_string(ctx.a.short_id)
      assert short == "WEB-#{ctx.a.short_id}"
      assert [link] = get_resp_header(conn, "link")
      assert link =~ ~s(rel="next"; results="false")

      assert [%{"title" => "Old one"}] =
               t |> api() |> get(path, query: "is:resolved") |> json_response(200)

      assert [%{"title" => "Timeout talking to db"}] =
               t |> api() |> get(path, query: "timeout") |> json_response(200)

      page = t |> api() |> get(path, per_page: "1")
      assert [_] = json_response(page, 200)
      assert [link] = get_resp_header(page, "link")
      assert link =~ ~s(rel="next"; results="true"; cursor="0:1:0")

      assert [%{"id" => second}] =
               t |> api() |> get(path, per_page: "1", cursor: "0:1:0") |> json_response(200)

      assert second == to_string(ctx.b.short_id)
    end

    test "bulk status changes by id, by status and by query", ctx do
      t = token(ctx.owner)
      path = ~p"/api/0/projects/#{ctx.org.slug}/web/issues/"

      assert %{"status" => "resolved"} =
               t
               |> api()
               |> put(path <> "?id=#{ctx.a.short_id}&id=#{ctx.b.id}", %{status: "resolved"})
               |> json_response(200)

      assert %{status: :resolved} = Issues.get_issue(ctx.project.id, ctx.a.id)
      assert %{status: :resolved} = Issues.get_issue(ctx.project.id, ctx.b.id)

      assert t
             |> api()
             |> put(path <> "?status=resolved", %{status: "muted"})
             |> json_response(200)

      for i <- [ctx.a, ctx.b, ctx.c],
          do: assert(%{status: :ignored} = Issues.get_issue(ctx.project.id, i.id))

      assert t
             |> api()
             |> put(path <> "?status=resolved", %{status: "unresolved"})
             |> response(204)

      assert t
             |> api()
             |> put(path <> "?query=is:ignored", %{status: "unresolved"})
             |> json_response(200)

      assert %{status: :unresolved} = Issues.get_issue(ctx.project.id, ctx.c.id)

      assert %{"status" => _} =
               t |> api() |> put(path <> "?id=1", %{status: "bogus"}) |> json_response(400)
    end

    test "403 without event scopes; 404 for an invisible project", ctx do
      path = ~p"/api/0/projects/#{ctx.org.slug}/web/issues/"

      assert ctx.owner |> token(["project:read"]) |> api() |> get(path) |> json_response(403)

      assert ctx.owner
             |> token(["event:read"])
             |> api()
             |> put(path <> "?id=#{ctx.a.short_id}", %{status: "resolved"})
             |> json_response(403)

      assert %{status: :unresolved} = Issues.get_issue(ctx.project.id, ctx.a.id)

      assert ctx.org |> outsider() |> token() |> api() |> get(path) |> json_response(404)
    end
  end

  test "uptime monitors, read-only", ctx do
    {:ok, _} =
      Uptime.create_monitor(ctx.scope, ctx.project, %{
        "name" => "Site",
        "type" => "get",
        "url" => "https://site.test/health"
      })

    assert [%{"name" => "Site", "status" => "pending", "target" => "https://site.test/health"}] =
             ctx.owner
             |> token(["project:read"])
             |> api()
             |> get(~p"/api/0/organizations/#{ctx.org.slug}/uptime/")
             |> json_response(200)

    assert ctx.owner
           |> token(["org:read"])
           |> api()
           |> get(~p"/api/0/organizations/#{ctx.org.slug}/uptime/")
           |> json_response(403)
  end
end