bones73k/test/real_estate_web/live/property_live_test.exs

371 lines
12 KiB
Elixir
Raw Normal View History

2020-09-14 12:22:25 -04:00
defmodule RealEstateWeb.PropertyLiveTest do
use RealEstateWeb.ConnCase
import Phoenix.LiveViewTest
import RealEstate.AccountsFixtures
2020-09-14 12:22:25 -04:00
alias RealEstate.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"
}
2020-09-14 12:22:25 -04:00
@invalid_attrs %{description: nil, name: nil, price: nil}
2020-09-27 09:42:19 -04:00
defp fixture(:property, user) do
2020-09-14 19:45:56 -04:00
create_attributes = Enum.into(%{user_id: user.id}, @create_attrs)
{:ok, property} = Properties.create_property(create_attributes)
2020-09-14 12:22:25 -04:00
property
end
describe "Index" do
setup %{conn: conn} do
2020-09-27 09:42:19 -04:00
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
2020-09-14 12:22:25 -04:00
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"
{:ok, _, html} =
index_live
|> form("#property-form", property: @create_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.property_index_path(conn, :index))
assert html =~ "Property created successfully"
assert html =~ "some 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"
{:ok, _, html} =
index_live
|> form("#property-form", property: @update_attrs)
|> render_submit()
|> follow_redirect(conn, 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
2020-09-27 09:42:19 -04:00
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"
{:ok, _, html} =
index_live
|> form("#property-form", property: @update_attrs)
|> render_submit()
|> follow_redirect(conn, 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
2020-09-28 19:55:00 -04:00
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
2020-09-14 12:22:25 -04:00
end
describe "Show" do
setup %{conn: conn} do
2020-09-27 09:42:19 -04:00
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
2020-09-14 12:22:25 -04:00
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"
{:ok, _, html} =
show_live
|> form("#property-form", property: @update_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.property_show_path(conn, :show, property))
assert html =~ "Property updated successfully"
assert html =~ "some updated description"
end
2020-09-27 09:42:19 -04:00
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"
{:ok, _, html} =
show_live
|> form("#property-form", property: @update_attrs)
|> render_submit()
|> follow_redirect(conn, 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
2020-09-28 19:55:00 -04:00
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
2020-09-14 12:22:25 -04:00
end
end