analytics/test/plausible_web/plugs/auth_plug_test.exs
Adam Rutkowski 5de43b758d
Run tests in async mode where applicable (#2542)
* Set pg pool size for MIX_ENV=test

* Include slow tests in CI run

* Exclude slow tests by default

* Mark tests slow/async where applicable

* Restructure captcha mocks

* Revert async where env is relied upon

* Add --max-failures=1 to CI run

* Set warnings as errors

* Disable async where various mocks are used

* Revert "Disable async where various mocks are used"

This reverts commit 2446b72a29.

* Disable async for test using vcr
2022-12-26 10:20:29 -03:00

45 lines
1.2 KiB
Elixir

defmodule PlausibleWeb.AuthPlugTest do
use Plausible.DataCase, async: true
use Plug.Test
alias PlausibleWeb.AuthPlug
test "does nothing if user is not logged in" do
conn =
conn(:get, "/")
|> init_test_session(%{})
|> AuthPlug.call(%{})
assert is_nil(conn.assigns[:current_user])
end
test "looks up current user if they are logged in" do
user = insert(:user)
subscription = insert(:subscription, user: user)
conn =
conn(:get, "/")
|> init_test_session(%{current_user_id: user.id})
|> AuthPlug.call(%{})
assert conn.assigns[:current_user].id == user.id
assert conn.assigns[:current_user].subscription.id == subscription.id
end
test "looks up the latest subscription" do
user = insert(:user)
_old_subscription =
insert(:subscription, user: user, inserted_at: Timex.now() |> Timex.shift(days: -1))
subscription = insert(:subscription, user: user, inserted_at: Timex.now())
conn =
conn(:get, "/")
|> init_test_session(%{current_user_id: user.id})
|> AuthPlug.call(%{})
assert conn.assigns[:current_user].id == user.id
assert conn.assigns[:current_user].subscription.id == subscription.id
end
end