defmodule VigilWeb.NeiamAuthTest do
@moduledoc """
"Sign in with NEIAM" (neiam_auth), against the library's fake identity
provider: off by default; next to passwords in `mixed`, making the
account on a first login; and in `sso_strict`, the only way in,
registration invites included.
"""
# Not async: neiam_auth's config is global (persistent_term).
use VigilWeb.ConnCase, async: false
import Phoenix.LiveViewTest
import Vigil.AccountsFixtures
alias Vigil.Accounts
alias NeiamAuth.Test.FakeIdP
setup do
on_exit(fn -> NeiamAuth.put_config(NeiamAuth.Config.load(%{})) end)
idp = FakeIdP.start()
on_exit(fn -> FakeIdP.stop(idp) end)
%{idp: idp}
end
defp start_neiam(idp, extra \\ %{}, provision \\ "auto") do
env =
idp
|> FakeIdP.env(nil,
redirect_uri: "http://localhost:4002/auth/oidc/neiam/callback",
provision: provision,
label: "Sign in with NEIAM"
)
|> Map.put("OIDC_ENABLED", "true")
|> Map.merge(extra)
start_supervised!(
{NeiamAuth, env: env, name: :neiam_auth_test, backoff_min: 50, backoff_max: 200}
)
wait_ready()
end
defp wait_ready(tries \\ 100) do
with {:ok, provider} <- NeiamAuth.config("neiam"), true <- NeiamAuth.ready?(provider) do
:ok
else
_ when tries > 0 ->
# The provider fetches the fake IdP's discovery document on start.
receive do
after
20 -> wait_ready(tries - 1)
end
_ ->
flunk("provider never loaded")
end
end
# Plays the browser through the IdP and back; returns the callback's conn.
defp sso_log_in(idp, conn) do
conn = get(conn, "/auth/oidc/neiam/login")
params = FakeIdP.authorize(idp, redirected_to(conn, 302))
conn |> recycle() |> get("/auth/oidc/neiam/callback", params)
end
# From one of our own routes that sends the browser on to NEIAM (an
# invite's "Sign in with NEIAM"), through the IdP and back.
defp sso_from(idp, conn, path) do
conn = get(conn, path)
conn = conn |> recycle() |> get(redirected_to(conn, 302))
params = FakeIdP.authorize(idp, redirected_to(conn, 302))
conn |> recycle() |> get("/auth/oidc/neiam/callback", params)
end
defp signed_in_user(conn) do
with token when is_binary(token) <- get_session(conn, :user_token),
{user, _} <- Accounts.get_user_by_session_token(token) do
user
else
_ -> nil
end
end
test "off by default: no button, and the routes are 404 but health", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/users/log-in")
refute has_element?(view, "#neiam-sign-in")
assert has_element?(view, "#login_form_password")
assert %{"enabled" => false} = conn |> get("/auth/oidc/health") |> json_response(200)
assert conn |> get("/auth/oidc/neiam/login") |> response(404)
end
describe "mixed" do
setup %{idp: idp}, do: start_neiam(idp)
test "the button sits beside the password form, and a first login makes the account",
%{conn: conn, idp: idp} do
{:ok, view, _html} = live(conn, ~p"/users/log-in")
assert has_element?(view, "#neiam-sign-in a", "Sign in with NEIAM")
assert has_element?(view, "#login_form_password")
user = conn |> then(&sso_log_in(idp, &1)) |> signed_in_user()
# FakeIdP's default user.
assert user.email == "alice@example.com"
assert user.confirmed_at
end
test "a registration invite offers NEIAM beside the local form", %{conn: conn} do
{:ok, invite} = Accounts.create_invite(nil)
{:ok, view, _html} = live(conn, ~p"/users/register?#{[invite: invite.token]}")
assert has_element?(view, "#invite-sso-neiam")
assert has_element?(view, "#registration_form")
end
end
describe "sso_strict" do
setup %{idp: idp}, do: start_neiam(idp, %{"OIDC_LOGIN_MODE" => "sso_strict"})
test "only the button is offered; passwords and sign-up are refused", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/users/log-in")
assert has_element?(view, "#neiam-sign-in")
refute has_element?(view, "#login_form_password")
refute has_element?(view, "#login_form_magic")
assert {:error, {:redirect, %{to: "/users/log-in"}}} = live(conn, ~p"/users/register")
user = set_password(user_fixture())
conn =
post(conn, ~p"/users/log-in", %{
"user" => %{"email" => user.email, "password" => valid_user_password()}
})
refute get_session(conn, :user_token)
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "signs in with NEIAM"
end
test "a registration invite offers only NEIAM", %{conn: conn} do
{:ok, invite} = Accounts.create_invite(nil, %{email: "sam@example.com"})
{:ok, view, _html} = live(conn, ~p"/users/register?#{[invite: invite.token]}")
assert has_element?(view, "#invite-sso-neiam")
refute has_element?(view, "#invite_registration_form")
refute has_element?(view, "#registration_form")
conn = post(conn, ~p"/users/register/invite", %{"invite" => invite.token})
refute get_session(conn, :user_token)
refute Accounts.get_user_by_email("sam@example.com")
end
end
# As deployed: invite-only, NEIAM included. A first NEIAM login needs an
# invite, and comes back to it to accept.
describe "PROVISION=invite_only" do
setup %{idp: idp} do
Application.put_env(:vigil, :registration_open, false)
on_exit(fn -> Application.put_env(:vigil, :registration_open, true) end)
# TRUST_EMAIL as deployed: an existing account links by address.
start_neiam(idp, %{"OIDC_TRUST_EMAIL" => "true"}, "invite_only")
end
test "with no invite, a first login is told to ask for one", %{conn: conn, idp: idp} do
conn = sso_log_in(idp, conn)
assert redirected_to(conn) == ~p"/users/log-in"
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "Ask for an invite"
refute Accounts.get_user_by_email("alice@example.com")
end
test "a registration invite, accepted through NEIAM", %{conn: conn, idp: idp} do
{:ok, invite} = Accounts.create_invite(nil)
register = ~p"/users/register?#{[invite: invite.token]}"
{:ok, view, _html} = live(conn, register)
assert has_element?(view, "#invite-sso-neiam")
conn =
sso_from(
idp,
conn,
~p"/users/register/invite/sso?#{[invite: invite.token, provider: "neiam"]}"
)
assert redirected_to(conn) == register
refute Accounts.get_user_by_email("alice@example.com")
{:ok, view, _html} = live(recycle(conn), register)
assert has_element?(view, "#invite-sso-accept")
conn =
conn |> recycle() |> post(~p"/users/register/invite/sso", %{"invite" => invite.token})
user = signed_in_user(conn)
# The address NEIAM gave, for an unaddressed invite.
assert user.email == "alice@example.com"
assert user.confirmed_at
assert [_] = NeiamAuth.identities(user)
refute Accounts.get_active_invite(invite.token)
# And from now on NEIAM signs them straight in.
assert build_conn() |> then(&sso_log_in(idp, &1)) |> signed_in_user()
end
test "accepting without a NEIAM login makes nothing", %{conn: conn} do
{:ok, invite} = Accounts.create_invite(nil)
conn = post(conn, ~p"/users/register/invite/sso", %{"invite" => invite.token})
refute get_session(conn, :user_token)
assert Accounts.get_active_invite(invite.token)
end
test "an addressed invite takes the invite's address, not NEIAM's", %{conn: conn, idp: idp} do
{:ok, invite} = Accounts.create_invite(nil, %{email: "sam@example.com"})
conn =
sso_from(
idp,
conn,
~p"/users/register/invite/sso?#{[invite: invite.token, provider: "neiam"]}"
)
conn =
conn |> recycle() |> post(~p"/users/register/invite/sso", %{"invite" => invite.token})
user = signed_in_user(conn)
assert user.email == "sam@example.com"
assert [_] = NeiamAuth.identities(user)
refute Accounts.get_user_by_email("alice@example.com")
end
test "someone with an account signs in from an invite, and the invite is kept",
%{conn: conn, idp: idp} do
alice = user_fixture(%{email: "alice@example.com"})
{:ok, invite} = Accounts.create_invite(nil)
# TRUST_EMAIL links by address, so no invite is needed to sign in.
conn =
sso_from(
idp,
conn,
~p"/users/register/invite/sso?#{[invite: invite.token, provider: "neiam"]}"
)
assert signed_in_user(conn).id == alice.id
assert Accounts.get_active_invite(invite.token)
end
end
# The siblings' way to accounts without an invite: LDAP group members.
describe "PROVISION_GROUPS" do
setup %{idp: idp} do
Application.put_env(:vigil, :registration_open, false)
on_exit(fn -> Application.put_env(:vigil, :registration_open, true) end)
start_neiam(
idp,
# As deployed: GROUPS_CLAIM too, without which the list is ignored.
%{
"OIDC_TRUST_EMAIL" => "true",
"OIDC_GROUPS_CLAIM" => "groups",
"OIDC_PROVISION_GROUPS" => "admins,dads,family,friends"
},
"invite_only"
)
end
test "a member gets an account on first login, confirmed", %{conn: conn, idp: idp} do
FakeIdP.put_user(idp, %{"groups" => ["family"]})
user = conn |> then(&sso_log_in(idp, &1)) |> signed_in_user()
assert user.email == "alice@example.com"
assert user.confirmed_at
assert [_] = NeiamAuth.identities(user)
end
test "anyone else still needs an invite", %{conn: conn, idp: idp} do
FakeIdP.put_user(idp, %{"groups" => ["guests"]})
conn = sso_log_in(idp, conn)
refute signed_in_user(conn)
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "Ask for an invite"
refute Accounts.get_user_by_email("alice@example.com")
end
end
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
⭐
Log in to mark this repository.
9.8 KiB · text
History
5901e75