<%= for {kind, color} <- alert_kinds() do %>
<%= if flash_content = live_flash(@flash, kind) do %>
diff --git a/lib/bones73k_web/templates/user_settings/edit.html.eex b/lib/bones73k_web/templates/user_settings/edit.html.eex
deleted file mode 100644
index f6c0801e..00000000
--- a/lib/bones73k_web/templates/user_settings/edit.html.eex
+++ /dev/null
@@ -1,49 +0,0 @@
-
Settings
-
-
Change email
-
-<%= form_for @email_changeset, Routes.user_settings_path(@conn, :update_email), fn f -> %>
- <%= if @email_changeset.action do %>
-
-
Oops, something went wrong! Please check the errors below.
-
- <% end %>
-
- <%= label f, :email %>
- <%= email_input f, :email, required: true %>
- <%= error_tag f, :email %>
-
- <%= label f, :current_password, for: "current_password_for_email" %>
- <%= password_input f, :current_password, required: true, name: "current_password", id: "current_password_for_email" %>
- <%= error_tag f, :current_password %>
-
-
- <%= submit "Change email" %>
-
-<% end %>
-
-
Change password
-
-<%= form_for @password_changeset, Routes.user_settings_path(@conn, :update_password), fn f -> %>
- <%= if @password_changeset.action do %>
-
-
Oops, something went wrong! Please check the errors below.
-
- <% end %>
-
- <%= label f, :password, "New password" %>
- <%= password_input f, :password, required: true %>
- <%= error_tag f, :password %>
-
- <%= label f, :password_confirmation, "Confirm new password" %>
- <%= password_input f, :password_confirmation, required: true %>
- <%= error_tag f, :password_confirmation %>
-
- <%= label f, :current_password, for: "current_password_for_password" %>
- <%= password_input f, :current_password, required: true, name: "current_password", id: "current_password_for_password" %>
- <%= error_tag f, :current_password %>
-
-
- <%= submit "Change password" %>
-
-<% end %>
diff --git a/lib/bones73k_web/views/user_settings_view.ex b/lib/bones73k_web/views/user_settings_view.ex
deleted file mode 100644
index 27735e86..00000000
--- a/lib/bones73k_web/views/user_settings_view.ex
+++ /dev/null
@@ -1,3 +0,0 @@
-defmodule Bones73kWeb.UserSettingsView do
- use Bones73kWeb, :view
-end
diff --git a/test/bones73k/accounts_test.exs b/test/bones73k/accounts_test.exs
index f12d70ff..30b777a8 100644
--- a/test/bones73k/accounts_test.exs
+++ b/test/bones73k/accounts_test.exs
@@ -58,19 +58,19 @@ defmodule Bones73k.AccountsTest do
end
test "validates email and password when given" do
- {:error, changeset} = Accounts.register_user(%{email: "not valid", password: "not valid"})
-
- assert %{
- email: ["must have the @ sign and no spaces"],
- password: ["should be at least 12 character(s)"]
- } = errors_on(changeset)
+ {:error, changeset} = Accounts.register_user(%{email: "not valid", password: "2shrt"})
+ pw_err = "should be at least #{User.min_password()} character(s)"
+ assert "must be a valid email address" in errors_on(changeset).email
+ assert pw_err in errors_on(changeset).password
end
test "validates maximum values for email and password for security" do
- too_long = String.duplicate("db", 100)
+ too_long = "#{String.duplicate("db", 300)}@example.com"
{:error, changeset} = Accounts.register_user(%{email: too_long, password: too_long})
- assert "should be at most 160 character(s)" in errors_on(changeset).email
- assert "should be at most 80 character(s)" in errors_on(changeset).password
+ em_err = "should be at most #{User.max_email()} character(s)"
+ pw_err = "should be at most #{User.max_password()} character(s)"
+ assert em_err in errors_on(changeset).email
+ assert pw_err in errors_on(changeset).password
end
test "validates email uniqueness" do
@@ -92,16 +92,22 @@ defmodule Bones73k.AccountsTest do
assert is_nil(user.password)
assert user.role == :user
end
- end
- describe "register_admin/1" do
- test "registers users with a hashed password and sets role to :admin" do
+ test "registers different role :manager and sets role to :manager" do
email = unique_user_email()
- {:ok, user} = Accounts.register_admin(%{email: email, password: valid_user_password()})
+ attrs = %{email: email, role: :manager, password: valid_user_password()}
+ {:ok, user} = Accounts.register_user(attrs)
assert user.email == email
assert is_binary(user.hashed_password)
assert is_nil(user.confirmed_at)
assert is_nil(user.password)
+ assert user.role == :manager
+ end
+
+ test "registers different role :admin and sets role to :admin" do
+ email = unique_user_email()
+ attrs = %{email: email, role: :admin, password: valid_user_password()}
+ {:ok, user} = Accounts.register_user(attrs)
assert user.role == :admin
end
end
@@ -109,7 +115,7 @@ defmodule Bones73k.AccountsTest do
describe "change_user_registration/2" do
test "returns a changeset" do
assert %Ecto.Changeset{} = changeset = Accounts.change_user_registration(%User{})
- assert changeset.required == [:password, :email]
+ assert changeset.required == [:password, :email, :role]
end
end
@@ -126,45 +132,42 @@ defmodule Bones73k.AccountsTest do
end
test "requires email to change", %{user: user} do
- {:error, changeset} = Accounts.apply_user_email(user, valid_user_password(), %{})
+ attrs = %{"current_password" => valid_user_password()}
+ {:error, changeset} = Accounts.apply_user_email(user, attrs)
assert %{email: ["did not change"]} = errors_on(changeset)
end
test "validates email", %{user: user} do
- {:error, changeset} =
- Accounts.apply_user_email(user, valid_user_password(), %{email: "not valid"})
-
- assert %{email: ["must have the @ sign and no spaces"]} = errors_on(changeset)
+ attrs = %{"current_password" => valid_user_password(), "email" => "not valid"}
+ {:error, changeset} = Accounts.apply_user_email(user, attrs)
+ assert %{email: ["must be a valid email address"]} = errors_on(changeset)
end
test "validates maximum value for email for security", %{user: user} do
- too_long = String.duplicate("db", 100)
-
- {:error, changeset} =
- Accounts.apply_user_email(user, valid_user_password(), %{email: too_long})
-
- assert "should be at most 160 character(s)" in errors_on(changeset).email
+ too_long = "#{String.duplicate("db", 300)}@example.com"
+ attrs = %{"current_password" => valid_user_password(), "email" => too_long}
+ {:error, changeset} = Accounts.apply_user_email(user, attrs)
+ em_err = "should be at most #{User.max_email()} character(s)"
+ assert em_err in errors_on(changeset).email
end
test "validates email uniqueness", %{user: user} do
%{email: email} = user_fixture()
-
- {:error, changeset} =
- Accounts.apply_user_email(user, valid_user_password(), %{email: email})
-
+ attrs = %{"current_password" => valid_user_password(), "email" => email}
+ {:error, changeset} = Accounts.apply_user_email(user, attrs)
assert "has already been taken" in errors_on(changeset).email
end
test "validates current password", %{user: user} do
- {:error, changeset} =
- Accounts.apply_user_email(user, "invalid", %{email: unique_user_email()})
-
+ attrs = %{"current_password" => "invalid", "email" => unique_user_email()}
+ {:error, changeset} = Accounts.apply_user_email(user, attrs)
assert %{current_password: ["is not valid"]} = errors_on(changeset)
end
test "applies the email without persisting it", %{user: user} do
email = unique_user_email()
- {:ok, user} = Accounts.apply_user_email(user, valid_user_password(), %{email: email})
+ attrs = %{"current_password" => valid_user_password(), "email" => email}
+ {:ok, user} = Accounts.apply_user_email(user, attrs)
assert user.email == email
assert Accounts.get_user!(user.id).email != email
end
@@ -245,52 +248,47 @@ defmodule Bones73k.AccountsTest do
end
test "validates password", %{user: user} do
- {:error, changeset} =
- Accounts.update_user_password(user, valid_user_password(), %{
- password: "not valid",
- password_confirmation: "another"
- })
+ attrs = %{
+ "current_password" => valid_user_password(),
+ "password" => "2shrt",
+ "password_confirmation" => "another"
+ }
- assert %{
- password: ["should be at least 12 character(s)"],
- password_confirmation: ["does not match password"]
- } = errors_on(changeset)
+ {:error, changeset} = Accounts.update_user_password(user, attrs)
+ pw_err = "should be at least #{User.min_password()} character(s)"
+ conf_err = "does not match password"
+ assert pw_err in errors_on(changeset).password
+ assert conf_err in errors_on(changeset).password_confirmation
end
test "validates maximum values for password for security", %{user: user} do
- too_long = String.duplicate("db", 100)
+ attrs = %{
+ "current_password" => valid_user_password(),
+ "password" => String.duplicate("db", 100)
+ }
- {:error, changeset} =
- Accounts.update_user_password(user, valid_user_password(), %{password: too_long})
-
- assert "should be at most 80 character(s)" in errors_on(changeset).password
+ {:error, changeset} = Accounts.update_user_password(user, attrs)
+ pw_err = "should be at most #{User.max_password()} character(s)"
+ assert pw_err in errors_on(changeset).password
end
test "validates current password", %{user: user} do
- {:error, changeset} =
- Accounts.update_user_password(user, "invalid", %{password: valid_user_password()})
-
+ attrs = %{"current_password" => "invalid", "password" => valid_user_password()}
+ {:error, changeset} = Accounts.update_user_password(user, attrs)
assert %{current_password: ["is not valid"]} = errors_on(changeset)
end
test "updates the password", %{user: user} do
- {:ok, user} =
- Accounts.update_user_password(user, valid_user_password(), %{
- password: "new valid password"
- })
-
+ attrs = %{"current_password" => valid_user_password(), "password" => "new valid password"}
+ {:ok, user} = Accounts.update_user_password(user, attrs)
assert is_nil(user.password)
assert Accounts.get_user_by_email_and_password(user.email, "new valid password")
end
test "deletes all tokens for the given user", %{user: user} do
_ = Accounts.generate_user_session_token(user)
-
- {:ok, _} =
- Accounts.update_user_password(user, valid_user_password(), %{
- password: "new valid password"
- })
-
+ attrs = %{"current_password" => valid_user_password(), "password" => "new valid password"}
+ {:ok, _} = Accounts.update_user_password(user, attrs)
refute Repo.get_by(UserToken, user_id: user.id)
end
end
@@ -456,14 +454,13 @@ defmodule Bones73k.AccountsTest do
test "validates password", %{user: user} do
{:error, changeset} =
Accounts.reset_user_password(user, %{
- password: "not valid",
+ password: "2shrt",
password_confirmation: "another"
})
- assert %{
- password: ["should be at least 12 character(s)"],
- password_confirmation: ["does not match password"]
- } = errors_on(changeset)
+ pw_err = "should be at least #{User.min_password()} character(s)"
+ assert pw_err in errors_on(changeset).password
+ assert "does not match password" in errors_on(changeset).password_confirmation
end
test "validates maximum values for password for security", %{user: user} do
diff --git a/test/bones73k_web/controllers/user_registration_controller_test.exs b/test/bones73k_web/controllers/user_registration_controller_test.exs
index f1d708e4..5466e1d3 100644
--- a/test/bones73k_web/controllers/user_registration_controller_test.exs
+++ b/test/bones73k_web/controllers/user_registration_controller_test.exs
@@ -13,7 +13,8 @@ defmodule Bones73kWeb.UserRegistrationControllerTest do
end
test "redirects if already logged in", %{conn: conn} do
- conn = conn |> log_in_user(user_fixture()) |> get(Routes.user_registration_path(conn, :new))
+ to = Routes.user_registration_path(conn, :new)
+ conn = conn |> log_in_user(user_fixture()) |> get(to)
assert redirected_to(conn) == "/"
end
end
diff --git a/test/bones73k_web/controllers/user_reset_password_controller_test.exs b/test/bones73k_web/controllers/user_reset_password_controller_test.exs
index 9a7de0ec..6e173ae6 100644
--- a/test/bones73k_web/controllers/user_reset_password_controller_test.exs
+++ b/test/bones73k_web/controllers/user_reset_password_controller_test.exs
@@ -15,6 +15,12 @@ defmodule Bones73kWeb.UserResetPasswordControllerTest do
response = html_response(conn, 200)
assert response =~ "Forgot your password?\n "
end
+
+ test "redirects if already logged in", %{conn: conn} do
+ to = Routes.user_reset_password_path(conn, :new)
+ conn = conn |> log_in_user(user_fixture()) |> get(to)
+ assert redirected_to(conn) == "/"
+ end
end
describe "POST /users/reset_password" do
diff --git a/test/bones73k_web/controllers/user_settings_controller_test.exs b/test/bones73k_web/controllers/user_settings_controller_test.exs
index 94dc734d..dde5fb66 100644
--- a/test/bones73k_web/controllers/user_settings_controller_test.exs
+++ b/test/bones73k_web/controllers/user_settings_controller_test.exs
@@ -10,7 +10,7 @@ defmodule Bones73kWeb.UserSettingsControllerTest do
test "renders settings page", %{conn: conn} do
conn = get(conn, Routes.user_settings_path(conn, :edit))
response = html_response(conn, 200)
- assert response =~ "
Settings
"
+ assert response =~ "User Settings\n"
end
test "redirects if user is not logged in" do
@@ -20,71 +20,6 @@ defmodule Bones73kWeb.UserSettingsControllerTest do
end
end
- describe "PUT /users/settings/update_password" do
- test "updates the user password and resets tokens", %{conn: conn, user: user} do
- new_password_conn =
- put(conn, Routes.user_settings_path(conn, :update_password), %{
- "current_password" => valid_user_password(),
- "user" => %{
- "password" => "new valid password",
- "password_confirmation" => "new valid password"
- }
- })
-
- assert redirected_to(new_password_conn) == Routes.user_settings_path(conn, :edit)
- assert get_session(new_password_conn, :user_token) != get_session(conn, :user_token)
- assert get_flash(new_password_conn, :info) =~ "Password updated successfully"
- assert Accounts.get_user_by_email_and_password(user.email, "new valid password")
- end
-
- test "does not update password on invalid data", %{conn: conn} do
- old_password_conn =
- put(conn, Routes.user_settings_path(conn, :update_password), %{
- "current_password" => "invalid",
- "user" => %{
- "password" => "too short",
- "password_confirmation" => "does not match"
- }
- })
-
- response = html_response(old_password_conn, 200)
- assert response =~ "
Settings
"
- assert response =~ "should be at least 12 character(s)"
- assert response =~ "does not match password"
- assert response =~ "is not valid"
-
- assert get_session(old_password_conn, :user_token) == get_session(conn, :user_token)
- end
- end
-
- describe "PUT /users/settings/update_email" do
- @tag :capture_log
- test "updates the user email", %{conn: conn, user: user} do
- conn =
- put(conn, Routes.user_settings_path(conn, :update_email), %{
- "current_password" => valid_user_password(),
- "user" => %{"email" => unique_user_email()}
- })
-
- assert redirected_to(conn) == Routes.user_settings_path(conn, :edit)
- assert get_flash(conn, :info) =~ "A link to confirm your email"
- assert Accounts.get_user_by_email(user.email)
- end
-
- test "does not update email on invalid data", %{conn: conn} do
- conn =
- put(conn, Routes.user_settings_path(conn, :update_email), %{
- "current_password" => "invalid",
- "user" => %{"email" => "with spaces"}
- })
-
- response = html_response(conn, 200)
- assert response =~ "
Settings
"
- assert response =~ "must have the @ sign and no spaces"
- assert response =~ "is not valid"
- end
- end
-
describe "GET /users/settings/confirm_email/:token" do
setup %{user: user} do
email = unique_user_email()
diff --git a/test/bones73k_web/live/admin_dashboard_live_test.exs b/test/bones73k_web/live/admin_dashboard_live_test.exs
index 356529d3..1f3c50a6 100644
--- a/test/bones73k_web/live/admin_dashboard_live_test.exs
+++ b/test/bones73k_web/live/admin_dashboard_live_test.exs
@@ -52,9 +52,6 @@ defmodule Bones73kWeb.AdminDashboardLiveTest do
assert "/" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
- assert "/users/log_in" = redir_path = redirected_to(conn, 302)
- conn = get(recycle(conn), redir_path)
-
assert html_response(conn, 200) =~
"You were logged out. Please login again to continue using our application."
end
diff --git a/test/bones73k_web/live/page_live_test.exs b/test/bones73k_web/live/page_live_test.exs
index b8719e7a..a271747e 100644
--- a/test/bones73k_web/live/page_live_test.exs
+++ b/test/bones73k_web/live/page_live_test.exs
@@ -4,12 +4,6 @@ defmodule Bones73kWeb.PageLiveTest do
import Phoenix.LiveViewTest
import Bones73k.AccountsFixtures
- test "disconnected and connected render without authentication should redirect to login page",
- %{conn: conn} do
- # If we don't previously log in we will be redirected to the login page
- assert {:error, {:redirect, %{to: "/users/log_in"}}} = live(conn, "/")
- end
-
test "disconnected and connected render with authentication should redirect to index page", %{
conn: conn
} do
@@ -44,9 +38,6 @@ defmodule Bones73kWeb.PageLiveTest do
assert "/" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
- assert "/users/log_in" = redir_path = redirected_to(conn, 302)
- conn = get(recycle(conn), redir_path)
-
assert html_response(conn, 200) =~
"You were logged out. Please login again to continue using our application."
end
diff --git a/test/bones73k_web/live/property_live_test.exs b/test/bones73k_web/live/property_live_test.exs
index c3429664..64a4fc7d 100644
--- a/test/bones73k_web/live/property_live_test.exs
+++ b/test/bones73k_web/live/property_live_test.exs
@@ -188,16 +188,13 @@ defmodule Bones73kWeb.PropertyLiveTest do
assert_receive {:DOWN, ^ref, _, _, _}
refute Process.alive?(index_live.pid)
- # Assert our liveview was redirected, following first to /users/force_logout, then to "/", and then to "/users/log_in"
+ # Assert our liveview was redirected, following first to /users/force_logout, then to "/"
assert_redirect(index_live, "/users/force_logout")
conn = get(conn, "/users/force_logout")
assert "/" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
- assert "/users/log_in" = redir_path = redirected_to(conn, 302)
- conn = get(recycle(conn), redir_path)
-
assert html_response(conn, 200) =~
"You were logged out. Please login again to continue using our application."
end
@@ -338,9 +335,6 @@ defmodule Bones73kWeb.PropertyLiveTest do
assert "/" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
- assert "/users/log_in" = redir_path = redirected_to(conn, 302)
- conn = get(recycle(conn), redir_path)
-
assert html_response(conn, 200) =~
"You were logged out. Please login again to continue using our application."
end
diff --git a/test/bones73k_web/live/user/reset_password_test.exs b/test/bones73k_web/live/user/reset_password_test.exs
index afab58d1..3526766c 100644
--- a/test/bones73k_web/live/user/reset_password_test.exs
+++ b/test/bones73k_web/live/user/reset_password_test.exs
@@ -6,8 +6,7 @@ defmodule Bones73kWeb.UserLive.ResetPasswordTest do
alias Bones73k.Repo
alias Bones73k.Accounts
- alias Bones73k.Accounts.User
- alias Bones73k.Accounts.UserToken
+ alias Bones73k.Accounts.{User, UserToken}
setup %{conn: conn} do
user = user_fixture()
@@ -46,7 +45,7 @@ defmodule Bones73kWeb.UserLive.ResetPasswordTest do
# Confirm redirected
flash = assert_redirected(view, Routes.user_session_path(conn, :new))
- assert flash["success"] == "Password reset successfully."
+ assert flash["info"] == "Password reset successfully."
# Confirm password was updated
assert Accounts.get_user_by_email_and_password(user.email, new_pw)
diff --git a/test/bones73k_web/live/user_dashboard_live_test.exs b/test/bones73k_web/live/user_dashboard_live_test.exs
index a0f4e8b2..d2fea218 100644
--- a/test/bones73k_web/live/user_dashboard_live_test.exs
+++ b/test/bones73k_web/live/user_dashboard_live_test.exs
@@ -54,9 +54,6 @@ defmodule Bones73kWeb.UserDashboardLiveTest do
assert "/" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
- assert "/users/log_in" = redir_path = redirected_to(conn, 302)
- conn = get(recycle(conn), redir_path)
-
assert html_response(conn, 200) =~
"You were logged out. Please login again to continue using our application."
end