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

Merge branch '46-fr-30-profile-page' into 'main'

Resolve "FR-30: Profile page"

Closes #46

See merge request !12
parents 6f49f310 a9c334eb
No related branches found
No related tags found
1 merge request!12Resolve "FR-30: Profile page"
Pipeline #44575 passed
......@@ -17,4 +17,8 @@ defmodule WhiteBreadConfig do
context: PasswordChangeContext,
feature_paths: ["features/password_change.feature"]
suite name: "User Profile Features",
context: UserProfileContext,
feature_paths: ["features/user_profile.feature"]
end
defmodule UserProfileContext do
use WhiteBread.Context
use Hound.Helpers
alias PropTrackr.Accounts
alias PropTrackr.Repo
alias PropTrackr.Accounts.User
scenario_starting_state fn _state ->
Ecto.Adapters.SQL.Sandbox.checkout(PropTrackr.Repo)
Ecto.Adapters.SQL.Sandbox.mode(PropTrackr.Repo, {:shared, self()})
Hound.start_session()
%{}
end
scenario_finalize fn _status, _state ->
Ecto.Adapters.SQL.Sandbox.checkin(PropTrackr.Repo)
Hound.end_session()
end
given_ ~r/^there exists following accounts$/, fn state, %{table_data: table} ->
table
|> Enum.map(fn user_details -> User.changeset(%User{}, user_details) end)
|> Enum.each(fn changeset -> Repo.insert!(changeset) end)
existing_user = List.first(table)
{
:ok,
state
|> Map.put(:user, existing_user)
}
end
and_ ~r/^I am logged in$/, fn state ->
setup_session(state[:user][:email], state[:user][:password])
{:ok, state}
end
when_ ~r/^I go to the profile page$/, fn state ->
navigate_to("/me")
{:ok, state}
end
then_ ~r/^I should see my profile details$/, fn state ->
assert visible_in_page? ~r/#{state[:user][:name]} #{state[:user][:surname]}'s profile/
assert visible_in_page? ~r/#{state[:user][:email]}/
assert visible_in_page? ~r/#{state[:user][:phone_number]}/
assert visible_in_page? ~r/#{state[:user][:bio]}/
assert visible_in_page? ~r/#{state[:user][:birth_date]}/
{:ok, state}
end
given_ ~r/^I am not logged in$/, fn state ->
navigate_to("/logout")
{:ok, state}
end
then_ ~r/^I should see an error message$/, fn state ->
assert visible_in_page? ~r/You are not logged in!/
{:ok, state}
end
and_ ~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)
fill_field({:id, "password"}, password)
click({:id, "login_button"})
end
end
Feature: FR-30 User profile
Scenario: Authenticated user should be able to view their profile
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 am logged in
When I go to the profile page
Then I should see my profile details
Scenario: Unauthenticated user should be shown an error message and redirected to the homepage
Given I am not logged in
When I go to the profile page
Then I should see an error message
And I should be redirected to the login page
......@@ -9,7 +9,7 @@ defmodule PropTrackrWeb.ProfileController do
user_id = get_session(conn, :user_id)
if user_id == nil do
conn
|> put_flash(:error, "You are not logged!")
|> put_flash(:error, "You are not logged in!")
|> redirect(to: ~p"/login")
else
user = Repo.get(User, user_id)
......
defmodule PropTrackrWeb.ProfileControllerTest do
use PropTrackrWeb.ConnCase
alias PropTrackr.Accounts.User
alias PropTrackr.Repo
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 see their data on the profile page", %{conn: conn, user: user} do
conn = conn |> setup_session(user)
conn = get conn, "/me"
assert html_response(conn, 200) =~ ~r/#{user.name} #{user.surname}'s profile/
assert html_response(conn, 200) =~ ~r/#{user.email}/
assert html_response(conn, 200) =~ ~r/#{user.phone_number}/
assert html_response(conn, 200) =~ ~r/#{user.bio}/
assert html_response(conn, 200) =~ ~r/#{user.birth_date}/
end
test "Unauthenticated user should see an error message", %{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
test "Unauthenticated user should be redirected to the login page", %{conn: conn} do
conn = get conn, "/me"
assert redirected_to(conn) == "/login"
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