Skip to content
Snippets Groups Projects
Commit 0db37470 authored by zeshan's avatar zeshan
Browse files

Edit Advertisement BDD updated

parent 46c955ba
No related branches found
No related tags found
1 merge request!107Edit Advertisement BDD updated
Pipeline #47099 passed
......@@ -16,4 +16,15 @@ Feature: Advertisement Management
Then the advertisement should not be visible in the advertisements list
And the user should see a message "Advertisement creation failed"
Scenario: User edits an advertisement
Given a registered and logged-in user with an existing advertisement titled "Beautiful Apartment" priced at "1500"
When the user edits the advertisement to change the title to "Luxury Apartment" and the price to "2000"
Then the advertisement should be updated in the advertisements list with the new title "Luxury Apartment" and price "2000"
And the user should see a flash indicating "Advertisement Updated Successfully"
Scenario: Negative Case: User fails to edit an advertisement with invalid data
Given a registered and logged-in user with an existing advertisement title "Beautiful Apartment" priced at "1500"
When the user tries to edit the advertisement and leaves the title and price empty
Then the advertisement should retain the original title "Beautiful Apartment" and price "1500" in the advertisements list
And the user should see a flash "Advertisement update failed"
......@@ -194,4 +194,185 @@ defmodule ProptrackerWeb.Features.Contexts.AdvertisementContext do
{:ok, state}
end
end
### Edit Advertisement
# Given a registered and logged-in user with an existing advertisement
given_ ~r/^a registered and logged-in user with an existing advertisement titled "(?<argument_one>[^"]+)" priced at "(?<argument_two>[^"]+)" $/ do
fn state, %{title: title, price: price} ->
# Prepare user registration data
user_data = %{username: "edit_user", password: "password123", password_confirmation: "password123"}
# Register the user
changeset = User.registration_changeset(%User{}, user_data)
{:ok, user} = Repo.insert(changeset)
# Simulate user login by initializing session
conn = %Plug.Conn{} |> Plug.Test.init_test_session(user_id: user.id)
# Create an existing advertisement
ad_data = %{
user_id: user.id,
title: title,
price: price,
description: "A sample advertisement",
type: "rent",
square_meters: 120,
location: "Tallinn",
rooms: 3,
floor: 2,
total_floors: 5
}
{:ok, advertisement} = Repo.insert(%Advertisement{} |> Advertisement.changeset(ad_data))
{:ok, state |> Map.put(:user, user) |> Map.put(:conn, conn) |> Map.put(:advertisement, advertisement)}
end
end
# When the user edits the advertisement to change the title and price
when_ ~r/^the user edits the advertisement to change the title to "(?<argument_one>[^"]+)" and the price to "(?<argument_two>[^"]+)" $/ do
fn state, %{new_title: new_title, new_price: new_price} ->
conn = state[:conn]
advertisement = state[:advertisement]
# Prepare updated advertisement data
updated_ad_data = %{
"title" => new_title,
"price" => new_price
}
# Simulate form submission to update the advertisement
conn =
Phoenix.ConnTest.put(
conn,
"/advertisements/#{advertisement.id}",
%{"advertisement" => updated_ad_data}
)
{:ok, state |> Map.put(:conn, conn)}
end
end
# Then the advertisement should be updated in the advertisements list
then_ ~r/^the advertisement should be updated in the advertisements list with the new title "(?<argument_one>[^"]+)" and price "(?<argument_two>[^"]+)" $/ do
fn state, %{new_title: new_title, new_price: new_price} ->
advertisement = state[:advertisement]
# Verify the advertisement is updated in the database
updated_ad = Repo.get(Advertisement, advertisement.id)
assert updated_ad.title == new_title
assert updated_ad.price == new_price
{:ok, state}
end
end
# Then the advertisement should retain the original title and price
then_ ~r/^the advertisement should retain the original title "(?<original_title>[^"]+)" and price "(?<original_price>[^"]+)" in the advertisements list$/ do
fn state, %{original_title: original_title, original_price: original_price} ->
advertisement = state[:advertisement]
# Verify the advertisement remains unchanged in the database
unchanged_ad = Repo.get(Advertisement, advertisement.id)
assert unchanged_ad.title == original_title
assert unchanged_ad.price == original_price
{:ok, state}
end
end
# And the user should see a message indicating success or failure
and_ ~r/^the user should see a flash indicating "(?<argument_one>[^"]+)" $/ do
fn state, %{message: message} ->
conn = state[:conn]
# Verify the response contains the expected message
assert conn.resp_body =~ message
{:ok, state}
end
end
### Edit Advertisement Negative Case:
# Given a registered and logged-in user with an existing advertisement
given_ ~r/^a registered and logged-in user with an existing advertisement title "(?<argument_one>[^"]+)" priced at "(?<argument_two>[^"]+)" $/ do
fn state, %{title: title, price: price} ->
# Prepare user registration data
user_data = %{username: "edit_user", password: "password123", password_confirmation: "password123"}
# Register the user
changeset = User.registration_changeset(%User{}, user_data)
{:ok, user} = Repo.insert(changeset)
# Simulate user login by initializing session
conn = %Plug.Conn{} |> Plug.Test.init_test_session(user_id: user.id)
# Create an existing advertisement
ad_data = %{
user_id: user.id,
title: title,
price: price,
description: "A sample advertisement",
type: "rent",
square_meters: 120,
location: "Tallinn",
rooms: 3,
floor: 2,
total_floors: 5
}
{:ok, advertisement} = Repo.insert(%Advertisement{} |> Advertisement.changeset(ad_data))
{:ok, state |> Map.put(:user, user) |> Map.put(:conn, conn) |> Map.put(:advertisement, advertisement)}
end
end
# When the user tries to edit the advertisement and leaves the title and price empty
when_ ~r/^the user tries to edit the advertisement and leaves the title and price empty $/ do
fn state ->
conn = state[:conn]
advertisement = state[:advertisement]
# Prepare invalid advertisement data
invalid_ad_data = %{"title" => "", "price" => ""}
# Simulate form submission with invalid data
conn =
Phoenix.ConnTest.put(
conn,
"/advertisements/#{advertisement.id}",
%{"advertisement" => invalid_ad_data}
)
{:ok, state |> Map.put(:conn, conn)}
end
end
# Then the advertisement should retain the original title and price
then_ ~r/^the advertisement should retain the original title "(?<argument_one>[^"]+)" and price "(?<argument_two>[^"]+)" in the advertisements list $/ do
fn state, %{original_title: original_title, original_price: original_price} ->
advertisement = state[:advertisement]
# Verify the advertisement remains unchanged in the database
unchanged_ad = Repo.get(Advertisement, advertisement.id)
assert unchanged_ad.title == original_title
assert unchanged_ad.price == original_price
{:ok, state}
end
end
# And the user should see a flash "Advertisement update failed"
and_ ~r/^the user should see a flash "(?<argument_one>[^"]+)" $/ do
fn state, %{message: message} ->
conn = state[:conn]
# Verify the response contains the expected message
assert conn.resp_body =~ message
{:ok, state}
end
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