From c5aeeccd63fb84bc868e238c2fc52224e4ef3323 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Balsini Moura Date: Mon, 28 Sep 2020 20:55:00 -0300 Subject: [PATCH] Add tests for force_logout feature --- .../live/admin_dashboard_live_test.exs | 53 +++++++++ test/real_estate_web/live/page_live_test.exs | 53 +++++++++ .../live/property_live_test.exs | 108 ++++++++++++++++++ .../live/user_dashboard_live_test.exs | 53 +++++++++ 4 files changed, 267 insertions(+) diff --git a/test/real_estate_web/live/admin_dashboard_live_test.exs b/test/real_estate_web/live/admin_dashboard_live_test.exs index 9534804..47973d9 100644 --- a/test/real_estate_web/live/admin_dashboard_live_test.exs +++ b/test/real_estate_web/live/admin_dashboard_live_test.exs @@ -27,4 +27,57 @@ defmodule RealEstateWeb.AdminDashboardLiveTest do assert disconnected_html =~ "Welcome to the admin dashboard!" assert render(admin_dashboard) =~ "Welcome to the admin dashboard!" 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 diff --git a/test/real_estate_web/live/page_live_test.exs b/test/real_estate_web/live/page_live_test.exs index 03a5d54..9052f4c 100644 --- a/test/real_estate_web/live/page_live_test.exs +++ b/test/real_estate_web/live/page_live_test.exs @@ -19,4 +19,57 @@ defmodule RealEstateWeb.PageLiveTest do assert disconnected_html =~ "Welcome to Phoenix!" assert render(page_live) =~ "Welcome to Phoenix!" 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 diff --git a/test/real_estate_web/live/property_live_test.exs b/test/real_estate_web/live/property_live_test.exs index aea9d22..63972e8 100644 --- a/test/real_estate_web/live/property_live_test.exs +++ b/test/real_estate_web/live/property_live_test.exs @@ -169,6 +169,61 @@ defmodule RealEstateWeb.PropertyLiveTest do assert {:error, {:redirect, %{to: "/"}}} = live(conn, Routes.property_index_path(conn, :edit, property)) 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 describe "Show" do @@ -258,5 +313,58 @@ defmodule RealEstateWeb.PropertyLiveTest do assert {:error, {:redirect, %{to: "/"}}} = live(conn, Routes.property_show_path(conn, :edit, property)) 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 diff --git a/test/real_estate_web/live/user_dashboard_live_test.exs b/test/real_estate_web/live/user_dashboard_live_test.exs index fcd20b3..350062d 100644 --- a/test/real_estate_web/live/user_dashboard_live_test.exs +++ b/test/real_estate_web/live/user_dashboard_live_test.exs @@ -29,4 +29,57 @@ defmodule RealEstateWeb.UserDashboardLiveTest do assert disconnected_html =~ "Welcome to the user dashboard!" assert render(user_dashboard) =~ "Welcome to the user dashboard!" 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