|
| 1 | +defmodule CraftplanWeb.AuthConfirmationControllerTest do |
| 2 | + use CraftplanWeb.ConnCase, async: true |
| 3 | + |
| 4 | + alias Craftplan.Accounts |
| 5 | + alias Craftplan.Test.AuthHelpers |
| 6 | + |
| 7 | + for role <- [:staff, :admin] do |
| 8 | + @role role |
| 9 | + |
| 10 | + test "invited #{role} users can confirm their accounts from the emailed link", %{conn: conn} do |
| 11 | + inviting_admin = AuthHelpers.register_user!(role: :admin) |
| 12 | + assert_receive {:email, _inviting_admin_confirmation} |
| 13 | + |
| 14 | + email_address = "invited-#{@role}+#{System.unique_integer([:positive])}@test.com" |
| 15 | + |
| 16 | + assert {:ok, invited_user} = |
| 17 | + Accounts.invite_member( |
| 18 | + %{email: email_address, role: @role}, |
| 19 | + actor: inviting_admin |
| 20 | + ) |
| 21 | + |
| 22 | + assert is_nil(invited_user.confirmed_at) |
| 23 | + assert_receive {:email, confirmation_email} |
| 24 | + |
| 25 | + assert [confirmation_url] = |
| 26 | + Regex.run(~r/href="([^"]+)"/, confirmation_email.html_body, capture: :all_but_first) |
| 27 | + |
| 28 | + uri = URI.parse(confirmation_url) |
| 29 | + confirmation_path = uri.path <> "?" <> uri.query |
| 30 | + |
| 31 | + conn = get(conn, confirmation_path) |
| 32 | + document = conn |> html_response(200) |> LazyHTML.from_document() |
| 33 | + confirmation_form = LazyHTML.query(document, "form[action='/auth/user/confirm_new_user']") |
| 34 | + |
| 35 | + assert ["post"] = LazyHTML.attribute(confirmation_form, "method") |
| 36 | + |
| 37 | + assert [csrf_token] = |
| 38 | + confirmation_form |
| 39 | + |> LazyHTML.query("input[name='_csrf_token']") |
| 40 | + |> LazyHTML.attribute("value") |
| 41 | + |
| 42 | + assert [confirmation_token] = |
| 43 | + confirmation_form |
| 44 | + |> LazyHTML.query("input[name='user[confirm]']") |
| 45 | + |> LazyHTML.attribute("value") |
| 46 | + |
| 47 | + conn = |
| 48 | + post(conn, "/auth/user/confirm_new_user", %{ |
| 49 | + "_csrf_token" => csrf_token, |
| 50 | + "user" => %{"confirm" => confirmation_token} |
| 51 | + }) |
| 52 | + |
| 53 | + assert redirected_to(conn) == ~p"/manage/overview" |
| 54 | + |
| 55 | + assert {:ok, confirmed_user} = Accounts.get_user_by_email(email_address, authorize?: false) |
| 56 | + assert %DateTime{} = confirmed_user.confirmed_at |
| 57 | + assert confirmed_user.role == @role |
| 58 | + end |
| 59 | + end |
| 60 | +end |
0 commit comments