90 lines
2.8 KiB
Elixir
90 lines
2.8 KiB
Elixir
defmodule ToweropsWeb.Plugs.CheckPolicyConsentTest do
|
|
use ToweropsWeb.ConnCase, async: true
|
|
|
|
import Towerops.AccountsFixtures
|
|
|
|
alias Towerops.Accounts
|
|
alias ToweropsWeb.Plugs.CheckPolicyConsent
|
|
|
|
describe "init/1" do
|
|
test "returns options unchanged" do
|
|
assert CheckPolicyConsent.init([]) == []
|
|
assert CheckPolicyConsent.init(some: :option) == [some: :option]
|
|
end
|
|
end
|
|
|
|
describe "call/2 - unauthenticated user" do
|
|
test "sets empty policies_needing_consent when no current_user" do
|
|
conn = CheckPolicyConsent.call(build_conn(), [])
|
|
|
|
assert conn.assigns.policies_needing_consent == []
|
|
end
|
|
|
|
test "sets empty policies_needing_consent when current_user is nil" do
|
|
conn =
|
|
build_conn()
|
|
|> assign(:current_user, nil)
|
|
|> CheckPolicyConsent.call([])
|
|
|
|
assert conn.assigns.policies_needing_consent == []
|
|
end
|
|
end
|
|
|
|
describe "call/2 - authenticated user with no policies needing consent" do
|
|
test "sets empty list when user has consented to all policies" do
|
|
user = user_fixture()
|
|
|
|
conn =
|
|
build_conn()
|
|
|> assign(:current_user, user)
|
|
|> CheckPolicyConsent.call([])
|
|
|
|
# User was just created with all required consents, so no policies needed
|
|
assert conn.assigns.policies_needing_consent == []
|
|
end
|
|
end
|
|
|
|
describe "call/2 - authenticated user with policies needing consent" do
|
|
test "includes policies when user needs to reconsent" do
|
|
user = user_fixture()
|
|
|
|
# Create a new policy version that the user hasn't consented to yet
|
|
{:ok, _policy_version} =
|
|
Accounts.create_policy_version(%{
|
|
policy_type: "privacy_policy",
|
|
version: "2.0",
|
|
content: "New privacy policy content",
|
|
effective_date: DateTime.utc_now()
|
|
})
|
|
|
|
conn =
|
|
build_conn()
|
|
|> assign(:current_user, user)
|
|
|> CheckPolicyConsent.call([])
|
|
|
|
# Should now have policies needing consent
|
|
policies = conn.assigns.policies_needing_consent
|
|
assert is_list(policies) and policies != []
|
|
# Note: The exact assertion depends on whether the policy_version creation
|
|
# triggers the need for reconsent. The test verifies the plug runs without error.
|
|
end
|
|
end
|
|
|
|
describe "call/2 - returns conn unchanged except for assigns" do
|
|
test "preserves all conn properties except assigns" do
|
|
user = user_fixture()
|
|
|
|
original_conn = assign(build_conn(), :current_user, user)
|
|
|
|
result_conn = CheckPolicyConsent.call(original_conn, [])
|
|
|
|
# Conn structure should be preserved
|
|
assert result_conn.adapter == original_conn.adapter
|
|
assert result_conn.method == original_conn.method
|
|
assert result_conn.path_info == original_conn.path_info
|
|
|
|
# But should have new assign
|
|
refute result_conn.halted
|
|
end
|
|
end
|
|
end
|