removing 'properties' from boilerplate

This commit is contained in:
Adam Piontek 2021-03-05 22:35:17 -05:00
parent 82ab1d1ea5
commit 3a9f00b14e
16 changed files with 16 additions and 2058 deletions

View file

@ -1,104 +0,0 @@
defmodule Shift73k.Properties do
@moduledoc """
The Properties context.
"""
import Ecto.Query, warn: false
alias Shift73k.Repo
alias Shift73k.Properties.Property
@doc """
Returns the list of properties.
## Examples
iex> list_properties()
[%Property{}, ...]
"""
def list_properties do
Repo.all(Property)
end
@doc """
Gets a single property.
Raises `Ecto.NoResultsError` if the Property does not exist.
## Examples
iex> get_property!(123)
%Property{}
iex> get_property!(456)
** (Ecto.NoResultsError)
"""
def get_property!(id), do: Repo.get!(Property, id)
@doc """
Creates a property.
## Examples
iex> create_property(%{field: value})
{:ok, %Property{}}
iex> create_property(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_property(attrs \\ %{}) do
%Property{}
|> Property.create_changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a property.
## Examples
iex> update_property(property, %{field: new_value})
{:ok, %Property{}}
iex> update_property(property, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_property(%Property{} = property, attrs) do
property
|> Property.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a property.
## Examples
iex> delete_property(property)
{:ok, %Property{}}
iex> delete_property(property)
{:error, %Ecto.Changeset{}}
"""
def delete_property(%Property{} = property) do
Repo.delete(property)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking property changes.
## Examples
iex> change_property(property)
%Ecto.Changeset{data: %Property{}}
"""
def change_property(%Property{} = property, attrs \\ %{}) do
Property.changeset(property, attrs)
end
end

View file

@ -1,28 +0,0 @@
defmodule Shift73k.Properties.Property do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "properties" do
field(:description, :string)
field(:name, :string)
field(:price, :decimal)
field(:user_id, :binary_id)
timestamps()
end
def create_changeset(property, attrs) do
property
|> cast(attrs, [:name, :price, :description, :user_id])
|> validate_required([:name, :price, :description, :user_id])
end
@doc false
def changeset(property, attrs) do
property
|> cast(attrs, [:name, :price, :description])
|> validate_required([:name, :price, :description])
end
end

View file

@ -1,66 +0,0 @@
defmodule Shift73kWeb.PropertyLive.FormComponent do
use Shift73kWeb, :live_component
alias Shift73k.Properties
@impl true
def update(%{property: property} = assigns, socket) do
socket
|> assign(assigns)
|> assign(:changeset, Properties.change_property(property))
|> live_okreply()
end
@impl true
def handle_event("validate", %{"property" => property_params}, socket) do
changeset =
socket.assigns.property
|> Properties.change_property(property_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
@impl true
def handle_event("save", %{"property" => property_params}, socket) do
save_property(socket, socket.assigns.action, property_params)
end
@impl true
def handle_event("cancel", _, socket) do
{:noreply, push_event(socket, "modal-please-hide", %{})}
end
defp save_property(socket, :edit, property_params) do
case Properties.update_property(socket.assigns.property, property_params) do
{:ok, _property} ->
flash = {:info, "Property updated successfully"}
send(self(), {:put_flash_message, flash})
socket
|> push_event("modal-please-hide", %{})
|> live_noreply()
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_property(socket, :new, property_params) do
current_user = socket.assigns.current_user
property_params = Map.put(property_params, "user_id", current_user.id)
case Properties.create_property(property_params) do
{:ok, _property} ->
flash = {:info, "Property created successfully"}
send(self(), {:put_flash_message, flash})
socket
|> push_event("modal-please-hide", %{})
|> live_noreply()
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end

View file

@ -1,57 +0,0 @@
<%= form_for @changeset, "#", [
id: "property-form",
phx_target: @myself,
phx_change: "validate",
phx_submit: "save"
], fn f -> %>
<div class="modal-body">
<div class="mb-3" phx-feedback-for="<%= input_id(f, :name)%>">
<%= label f, :name, class: "form-label" %>
<div class="input-group has-validation">
<%= text_input f, :name,
class: input_class(f, :name, "form-control"),
aria_describedby: error_id(f, :name)
%>
<%= error_tag f, :name %>
</div>
</div>
<div class="mb-3" phx-feedback-for="<%= input_id(f, :price)%>">
<%= label f, :price, class: "form-label" %>
<div class="input-group has-validation">
<%= number_input f, :price,
class: input_class(f, :price, "form-control"),
step: "any",
aria_describedby: error_id(f, :price)
%>
<%= error_tag f, :price %>
</div>
</div>
<div class="mb-3" phx-feedback-for="<%= input_id(f, :description)%>">
<%= label f, :description, class: "form-label" %>
<div class="input-group has-validation">
<%= textarea f, :description,
class: input_class(f, :description, "form-control"),
aria_describedby: error_id(f, :description)
%>
<%= error_tag f, :description %>
</div>
</div>
</div>
<div class="modal-footer">
<%= link "Cancel", to: "#", class: "btn btn-secondary me-2", phx_click: "cancel", phx_target: @myself %>
<%= submit "Save",
class: "btn btn-primary ",
disabled: !@changeset.valid?,
aria_disabled: !@changeset.valid? && "true" || false,
phx_disable_with: "Saving..."
%>
</div>
<% end %>

View file

@ -1,90 +0,0 @@
defmodule Shift73kWeb.PropertyLive.Index do
use Shift73kWeb, :live_view
alias Shift73k.Properties
alias Shift73k.Properties.Property
alias Shift73kWeb.Roles
@impl true
def mount(_params, session, socket) do
socket = assign_defaults(socket, session)
{:ok, assign(socket, :properties, [])}
end
@impl true
def handle_params(params, _url, socket) do
current_user = socket.assigns.current_user
live_action = socket.assigns.live_action
property = property_from_params(params)
if Roles.can?(current_user, property, live_action) do
socket
|> assign(:properties, list_properties())
|> assign(:modal_return_to, Routes.property_index_path(socket, :index))
|> apply_action(live_action, params)
|> live_noreply()
else
socket
|> put_flash(:error, "Unauthorised")
|> redirect(to: "/")
|> live_noreply()
end
end
defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Property")
|> assign(:property, Properties.get_property!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Property")
|> assign(:property, %Property{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Properties")
|> assign(:property, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
current_user = socket.assigns.current_user
property = Properties.get_property!(id)
if Shift73kWeb.Roles.can?(current_user, property, :delete) do
property = Properties.get_property!(id)
{:ok, _} = Properties.delete_property(property)
{:noreply, assign(socket, :properties, list_properties())}
else
{:noreply,
socket
|> put_flash(:error, "Unauthorised")
|> redirect(to: "/")}
end
end
@impl true
def handle_info({:close_modal, _}, %{assigns: %{modal_return_to: to}} = socket) do
socket |> copy_flash() |> push_patch(to: to) |> live_noreply()
end
@impl true
def handle_info({:put_flash_message, {flash_type, msg}}, socket) do
socket |> put_flash(flash_type, msg) |> live_noreply()
end
defp property_from_params(params)
defp property_from_params(%{"id" => id}),
do: Properties.get_property!(id)
defp property_from_params(_params), do: %Property{}
defp list_properties do
Properties.list_properties()
end
end

View file

@ -1,46 +0,0 @@
<%= if @live_action in [:new, :edit] do %>
<%= live_modal @socket, Shift73kWeb.PropertyLive.FormComponent,
id: @property.id || :new,
title: @page_title,
action: @live_action,
property: @property,
current_user: @current_user %>
<% end %>
<div class="d-flex justify-content-between d-flex align-items-end">
<h2>Listing Properties</h2>
<span><%= live_patch "New Property", to: Routes.property_index_path(@socket, :new), class: "btn btn-primary" %></span>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Price</th>
<th scope="col">Description</th>
<th></th>
</tr>
</thead>
<tbody id="properties">
<%= for property <- @properties do %>
<tr id="property-<%= property.id %>">
<td><%= property.name %></td>
<td><%= property.price %></td>
<td><%= property.description %></td>
<td>
<%= if Roles.can?(@current_user, property, :show) do %>
<%= live_redirect "Show", to: Routes.property_show_path(@socket, :show, property), class: "link-secondary mx-1" %>
<% end %>
<%= if Roles.can?(@current_user, property, :edit) do %>
<%= live_patch "Edit", to: Routes.property_index_path(@socket, :edit, property), class: "mx-1" %>
<% end %>
<%= if Roles.can?(@current_user, property, :delete) do %>
<%= link "Delete", to: "#", phx_click: "delete", phx_value_id: property.id, data: [confirm: "Are you sure?"], class: "link-danger mx-1" %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>

View file

@ -1,45 +0,0 @@
defmodule Shift73kWeb.PropertyLive.Show do
use Shift73kWeb, :live_view
alias Shift73k.Properties
alias Shift73kWeb.Roles
@impl true
def mount(_params, session, socket) do
socket = assign_defaults(socket, session)
{:ok, socket}
end
@impl true
def handle_params(%{"id" => id}, _, socket) do
current_user = socket.assigns.current_user
live_action = socket.assigns.live_action
property = Properties.get_property!(id)
if Roles.can?(current_user, property, live_action) do
socket
|> assign(:property, property)
|> assign(:page_title, page_title(live_action))
|> assign(:modal_return_to, Routes.property_show_path(socket, :show, property))
|> live_noreply()
else
socket
|> put_flash(:error, "Unauthorised")
|> redirect(to: "/")
|> live_noreply()
end
end
@impl true
def handle_info({:close_modal, _}, %{assigns: %{modal_return_to: to}} = socket) do
socket |> copy_flash() |> push_patch(to: to) |> live_noreply()
end
@impl true
def handle_info({:put_flash_message, {flash_type, msg}}, socket) do
socket |> put_flash(flash_type, msg) |> live_noreply()
end
defp page_title(:show), do: "Show Property"
defp page_title(:edit), do: "Edit Property"
end

View file

@ -1,33 +0,0 @@
<h2>Show Property</h2>
<%= if @live_action in [:edit] do %>
<%= live_modal @socket, Shift73kWeb.PropertyLive.FormComponent,
id: @property.id,
title: @page_title,
action: @live_action,
property: @property %>
<% end %>
<table class="table table-nonfluid">
<tbody>
<tr>
<th scope="row" class="text-end">Name</th>
<td><%= @property.name %></td>
</tr>
<tr>
<th scope="row" class="text-end">Price</th>
<td><%= @property.price %></td>
</tr>
<tr>
<th scope="row" class="text-end">Description</th>
<td><%= @property.description %></td>
</tr>
</tbody>
</table>
<%= if Roles.can?(@current_user, @property, :index) do %>
<%= live_redirect "Back", to: Routes.property_index_path(@socket, :index), class: "btn btn-secondary" %>
<% end %>
<%= if Roles.can?(@current_user, @property, :edit) do %>
<%= live_patch "Edit", to: Routes.property_show_path(@socket, :edit, @property), class: "btn btn-primary" %>
<% end %>

View file

@ -4,7 +4,7 @@ defmodule Shift73kWeb.Roles do
"""
alias Shift73k.Accounts.User
alias Shift73k.Properties.Property
# alias Shift73k.Properties.Property
@type entity :: struct()
@type action :: :new | :index | :edit | :show | :delete | :edit_role
@ -12,13 +12,13 @@ defmodule Shift73kWeb.Roles do
def can?(user, entity, action)
# Properties / Property
def can?(%User{role: :admin}, %Property{}, _any), do: true
def can?(%User{}, %Property{}, :index), do: true
def can?(%User{}, %Property{}, :new), do: true
def can?(%User{}, %Property{}, :show), do: true
def can?(%User{id: id}, %Property{user_id: id}, :edit), do: true
def can?(%User{id: id}, %Property{user_id: id}, :delete), do: true
# # Properties / Property
# def can?(%User{role: :admin}, %Property{}, _any), do: true
# def can?(%User{}, %Property{}, :index), do: true
# def can?(%User{}, %Property{}, :new), do: true
# def can?(%User{}, %Property{}, :show), do: true
# def can?(%User{id: id}, %Property{user_id: id}, :edit), do: true
# def can?(%User{id: id}, %Property{user_id: id}, :delete), do: true
# Accounts / User
def can?(%User{role: :admin}, %User{}, _any), do: true

View file

@ -92,12 +92,6 @@ defmodule Shift73kWeb.Router do
pipe_through([:browser, :require_authenticated_user, :user])
live("/user_dashboard", UserDashboardLive, :index)
live("/properties", PropertyLive.Index, :index)
live("/properties/new", PropertyLive.Index, :new)
live("/properties/:id/edit", PropertyLive.Index, :edit)
live("/properties/:id", PropertyLive.Show, :show)
live("/properties/:id/show/edit", PropertyLive.Show, :edit)
end
scope "/", Shift73kWeb do

View file

@ -19,9 +19,10 @@
<%# nav LEFT items %>
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<%= link "Properties", nav_link_opts(@conn, to: Routes.property_index_path(@conn, :index), class: "nav-link") %>
</li>
<%# normal navbar link example %>
<%# <li class="nav-item"> %>
<%#= link "Properties", nav_link_opts(@conn, to: Routes.property_index_path(@conn, :index), class: "nav-link") %>
<%# </li> %>
<%# ACTIVE page link example %>
<%# <li class="nav-item">
@ -33,7 +34,8 @@
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li> %>
<li class="nav-item dropdown">
<%# normal dropdown menu example %>
<%# <li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownExample" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownExample">
<li><a class="dropdown-item" href="#">Action</a></li>
@ -41,7 +43,7 @@
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
</li> %>
</ul>

File diff suppressed because it is too large Load diff

View file

@ -1,17 +0,0 @@
defmodule Shift73k.Repo.Migrations.CreateProperties do
use Ecto.Migration
def change do
create table(:properties, primary_key: false) do
add(:id, :binary_id, primary_key: true)
add(:name, :string)
add(:price, :decimal)
add(:description, :text)
add(:user_id, references(:users, type: :binary_id, on_delete: :delete_all), null: false)
timestamps()
end
create(index(:properties, [:user_id]))
end
end

View file

@ -10,11 +10,9 @@
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.
import Ecto.Query
alias Shift73k.Repo
alias Shift73k.Accounts
alias Shift73k.Accounts.User
alias Shift73k.Properties.Property
############################################################################
## INSERTING MOCK USER DATA
@ -48,7 +46,7 @@ this_path = Path.dirname(__ENV__.file)
users_json = Path.join(this_path, "MOCK_DATA_users.json")
count_to_take = 123
count_to_take = 65
mock_users = users_json |> File.read!() |> Jason.decode!() |> Enum.take_random(count_to_take)
@ -74,62 +72,3 @@ mock_users =
Repo.insert_all(User, mock_users)
# end
############################################################################
## IF ENV IS DEV
## INSERTING MOCK PROPERTIES DATA
Enum.each(1..10, fn i ->
%{
name: "Property #{i} - User 1",
price: :rand.uniform(5) * 100_000,
description: "Property that belongs to user 1",
user_id: user_1.id
}
|> Shift73k.Properties.create_property()
%{
name: "Property #{i} - User 2",
price: :rand.uniform(5) * 100_000,
description: "Property that belongs to user 2",
user_id: user_2.id
}
|> Shift73k.Properties.create_property()
%{
name: "Property #{i} - Admin",
price: :rand.uniform(5) * 100_000,
description: "Property that belongs to admin",
user_id: admin.id
}
|> Shift73k.Properties.create_property()
end)
# if Mix.env() == :dev do
# this_path = Path.dirname(__ENV__.file)
props_json = Path.join(this_path, "MOCK_DATA_properties.json")
count_to_take = 123
mock_props = props_json |> File.read!() |> Jason.decode!() |> Enum.take_random(count_to_take)
random_user_query = from(User, order_by: fragment("RANDOM()"), limit: 1)
mock_props =
Enum.map(mock_props, fn e ->
add_dt = NaiveDateTime.from_iso8601!(e["inserted_at"])
rand_user = Repo.one(random_user_query)
%{
name: e["name"],
price: e["price"],
description: e["description"],
user_id: rand_user.id,
inserted_at: add_dt,
updated_at: add_dt
}
end)
Repo.insert_all(Property, mock_props)
# end

View file

@ -1,82 +0,0 @@
defmodule Shift73k.PropertiesTest do
use Shift73k.DataCase
alias Shift73k.Properties
import Shift73k.AccountsFixtures
describe "properties" do
alias Shift73k.Properties.Property
@valid_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}
def property_fixture(attrs \\ %{}) do
user = user_fixture()
{:ok, property} =
attrs
|> Enum.into(@valid_attrs)
|> Enum.into(%{user_id: user.id})
|> Properties.create_property()
property
end
test "list_properties/0 returns all properties" do
property = property_fixture()
assert Properties.list_properties() == [property]
end
test "get_property!/1 returns the property with given id" do
property = property_fixture()
assert Properties.get_property!(property.id) == property
end
test "create_property/1 with valid data creates a property" do
user = user_fixture()
create_attributes = Enum.into(%{user_id: user.id}, @valid_attrs)
assert {:ok, %Property{} = property} = Properties.create_property(create_attributes)
assert property.description == "some description"
assert property.name == "some name"
assert property.price == Decimal.new("120.5")
assert property.user_id == user.id
end
test "create_property/1 with invalid data returns error changeset" do
user = user_fixture()
create_attributes = Enum.into(%{user_id: user.id}, @invalid_attrs)
assert {:error, %Ecto.Changeset{}} = Properties.create_property(create_attributes)
end
test "update_property/2 with valid data updates the property" do
property = property_fixture()
assert {:ok, %Property{} = property} = Properties.update_property(property, @update_attrs)
assert property.description == "some updated description"
assert property.name == "some updated name"
assert property.price == Decimal.new("456.7")
end
test "update_property/2 with invalid data returns error changeset" do
property = property_fixture()
assert {:error, %Ecto.Changeset{}} = Properties.update_property(property, @invalid_attrs)
assert property == Properties.get_property!(property.id)
end
test "delete_property/1 deletes the property" do
property = property_fixture()
assert {:ok, %Property{}} = Properties.delete_property(property)
assert_raise Ecto.NoResultsError, fn -> Properties.get_property!(property.id) end
end
test "change_property/1 returns a property changeset" do
property = property_fixture()
assert %Ecto.Changeset{} = Properties.change_property(property)
end
end
end

View file

@ -1,409 +0,0 @@
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&#39;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&#39;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&#39;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&#39;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&#39;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