changed project/app name
This commit is contained in:
parent
4fbafbfa5e
commit
cd31432f88
89 changed files with 421 additions and 417 deletions
171
test/bones73k_web/controllers/user_auth_test.exs
Normal file
171
test/bones73k_web/controllers/user_auth_test.exs
Normal file
|
@ -0,0 +1,171 @@
|
|||
defmodule Bones73kWeb.UserAuthTest do
|
||||
use Bones73kWeb.ConnCase, async: true
|
||||
|
||||
alias Bones73k.Accounts
|
||||
alias Bones73kWeb.UserAuth
|
||||
import Bones73k.AccountsFixtures
|
||||
|
||||
setup %{conn: conn} do
|
||||
conn =
|
||||
conn
|
||||
|> Map.replace!(:secret_key_base, Bones73kWeb.Endpoint.config(:secret_key_base))
|
||||
|> init_test_session(%{})
|
||||
|
||||
%{user: user_fixture(), conn: conn}
|
||||
end
|
||||
|
||||
describe "log_in_user/3" do
|
||||
test "stores the user token in the session", %{conn: conn, user: user} do
|
||||
conn = UserAuth.log_in_user(conn, user)
|
||||
assert token = get_session(conn, :user_token)
|
||||
assert get_session(conn, :live_socket_id) == "users_sessions:#{Base.url_encode64(token)}"
|
||||
assert redirected_to(conn) == "/"
|
||||
assert Accounts.get_user_by_session_token(token)
|
||||
end
|
||||
|
||||
test "clears everything previously stored in the session", %{conn: conn, user: user} do
|
||||
conn = conn |> put_session(:to_be_removed, "value") |> UserAuth.log_in_user(user)
|
||||
refute get_session(conn, :to_be_removed)
|
||||
end
|
||||
|
||||
test "redirects to the configured path", %{conn: conn, user: user} do
|
||||
conn = conn |> put_session(:user_return_to, "/hello") |> UserAuth.log_in_user(user)
|
||||
assert redirected_to(conn) == "/hello"
|
||||
end
|
||||
|
||||
test "writes a cookie if remember_me is configured", %{conn: conn, user: user} do
|
||||
conn = conn |> fetch_cookies() |> UserAuth.log_in_user(user, %{"remember_me" => "true"})
|
||||
assert get_session(conn, :user_token) == conn.cookies["user_remember_me"]
|
||||
|
||||
assert %{value: signed_token, max_age: max_age} = conn.resp_cookies["user_remember_me"]
|
||||
assert signed_token != get_session(conn, :user_token)
|
||||
assert max_age == 5_184_000
|
||||
end
|
||||
end
|
||||
|
||||
describe "logout_user/1" do
|
||||
test "erases session and cookies", %{conn: conn, user: user} do
|
||||
user_token = Accounts.generate_user_session_token(user)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> put_session(:user_token, user_token)
|
||||
|> put_req_cookie("user_remember_me", user_token)
|
||||
|> fetch_cookies()
|
||||
|> UserAuth.log_out_user()
|
||||
|
||||
refute get_session(conn, :user_token)
|
||||
refute conn.cookies["user_remember_me"]
|
||||
assert %{max_age: 0} = conn.resp_cookies["user_remember_me"]
|
||||
assert redirected_to(conn) == "/"
|
||||
refute Accounts.get_user_by_session_token(user_token)
|
||||
end
|
||||
|
||||
test "broadcasts to the given live_socket_id", %{conn: conn} do
|
||||
live_socket_id = "users_sessions:abcdef-token"
|
||||
Bones73kWeb.Endpoint.subscribe(live_socket_id)
|
||||
|
||||
conn
|
||||
|> put_session(:live_socket_id, live_socket_id)
|
||||
|> UserAuth.log_out_user()
|
||||
|
||||
assert_receive %Phoenix.Socket.Broadcast{
|
||||
event: "disconnect",
|
||||
topic: "users_sessions:abcdef-token"
|
||||
}
|
||||
end
|
||||
|
||||
test "works even if user is already logged out", %{conn: conn} do
|
||||
conn = conn |> fetch_cookies() |> UserAuth.log_out_user()
|
||||
refute get_session(conn, :user_token)
|
||||
assert %{max_age: 0} = conn.resp_cookies["user_remember_me"]
|
||||
assert redirected_to(conn) == "/"
|
||||
end
|
||||
end
|
||||
|
||||
describe "fetch_current_user/2" do
|
||||
test "authenticates user from session", %{conn: conn, user: user} do
|
||||
user_token = Accounts.generate_user_session_token(user)
|
||||
conn = conn |> put_session(:user_token, user_token) |> UserAuth.fetch_current_user([])
|
||||
assert conn.assigns.current_user.id == user.id
|
||||
end
|
||||
|
||||
test "authenticates user from cookies", %{conn: conn, user: user} do
|
||||
logged_in_conn =
|
||||
conn |> fetch_cookies() |> UserAuth.log_in_user(user, %{"remember_me" => "true"})
|
||||
|
||||
user_token = logged_in_conn.cookies["user_remember_me"]
|
||||
%{value: signed_token} = logged_in_conn.resp_cookies["user_remember_me"]
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> put_req_cookie("user_remember_me", signed_token)
|
||||
|> UserAuth.fetch_current_user([])
|
||||
|
||||
assert get_session(conn, :user_token) == user_token
|
||||
assert conn.assigns.current_user.id == user.id
|
||||
end
|
||||
|
||||
test "does not authenticate if data is missing", %{conn: conn, user: user} do
|
||||
_ = Accounts.generate_user_session_token(user)
|
||||
conn = UserAuth.fetch_current_user(conn, [])
|
||||
refute get_session(conn, :user_token)
|
||||
refute conn.assigns.current_user
|
||||
end
|
||||
end
|
||||
|
||||
describe "redirect_if_user_is_authenticated/2" do
|
||||
test "redirects if user is authenticated", %{conn: conn, user: user} do
|
||||
conn = conn |> assign(:current_user, user) |> UserAuth.redirect_if_user_is_authenticated([])
|
||||
assert conn.halted
|
||||
assert redirected_to(conn) == "/"
|
||||
end
|
||||
|
||||
test "does not redirect if user is not authenticated", %{conn: conn} do
|
||||
conn = UserAuth.redirect_if_user_is_authenticated(conn, [])
|
||||
refute conn.halted
|
||||
refute conn.status
|
||||
end
|
||||
end
|
||||
|
||||
describe "require_authenticated_user/2" do
|
||||
test "redirects if user is not authenticated", %{conn: conn} do
|
||||
conn = conn |> fetch_flash() |> UserAuth.require_authenticated_user([])
|
||||
assert conn.halted
|
||||
assert redirected_to(conn) == Routes.user_session_path(conn, :new)
|
||||
assert get_flash(conn, :error) == "You must log in to access this page."
|
||||
end
|
||||
|
||||
test "stores the path to redirect to on GET", %{conn: conn} do
|
||||
halted_conn =
|
||||
%{conn | request_path: "/foo", query_string: ""}
|
||||
|> fetch_flash()
|
||||
|> UserAuth.require_authenticated_user([])
|
||||
|
||||
assert halted_conn.halted
|
||||
assert get_session(halted_conn, :user_return_to) == "/foo"
|
||||
|
||||
halted_conn =
|
||||
%{conn | request_path: "/foo", query_string: "bar=baz"}
|
||||
|> fetch_flash()
|
||||
|> UserAuth.require_authenticated_user([])
|
||||
|
||||
assert halted_conn.halted
|
||||
assert get_session(halted_conn, :user_return_to) == "/foo?bar=baz"
|
||||
|
||||
halted_conn =
|
||||
%{conn | request_path: "/foo?bar", method: "POST"}
|
||||
|> fetch_flash()
|
||||
|> UserAuth.require_authenticated_user([])
|
||||
|
||||
assert halted_conn.halted
|
||||
refute get_session(halted_conn, :user_return_to)
|
||||
end
|
||||
|
||||
test "does not redirect if user is authenticated", %{conn: conn, user: user} do
|
||||
conn = conn |> assign(:current_user, user) |> UserAuth.require_authenticated_user([])
|
||||
refute conn.halted
|
||||
refute conn.status
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,84 @@
|
|||
defmodule Bones73kWeb.UserConfirmationControllerTest do
|
||||
use Bones73kWeb.ConnCase, async: true
|
||||
|
||||
alias Bones73k.Accounts
|
||||
alias Bones73k.Repo
|
||||
import Bones73k.AccountsFixtures
|
||||
|
||||
setup do
|
||||
%{user: user_fixture()}
|
||||
end
|
||||
|
||||
describe "GET /users/confirm" do
|
||||
test "renders the confirmation page", %{conn: conn} do
|
||||
conn = get(conn, Routes.user_confirmation_path(conn, :new))
|
||||
response = html_response(conn, 200)
|
||||
assert response =~ "<h1>Resend confirmation instructions</h1>"
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /users/confirm" do
|
||||
@tag :capture_log
|
||||
test "sends a new confirmation token", %{conn: conn, user: user} do
|
||||
conn =
|
||||
post(conn, Routes.user_confirmation_path(conn, :create), %{
|
||||
"user" => %{"email" => user.email}
|
||||
})
|
||||
|
||||
assert redirected_to(conn) == "/"
|
||||
assert get_flash(conn, :info) =~ "If your email is in our system"
|
||||
assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context == "confirm"
|
||||
end
|
||||
|
||||
test "does not send confirmation token if account is confirmed", %{conn: conn, user: user} do
|
||||
Repo.update!(Accounts.User.confirm_changeset(user))
|
||||
|
||||
conn =
|
||||
post(conn, Routes.user_confirmation_path(conn, :create), %{
|
||||
"user" => %{"email" => user.email}
|
||||
})
|
||||
|
||||
assert redirected_to(conn) == "/"
|
||||
assert get_flash(conn, :info) =~ "If your email is in our system"
|
||||
refute Repo.get_by(Accounts.UserToken, user_id: user.id)
|
||||
end
|
||||
|
||||
test "does not send confirmation token if email is invalid", %{conn: conn} do
|
||||
conn =
|
||||
post(conn, Routes.user_confirmation_path(conn, :create), %{
|
||||
"user" => %{"email" => "unknown@example.com"}
|
||||
})
|
||||
|
||||
assert redirected_to(conn) == "/"
|
||||
assert get_flash(conn, :info) =~ "If your email is in our system"
|
||||
assert Repo.all(Accounts.UserToken) == []
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /users/confirm/:token" do
|
||||
test "confirms the given token once", %{conn: conn, user: user} do
|
||||
token =
|
||||
extract_user_token(fn url ->
|
||||
Accounts.deliver_user_confirmation_instructions(user, url)
|
||||
end)
|
||||
|
||||
conn = get(conn, Routes.user_confirmation_path(conn, :confirm, token))
|
||||
assert redirected_to(conn) == "/"
|
||||
assert get_flash(conn, :info) =~ "Account confirmed successfully"
|
||||
assert Accounts.get_user!(user.id).confirmed_at
|
||||
refute get_session(conn, :user_token)
|
||||
assert Repo.all(Accounts.UserToken) == []
|
||||
|
||||
conn = get(conn, Routes.user_confirmation_path(conn, :confirm, token))
|
||||
assert redirected_to(conn) == "/"
|
||||
assert get_flash(conn, :error) =~ "Confirmation link is invalid or it has expired"
|
||||
end
|
||||
|
||||
test "does not confirm email with invalid token", %{conn: conn, user: user} do
|
||||
conn = get(conn, Routes.user_confirmation_path(conn, :confirm, "oops"))
|
||||
assert redirected_to(conn) == "/"
|
||||
assert get_flash(conn, :error) =~ "Confirmation link is invalid or it has expired"
|
||||
refute Accounts.get_user!(user.id).confirmed_at
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,54 @@
|
|||
defmodule Bones73kWeb.UserRegistrationControllerTest do
|
||||
use Bones73kWeb.ConnCase, async: true
|
||||
|
||||
import Bones73k.AccountsFixtures
|
||||
|
||||
describe "GET /users/register" do
|
||||
test "renders registration page", %{conn: conn} do
|
||||
conn = get(conn, Routes.user_registration_path(conn, :new))
|
||||
response = html_response(conn, 200)
|
||||
assert response =~ "<h1>Register</h1>"
|
||||
assert response =~ "Log in</a>"
|
||||
assert response =~ "Register</a>"
|
||||
end
|
||||
|
||||
test "redirects if already logged in", %{conn: conn} do
|
||||
conn = conn |> log_in_user(user_fixture()) |> get(Routes.user_registration_path(conn, :new))
|
||||
assert redirected_to(conn) == "/"
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /users/register" do
|
||||
@tag :capture_log
|
||||
test "creates account and logs the user in", %{conn: conn} do
|
||||
email = unique_user_email()
|
||||
|
||||
conn =
|
||||
post(conn, Routes.user_registration_path(conn, :create), %{
|
||||
"user" => %{"email" => email, "password" => valid_user_password()}
|
||||
})
|
||||
|
||||
assert get_session(conn, :user_token)
|
||||
assert redirected_to(conn) =~ "/"
|
||||
|
||||
# Now do a logged in request and assert on the menu
|
||||
conn = get(conn, "/")
|
||||
response = html_response(conn, 200)
|
||||
assert response =~ email
|
||||
assert response =~ "Settings</a>"
|
||||
assert response =~ "Log out</a>"
|
||||
end
|
||||
|
||||
test "render errors for invalid data", %{conn: conn} do
|
||||
conn =
|
||||
post(conn, Routes.user_registration_path(conn, :create), %{
|
||||
"user" => %{"email" => "with spaces", "password" => "too short"}
|
||||
})
|
||||
|
||||
response = html_response(conn, 200)
|
||||
assert response =~ "<h1>Register</h1>"
|
||||
assert response =~ "must have the @ sign and no spaces"
|
||||
assert response =~ "should be at least 12 character"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,113 @@
|
|||
defmodule Bones73kWeb.UserResetPasswordControllerTest do
|
||||
use Bones73kWeb.ConnCase, async: true
|
||||
|
||||
alias Bones73k.Accounts
|
||||
alias Bones73k.Repo
|
||||
import Bones73k.AccountsFixtures
|
||||
|
||||
setup do
|
||||
%{user: user_fixture()}
|
||||
end
|
||||
|
||||
describe "GET /users/reset_password" do
|
||||
test "renders the reset password page", %{conn: conn} do
|
||||
conn = get(conn, Routes.user_reset_password_path(conn, :new))
|
||||
response = html_response(conn, 200)
|
||||
assert response =~ "<h1>Forgot your password?</h1>"
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /users/reset_password" do
|
||||
@tag :capture_log
|
||||
test "sends a new reset password token", %{conn: conn, user: user} do
|
||||
conn =
|
||||
post(conn, Routes.user_reset_password_path(conn, :create), %{
|
||||
"user" => %{"email" => user.email}
|
||||
})
|
||||
|
||||
assert redirected_to(conn) == "/"
|
||||
assert get_flash(conn, :info) =~ "If your email is in our system"
|
||||
assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context == "reset_password"
|
||||
end
|
||||
|
||||
test "does not send reset password token if email is invalid", %{conn: conn} do
|
||||
conn =
|
||||
post(conn, Routes.user_reset_password_path(conn, :create), %{
|
||||
"user" => %{"email" => "unknown@example.com"}
|
||||
})
|
||||
|
||||
assert redirected_to(conn) == "/"
|
||||
assert get_flash(conn, :info) =~ "If your email is in our system"
|
||||
assert Repo.all(Accounts.UserToken) == []
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /users/reset_password/:token" do
|
||||
setup %{user: user} do
|
||||
token =
|
||||
extract_user_token(fn url ->
|
||||
Accounts.deliver_user_reset_password_instructions(user, url)
|
||||
end)
|
||||
|
||||
%{token: token}
|
||||
end
|
||||
|
||||
test "renders reset password", %{conn: conn, token: token} do
|
||||
conn = get(conn, Routes.user_reset_password_path(conn, :edit, token))
|
||||
assert html_response(conn, 200) =~ "<h1>Reset password</h1>"
|
||||
end
|
||||
|
||||
test "does not render reset password with invalid token", %{conn: conn} do
|
||||
conn = get(conn, Routes.user_reset_password_path(conn, :edit, "oops"))
|
||||
assert redirected_to(conn) == "/"
|
||||
assert get_flash(conn, :error) =~ "Reset password link is invalid or it has expired"
|
||||
end
|
||||
end
|
||||
|
||||
describe "PUT /users/reset_password/:token" do
|
||||
setup %{user: user} do
|
||||
token =
|
||||
extract_user_token(fn url ->
|
||||
Accounts.deliver_user_reset_password_instructions(user, url)
|
||||
end)
|
||||
|
||||
%{token: token}
|
||||
end
|
||||
|
||||
test "resets password once", %{conn: conn, user: user, token: token} do
|
||||
conn =
|
||||
put(conn, Routes.user_reset_password_path(conn, :update, token), %{
|
||||
"user" => %{
|
||||
"password" => "new valid password",
|
||||
"password_confirmation" => "new valid password"
|
||||
}
|
||||
})
|
||||
|
||||
assert redirected_to(conn) == Routes.user_session_path(conn, :new)
|
||||
refute get_session(conn, :user_token)
|
||||
assert get_flash(conn, :info) =~ "Password reset successfully"
|
||||
assert Accounts.get_user_by_email_and_password(user.email, "new valid password")
|
||||
end
|
||||
|
||||
test "does not reset password on invalid data", %{conn: conn, token: token} do
|
||||
conn =
|
||||
put(conn, Routes.user_reset_password_path(conn, :update, token), %{
|
||||
"user" => %{
|
||||
"password" => "too short",
|
||||
"password_confirmation" => "does not match"
|
||||
}
|
||||
})
|
||||
|
||||
response = html_response(conn, 200)
|
||||
assert response =~ "<h1>Reset password</h1>"
|
||||
assert response =~ "should be at least 12 character(s)"
|
||||
assert response =~ "does not match password"
|
||||
end
|
||||
|
||||
test "does not reset password with invalid token", %{conn: conn} do
|
||||
conn = put(conn, Routes.user_reset_password_path(conn, :update, "oops"))
|
||||
assert redirected_to(conn) == "/"
|
||||
assert get_flash(conn, :error) =~ "Reset password link is invalid or it has expired"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,84 @@
|
|||
defmodule Bones73kWeb.UserSessionControllerTest do
|
||||
use Bones73kWeb.ConnCase, async: true
|
||||
|
||||
import Bones73k.AccountsFixtures
|
||||
|
||||
setup do
|
||||
%{user: user_fixture()}
|
||||
end
|
||||
|
||||
describe "GET /users/log_in" do
|
||||
test "renders log in page", %{conn: conn} do
|
||||
conn = get(conn, Routes.user_session_path(conn, :new))
|
||||
response = html_response(conn, 200)
|
||||
assert response =~ "<h1>Log in</h1>"
|
||||
assert response =~ "Log in</a>"
|
||||
assert response =~ "Register</a>"
|
||||
end
|
||||
|
||||
test "redirects if already logged in", %{conn: conn, user: user} do
|
||||
conn = conn |> log_in_user(user) |> get(Routes.user_session_path(conn, :new))
|
||||
assert redirected_to(conn) == "/"
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /users/log_in" do
|
||||
test "logs the user in", %{conn: conn, user: user} do
|
||||
conn =
|
||||
post(conn, Routes.user_session_path(conn, :create), %{
|
||||
"user" => %{"email" => user.email, "password" => valid_user_password()}
|
||||
})
|
||||
|
||||
assert get_session(conn, :user_token)
|
||||
assert redirected_to(conn) =~ "/"
|
||||
|
||||
# Now do a logged in request and assert on the menu
|
||||
conn = get(conn, "/")
|
||||
response = html_response(conn, 200)
|
||||
assert response =~ user.email
|
||||
assert response =~ "Settings</a>"
|
||||
assert response =~ "Log out</a>"
|
||||
end
|
||||
|
||||
test "logs the user in with remember me", %{conn: conn, user: user} do
|
||||
conn =
|
||||
post(conn, Routes.user_session_path(conn, :create), %{
|
||||
"user" => %{
|
||||
"email" => user.email,
|
||||
"password" => valid_user_password(),
|
||||
"remember_me" => "true"
|
||||
}
|
||||
})
|
||||
|
||||
assert conn.resp_cookies["user_remember_me"]
|
||||
assert redirected_to(conn) =~ "/"
|
||||
end
|
||||
|
||||
test "emits error message with invalid credentials", %{conn: conn, user: user} do
|
||||
conn =
|
||||
post(conn, Routes.user_session_path(conn, :create), %{
|
||||
"user" => %{"email" => user.email, "password" => "invalid_password"}
|
||||
})
|
||||
|
||||
response = html_response(conn, 200)
|
||||
assert response =~ "<h1>Log in</h1>"
|
||||
assert response =~ "Invalid email or password"
|
||||
end
|
||||
end
|
||||
|
||||
describe "DELETE /users/log_out" do
|
||||
test "logs the user out", %{conn: conn, user: user} do
|
||||
conn = conn |> log_in_user(user) |> delete(Routes.user_session_path(conn, :delete))
|
||||
assert redirected_to(conn) == "/"
|
||||
refute get_session(conn, :user_token)
|
||||
assert get_flash(conn, :info) =~ "Logged out successfully"
|
||||
end
|
||||
|
||||
test "succeeds even if the user is not logged in", %{conn: conn} do
|
||||
conn = delete(conn, Routes.user_session_path(conn, :delete))
|
||||
assert redirected_to(conn) == "/"
|
||||
refute get_session(conn, :user_token)
|
||||
assert get_flash(conn, :info) =~ "Logged out successfully"
|
||||
end
|
||||
end
|
||||
end
|
125
test/bones73k_web/controllers/user_settings_controller_test.exs
Normal file
125
test/bones73k_web/controllers/user_settings_controller_test.exs
Normal file
|
@ -0,0 +1,125 @@
|
|||
defmodule Bones73kWeb.UserSettingsControllerTest do
|
||||
use Bones73kWeb.ConnCase, async: true
|
||||
|
||||
alias Bones73k.Accounts
|
||||
import Bones73k.AccountsFixtures
|
||||
|
||||
setup :register_and_log_in_user
|
||||
|
||||
describe "GET /users/settings" 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>"
|
||||
end
|
||||
|
||||
test "redirects if user is not logged in" do
|
||||
conn = build_conn()
|
||||
conn = get(conn, Routes.user_settings_path(conn, :edit))
|
||||
assert redirected_to(conn) == Routes.user_session_path(conn, :new)
|
||||
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()
|
||||
|
||||
token =
|
||||
extract_user_token(fn url ->
|
||||
Accounts.deliver_update_email_instructions(%{user | email: email}, user.email, url)
|
||||
end)
|
||||
|
||||
%{token: token, email: email}
|
||||
end
|
||||
|
||||
test "updates the user email once", %{conn: conn, user: user, token: token, email: email} do
|
||||
conn = get(conn, Routes.user_settings_path(conn, :confirm_email, token))
|
||||
assert redirected_to(conn) == Routes.user_settings_path(conn, :edit)
|
||||
assert get_flash(conn, :info) =~ "Email changed successfully"
|
||||
refute Accounts.get_user_by_email(user.email)
|
||||
assert Accounts.get_user_by_email(email)
|
||||
|
||||
conn = get(conn, Routes.user_settings_path(conn, :confirm_email, token))
|
||||
assert redirected_to(conn) == Routes.user_settings_path(conn, :edit)
|
||||
assert get_flash(conn, :error) =~ "Email change link is invalid or it has expired"
|
||||
end
|
||||
|
||||
test "does not update email with invalid token", %{conn: conn, user: user} do
|
||||
conn = get(conn, Routes.user_settings_path(conn, :confirm_email, "oops"))
|
||||
assert redirected_to(conn) == Routes.user_settings_path(conn, :edit)
|
||||
assert get_flash(conn, :error) =~ "Email change link is invalid or it has expired"
|
||||
assert Accounts.get_user_by_email(user.email)
|
||||
end
|
||||
|
||||
test "redirects if user is not logged in", %{token: token} do
|
||||
conn = build_conn()
|
||||
conn = get(conn, Routes.user_settings_path(conn, :confirm_email, token))
|
||||
assert redirected_to(conn) == Routes.user_session_path(conn, :new)
|
||||
end
|
||||
end
|
||||
end
|
83
test/bones73k_web/live/admin_dashboard_live_test.exs
Normal file
83
test/bones73k_web/live/admin_dashboard_live_test.exs
Normal file
|
@ -0,0 +1,83 @@
|
|||
defmodule Bones73kWeb.AdminDashboardLiveTest do
|
||||
use Bones73kWeb.ConnCase
|
||||
|
||||
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, "/admin_dashboard")
|
||||
end
|
||||
|
||||
test "disconnected and connected render authenticated with user role should redirect to index page",
|
||||
%{
|
||||
conn: conn
|
||||
} do
|
||||
conn = conn |> log_in_user(user_fixture())
|
||||
assert {:error, {:redirect, %{to: "/"}}} = live(conn, "/admin_dashboard")
|
||||
end
|
||||
|
||||
test "disconnected and connected render authenticated with admin role should redirect to index page",
|
||||
%{
|
||||
conn: conn
|
||||
} do
|
||||
conn = conn |> log_in_user(admin_fixture())
|
||||
{:ok, admin_dashboard, disconnected_html} = live(conn, "/admin_dashboard")
|
||||
assert disconnected_html =~ "Welcome to the admin dashboard!"
|
||||
assert render(admin_dashboard) =~ "Welcome to the admin dashboard!"
|
||||
end
|
||||
|
||||
test "logs out when force logout on logged user", %{
|
||||
conn: conn
|
||||
} do
|
||||
admin = admin_fixture()
|
||||
conn = conn |> log_in_user(admin)
|
||||
|
||||
{:ok, admin_dashboard, disconnected_html} = live(conn, "/admin_dashboard")
|
||||
assert disconnected_html =~ "Welcome to the admin dashboard!"
|
||||
assert render(admin_dashboard) =~ "Welcome to the admin dashboard!"
|
||||
|
||||
Bones73k.Accounts.logout_user(admin)
|
||||
|
||||
# Assert our liveview process is down
|
||||
ref = Process.monitor(admin_dashboard.pid)
|
||||
assert_receive {:DOWN, ^ref, _, _, _}
|
||||
refute Process.alive?(admin_dashboard.pid)
|
||||
|
||||
# Assert our liveview was redirected, following first to /users/force_logout, then to "/", and then to "/users/log_in"
|
||||
assert_redirect(admin_dashboard, "/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
|
||||
|
||||
test "doesn't log out when force logout on another user", %{
|
||||
conn: conn
|
||||
} do
|
||||
admin1 = admin_fixture()
|
||||
admin2 = admin_fixture()
|
||||
conn = conn |> log_in_user(admin1)
|
||||
|
||||
{:ok, admin_dashboard, disconnected_html} = live(conn, "/admin_dashboard")
|
||||
assert disconnected_html =~ "Welcome to the admin dashboard!"
|
||||
assert render(admin_dashboard) =~ "Welcome to the admin dashboard!"
|
||||
|
||||
Bones73k.Accounts.logout_user(admin2)
|
||||
|
||||
# Assert our liveview is alive
|
||||
ref = Process.monitor(admin_dashboard.pid)
|
||||
refute_receive {:DOWN, ^ref, _, _, _}
|
||||
assert Process.alive?(admin_dashboard.pid)
|
||||
|
||||
# If we are able to rerender the page it means nothing happened
|
||||
assert render(admin_dashboard) =~ "Welcome to the admin dashboard!"
|
||||
end
|
||||
end
|
75
test/bones73k_web/live/page_live_test.exs
Normal file
75
test/bones73k_web/live/page_live_test.exs
Normal file
|
@ -0,0 +1,75 @@
|
|||
defmodule Bones73kWeb.PageLiveTest do
|
||||
use Bones73kWeb.ConnCase
|
||||
|
||||
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
|
||||
conn = conn |> log_in_user(user_fixture())
|
||||
|
||||
{:ok, page_live, disconnected_html} = live(conn, "/")
|
||||
assert disconnected_html =~ "Welcome to Phoenix!"
|
||||
assert render(page_live) =~ "Welcome to Phoenix!"
|
||||
end
|
||||
|
||||
test "logs out when force logout on logged user", %{
|
||||
conn: conn
|
||||
} do
|
||||
user = user_fixture()
|
||||
conn = conn |> log_in_user(user)
|
||||
|
||||
{:ok, page_live, disconnected_html} = live(conn, "/")
|
||||
assert disconnected_html =~ "Welcome to Phoenix!"
|
||||
assert render(page_live) =~ "Welcome to Phoenix!"
|
||||
|
||||
Bones73k.Accounts.logout_user(user)
|
||||
|
||||
# Assert our liveview process is down
|
||||
ref = Process.monitor(page_live.pid)
|
||||
assert_receive {:DOWN, ^ref, _, _, _}
|
||||
refute Process.alive?(page_live.pid)
|
||||
|
||||
# Assert our liveview was redirected, following first to /users/force_logout, then to "/", and then to "/users/log_in"
|
||||
assert_redirect(page_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
|
||||
|
||||
test "doesn't log out when force logout on another user", %{
|
||||
conn: conn
|
||||
} do
|
||||
user1 = user_fixture()
|
||||
user2 = user_fixture()
|
||||
conn = conn |> log_in_user(user2)
|
||||
|
||||
{:ok, page_live, disconnected_html} = live(conn, "/")
|
||||
assert disconnected_html =~ "Welcome to Phoenix!"
|
||||
assert render(page_live) =~ "Welcome to Phoenix!"
|
||||
|
||||
Bones73k.Accounts.logout_user(user1)
|
||||
|
||||
# Assert our liveview is alive
|
||||
ref = Process.monitor(page_live.pid)
|
||||
refute_receive {:DOWN, ^ref, _, _, _}
|
||||
assert Process.alive?(page_live.pid)
|
||||
|
||||
# If we are able to rerender the page it means nothing happened
|
||||
assert render(page_live) =~ "Welcome to Phoenix!"
|
||||
end
|
||||
end
|
370
test/bones73k_web/live/property_live_test.exs
Normal file
370
test/bones73k_web/live/property_live_test.exs
Normal file
|
@ -0,0 +1,370 @@
|
|||
defmodule Bones73kWeb.PropertyLiveTest do
|
||||
use Bones73kWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Bones73k.AccountsFixtures
|
||||
|
||||
alias Bones73k.Properties
|
||||
|
||||
@create_attrs %{description: "some description", name: "some name", price: "120.5"}
|
||||
@update_attrs %{
|
||||
description: "some updated description",
|
||||
name: "some updated name",
|
||||
price: "456.7"
|
||||
}
|
||||
@invalid_attrs %{description: nil, name: nil, price: nil}
|
||||
|
||||
defp fixture(:property, user) do
|
||||
create_attributes = Enum.into(%{user_id: user.id}, @create_attrs)
|
||||
{:ok, property} = Properties.create_property(create_attributes)
|
||||
property
|
||||
end
|
||||
|
||||
describe "Index" do
|
||||
setup %{conn: conn} do
|
||||
user = user_fixture()
|
||||
conn = log_in_user(conn, user)
|
||||
property = fixture(:property, user)
|
||||
property_from_another_user = fixture(:property, user_fixture())
|
||||
|
||||
%{
|
||||
conn: conn,
|
||||
property: property,
|
||||
property_from_another_user: property_from_another_user,
|
||||
user: user
|
||||
}
|
||||
end
|
||||
|
||||
test "lists all properties", %{conn: conn, property: property} do
|
||||
{:ok, _index_live, html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert html =~ "Listing Properties"
|
||||
assert html =~ property.description
|
||||
end
|
||||
|
||||
test "saves new property", %{conn: conn} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("a", "New Property") |> render_click() =~
|
||||
"New Property"
|
||||
|
||||
assert_patch(index_live, Routes.property_index_path(conn, :new))
|
||||
|
||||
assert index_live
|
||||
|> form("#property-form", property: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
{:ok, _, html} =
|
||||
index_live
|
||||
|> form("#property-form", property: @create_attrs)
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert html =~ "Property created successfully"
|
||||
assert html =~ "some description"
|
||||
end
|
||||
|
||||
test "updates property in listing", %{conn: conn, property: property} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("#property-#{property.id} a", "Edit") |> render_click() =~
|
||||
"Edit Property"
|
||||
|
||||
assert_patch(index_live, Routes.property_index_path(conn, :edit, property))
|
||||
|
||||
assert index_live
|
||||
|> form("#property-form", property: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
{:ok, _, html} =
|
||||
index_live
|
||||
|> form("#property-form", property: @update_attrs)
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert html =~ "Property updated successfully"
|
||||
assert html =~ "some updated description"
|
||||
end
|
||||
|
||||
test "deletes property in listing", %{conn: conn, property: property} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("#property-#{property.id} a", "Delete") |> render_click()
|
||||
refute has_element?(index_live, "#property-#{property.id}")
|
||||
end
|
||||
|
||||
test "can see property from from other user in listing",
|
||||
%{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert has_element?(index_live, "#property-#{property.id}")
|
||||
end
|
||||
|
||||
test "can't see edit action for property from other user in listing",
|
||||
%{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
refute has_element?(index_live, "#property-#{property.id} a", "Edit")
|
||||
end
|
||||
|
||||
test "as an admin, I can update property from other user in listing", %{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
conn = log_in_user(conn, admin_fixture())
|
||||
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("#property-#{property.id} a", "Edit") |> render_click() =~
|
||||
"Edit Property"
|
||||
|
||||
assert_patch(index_live, Routes.property_index_path(conn, :edit, property))
|
||||
|
||||
assert index_live
|
||||
|> form("#property-form", property: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
{:ok, _, html} =
|
||||
index_live
|
||||
|> form("#property-form", property: @update_attrs)
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert html =~ "Property updated successfully"
|
||||
assert html =~ "some updated description"
|
||||
end
|
||||
|
||||
test "can't see delete action for property from other user in listing", %{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
refute has_element?(index_live, "#property-#{property.id} a", "Delete")
|
||||
end
|
||||
|
||||
test "as an admin, I can delete property from others in listing", %{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
conn = log_in_user(conn, admin_fixture())
|
||||
|
||||
{:ok, index_live, _html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert index_live |> element("#property-#{property.id} a", "Delete") |> render_click()
|
||||
refute has_element?(index_live, "#property-#{property.id}")
|
||||
end
|
||||
|
||||
test "can't edit property from other user in listing",
|
||||
%{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
assert {:error, {:redirect, %{to: "/"}}} =
|
||||
live(conn, Routes.property_index_path(conn, :edit, property))
|
||||
end
|
||||
|
||||
test "logs out when force logout on logged user", %{
|
||||
conn: conn
|
||||
} do
|
||||
user = user_fixture()
|
||||
conn = conn |> log_in_user(user)
|
||||
|
||||
{:ok, index_live, html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert html =~ "Listing Properties"
|
||||
assert render(index_live) =~ "Listing Properties"
|
||||
|
||||
Bones73k.Accounts.logout_user(user)
|
||||
|
||||
# Assert our liveview process is down
|
||||
ref = Process.monitor(index_live.pid)
|
||||
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_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
|
||||
|
||||
test "doesn't log out when force logout on another user", %{
|
||||
conn: conn
|
||||
} do
|
||||
user1 = user_fixture()
|
||||
user2 = user_fixture()
|
||||
conn = conn |> log_in_user(user2)
|
||||
|
||||
{:ok, index_live, html} = live(conn, Routes.property_index_path(conn, :index))
|
||||
|
||||
assert html =~ "Listing Properties"
|
||||
assert render(index_live) =~ "Listing Properties"
|
||||
|
||||
Bones73k.Accounts.logout_user(user1)
|
||||
|
||||
# Assert our liveview is alive
|
||||
ref = Process.monitor(index_live.pid)
|
||||
refute_receive {:DOWN, ^ref, _, _, _}
|
||||
assert Process.alive?(index_live.pid)
|
||||
|
||||
# If we are able to rerender the page it means nothing happened
|
||||
assert render(index_live) =~ "Listing Properties"
|
||||
end
|
||||
end
|
||||
|
||||
describe "Show" do
|
||||
setup %{conn: conn} do
|
||||
user = user_fixture()
|
||||
conn = log_in_user(conn, user)
|
||||
property = fixture(:property, user)
|
||||
property_from_another_user = fixture(:property, user_fixture())
|
||||
|
||||
%{
|
||||
conn: conn,
|
||||
property: property,
|
||||
property_from_another_user: property_from_another_user,
|
||||
user: user
|
||||
}
|
||||
end
|
||||
|
||||
test "displays property", %{conn: conn, property: property} do
|
||||
{:ok, _show_live, html} = live(conn, Routes.property_show_path(conn, :show, property))
|
||||
|
||||
assert html =~ "Show Property"
|
||||
assert html =~ property.description
|
||||
end
|
||||
|
||||
test "updates property within modal", %{conn: conn, property: property} do
|
||||
{:ok, show_live, _html} = live(conn, Routes.property_show_path(conn, :show, property))
|
||||
|
||||
assert show_live |> element("a", "Edit") |> render_click() =~
|
||||
"Edit Property"
|
||||
|
||||
assert_patch(show_live, Routes.property_show_path(conn, :edit, property))
|
||||
|
||||
assert show_live
|
||||
|> form("#property-form", property: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
{:ok, _, html} =
|
||||
show_live
|
||||
|> form("#property-form", property: @update_attrs)
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, Routes.property_show_path(conn, :show, property))
|
||||
|
||||
assert html =~ "Property updated successfully"
|
||||
assert html =~ "some updated description"
|
||||
end
|
||||
|
||||
test "can't see edit action for property from another user in show page", %{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
{:ok, show_live, _html} = live(conn, Routes.property_show_path(conn, :show, property))
|
||||
|
||||
refute has_element?(show_live, "a", "Edit")
|
||||
end
|
||||
|
||||
test "as an admin, can updates property from others within modal", %{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
conn = log_in_user(conn, admin_fixture())
|
||||
|
||||
{:ok, show_live, _html} = live(conn, Routes.property_show_path(conn, :show, property))
|
||||
|
||||
assert show_live |> element("a", "Edit") |> render_click() =~
|
||||
"Edit Property"
|
||||
|
||||
assert_patch(show_live, Routes.property_show_path(conn, :edit, property))
|
||||
|
||||
assert show_live
|
||||
|> form("#property-form", property: @invalid_attrs)
|
||||
|> render_change() =~ "can't be blank"
|
||||
|
||||
{:ok, _, html} =
|
||||
show_live
|
||||
|> form("#property-form", property: @update_attrs)
|
||||
|> render_submit()
|
||||
|> follow_redirect(conn, Routes.property_show_path(conn, :show, property))
|
||||
|
||||
assert html =~ "Property updated successfully"
|
||||
assert html =~ "some updated description"
|
||||
end
|
||||
|
||||
test "can't edit property from another user in show page", %{
|
||||
conn: conn,
|
||||
property_from_another_user: property
|
||||
} do
|
||||
assert {:error, {:redirect, %{to: "/"}}} =
|
||||
live(conn, Routes.property_show_path(conn, :edit, property))
|
||||
end
|
||||
|
||||
test "logs out when force logout on logged user", %{conn: conn, property: property} do
|
||||
user = user_fixture()
|
||||
conn = conn |> log_in_user(user)
|
||||
|
||||
{:ok, show_live, html} = live(conn, Routes.property_show_path(conn, :show, property))
|
||||
|
||||
assert html =~ "Show Property"
|
||||
assert html =~ property.description
|
||||
assert render(show_live) =~ property.description
|
||||
|
||||
Bones73k.Accounts.logout_user(user)
|
||||
|
||||
# Assert our liveview process is down
|
||||
ref = Process.monitor(show_live.pid)
|
||||
assert_receive {:DOWN, ^ref, _, _, _}
|
||||
refute Process.alive?(show_live.pid)
|
||||
|
||||
# Assert our liveview was redirected, following first to /users/force_logout, then to "/", and then to "/users/log_in"
|
||||
assert_redirect(show_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
|
||||
|
||||
test "doesn't log out when force logout on another user", %{conn: conn, property: property} do
|
||||
user1 = user_fixture()
|
||||
user2 = user_fixture()
|
||||
conn = conn |> log_in_user(user2)
|
||||
|
||||
{:ok, show_live, html} = live(conn, Routes.property_show_path(conn, :show, property))
|
||||
|
||||
assert html =~ "Show Property"
|
||||
assert html =~ property.description
|
||||
assert render(show_live) =~ property.description
|
||||
|
||||
Bones73k.Accounts.logout_user(user1)
|
||||
|
||||
# Assert our liveview is alive
|
||||
ref = Process.monitor(show_live.pid)
|
||||
refute_receive {:DOWN, ^ref, _, _, _}
|
||||
assert Process.alive?(show_live.pid)
|
||||
|
||||
# If we are able to rerender the page it means nothing happened
|
||||
assert render(show_live) =~ property.description
|
||||
end
|
||||
end
|
||||
end
|
85
test/bones73k_web/live/user_dashboard_live_test.exs
Normal file
85
test/bones73k_web/live/user_dashboard_live_test.exs
Normal file
|
@ -0,0 +1,85 @@
|
|||
defmodule Bones73kWeb.UserDashboardLiveTest do
|
||||
use Bones73kWeb.ConnCase
|
||||
|
||||
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, "/user_dashboard")
|
||||
end
|
||||
|
||||
test "disconnected and connected render authenticated with user role should redirect to index page",
|
||||
%{
|
||||
conn: conn
|
||||
} do
|
||||
conn = conn |> log_in_user(user_fixture())
|
||||
{:ok, user_dashboard, disconnected_html} = live(conn, "/user_dashboard")
|
||||
assert disconnected_html =~ "Welcome to the user dashboard!"
|
||||
assert render(user_dashboard) =~ "Welcome to the user dashboard!"
|
||||
end
|
||||
|
||||
test "disconnected and connected render authenticated with admin role should redirect to index page",
|
||||
%{
|
||||
conn: conn
|
||||
} do
|
||||
conn = conn |> log_in_user(admin_fixture())
|
||||
{:ok, user_dashboard, disconnected_html} = live(conn, "/user_dashboard")
|
||||
assert disconnected_html =~ "Welcome to the user dashboard!"
|
||||
assert render(user_dashboard) =~ "Welcome to the user dashboard!"
|
||||
end
|
||||
|
||||
test "logs out when force logout on logged user", %{
|
||||
conn: conn
|
||||
} do
|
||||
user = user_fixture()
|
||||
conn = conn |> log_in_user(user)
|
||||
|
||||
{:ok, user_dashboard, disconnected_html} = live(conn, "/user_dashboard")
|
||||
assert disconnected_html =~ "Welcome to the user dashboard!"
|
||||
assert render(user_dashboard) =~ "Welcome to the user dashboard!"
|
||||
|
||||
Bones73k.Accounts.logout_user(user)
|
||||
|
||||
# Assert our liveview process is down
|
||||
ref = Process.monitor(user_dashboard.pid)
|
||||
assert_receive {:DOWN, ^ref, _, _, _}
|
||||
refute Process.alive?(user_dashboard.pid)
|
||||
|
||||
# Assert our liveview was redirected, following first to /users/force_logout, then to "/", and then to "/users/log_in"
|
||||
assert_redirect(user_dashboard, "/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
|
||||
|
||||
test "doesn't log out when force logout on another user", %{
|
||||
conn: conn
|
||||
} do
|
||||
user1 = user_fixture()
|
||||
user2 = user_fixture()
|
||||
conn = conn |> log_in_user(user1)
|
||||
|
||||
{:ok, user_dashboard, disconnected_html} = live(conn, "/user_dashboard")
|
||||
assert disconnected_html =~ "Welcome to the user dashboard!"
|
||||
assert render(user_dashboard) =~ "Welcome to the user dashboard!"
|
||||
|
||||
Bones73k.Accounts.logout_user(user2)
|
||||
|
||||
# Assert our liveview is alive
|
||||
ref = Process.monitor(user_dashboard.pid)
|
||||
refute_receive {:DOWN, ^ref, _, _, _}
|
||||
assert Process.alive?(user_dashboard.pid)
|
||||
|
||||
# If we are able to rerender the page it means nothing happened
|
||||
assert render(user_dashboard) =~ "Welcome to the user dashboard!"
|
||||
end
|
||||
end
|
14
test/bones73k_web/views/error_view_test.exs
Normal file
14
test/bones73k_web/views/error_view_test.exs
Normal file
|
@ -0,0 +1,14 @@
|
|||
defmodule Bones73kWeb.ErrorViewTest do
|
||||
use Bones73kWeb.ConnCase, async: true
|
||||
|
||||
# Bring render/3 and render_to_string/3 for testing custom views
|
||||
import Phoenix.View
|
||||
|
||||
test "renders 404.html" do
|
||||
assert render_to_string(Bones73kWeb.ErrorView, "404.html", []) == "Not Found"
|
||||
end
|
||||
|
||||
test "renders 500.html" do
|
||||
assert render_to_string(Bones73kWeb.ErrorView, "500.html", []) == "Internal Server Error"
|
||||
end
|
||||
end
|
8
test/bones73k_web/views/layout_view_test.exs
Normal file
8
test/bones73k_web/views/layout_view_test.exs
Normal file
|
@ -0,0 +1,8 @@
|
|||
defmodule Bones73kWeb.LayoutViewTest do
|
||||
use Bones73kWeb.ConnCase, async: true
|
||||
|
||||
# When testing helpers, you may want to import Phoenix.HTML and
|
||||
# use functions such as safe_to_string() to convert the helper
|
||||
# result into an HTML string.
|
||||
# import Phoenix.HTML
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue