-
-
Notifications
You must be signed in to change notification settings - Fork 936
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No auto sign in after email confirmation (#4810)
* Don't sign in user on email confirmation so that all users use the main path to sign in. * Remove unused user_id in tests leftover from previous change
- Loading branch information
1 parent
b092b51
commit 03026a7
Showing
6 changed files
with
129 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,17 +5,35 @@ class EmailConfirmationsControllerTest < ActionController::TestCase | |
include ActiveJob::TestHelper | ||
|
||
context "on GET to update" do | ||
setup { @user = create(:user) } | ||
setup { @user = create(:user, :unconfirmed) } | ||
|
||
context "user exists and token has not expired" do | ||
setup do | ||
get :update, params: { token: @user.confirmation_token } | ||
end | ||
|
||
should "should confirm user account" do | ||
assert @user.email_confirmed | ||
assert @user.reload.email_confirmed | ||
end | ||
should "not sign in user" do | ||
refute cookies[:remember_token] | ||
end | ||
end | ||
|
||
context "successful confirmation while signed in" do | ||
setup do | ||
@user.confirm_email! # must be confirmed to sign in | ||
sign_in_as(@user) | ||
@user.update!(unconfirmed_email: "[email protected]") | ||
get :update, params: { token: @user.confirmation_token } | ||
end | ||
|
||
should redirect_to("the dashboard") { dashboard_url } | ||
|
||
should "should confirm user account" do | ||
assert @user.reload.email_confirmed | ||
end | ||
should "sign in user" do | ||
should "keep the user signed in" do | ||
assert cookies[:remember_token] | ||
end | ||
end | ||
|
@@ -65,7 +83,7 @@ class EmailConfirmationsControllerTest < ActionController::TestCase | |
get :update, params: { token: @user.confirmation_token } | ||
end | ||
|
||
should redirect_to("the homepage") { root_url } | ||
should redirect_to("the sign in page") { sign_in_url } | ||
|
||
should "confirm email for first user" do | ||
assert_equal @email, @user.reload.email | ||
|
@@ -76,7 +94,7 @@ class EmailConfirmationsControllerTest < ActionController::TestCase | |
get :update, params: { token: @second_user.confirmation_token } | ||
end | ||
|
||
should "show error to second user on confirmation request and not " do | ||
should "show error to second user on confirmation request" do | ||
assert_equal "Email address has already been taken", flash[:alert] | ||
end | ||
|
||
|
@@ -162,29 +180,52 @@ class EmailConfirmationsControllerTest < ActionController::TestCase | |
context "on POST to otp_update" do | ||
context "user has mfa enabled" do | ||
setup do | ||
@user = create(:user) | ||
@user = create(:user, :unconfirmed) | ||
@user.enable_totp!(ROTP::Base32.random_base32, :ui_only) | ||
end | ||
|
||
context "when OTP is correct" do | ||
setup do | ||
get :update, params: { token: @user.confirmation_token, user_id: @user.id } | ||
get :update, params: { token: @user.confirmation_token } | ||
post :otp_update, params: { token: @user.confirmation_token, otp: ROTP::TOTP.new(@user.totp_seed).now } | ||
end | ||
|
||
should redirect_to("the homepage") { root_url } | ||
should set_flash[:notice] | ||
should redirect_to("the sign in page") { sign_in_url } | ||
|
||
should "should confirm user account" do | ||
assert @user.email_confirmed | ||
assert @user.reload.email_confirmed | ||
end | ||
|
||
should "clear mfa_expires_at" do | ||
assert_nil @controller.session[:mfa_expires_at] | ||
end | ||
end | ||
|
||
context "user is already signed in and OTP is correct" do | ||
setup do | ||
@user.confirm_email! | ||
sign_in_as(@user) | ||
@user.update!(unconfirmed_email: "[email protected]") | ||
|
||
assert @user.confirmation_token | ||
get :update, params: { token: @user.confirmation_token } | ||
post :otp_update, params: { token: @user.confirmation_token, otp: ROTP::TOTP.new(@user.totp_seed).now } | ||
end | ||
|
||
should redirect_to("the dashboard") { dashboard_url } | ||
|
||
should "should confirm user account" do | ||
assert @user.reload.email_confirmed | ||
end | ||
should "keep the user signed in" do | ||
assert cookies[:remember_token] | ||
end | ||
end | ||
|
||
context "when OTP is incorrect" do | ||
setup do | ||
get :update, params: { token: @user.confirmation_token, user_id: @user.id } | ||
get :update, params: { token: @user.confirmation_token } | ||
post :otp_update, params: { token: @user.confirmation_token, otp: "incorrect" } | ||
end | ||
|
||
|
@@ -197,7 +238,7 @@ class EmailConfirmationsControllerTest < ActionController::TestCase | |
|
||
context "when the OTP session is expired" do | ||
setup do | ||
get :update, params: { token: @user.confirmation_token, user_id: @user.id } | ||
get :update, params: { token: @user.confirmation_token } | ||
travel 16.minutes do | ||
post :otp_update, params: { token: @user.confirmation_token, otp: ROTP::TOTP.new(@user.totp_seed).now } | ||
end | ||
|
@@ -223,9 +264,9 @@ class EmailConfirmationsControllerTest < ActionController::TestCase | |
|
||
context "on POST to webauthn_update" do | ||
setup do | ||
@user = create(:user) | ||
@user = create(:user, :unconfirmed) | ||
@webauthn_credential = create(:webauthn_credential, user: @user) | ||
get :update, params: { token: @user.confirmation_token, user_id: @user.id } | ||
get :update, params: { token: @user.confirmation_token } | ||
@origin = WebAuthn.configuration.origin | ||
@rp_id = URI.parse(@origin).host | ||
@client = WebAuthn::FakeClient.new(@origin, encoding: false) | ||
|
@@ -241,7 +282,6 @@ class EmailConfirmationsControllerTest < ActionController::TestCase | |
post( | ||
:webauthn_update, | ||
params: { | ||
user_id: @user.id, | ||
token: @user.confirmation_token, | ||
credentials: | ||
WebauthnHelpers.get_result( | ||
|
@@ -252,25 +292,65 @@ class EmailConfirmationsControllerTest < ActionController::TestCase | |
) | ||
end | ||
|
||
should "redirect to root" do | ||
assert_redirected_to root_url | ||
should redirect_to("the sign in page") { sign_in_url } | ||
|
||
should "change the user's email" do | ||
assert @user.reload.email_confirmed | ||
end | ||
|
||
should "clear mfa_expires_at" do | ||
assert_nil @controller.session[:mfa_expires_at] | ||
end | ||
|
||
should "set flash notice" do | ||
assert_equal "Your email address has been verified.", flash[:notice] | ||
end | ||
end | ||
|
||
context "while signed in with successful webauthn" do | ||
setup do | ||
@user.confirm_email! | ||
sign_in_as(@user) | ||
@user.update!(unconfirmed_email: "[email protected]") | ||
@challenge = session[:webauthn_authentication]["challenge"] | ||
WebauthnHelpers.create_credential( | ||
webauthn_credential: @webauthn_credential, | ||
client: @client | ||
) | ||
post( | ||
:webauthn_update, | ||
params: { | ||
token: @user.confirmation_token, | ||
credentials: | ||
WebauthnHelpers.get_result( | ||
client: @client, | ||
challenge: @challenge | ||
) | ||
} | ||
) | ||
end | ||
|
||
should redirect_to("the dashboard") { dashboard_url } | ||
|
||
should "change the user's email" do | ||
assert @user.reload.email_confirmed | ||
assert_equal "[email protected]", @user.email | ||
end | ||
|
||
should "clear mfa_expires_at" do | ||
assert_nil @controller.session[:mfa_expires_at] | ||
end | ||
|
||
should "set flash notice" do | ||
assert_equal "Your email address has been verified.", flash[:notice] | ||
end | ||
end | ||
|
||
context "when not providing credentials" do | ||
setup do | ||
post( | ||
:webauthn_update, | ||
params: { | ||
user_id: @user.id, | ||
token: @user.confirmation_token | ||
} | ||
) | ||
|
@@ -293,7 +373,6 @@ class EmailConfirmationsControllerTest < ActionController::TestCase | |
post( | ||
:webauthn_update, | ||
params: { | ||
user_id: @user.id, | ||
token: @user.confirmation_token, | ||
credentials: | ||
WebauthnHelpers.get_result( | ||
|
@@ -325,7 +404,6 @@ class EmailConfirmationsControllerTest < ActionController::TestCase | |
post( | ||
:webauthn_update, | ||
params: { | ||
user_id: @user.id, | ||
token: @user.confirmation_token, | ||
credentials: | ||
WebauthnHelpers.get_result( | ||
|
@@ -493,7 +571,7 @@ class EmailConfirmationsControllerTest < ActionController::TestCase | |
end | ||
|
||
should "should confirm user account" do | ||
assert @user.email_confirmed | ||
assert @user.reload.email_confirmed | ||
end | ||
end | ||
|
||
|
@@ -546,7 +624,7 @@ class EmailConfirmationsControllerTest < ActionController::TestCase | |
end | ||
|
||
should "should confirm user account" do | ||
assert @user.email_confirmed | ||
assert @user.reload.email_confirmed | ||
end | ||
end | ||
|
||
|
@@ -599,7 +677,7 @@ class EmailConfirmationsControllerTest < ActionController::TestCase | |
end | ||
|
||
should "should confirm user account" do | ||
assert @user.email_confirmed | ||
assert @user.reload.email_confirmed | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,8 +94,14 @@ class SignUpTest < SystemTest | |
assert_not_nil link | ||
visit link | ||
|
||
assert page.has_content? "Sign out" | ||
assert page.has_content? "Sign in" | ||
assert page.has_selector? "#flash_notice", text: "Your email address has been verified" | ||
|
||
fill_in "Email or Username", with: "[email protected]" | ||
fill_in "Password", with: PasswordHelpers::SECURE_TEST_PASSWORD | ||
click_button "Sign in" | ||
|
||
assert page.has_content? "Sign out" | ||
end | ||
|
||
teardown do | ||
|