2024-07-22 15:37:03 +02:00
|
|
|
require "test_helper"
|
|
|
|
|
|
|
|
class SessionsControllerTest < ActionDispatch::IntegrationTest
|
|
|
|
setup do
|
|
|
|
@user = users(:family_admin)
|
|
|
|
end
|
|
|
|
|
2024-10-03 14:42:22 -04:00
|
|
|
test "login page" do
|
|
|
|
get new_session_url
|
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
2024-07-22 15:37:03 +02:00
|
|
|
test "can sign in" do
|
2024-10-03 14:42:22 -04:00
|
|
|
sign_in @user
|
2024-07-22 15:37:03 +02:00
|
|
|
assert_redirected_to root_url
|
2025-02-06 14:16:53 -06:00
|
|
|
assert Session.exists?(user_id: @user.id)
|
2024-10-03 14:42:22 -04:00
|
|
|
|
|
|
|
get root_url
|
|
|
|
assert_response :success
|
2024-07-22 15:37:03 +02:00
|
|
|
end
|
|
|
|
|
2024-10-03 14:42:22 -04:00
|
|
|
test "fails to sign in with bad password" do
|
|
|
|
post sessions_url, params: { email: @user.email, password: "bad" }
|
|
|
|
assert_response :unprocessable_entity
|
|
|
|
assert_equal "Invalid email or password.", flash[:alert]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "can sign out" do
|
|
|
|
sign_in @user
|
2025-02-06 14:16:53 -06:00
|
|
|
session_record = @user.sessions.last
|
2024-10-03 14:42:22 -04:00
|
|
|
|
2025-02-06 14:16:53 -06:00
|
|
|
delete session_url(session_record)
|
2024-10-23 11:20:55 -04:00
|
|
|
assert_redirected_to new_session_path
|
2024-10-03 14:42:22 -04:00
|
|
|
assert_equal "You have signed out successfully.", flash[:notice]
|
2025-02-06 14:16:53 -06:00
|
|
|
|
|
|
|
# Verify session is destroyed
|
|
|
|
assert_nil Session.find_by(id: session_record.id)
|
2024-07-22 15:37:03 +02:00
|
|
|
end
|
2024-10-24 17:28:29 -04:00
|
|
|
|
2025-02-06 14:16:53 -06:00
|
|
|
test "redirects to MFA verification when MFA enabled" do
|
|
|
|
@user.setup_mfa!
|
|
|
|
@user.enable_mfa!
|
|
|
|
@user.sessions.destroy_all # Clean up any existing sessions
|
|
|
|
|
2025-04-14 08:41:49 -04:00
|
|
|
post sessions_path, params: { email: @user.email, password: user_password_test }
|
2025-02-06 14:16:53 -06:00
|
|
|
|
|
|
|
assert_redirected_to verify_mfa_path
|
|
|
|
assert_equal @user.id, session[:mfa_user_id]
|
|
|
|
assert_not Session.exists?(user_id: @user.id)
|
|
|
|
end
|
2024-07-22 15:37:03 +02:00
|
|
|
end
|