Skip to content

Commit

Permalink
Use Guardian.Plug to assert sign in status (#496)
Browse files Browse the repository at this point in the history
* Use Guardian.Plug to assert sign in status

We use the guardian auth framework to assert whether a user
has been signed in, rather than inspecting the HTML response.
This change makes tests less brittle to HTML or other view changes.

* Shorten assert statements
  • Loading branch information
bigbluejay9 authored Oct 1, 2024
1 parent fca2dbe commit ed73402
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions test/teiserver_web/controllers/account/session_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule TeiserverWeb.Account.SessionControllerTest do

alias Central.Helpers.GeneralTestLib
alias Teiserver.Account
alias Teiserver.Account.Guardian
alias Teiserver.Config

describe "one time codes" do
Expand All @@ -29,12 +30,7 @@ defmodule TeiserverWeb.Account.SessionControllerTest do
})

conn = get(conn, ~p"/one_time_login/test_code_valid_value")
assert conn.assigns.flash["info"] =~ "Welcome back!"
assert redirected_to(conn) == rdr

# Profile page should be accessible.
conn = get(conn, rdr)
assert html_response(conn, 200) =~ user.name
assert Guardian.Plug.current_resource(conn).id == user.id
end

test "Valid code without IP", %{conn: conn, user: user} do
Expand All @@ -52,17 +48,12 @@ defmodule TeiserverWeb.Account.SessionControllerTest do
})

conn = get(conn, ~p"/one_time_login/test_code_valid_value")
assert conn.assigns.flash["info"] =~ "Welcome back!"
assert redirected_to(conn) == rdr

conn = get(conn, rdr)
assert html_response(conn, 200) =~ user.name
assert Guardian.Plug.current_resource(conn).id == user.id
end

test "Unknown one_time_code invalid", %{conn: conn} do
conn = get(conn, ~p"/one_time_login/some_invalid_code")
assert redirected_to(conn) == ~p"/"
assert conn.assigns.flash["info"] != "Welcome back!"
assert Guardian.Plug.current_resource(conn) == nil
end

test "bad ip", %{conn: conn, user: user} do
Expand All @@ -79,8 +70,7 @@ defmodule TeiserverWeb.Account.SessionControllerTest do
})

conn = get(conn, ~p"/one_time_login/test_code_valid_value")
assert conn.assigns.flash["info"] != "Welcome back!"
assert redirected_to(conn) == ~p"/"
assert Guardian.Plug.current_resource(conn) == nil
end

test "expired code", %{conn: conn, user: user} do
Expand All @@ -96,8 +86,7 @@ defmodule TeiserverWeb.Account.SessionControllerTest do
})

conn = get(conn, ~p"/one_time_login/test_code_valid_value")
assert conn.assigns.flash["info"] != "Welcome back!"
assert redirected_to(conn) == ~p"/"
assert Guardian.Plug.current_resource(conn) == nil
end

test "disabled via site config", %{conn: conn, user: user} do
Expand All @@ -116,8 +105,7 @@ defmodule TeiserverWeb.Account.SessionControllerTest do
})

conn = get(conn, ~p"/one_time_login/test_code_valid_value")
assert conn.assigns.flash["info"] != "Welcome back!"
assert redirected_to(conn) == ~p"/"
assert Guardian.Plug.current_resource(conn) == nil
end
end
end

0 comments on commit ed73402

Please sign in to comment.