refactored for new project name
This commit is contained in:
parent
0039146cd4
commit
82ab1d1ea5
113 changed files with 417 additions and 412 deletions
80
test/shift73k_web/live/admin_dashboard_live_test.exs
Normal file
80
test/shift73k_web/live/admin_dashboard_live_test.exs
Normal file
|
@ -0,0 +1,80 @@
|
|||
defmodule Shift73kWeb.AdminDashboardLiveTest do
|
||||
use Shift73kWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Shift73k.AccountsFixtures
|
||||
|
||||
test "disconnected and connected render without authentication should redirect to login page",
|
||||
%{conn: conn} do
|
||||
# If we don't previously log in we will be redirected to the login page
|
||||
assert {:error, {:redirect, %{to: "/users/log_in"}}} = live(conn, "/admin_dashboard")
|
||||
end
|
||||
|
||||
test "disconnected and connected render authenticated with user role should redirect to index page",
|
||||
%{
|
||||
conn: conn
|
||||
} do
|
||||
conn = conn |> log_in_user(user_fixture())
|
||||
assert {:error, {:redirect, %{to: "/"}}} = live(conn, "/admin_dashboard")
|
||||
end
|
||||
|
||||
test "disconnected and connected render authenticated with admin role should redirect to index page",
|
||||
%{
|
||||
conn: conn
|
||||
} do
|
||||
conn = conn |> log_in_user(admin_fixture())
|
||||
{: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!"
|
||||
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!"
|
||||
|
||||
Shift73k.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 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!"
|
||||
|
||||
Shift73k.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
|
66
test/shift73k_web/live/page_live_test.exs
Normal file
66
test/shift73k_web/live/page_live_test.exs
Normal file
|
@ -0,0 +1,66 @@
|
|||
defmodule Shift73kWeb.PageLiveTest do
|
||||
use Shift73kWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Shift73k.AccountsFixtures
|
||||
|
||||
test "disconnected and connected render with authentication should redirect to index page", %{
|
||||
conn: conn
|
||||
} do
|
||||
conn = conn |> log_in_user(user_fixture())
|
||||
|
||||
{:ok, page_live, disconnected_html} = live(conn, "/")
|
||||
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!"
|
||||
|
||||
Shift73k.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 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!"
|
||||
|
||||
Shift73k.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
|
409
test/shift73k_web/live/property_live_test.exs
Normal file
409
test/shift73k_web/live/property_live_test.exs
Normal file
|
@ -0,0 +1,409 @@
|
|||
defmodule Shift73kWeb.PropertyLiveTest do
|
||||
use Shift73kWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Shift73k.AccountsFixtures
|
||||
|
||||
alias Shift73k.Properties
|
||||
|
||||
@create_attrs %{description: "some description", name: "some name", price: "120.5"}
|
||||
@update_attrs %{
|
||||
description: "some updated description",
|
||||
name: "some updated name",
|
||||
price: "456.7"
|
||||
}
|
||||
@invalid_attrs %{description: nil, name: nil, price: nil}
|
||||
|
||||
defp fixture(:property, user) do
|
||||
create_attributes = Enum.into(%{user_id: user.id}, @create_attrs)
|
||||
{:ok, property} = Properties.create_property(create_attributes)
|
||||
property
|
||||
end
|
||||
|
||||
describe "Index" do
|
||||
setup %{conn: conn} do
|
||||
user = user_fixture()
|
||||
conn = log_in_user(conn, user)
|
||||
property = fixture(:property, user)
|
||||
property_from_another_user = fixture(:property, user_fixture())
|
||||
|
||||
%{
|
||||
conn: conn,
|
||||
property: property,
|
||||
property_from_another_user: property_from_another_user,
|
||||
user: user
|
||||
}
|
||||
end
|
||||
|
||||
test "lists all properties", %{conn: conn, property: property} do
|
||||
{:ok, _index_live, html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert html =~ "Listing Properties"
|
||||
assert html =~ property.description
|
||||
end
|
||||
|
||||
test "saves new property", %{conn: conn} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("a", "New Property") |> render_click() =~
|
||||
"New Property"
|
||||
|
||||
assert_patch(index_live, Routes.property_index_path(conn, :new))
|
||||
|
||||
assert index_live
|
||||
|> form("#property-form", property: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
# update form attrs
|
||||
index_live
|
||||
|> form("#property-form", property: @update_attrs)
|
||||
|> render_change()
|
||||
|
||||
# submit new form attrs
|
||||
index_live
|
||||
|> form("#property-form", property: @update_attrs)
|
||||
|> render_submit()
|
||||
|
||||
# send modal close event & observe results
|
||||
send(index_live.pid, {:close_modal, true})
|
||||
html = render(index_live)
|
||||
|
||||
assert_patched(index_live, Routes.property_index_path(conn, :index))
|
||||
assert html =~ "Property created successfully"
|
||||
assert html =~ "some updated description"
|
||||
end
|
||||
|
||||
test "updates property in listing", %{conn: conn, property: property} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("#property-#{property.id} a", "Edit") |> render_click() =~
|
||||
"Edit Property"
|
||||
|
||||
assert_patch(index_live, Routes.property_index_path(conn, :edit, property))
|
||||
|
||||
assert index_live
|
||||
|> form("#property-form", property: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
# update form attrs
|
||||
index_live
|
||||
|> form("#property-form", property: @update_attrs)
|
||||
|> render_change()
|
||||
|
||||
# submit new form attrs
|
||||
index_live
|
||||
|> form("#property-form", property: @update_attrs)
|
||||
|> render_submit()
|
||||
|
||||
# send modal close event & observe results
|
||||
send(index_live.pid, {:close_modal, true})
|
||||
html = render(index_live)
|
||||
|
||||
assert_patched(index_live, Routes.property_index_path(conn, :index))
|
||||
assert html =~ "Property updated successfully"
|
||||
assert html =~ "some updated description"
|
||||
end
|
||||
|
||||
test "deletes property in listing", %{conn: conn, property: property} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("#property-#{property.id} a", "Delete") |> render_click()
|
||||
refute has_element?(index_live, "#property-#{property.id}")
|
||||
end
|
||||
|
||||
test "can see property from from other user in listing",
|
||||
%{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert has_element?(index_live, "#property-#{property.id}")
|
||||
end
|
||||
|
||||
test "can't see edit action for property from other user in listing",
|
||||
%{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
refute has_element?(index_live, "#property-#{property.id} a", "Edit")
|
||||
end
|
||||
|
||||
test "as an admin, I can update property from other user in listing", %{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
conn = log_in_user(conn, admin_fixture())
|
||||
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("#property-#{property.id} a", "Edit") |> render_click() =~
|
||||
"Edit Property"
|
||||
|
||||
assert_patch(index_live, Routes.property_index_path(conn, :edit, property))
|
||||
|
||||
assert index_live
|
||||
|> form("#property-form", property: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
# update form attrs
|
||||
index_live
|
||||
|> form("#property-form", property: @update_attrs)
|
||||
|> render_change()
|
||||
|
||||
# submit new form attrs
|
||||
index_live
|
||||
|> form("#property-form", property: @update_attrs)
|
||||
|> render_submit()
|
||||
|
||||
# send modal close event & observe results
|
||||
send(index_live.pid, {:close_modal, true})
|
||||
html = render(index_live)
|
||||
|
||||
assert_patched(index_live, Routes.property_index_path(conn, :index))
|
||||
assert html =~ "Property updated successfully"
|
||||
assert html =~ "some updated description"
|
||||
end
|
||||
|
||||
test "can't see delete action for property from other user in listing", %{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
refute has_element?(index_live, "#property-#{property.id} a", "Delete")
|
||||
end
|
||||
|
||||
test "as an admin, I can delete property from others in listing", %{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
conn = log_in_user(conn, admin_fixture())
|
||||
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("#property-#{property.id} a", "Delete") |> render_click()
|
||||
refute has_element?(index_live, "#property-#{property.id}")
|
||||
end
|
||||
|
||||
test "can't edit property from other user in listing",
|
||||
%{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} 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"
|
||||
|
||||
Shift73k.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 "/"
|
||||
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 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"
|
||||
|
||||
Shift73k.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
|
||||
setup %{conn: conn} do
|
||||
user = user_fixture()
|
||||
conn = log_in_user(conn, user)
|
||||
property = fixture(:property, user)
|
||||
property_from_another_user = fixture(:property, user_fixture())
|
||||
|
||||
%{
|
||||
conn: conn,
|
||||
property: property,
|
||||
property_from_another_user: property_from_another_user,
|
||||
user: user
|
||||
}
|
||||
end
|
||||
|
||||
test "displays property", %{conn: conn, property: property} do
|
||||
{:ok, _show_live, html} = live(conn, Routes.property_show_path(conn, :show, property))
|
||||
|
||||
assert html =~ "Show Property"
|
||||
assert html =~ property.description
|
||||
end
|
||||
|
||||
test "updates property within modal", %{conn: conn, property: property} do
|
||||
{:ok, show_live, _html} = live(conn, Routes.property_show_path(conn, :show, property))
|
||||
|
||||
assert show_live |> element("a", "Edit") |> render_click() =~
|
||||
"Edit Property"
|
||||
|
||||
assert_patch(show_live, Routes.property_show_path(conn, :edit, property))
|
||||
|
||||
assert show_live
|
||||
|> form("#property-form", property: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
# update form attrs
|
||||
show_live
|
||||
|> form("#property-form", property: @update_attrs)
|
||||
|> render_change()
|
||||
|
||||
# submit new form attrs
|
||||
show_live
|
||||
|> form("#property-form", property: @update_attrs)
|
||||
|> render_submit()
|
||||
|
||||
# send modal close event & observe results
|
||||
send(show_live.pid, {:close_modal, true})
|
||||
html = render(show_live)
|
||||
|
||||
assert_patched(show_live, Routes.property_show_path(conn, :show, property))
|
||||
assert html =~ "Property updated successfully"
|
||||
assert html =~ "some updated description"
|
||||
end
|
||||
|
||||
test "can't see edit action for property from another user in show page", %{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
{:ok, show_live, _html} = live(conn, Routes.property_show_path(conn, :show, property))
|
||||
|
||||
refute has_element?(show_live, "a", "Edit")
|
||||
end
|
||||
|
||||
test "as an admin, can updates property from others within modal", %{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
conn = log_in_user(conn, admin_fixture())
|
||||
|
||||
{:ok, show_live, _html} = live(conn, Routes.property_show_path(conn, :show, property))
|
||||
|
||||
assert show_live |> element("a", "Edit") |> render_click() =~
|
||||
"Edit Property"
|
||||
|
||||
assert_patch(show_live, Routes.property_show_path(conn, :edit, property))
|
||||
|
||||
assert show_live
|
||||
|> form("#property-form", property: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
# update form attrs
|
||||
show_live
|
||||
|> form("#property-form", property: @update_attrs)
|
||||
|> render_change()
|
||||
|
||||
# submit new form attrs
|
||||
show_live
|
||||
|> form("#property-form", property: @update_attrs)
|
||||
|> render_submit()
|
||||
|
||||
# send modal close event & observe results
|
||||
send(show_live.pid, {:close_modal, true})
|
||||
html = render(show_live)
|
||||
|
||||
assert_patched(show_live, Routes.property_show_path(conn, :show, property))
|
||||
assert html =~ "Property updated successfully"
|
||||
assert html =~ "some updated description"
|
||||
end
|
||||
|
||||
test "can't edit property from another user in show page", %{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} 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
|
||||
|
||||
Shift73k.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 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
|
||||
|
||||
Shift73k.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
|
71
test/shift73k_web/live/user/registration_test.exs
Normal file
71
test/shift73k_web/live/user/registration_test.exs
Normal file
|
@ -0,0 +1,71 @@
|
|||
defmodule Shift73kWeb.UserLive.RegistrationTest do
|
||||
use Shift73kWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Shift73k.AccountsFixtures
|
||||
|
||||
alias Shift73k.Accounts
|
||||
alias Shift73k.Accounts.User
|
||||
|
||||
describe "Registration" do
|
||||
setup %{conn: conn} do
|
||||
user_return_to = "/path-requires-auth"
|
||||
conn = init_test_session(conn, %{"user_return_to" => user_return_to})
|
||||
%{conn: conn, user_return_to: user_return_to}
|
||||
end
|
||||
|
||||
test "displays registration form", %{conn: conn} do
|
||||
{:ok, _view, html} = live_isolated(conn, Shift73kWeb.UserLive.Registration)
|
||||
|
||||
assert html =~ "Register\n </h2>"
|
||||
assert html =~ "Email</label>"
|
||||
end
|
||||
|
||||
test "render errors for invalid data", %{conn: conn} do
|
||||
{:ok, view, _html} = live_isolated(conn, Shift73kWeb.UserLive.Registration)
|
||||
|
||||
html =
|
||||
view
|
||||
|> form("#reg_form", %{"user" => %{"email" => "abc", "password" => "abc"}})
|
||||
|> render_change()
|
||||
|
||||
assert html =~ "Register\n </h2>"
|
||||
assert html =~ "must be a valid email address"
|
||||
assert html =~ "should be at least #{User.min_password()} character(s)"
|
||||
assert html =~ "type=\"submit\" disabled=\"disabled\""
|
||||
end
|
||||
|
||||
@tag :capture_log
|
||||
test "creates account, sets login token & phx-trigger-action", %{
|
||||
conn: conn,
|
||||
user_return_to: user_return_to
|
||||
} do
|
||||
{:ok, view, html} = live_isolated(conn, Shift73kWeb.UserLive.Registration)
|
||||
|
||||
# Login trigger form not triggered yet
|
||||
refute html =~ "phx-trigger-action=\"phx-trigger-action\""
|
||||
|
||||
# Render registering a new user
|
||||
email = unique_user_email()
|
||||
form_data = %{"user" => %{"email" => email, "password" => valid_user_password()}}
|
||||
html = form(view, "#reg_form", form_data) |> render_submit()
|
||||
|
||||
# Confirm user was registered
|
||||
%User{email: new_user_email, id: new_user_id} = Accounts.get_user_by_email(email)
|
||||
assert new_user_email == email
|
||||
|
||||
# Login trigger form activated?
|
||||
assert html =~ "phx-trigger-action=\"phx-trigger-action\""
|
||||
|
||||
# Collect the rendered login params token
|
||||
[params_token] = Floki.attribute(html, "input#user_params_token", "value")
|
||||
{:ok, params} = Phoenix.Token.decrypt(Shift73kWeb.Endpoint, "login_params", params_token)
|
||||
%{user_id: param_user_id, user_return_to: param_return_path} = params
|
||||
|
||||
# Token in login trigger form has correct user ID?
|
||||
assert new_user_id == param_user_id
|
||||
# ... and correct user_return_to path?
|
||||
assert user_return_to == param_return_path
|
||||
end
|
||||
end
|
||||
end
|
56
test/shift73k_web/live/user/reset_password_test.exs
Normal file
56
test/shift73k_web/live/user/reset_password_test.exs
Normal file
|
@ -0,0 +1,56 @@
|
|||
defmodule Shift73kWeb.UserLive.ResetPasswordTest do
|
||||
use Shift73kWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Shift73k.AccountsFixtures
|
||||
|
||||
alias Shift73k.Repo
|
||||
alias Shift73k.Accounts
|
||||
alias Shift73k.Accounts.{User, UserToken}
|
||||
|
||||
setup %{conn: conn} do
|
||||
user = user_fixture()
|
||||
conn = init_test_session(conn, %{"user_id" => user.id})
|
||||
%{conn: conn, user: user}
|
||||
end
|
||||
|
||||
test "displays registration form", %{conn: conn, user: user} do
|
||||
{:ok, _view, html} = live_isolated(conn, Shift73kWeb.UserLive.ResetPassword)
|
||||
|
||||
assert html =~ "Reset password\n </h2>"
|
||||
assert html =~ user.email
|
||||
assert html =~ "New password</label>"
|
||||
end
|
||||
|
||||
test "render errors for invalid data", %{conn: conn} do
|
||||
{:ok, view, _html} = live_isolated(conn, Shift73kWeb.UserLive.ResetPassword)
|
||||
|
||||
form_data = %{"user" => %{"password" => "abc", "password_confirmation" => "def"}}
|
||||
html = form(view, "#pw_reset_form", form_data) |> render_change()
|
||||
|
||||
assert html =~ "Reset password\n </h2>"
|
||||
assert html =~ "should be at least #{User.min_password()} character(s)"
|
||||
assert html =~ "does not match password"
|
||||
assert html =~ "type=\"submit\" disabled=\"disabled\""
|
||||
end
|
||||
|
||||
@tag :capture_log
|
||||
test "saves new password once", %{conn: conn, user: user} do
|
||||
{:ok, view, _html} = live_isolated(conn, Shift73kWeb.UserLive.ResetPassword)
|
||||
|
||||
# Render submitting a new password
|
||||
new_pw = "valid_new_pass_123"
|
||||
form_data = %{"user" => %{"password" => new_pw, "password_confirmation" => new_pw}}
|
||||
_html = form(view, "#pw_reset_form", form_data) |> render_submit()
|
||||
|
||||
# Confirm redirected
|
||||
flash = assert_redirected(view, Routes.user_session_path(conn, :new))
|
||||
assert flash["info"] == "Password reset successfully."
|
||||
|
||||
# Confirm password was updated
|
||||
assert Accounts.get_user_by_email_and_password(user.email, new_pw)
|
||||
|
||||
# Tokens have been deleted
|
||||
assert [] == Repo.all(UserToken.user_and_contexts_query(user, :all))
|
||||
end
|
||||
end
|
82
test/shift73k_web/live/user_dashboard_live_test.exs
Normal file
82
test/shift73k_web/live/user_dashboard_live_test.exs
Normal file
|
@ -0,0 +1,82 @@
|
|||
defmodule Shift73kWeb.UserDashboardLiveTest do
|
||||
use Shift73kWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Shift73k.AccountsFixtures
|
||||
|
||||
test "disconnected and connected render without authentication should redirect to login page",
|
||||
%{conn: conn} do
|
||||
# If we don't previously log in we will be redirected to the login page
|
||||
assert {:error, {:redirect, %{to: "/users/log_in"}}} = live(conn, "/user_dashboard")
|
||||
end
|
||||
|
||||
test "disconnected and connected render authenticated with user role should redirect to index page",
|
||||
%{
|
||||
conn: conn
|
||||
} do
|
||||
conn = conn |> log_in_user(user_fixture())
|
||||
{: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!"
|
||||
end
|
||||
|
||||
test "disconnected and connected render authenticated with admin role should redirect to index page",
|
||||
%{
|
||||
conn: conn
|
||||
} do
|
||||
conn = conn |> log_in_user(admin_fixture())
|
||||
{: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!"
|
||||
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!"
|
||||
|
||||
Shift73k.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 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!"
|
||||
|
||||
Shift73k.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
|
Loading…
Add table
Add a link
Reference in a new issue