Skip to content
Snippets Groups Projects
Commit a8c6df53 authored by Kerdo Kurs's avatar Kerdo Kurs
Browse files

add TDD for #18

parent 9a35007c
No related branches found
No related tags found
1 merge request!13Resolve "FR-08: Create Property Advertisement" and "FR-07: View Properties (Unauthenticated)"
Pipeline #44601 passed
......@@ -52,12 +52,16 @@ defmodule PropTrackr.Properties.Property do
cond do
floor_count < floor ->
add_error(changeset, :floor_count, "Floor count must be greater than floor")
changeset
|> add_error(:floor_count, "Floor count must be greater than floor")
floor_count < 1 ->
add_error(changeset, :floor_count, "Floor count must be greater than 0")
changeset
|> add_error(:floor_count, "Floor count must be greater than 0")
true ->
changeset
end
else
changeset
end
end
end
......@@ -22,23 +22,27 @@ defmodule PropTrackrWeb.PropertiesController do
def create(conn, %{"property" => property}) do
current_user = conn.assigns.current_user
values_to_atoms = [:type, :property_type, :state]
property = property |> Map.put("reference", Ecto.UUID.generate())
property_assoc = Ecto.build_assoc(current_user, :properties, Enum.map(property, fn {key, value} -> {String.to_atom(key), value} end))
property_changeset = Property.changeset(property_assoc, property)
# Note: IDK this does not work properly
# |> Ecto.Changeset.put_change(:reference, Ecto.UUID.generate())
case Repo.insert(property_changeset) do
{:ok, property} ->
conn
|> put_flash(:info, "Property created successfully!")
|> redirect(to: ~p"/properties/#{property.reference}")
{:error, changeset} ->
render conn, "new.html", changeset: changeset
if current_user == nil do
conn
|> put_flash(:error, "You are not logged in!")
|> redirect(to: "/")
else
property = property |> Map.put("reference", Ecto.UUID.generate())
property_assoc = Ecto.build_assoc(current_user, :properties, Enum.map(property, fn {key, value} -> {String.to_atom(key), value} end))
property_changeset = Property.changeset(property_assoc, property)
# Note: IDK this does not work properly
# |> Ecto.Changeset.put_change(:reference, Ecto.UUID.generate())
case Repo.insert(property_changeset) do
{:ok, property} ->
conn
|> put_flash(:info, "Property created successfully!")
|> redirect(to: ~p"/properties/#{property.reference}")
{:error, changeset} ->
render conn, "new.html", changeset: changeset
end
end
end
......
defmodule PropTrackrWeb.PropertiesControllerTest do
use PropTrackrWeb.ConnCase
alias PropTrackr.Accounts.User
alias PropTrackr.Properties.Property
alias PropTrackr.Repo
@valid_data %{
title: "Apartment",
description: "Small apartment",
type: "rent",
property_type: "apartment",
location: "Tartu, Estonia",
room_count: 1,
area: 13.0,
floor: 2,
floor_count: 4,
price: 430.0,
}
@invalid_data %{
title: "wrong",
description: "invalid_obvs",
type: "rent",
property_type: "apartment",
location: "Tartu, Estonia",
room_count: 0,
area: -1.0,
floor: -1,
floor_count: -1,
price: -1.0,
}
setup do
user = %User{
name: "Test",
surname: "User",
birth_date: "2000-01-01",
phone_number: "000",
bio: "Yo",
email: "test.user@gmail.com",
password: "testing",
confirm_password: "testing",
}
user = Repo.insert!(user)
{:ok, %{user: user}}
end
test "Authenticated user should be able to create a new property advertisement with valid data", %{ conn: conn, user: user } do
conn = conn |> setup_session(user)
conn = conn |> post("/properties", property: @valid_data)
uuidv4_regex = ~r/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
path = String.split(redirected_to(conn), "/")
assert Enum.at(path, -1) =~ uuidv4_regex
assert Enum.at(path, -2) == "properties"
advertisement_reference = Enum.at(path, -1)
conn = get conn, redirected_to(conn)
assert html_response(conn, 200) =~ "Property created successfully!"
advertisement = Repo.get_by!(Property, reference: advertisement_reference)
assert advertisement.title == @valid_data[:title]
assert advertisement.description == @valid_data[:description]
assert advertisement.type == String.to_atom(@valid_data[:type])
assert advertisement.property_type == String.to_atom(@valid_data[:property_type])
assert advertisement.location == @valid_data[:location]
assert advertisement.room_count == @valid_data[:room_count]
assert advertisement.area == @valid_data[:area]
assert advertisement.floor == @valid_data[:floor]
assert advertisement.floor_count == @valid_data[:floor_count]
assert advertisement.price == @valid_data[:price]
end
test "Authenticated user should not be able to create an advertisement with invalid data", %{ conn: conn, user: user } do
conn = conn |> setup_session(user)
conn = conn |> post("/properties", property: @invalid_data)
assert html_response(conn, 200) =~ "must be greater than 0"
end
test "Authenticated user should not be able to create an advertisement with invalid floor values", %{ conn: conn, user: user } do
conn = conn |> setup_session(user)
data = %{ @invalid_data | floor: 2, floor_count: 1 }
conn = conn |> post("/properties", property: data)
assert html_response(conn, 200) =~ "Floor count must be greater than floor"
end
test "Authenticated user should not be able to create an advertisement with invalid floor count data on a house listing", %{ conn: conn, user: user } do
conn = conn |> setup_session(user)
data = %{ @invalid_data | property_type: :house, floor: 2, floor_count: 1 }
conn = conn |> post("/properties", property: data)
# Just assert that a redirect happened, don't care about data since it does not matter here
assert redirected_to(conn) != "/properties"
end
test "Unauthenticated user should be redirected to the homepage", %{ conn: conn, user: user } do
conn = post(conn, "/properties", property: @valid_data)
assert redirected_to(conn) == "/"
end
defp setup_session(conn, user) do
conn = conn |> post("/login", email: user.email, password: user.password)
conn = get conn, redirected_to(conn)
conn
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment