2019-09-02 14:29:19 +03:00
|
|
|
defmodule Plausible.TestUtils do
|
|
|
|
use Plausible.Repo
|
|
|
|
alias Plausible.Factory
|
|
|
|
|
|
|
|
def create_user(_) do
|
|
|
|
{:ok, user: Factory.insert(:user)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_site(%{user: user}) do
|
2020-05-18 12:44:52 +03:00
|
|
|
site = Factory.insert(:site, domain: "test-site.com", members: [user])
|
2019-09-02 14:29:19 +03:00
|
|
|
{:ok, site: site}
|
|
|
|
end
|
|
|
|
|
2020-05-18 12:44:52 +03:00
|
|
|
def create_pageviews(pageviews) do
|
2020-11-03 12:20:11 +03:00
|
|
|
pageviews =
|
|
|
|
Enum.map(pageviews, fn pageview ->
|
|
|
|
Factory.build(:pageview, pageview) |> Map.from_struct() |> Map.delete(:__meta__)
|
|
|
|
end)
|
2020-09-17 16:36:01 +03:00
|
|
|
|
|
|
|
Plausible.ClickhouseRepo.insert_all("events", pageviews)
|
2020-05-18 12:44:52 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_events(events) do
|
2020-11-03 12:20:11 +03:00
|
|
|
events =
|
|
|
|
Enum.map(events, fn event ->
|
|
|
|
Factory.build(:event, event) |> Map.from_struct() |> Map.delete(:__meta__)
|
|
|
|
end)
|
2020-09-17 16:36:01 +03:00
|
|
|
|
|
|
|
Plausible.ClickhouseRepo.insert_all("events", events)
|
2020-05-18 12:44:52 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_sessions(sessions) do
|
2020-11-03 12:20:11 +03:00
|
|
|
sessions =
|
|
|
|
Enum.map(sessions, fn session ->
|
|
|
|
Factory.build(:ch_session, session) |> Map.from_struct() |> Map.delete(:__meta__)
|
|
|
|
end)
|
2020-09-17 16:36:01 +03:00
|
|
|
|
|
|
|
Plausible.ClickhouseRepo.insert_all("sessions", sessions)
|
2020-05-18 12:44:52 +03:00
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
def log_in(%{user: user, conn: conn}) do
|
2020-06-08 10:35:13 +03:00
|
|
|
conn =
|
|
|
|
init_session(conn)
|
|
|
|
|> Plug.Conn.put_session(:current_user_id, user.id)
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
{:ok, conn: conn}
|
|
|
|
end
|
|
|
|
|
|
|
|
def init_session(conn) do
|
2019-09-02 14:29:19 +03:00
|
|
|
opts =
|
|
|
|
Plug.Session.init(
|
|
|
|
store: :cookie,
|
|
|
|
key: "foobar",
|
|
|
|
encryption_salt: "encrypted cookie salt",
|
|
|
|
signing_salt: "signing salt",
|
|
|
|
log: false,
|
|
|
|
encrypt: false
|
|
|
|
)
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
conn
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Plug.Session.call(opts)
|
|
|
|
|> Plug.Conn.fetch_session()
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|