From d991990ad726d41df9f1dbcde4dec3a7628c210b Mon Sep 17 00:00:00 2001 From: Kerdo Kurs <kerdokurs@gmail.com> Date: Tue, 3 Dec 2024 14:40:54 +0200 Subject: [PATCH] update BDD to match 3.2 and 3.3 #29 --- features/contexts/property_search_context.exs | 22 ++++++ features/property_search.feature | 23 ++++-- .../properties_html/index.html.heex | 2 +- .../controllers/search_controller.ex | 71 +++++++++++-------- 4 files changed, 82 insertions(+), 36 deletions(-) diff --git a/features/contexts/property_search_context.exs b/features/contexts/property_search_context.exs index 7674333..5ac58ab 100644 --- a/features/contexts/property_search_context.exs +++ b/features/contexts/property_search_context.exs @@ -168,6 +168,28 @@ defmodule PropertySearchContext do {:ok, state} end + and_ ~r/^I go to the homepage$/, fn state -> + navigate_to("/") + {:ok, state} + end + + then_ ~r/^I should not see a search button$/, fn state -> + refute visible_in_page? ~r/Search/ + els = find_all_elements(:id, "search_button") + assert length(els) == 0 + {:ok, state} + end + + and_ ~r/^I go to the search URL$/, fn state -> + navigate_to("/search") + {:ok, state} + end + + then_ ~r/^I should be redirected to the login page$/, fn state -> + assert current_path() == "/login" + {:ok, state} + end + defp setup_session(email, password) do navigate_to("/login") fill_field({:id, "email"}, email) diff --git a/features/property_search.feature b/features/property_search.feature index df75d1a..3c0b40d 100644 --- a/features/property_search.feature +++ b/features/property_search.feature @@ -1,6 +1,6 @@ Feature: FR-13, FR-19 & FR-20: Property Search and Revisit - Scenario: User should see filter options and boundaries for search + Scenario: 3.2, 3.3 Authenticated user should see filter options and boundaries for search Given there exists following accounts | name | surname | birth_date | phone_number | email | password | confirm_password | | Existing | Account | 2000-01-01 | 000 | existing.account@gmail.com | password | password | @@ -10,6 +10,7 @@ Feature: FR-13, FR-19 & FR-20: Property Search and Revisit | Rent property | Also a really cool property | rent | apartment | available | London, UK | 3 | 160.0 | 2 | 5 | 6000 | | Rent property Tartu | Also a really cool property | rent | apartment | available | Tartu, Estonia | 2 | 120.0 | 1 | 2 | 800 | | Buy property Tartu | Also a really cool property | sell | apartment | available | Tartu, Estonia | 2 | 120.0 | 1 | 2 | 800000 | + And I am logged in And I want to perform a search Then I should see all countries of the properties in the filter options And I should see all cities of the properties in the filter options @@ -18,7 +19,7 @@ Feature: FR-13, FR-19 & FR-20: Property Search and Revisit And I should see a max room filter with the room count of the properties And I should see a min room filter with the room count of the properties - Scenario: User should be able to search with default values + Scenario: 3.2, 3.3 Authenticated user should be able to search with default values Given there exists following accounts | name | surname | birth_date | phone_number | email | password | confirm_password | | Existing | Account | 2000-01-01 | 000 | existing.account@gmail.com | password | password | @@ -28,11 +29,26 @@ Feature: FR-13, FR-19 & FR-20: Property Search and Revisit | Rent property | Also a really cool property | rent | apartment | available | London, UK | 3 | 160.0 | 2 | 5 | 6000 | | Rent property Tartu | Also a really cool property | rent | apartment | available | Tartu, Estonia | 2 | 120.0 | 1 | 2 | 800 | | Buy property Tartu | Also a really cool property | sell | apartment | available | Tartu, Estonia | 2 | 120.0 | 1 | 2 | 800000 | + And I am logged in And I want to perform a search When I click on the search button Then I should see "3" properties - Scenario: Authenticated users should see saved search queries + Scenario: 3.2 Unauthenticated user should not see a search button + Given there exists following accounts + | name | surname | birth_date | phone_number | email | password | confirm_password | + | Existing | Account | 2000-01-01 | 000 | existing.account@gmail.com | password | password | + And I go to the homepage + Then I should not see a search button + + Scenario: 3.2 Unauthenticated user should be redirected to login when visiting search URL + Given there exists following accounts + | name | surname | birth_date | phone_number | email | password | confirm_password | + | Existing | Account | 2000-01-01 | 000 | existing.account@gmail.com | password | password | + And I go to the search URL + Then I should be redirected to the login page + + Scenario: 3.2 Authenticated users should see saved search queries Given there exists following accounts | name | surname | birth_date | phone_number | email | password | confirm_password | | Existing | Account | 2000-01-01 | 000 | existing.account@gmail.com | password | password | @@ -44,4 +60,3 @@ Feature: FR-13, FR-19 & FR-20: Property Search and Revisit And I perform a search And I want to search Then I should see a saved search query - diff --git a/lib/proptrackr_web/controllers/properties_html/index.html.heex b/lib/proptrackr_web/controllers/properties_html/index.html.heex index c091484..47aeaca 100644 --- a/lib/proptrackr_web/controllers/properties_html/index.html.heex +++ b/lib/proptrackr_web/controllers/properties_html/index.html.heex @@ -16,7 +16,7 @@ </.link> <% end %> - <%= if not @is_search do %> + <%= if not @is_search and @conn.assigns[:current_user] do %> <.link href={~p"/search"}> <.button type="button" class="text-white rounded px-4 py-2" id="search"> Search diff --git a/lib/proptrackr_web/controllers/search_controller.ex b/lib/proptrackr_web/controllers/search_controller.ex index 44f4521..183515c 100644 --- a/lib/proptrackr_web/controllers/search_controller.ex +++ b/lib/proptrackr_web/controllers/search_controller.ex @@ -10,42 +10,51 @@ defmodule PropTrackrWeb.SearchController do alias PropTrackr.NotInterested def index(conn, _params) do - min_price = from(p in Property, select: min(p.price)) |> Repo.one - max_price = from(p in Property, select: max(p.price)) |> Repo.one + current_user = conn.assigns.current_user - min_rooms = 0 - max_rooms = from(p in Property, select: max(p.room_count)) |> Repo.one - locations = from(p in Property, select: p.location) |> Repo.all - cities = Enum.map(locations, fn location -> String.split(location, ",") |> hd |> String.trim() end) - cities = Enum.uniq(cities) - countries = Enum.map(locations, fn location -> String.split(location, ",") |> tl |> hd |> String.trim() end) - countries = Enum.uniq(countries) + if current_user do + min_price = from(p in Property, select: min(p.price)) |> Repo.one + max_price = from(p in Property, select: max(p.price)) |> Repo.one - changeset = Search.changeset(%Search{}) + min_rooms = 0 + max_rooms = from(p in Property, select: max(p.room_count)) |> Repo.one - current_user = conn.assigns.current_user - previous_searches = case current_user do - nil -> [] - _ -> - Repo.all( - from s in Search, - where: s.user_id == ^current_user.id, - order_by: [desc: s.inserted_at] - ) - end + locations = from(p in Property, select: p.location) |> Repo.all + cities = Enum.map(locations, fn location -> String.split(location, ",") |> hd |> String.trim() end) + cities = Enum.uniq(cities) + countries = Enum.map(locations, fn location -> String.split(location, ",") |> tl |> hd |> String.trim() end) + countries = Enum.uniq(countries) - render( - conn, "search.html", - changeset: changeset, - min_price: min_price, - max_price: max_price, - min_rooms: min_rooms, - max_rooms: max_rooms, - cities: cities, - countries: countries, - previous_searches: previous_searches - ) + changeset = Search.changeset(%Search{}) + + current_user = conn.assigns.current_user + previous_searches = case current_user do + nil -> [] + _ -> + Repo.all( + from s in Search, + where: s.user_id == ^current_user.id, + order_by: [desc: s.inserted_at] + ) + end + + render( + conn, "search.html", + changeset: changeset, + min_price: min_price, + max_price: max_price, + min_rooms: min_rooms, + max_rooms: max_rooms, + cities: cities, + countries: countries, + previous_searches: previous_searches + ) + else + conn + |> put_flash(:error, "You are not logged in") + |> redirect(to: "/login") + end end def create(conn, %{ "search" => search }) do -- GitLab