-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2545 from alphagov/split-out-edit-user-2sv-pages-…
…from-user-edit-page Add separate page for resetting another user's 2SV
- Loading branch information
Showing
9 changed files
with
293 additions
and
58 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
app/controllers/users/two_step_verification_resets_controller.rb
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Users::TwoStepVerificationResetsController < ApplicationController | ||
layout "admin_layout" | ||
|
||
before_action :authenticate_user! | ||
before_action :load_user | ||
before_action :authorize_user | ||
before_action :redirect_to_account_page_if_acting_on_own_user, only: %i[edit] | ||
|
||
def edit; end | ||
|
||
def update | ||
@user.reset_2sv!(current_user) | ||
UserMailer.two_step_reset(@user).deliver_later | ||
|
||
redirect_to edit_user_path(@user), notice: "Reset 2-step verification for #{@user.email}" | ||
end | ||
|
||
private | ||
|
||
def load_user | ||
@user = User.find(params[:user_id]) | ||
end | ||
|
||
def authorize_user | ||
authorize(@user, :reset_2sv?) | ||
end | ||
|
||
def redirect_to_account_page_if_acting_on_own_user | ||
redirect_to two_step_verification_path if current_user == @user | ||
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
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
42 changes: 42 additions & 0 deletions
42
app/views/users/two_step_verification_resets/edit.html.erb
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<% content_for :title_caption, "Manage other users" %> | ||
<% content_for :title, "Reset 2-step verification for #{@user.name}" %> | ||
|
||
<% content_for :breadcrumbs, | ||
render("govuk_publishing_components/components/breadcrumbs", { | ||
collapse_on_mobile: true, | ||
breadcrumbs: [ | ||
{ | ||
title: "Dashboard", | ||
url: root_path, | ||
}, | ||
{ | ||
title: "Users", | ||
url: users_path, | ||
}, | ||
{ | ||
title: @user.name, | ||
url: edit_user_path(@user), | ||
}, | ||
{ | ||
title: "Reset 2-step verification", | ||
} | ||
] | ||
}) | ||
%> | ||
|
||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
<%= form_for @user, url: user_two_step_verification_reset_path(@user) do %> | ||
<%= render "govuk_publishing_components/components/hint", { | ||
text: "Allows user to sign in without a verification code. User will be prompted to set up 2-step verification again the next time they sign in." | ||
} %> | ||
<div class="govuk-button-group"> | ||
<%= render "govuk_publishing_components/components/button", { | ||
text: "Reset 2-step verification", | ||
destructive: true, | ||
} %> | ||
<%= link_to "Cancel", edit_user_path(@user), class: "govuk-link govuk-link--no-visited-state" %> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> |
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
173 changes: 173 additions & 0 deletions
173
test/controllers/users/two_step_verification_resets_controller_test.rb
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 |
---|---|---|
@@ -0,0 +1,173 @@ | ||
require "test_helper" | ||
|
||
class Users::TwoStepVerificationResetsControllerTest < ActionController::TestCase | ||
include ActiveJob::TestHelper | ||
|
||
context "GET edit" do | ||
context "signed in as Admin user" do | ||
setup do | ||
@admin = create(:admin_user) | ||
sign_in(@admin) | ||
end | ||
|
||
should "display form with submit button & cancel link" do | ||
user = create(:two_step_enabled_user) | ||
|
||
get :edit, params: { user_id: user } | ||
|
||
assert_template :edit | ||
assert_select "form[action='#{user_two_step_verification_reset_path(user)}']" do | ||
assert_select "button[type='submit']", text: "Reset 2-step verification" | ||
assert_select "a[href='#{edit_user_path(user)}']", text: "Cancel" | ||
end | ||
end | ||
|
||
should "authorize access if UserPolicy#reset_2sv? returns true" do | ||
user = create(:two_step_enabled_user) | ||
|
||
user_policy = stub_everything("user-policy", reset_2sv?: true) | ||
UserPolicy.stubs(:new).returns(user_policy) | ||
|
||
get :edit, params: { user_id: user } | ||
|
||
assert_template :edit | ||
end | ||
|
||
should "not authorize access if UserPolicy#reset_2sv? returns false" do | ||
user = create(:two_step_enabled_user) | ||
|
||
user_policy = stub_everything("user-policy", reset_2sv?: false) | ||
UserPolicy.stubs(:new).returns(user_policy) | ||
|
||
get :edit, params: { user_id: user } | ||
|
||
assert_not_authorised | ||
end | ||
|
||
should "redirect to account change 2-step verification phone page if admin is acting on their own user" do | ||
get :edit, params: { user_id: @admin } | ||
|
||
assert_redirected_to two_step_verification_path | ||
end | ||
end | ||
|
||
context "signed in as Normal user" do | ||
setup do | ||
sign_in(create(:user)) | ||
end | ||
|
||
should "not be authorized" do | ||
user = create(:user) | ||
|
||
get :edit, params: { user_id: user } | ||
|
||
assert_not_authorised | ||
end | ||
end | ||
|
||
context "not signed in" do | ||
should "not be allowed access" do | ||
user = create(:user) | ||
|
||
get :edit, params: { user_id: user } | ||
|
||
assert_not_authenticated | ||
end | ||
end | ||
end | ||
|
||
context "PUT update" do | ||
context "signed in as Admin user" do | ||
setup do | ||
@admin = create(:admin_user) | ||
sign_in(@admin) | ||
end | ||
|
||
should "reset 2SV for user" do | ||
user = create(:two_step_enabled_user) | ||
|
||
put :update, params: { user_id: user } | ||
|
||
user.reload | ||
assert user.otp_secret_key.blank? | ||
assert user.require_2sv? | ||
end | ||
|
||
should "record account updated event" do | ||
user = create(:two_step_enabled_user) | ||
|
||
EventLog.expects(:record_event).with(user, EventLog::TWO_STEP_RESET, initiator: @admin) | ||
|
||
put :update, params: { user_id: user } | ||
end | ||
|
||
should "should send email notifying user that their 2SV has been reset" do | ||
user = create(:two_step_enabled_user) | ||
|
||
perform_enqueued_jobs do | ||
put :update, params: { user_id: user } | ||
end | ||
|
||
email = ActionMailer::Base.deliveries.last | ||
assert email.present? | ||
assert_equal "2-step verification has been reset", email.subject | ||
end | ||
|
||
should "redirect to user page and display success notice" do | ||
user = create(:two_step_enabled_user, email: "user@gov.uk") | ||
|
||
put :update, params: { user_id: user } | ||
|
||
assert_redirected_to edit_user_path(user) | ||
assert_equal "Reset 2-step verification for user@gov.uk", flash[:notice] | ||
end | ||
|
||
should "reset 2SV for user if UserPolicy#reset_2sv? returns true" do | ||
user = create(:two_step_enabled_user) | ||
|
||
user_policy = stub_everything("user-policy", reset_2sv?: true) | ||
UserPolicy.stubs(:new).returns(user_policy) | ||
|
||
put :update, params: { user_id: user } | ||
|
||
assert user.reload.otp_secret_key.blank? | ||
end | ||
|
||
should "not reset 2SV for user if UserPolicy#reset_2sv? returns false" do | ||
user = create(:two_step_enabled_user) | ||
|
||
user_policy = stub_everything("user-policy", reset_2sv?: false) | ||
UserPolicy.stubs(:new).returns(user_policy) | ||
|
||
put :update, params: { user_id: user } | ||
|
||
assert user.reload.otp_secret_key.present? | ||
assert_not_authorised | ||
end | ||
end | ||
|
||
context "signed in as Normal user" do | ||
setup do | ||
sign_in(create(:user)) | ||
end | ||
|
||
should "not be authorized" do | ||
user = create(:two_step_enabled_user) | ||
|
||
put :update, params: { user_id: user } | ||
|
||
assert_not_authorised | ||
end | ||
end | ||
|
||
context "not signed in" do | ||
should "not be allowed access" do | ||
user = create(:two_step_enabled_user) | ||
|
||
put :update, params: { user_id: user } | ||
|
||
assert_not_authenticated | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.