user settings updated to lv and lv tests created. all tests working
This commit is contained in:
parent
24502e2667
commit
ef7b8e0bb8
26 changed files with 389 additions and 288 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -15,6 +15,12 @@ defmodule Bones73kWeb.UserResetPasswordControllerTest do
|
|||
response = html_response(conn, 200)
|
||||
assert response =~ "Forgot your password?\n </h3>"
|
||||
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
|
||||
|
|
|
@ -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 =~ "<h1>Settings</h1>"
|
||||
assert response =~ "User Settings\n</h3>"
|
||||
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 =~ "<h1>Settings</h1>"
|
||||
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 =~ "<h1>Settings</h1>"
|
||||
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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue