Add tests for force_logout feature

This commit is contained in:
Joao Gilberto Balsini Moura 2020-09-28 20:55:00 -03:00
parent b6524131c1
commit c5aeeccd63
4 changed files with 267 additions and 0 deletions

View file

@ -27,4 +27,57 @@ defmodule RealEstateWeb.AdminDashboardLiveTest do
assert disconnected_html =~ "Welcome to the admin dashboard!" assert disconnected_html =~ "Welcome to the admin dashboard!"
assert render(admin_dashboard) =~ "Welcome to the admin dashboard!" assert render(admin_dashboard) =~ "Welcome to the admin dashboard!"
end end
test "logs out when force logout on logged user", %{
conn: conn
} do
admin = admin_fixture()
conn = conn |> log_in_user(admin)
{:ok, admin_dashboard, disconnected_html} = live(conn, "/admin_dashboard")
assert disconnected_html =~ "Welcome to the admin dashboard!"
assert render(admin_dashboard) =~ "Welcome to the admin dashboard!"
RealEstate.Accounts.logout_user(admin)
# Assert our liveview process is down
ref = Process.monitor(admin_dashboard.pid)
assert_receive {:DOWN, ^ref, _, _, _}
refute Process.alive?(admin_dashboard.pid)
# Assert our liveview was redirected, following first to /users/force_logout, then to "/", and then to "/users/log_in"
assert_redirect(admin_dashboard, "/users/force_logout")
conn = get(conn, "/users/force_logout")
assert "/" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
assert "/users/log_in" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
assert html_response(conn, 200) =~
"You were logged out. Please login again to continue using our application."
end
test "doesn't log out when force logout on another user", %{
conn: conn
} do
admin1 = admin_fixture()
admin2 = admin_fixture()
conn = conn |> log_in_user(admin1)
{:ok, admin_dashboard, disconnected_html} = live(conn, "/admin_dashboard")
assert disconnected_html =~ "Welcome to the admin dashboard!"
assert render(admin_dashboard) =~ "Welcome to the admin dashboard!"
RealEstate.Accounts.logout_user(admin2)
# Assert our liveview is alive
ref = Process.monitor(admin_dashboard.pid)
refute_receive {:DOWN, ^ref, _, _, _}
assert Process.alive?(admin_dashboard.pid)
# If we are able to rerender the page it means nothing happened
assert render(admin_dashboard) =~ "Welcome to the admin dashboard!"
end
end end

View file

@ -19,4 +19,57 @@ defmodule RealEstateWeb.PageLiveTest do
assert disconnected_html =~ "Welcome to Phoenix!" assert disconnected_html =~ "Welcome to Phoenix!"
assert render(page_live) =~ "Welcome to Phoenix!" assert render(page_live) =~ "Welcome to Phoenix!"
end end
test "logs out when force logout on logged user", %{
conn: conn
} do
user = user_fixture()
conn = conn |> log_in_user(user)
{:ok, page_live, disconnected_html} = live(conn, "/")
assert disconnected_html =~ "Welcome to Phoenix!"
assert render(page_live) =~ "Welcome to Phoenix!"
RealEstate.Accounts.logout_user(user)
# Assert our liveview process is down
ref = Process.monitor(page_live.pid)
assert_receive {:DOWN, ^ref, _, _, _}
refute Process.alive?(page_live.pid)
# Assert our liveview was redirected, following first to /users/force_logout, then to "/", and then to "/users/log_in"
assert_redirect(page_live, "/users/force_logout")
conn = get(conn, "/users/force_logout")
assert "/" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
assert "/users/log_in" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
assert html_response(conn, 200) =~
"You were logged out. Please login again to continue using our application."
end
test "doesn't log out when force logout on another user", %{
conn: conn
} do
user1 = user_fixture()
user2 = user_fixture()
conn = conn |> log_in_user(user2)
{:ok, page_live, disconnected_html} = live(conn, "/")
assert disconnected_html =~ "Welcome to Phoenix!"
assert render(page_live) =~ "Welcome to Phoenix!"
RealEstate.Accounts.logout_user(user1)
# Assert our liveview is alive
ref = Process.monitor(page_live.pid)
refute_receive {:DOWN, ^ref, _, _, _}
assert Process.alive?(page_live.pid)
# If we are able to rerender the page it means nothing happened
assert render(page_live) =~ "Welcome to Phoenix!"
end
end end

View file

@ -169,6 +169,61 @@ defmodule RealEstateWeb.PropertyLiveTest do
assert {:error, {:redirect, %{to: "/"}}} = assert {:error, {:redirect, %{to: "/"}}} =
live(conn, Routes.property_index_path(conn, :edit, property)) live(conn, Routes.property_index_path(conn, :edit, property))
end end
test "logs out when force logout on logged user", %{
conn: conn
} do
user = user_fixture()
conn = conn |> log_in_user(user)
{:ok, index_live, html} = live(conn, Routes.property_index_path(conn, :index))
assert html =~ "Listing Properties"
assert render(index_live) =~ "Listing Properties"
RealEstate.Accounts.logout_user(user)
# Assert our liveview process is down
ref = Process.monitor(index_live.pid)
assert_receive {:DOWN, ^ref, _, _, _}
refute Process.alive?(index_live.pid)
# Assert our liveview was redirected, following first to /users/force_logout, then to "/", and then to "/users/log_in"
assert_redirect(index_live, "/users/force_logout")
conn = get(conn, "/users/force_logout")
assert "/" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
assert "/users/log_in" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
assert html_response(conn, 200) =~
"You were logged out. Please login again to continue using our application."
end
test "doesn't log out when force logout on another user", %{
conn: conn
} do
user1 = user_fixture()
user2 = user_fixture()
conn = conn |> log_in_user(user2)
{:ok, index_live, html} = live(conn, Routes.property_index_path(conn, :index))
assert html =~ "Listing Properties"
assert render(index_live) =~ "Listing Properties"
RealEstate.Accounts.logout_user(user1)
# Assert our liveview is alive
ref = Process.monitor(index_live.pid)
refute_receive {:DOWN, ^ref, _, _, _}
assert Process.alive?(index_live.pid)
# If we are able to rerender the page it means nothing happened
assert render(index_live) =~ "Listing Properties"
end
end end
describe "Show" do describe "Show" do
@ -258,5 +313,58 @@ defmodule RealEstateWeb.PropertyLiveTest do
assert {:error, {:redirect, %{to: "/"}}} = assert {:error, {:redirect, %{to: "/"}}} =
live(conn, Routes.property_show_path(conn, :edit, property)) live(conn, Routes.property_show_path(conn, :edit, property))
end end
test "logs out when force logout on logged user", %{conn: conn, property: property} do
user = user_fixture()
conn = conn |> log_in_user(user)
{:ok, show_live, html} = live(conn, Routes.property_show_path(conn, :show, property))
assert html =~ "Show Property"
assert html =~ property.description
assert render(show_live) =~ property.description
RealEstate.Accounts.logout_user(user)
# Assert our liveview process is down
ref = Process.monitor(show_live.pid)
assert_receive {:DOWN, ^ref, _, _, _}
refute Process.alive?(show_live.pid)
# Assert our liveview was redirected, following first to /users/force_logout, then to "/", and then to "/users/log_in"
assert_redirect(show_live, "/users/force_logout")
conn = get(conn, "/users/force_logout")
assert "/" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
assert "/users/log_in" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
assert html_response(conn, 200) =~
"You were logged out. Please login again to continue using our application."
end
test "doesn't log out when force logout on another user", %{conn: conn, property: property} do
user1 = user_fixture()
user2 = user_fixture()
conn = conn |> log_in_user(user2)
{:ok, show_live, html} = live(conn, Routes.property_show_path(conn, :show, property))
assert html =~ "Show Property"
assert html =~ property.description
assert render(show_live) =~ property.description
RealEstate.Accounts.logout_user(user1)
# Assert our liveview is alive
ref = Process.monitor(show_live.pid)
refute_receive {:DOWN, ^ref, _, _, _}
assert Process.alive?(show_live.pid)
# If we are able to rerender the page it means nothing happened
assert render(show_live) =~ property.description
end
end end
end end

View file

@ -29,4 +29,57 @@ defmodule RealEstateWeb.UserDashboardLiveTest do
assert disconnected_html =~ "Welcome to the user dashboard!" assert disconnected_html =~ "Welcome to the user dashboard!"
assert render(user_dashboard) =~ "Welcome to the user dashboard!" assert render(user_dashboard) =~ "Welcome to the user dashboard!"
end end
test "logs out when force logout on logged user", %{
conn: conn
} do
user = user_fixture()
conn = conn |> log_in_user(user)
{:ok, user_dashboard, disconnected_html} = live(conn, "/user_dashboard")
assert disconnected_html =~ "Welcome to the user dashboard!"
assert render(user_dashboard) =~ "Welcome to the user dashboard!"
RealEstate.Accounts.logout_user(user)
# Assert our liveview process is down
ref = Process.monitor(user_dashboard.pid)
assert_receive {:DOWN, ^ref, _, _, _}
refute Process.alive?(user_dashboard.pid)
# Assert our liveview was redirected, following first to /users/force_logout, then to "/", and then to "/users/log_in"
assert_redirect(user_dashboard, "/users/force_logout")
conn = get(conn, "/users/force_logout")
assert "/" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
assert "/users/log_in" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
assert html_response(conn, 200) =~
"You were logged out. Please login again to continue using our application."
end
test "doesn't log out when force logout on another user", %{
conn: conn
} do
user1 = user_fixture()
user2 = user_fixture()
conn = conn |> log_in_user(user1)
{:ok, user_dashboard, disconnected_html} = live(conn, "/user_dashboard")
assert disconnected_html =~ "Welcome to the user dashboard!"
assert render(user_dashboard) =~ "Welcome to the user dashboard!"
RealEstate.Accounts.logout_user(user2)
# Assert our liveview is alive
ref = Process.monitor(user_dashboard.pid)
refute_receive {:DOWN, ^ref, _, _, _}
assert Process.alive?(user_dashboard.pid)
# If we are able to rerender the page it means nothing happened
assert render(user_dashboard) =~ "Welcome to the user dashboard!"
end
end end