From 2564b1b727d60568dbc83acef14b52d844c2e46a Mon Sep 17 00:00:00 2001 From: Joao Gilberto Balsini Moura Date: Sun, 27 Sep 2020 10:42:19 -0300 Subject: [PATCH] Update tests to check new restrictions --- .../live/property_live_test.exs | 159 ++++++++++++++++-- 1 file changed, 144 insertions(+), 15 deletions(-) diff --git a/test/real_estate_web/live/property_live_test.exs b/test/real_estate_web/live/property_live_test.exs index f3376343..aea9d22d 100644 --- a/test/real_estate_web/live/property_live_test.exs +++ b/test/real_estate_web/live/property_live_test.exs @@ -14,24 +14,25 @@ defmodule RealEstateWeb.PropertyLiveTest do } @invalid_attrs %{description: nil, name: nil, price: nil} - defp fixture(:property) do - user = user_fixture() + defp fixture(:property, user) do create_attributes = Enum.into(%{user_id: user.id}, @create_attrs) {:ok, property} = Properties.create_property(create_attributes) property end - defp create_property(_) do - property = fixture(:property) - %{property: property} - end - describe "Index" do - setup [:create_property] - setup %{conn: conn} do - conn = log_in_user(conn, user_fixture()) - %{conn: conn} + 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 @@ -91,14 +92,98 @@ defmodule RealEstateWeb.PropertyLiveTest do 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" + + {: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 end describe "Show" do - setup [:create_property] - setup %{conn: conn} do - conn = log_in_user(conn, user_fixture()) - %{conn: conn} + 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 @@ -129,5 +214,49 @@ defmodule RealEstateWeb.PropertyLiveTest do 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" + + {: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 end end