2023-10-02 12:18:49 +03:00
|
|
|
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
|
|
|
|
# The default endpoint for testing
|
|
|
|
@endpoint PlausibleWeb.Endpoint
|
|
|
|
|
|
|
|
# Import conveniences for testing with connections
|
|
|
|
use Plausible.TestUtils
|
|
|
|
import Plug.Conn
|
|
|
|
import Phoenix.ConnTest
|
|
|
|
import PlausibleWeb.Plugins.API.Spec, only: [spec: 0]
|
|
|
|
import Plausible.Factory
|
|
|
|
|
|
|
|
import OpenApiSpex.TestAssertions
|
|
|
|
|
2024-02-12 12:44:32 +03:00
|
|
|
alias PlausibleWeb.Router.Helpers, as: Routes
|
2023-10-05 12:54:18 +03:00
|
|
|
alias PlausibleWeb.Plugins.API.Schemas
|
|
|
|
|
2023-10-02 12:18:49 +03:00
|
|
|
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
|
|
|
|
|
2023-10-05 12:54:18 +03:00
|
|
|
setup %{test: test} = tags do
|
2023-10-02 12:18:49 +03:00
|
|
|
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Plausible.Repo)
|
|
|
|
|
|
|
|
unless tags[:async] do
|
|
|
|
Ecto.Adapters.SQL.Sandbox.mode(Plausible.Repo, {:shared, self()})
|
|
|
|
end
|
|
|
|
|
|
|
|
conn = Phoenix.ConnTest.build_conn()
|
|
|
|
|
2024-11-18 11:52:03 +03:00
|
|
|
site = Plausible.Teams.Test.new_site()
|
2023-10-05 12:54:18 +03:00
|
|
|
{:ok, _token, raw_token} = Plausible.Plugins.API.Tokens.create(site, Atom.to_string(test))
|
|
|
|
|
|
|
|
{:ok, conn: conn, site: site, token: raw_token}
|
2023-10-02 12:18:49 +03:00
|
|
|
end
|
|
|
|
end
|