Skip to content
Snippets Groups Projects
Commit 2ecac50d authored by kerdo's avatar kerdo
Browse files

Merge branch '22-fr-12-view-own-advertisements' into 'main'

Added TDD for "FR-12: View Own Advertisements"

Closes #22

See merge request !18
parents ea203658 2c42b894
No related branches found
No related tags found
1 merge request!18Added TDD for "FR-12: View Own Advertisements"
Pipeline #45121 passed
defmodule PropTrackrWeb.MyPropertiesControllerTest do
use PropTrackrWeb.ConnCase
alias PropTrackr.Accounts.User
alias PropTrackr.Properties.Property
alias PropTrackr.Repo
@valid_property %{
title: "Apartment",
description: "Small apartment with a beautiful view of the city center",
type: "rent",
property_type: "apartment",
location: "Tartu, Estonia",
room_count: 1,
area: 13.0,
floor: 2,
floor_count: 4,
price: 430.0,
}
setup do
# Create test user
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)
# Create another user to test property visibility
other_user = %User{
name: "Other",
surname: "User",
birth_date: "2000-01-01",
phone_number: "111",
bio: "Hey",
email: "other.user@gmail.com",
password: "testing",
confirm_password: "testing",
}
other_user = Repo.insert!(other_user)
# Create some test properties for the main user
property1 = %Property{
reference: Ecto.UUID.generate(),
user_id: user.id
}
property1 = property1
|> Property.changeset(@valid_property)
|> Repo.insert!()
property2 = %Property{
reference: Ecto.UUID.generate(),
user_id: user.id
}
property2 = property2
|> Property.changeset(%{@valid_property | title: "Second Property", price: 500.0})
|> Repo.insert!()
# Create a property for the other user
other_property = %Property{
reference: Ecto.UUID.generate(),
user_id: other_user.id
}
other_property = other_property
|> Property.changeset(%{@valid_property | title: "Other User Property"})
|> Repo.insert!()
{:ok, %{
user: user,
other_user: other_user,
property1: property1,
property2: property2,
other_property: other_property
}}
end
test "Authenticated user can view their properties", %{conn: conn, user: user, property1: property1, property2: property2, other_property: other_property} do
# Login user
conn = conn |> setup_session(user)
conn = get(conn, "/me")
response = html_response(conn, 200)
assert response =~ ~s|id="my_properties"|
# Access my properties page
conn = get(conn, "/me/properties")
assert html_response(conn, 200) =~ "My Properties"
# Assert successful response
response = html_response(conn, 200)
# Check if user's properties are visible
assert response =~ property1.title
assert response =~ property2.title
assert response =~ Float.to_string(property1.price)
assert response =~ Float.to_string(property2.price)
assert response =~ Float.to_string(property1.area)
assert response =~ Float.to_string(property1.area)
# Make sure other user's property is not visible
refute response =~ other_property.title
end
test "Unauthorized user cannot access my properties page", %{conn: conn} do
conn = get conn, "/me"
assert redirected_to(conn) == "/login"
conn = get conn, redirected_to(conn)
assert html_response(conn, 200) =~ ~r/You are not logged in!/
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