File size: 1,357 Bytes
6778ee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
defmodule PlausibleWeb.PluginsAPICase do
  @moduledoc """
  This module defines the test case to be used by
  tests that require setting up a Plugins API connection.
  """

  use ExUnit.CaseTemplate

  using do
    quote do
      @endpoint PlausibleWeb.Endpoint

      use Plausible.TestUtils
      use Plausible.Teams.Test
      use Plausible

      import Plug.Conn
      import Phoenix.ConnTest
      import PlausibleWeb.Plugins.API.Spec, only: [spec: 0]
      import Plausible.Factory

      import OpenApiSpex.TestAssertions

      alias PlausibleWeb.Router.Helpers, as: Routes
      alias PlausibleWeb.Plugins.API.Schemas

      def authenticate(conn, domain, raw_token) do
        conn
        |> Plug.Conn.put_req_header(
          "authorization",
          Plug.BasicAuth.encode_basic_auth(domain, raw_token)
        )
      end
    end
  end

  setup %{test: test} = tags do
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Plausible.Repo)

    unless tags[:async] do
      Ecto.Adapters.SQL.Sandbox.mode(Plausible.Repo, {:shared, self()})
    end

    Plausible.Test.Support.Sandbox.allow_salts_process()

    conn = Phoenix.ConnTest.build_conn()

    site = Plausible.Teams.Test.new_site()
    {:ok, _token, raw_token} = Plausible.Plugins.API.Tokens.create(site, Atom.to_string(test))

    {:ok, conn: conn, site: site, token: raw_token}
  end
end