-
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 #2350 from alphagov/batch-invitations-two-step-flow
Break batch invitations creation into two steps
- Loading branch information
Showing
14 changed files
with
397 additions
and
88 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
app/controllers/batch_invitation_permissions_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,45 @@ | ||
class BatchInvitationPermissionsController < ApplicationController | ||
include UserPermissionsControllerMethods | ||
before_action :authenticate_user! | ||
before_action :load_batch_invitation | ||
before_action :authorise_to_manage_permissions | ||
before_action :prevent_updating | ||
|
||
helper_method :applications_and_permissions | ||
|
||
def new; end | ||
|
||
def create | ||
@batch_invitation.supported_permission_ids = params[:user][:supported_permission_ids] if params[:user] | ||
grant_default_permissions(@batch_invitation) | ||
|
||
@batch_invitation.save! | ||
|
||
@batch_invitation.enqueue | ||
flash[:notice] = "Scheduled invitation of #{@batch_invitation.batch_invitation_users.count} users" | ||
redirect_to batch_invitation_path(@batch_invitation) | ||
end | ||
|
||
private | ||
|
||
def load_batch_invitation | ||
@batch_invitation = current_user.batch_invitations.find(params[:batch_invitation_id]) | ||
end | ||
|
||
def authorise_to_manage_permissions | ||
authorize @batch_invitation, :manage_permissions? | ||
end | ||
|
||
def prevent_updating | ||
if @batch_invitation.has_permissions? | ||
flash[:alert] = "Permissions have already been set for this batch of users" | ||
redirect_to batch_invitation_path(@batch_invitation) | ||
end | ||
end | ||
|
||
def grant_default_permissions(batch_invitation) | ||
SupportedPermission.default.each do |default_permission| | ||
batch_invitation.grant_permission(default_permission) | ||
end | ||
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
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<% content_for :title, "Manage permissions for new users" %> | ||
|
||
<div class="page-title"> | ||
<h1>Manage permissions for new users</h1> | ||
</div> | ||
|
||
<div class="well"> | ||
<%= form_for @batch_invitation, url: :batch_invitation_permissions, method: :post do |f| %> | ||
<%= render partial: "shared/user_permissions", locals: { user_object: User.new } %> | ||
|
||
<%= f.submit "Create users and send emails", :class => 'btn btn-success' %> | ||
<% end %> | ||
</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
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
103 changes: 103 additions & 0 deletions
103
test/controllers/batch_invitation_permissions_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,103 @@ | ||
require "test_helper" | ||
|
||
class BatchInvitationPermissionsControllerTest < ActionController::TestCase | ||
include ActiveJob::TestHelper | ||
|
||
setup do | ||
@user = create(:admin_user) | ||
sign_in @user | ||
|
||
@app = create(:application, name: "Profound Publisher") | ||
|
||
@batch_invitation = create(:batch_invitation, user: @user) | ||
create( | ||
:batch_invitation_user, | ||
name: "Darayavaush Ayers", | ||
email: "darayavaush.ayers@department.gov.uk", | ||
batch_invitation: @batch_invitation, | ||
) | ||
create( | ||
:batch_invitation_user, | ||
name: "Precious Kumar", | ||
email: "precious.kumar@department.gov.uk", | ||
batch_invitation: @batch_invitation, | ||
) | ||
end | ||
|
||
context "GET new" do | ||
should "not allow access if batch invitation already has permissions" do | ||
@batch_invitation.supported_permission_ids = [@app.signin_permission.id] | ||
@batch_invitation.save! | ||
|
||
get :new, params: { batch_invitation_id: @batch_invitation.id } | ||
|
||
assert_match(/Permissions have already been set for this batch of users/, flash[:alert]) | ||
assert_redirected_to "/batch_invitations/#{@batch_invitation.id}" | ||
end | ||
|
||
should "allow selection of application permissions to grant to users" do | ||
get :new, params: { batch_invitation_id: @batch_invitation.id } | ||
|
||
assert_select "table#editable-permissions" do | ||
assert_select "td", "Has access to Profound Publisher?" | ||
assert_select "td", "Permissions for Profound Publisher" | ||
end | ||
end | ||
end | ||
|
||
context "POST create" do | ||
should "not accept submission if batch invitation already has permissions" do | ||
@batch_invitation.supported_permission_ids = [@app.signin_permission.id] | ||
@batch_invitation.save! | ||
|
||
post :create, params: { batch_invitation_id: @batch_invitation.id } | ||
|
||
assert_match(/Permissions have already been set for this batch of users/, flash[:alert]) | ||
assert_redirected_to "/batch_invitations/#{@batch_invitation.id}" | ||
end | ||
|
||
should "grant selected permissions and default permissions to BatchInvitation" do | ||
support_app = create(:application, name: "Support") | ||
support_app.signin_permission.update!(default: true) | ||
|
||
post :create, params: { | ||
batch_invitation_id: @batch_invitation.id, | ||
user: { supported_permission_ids: [@app.signin_permission.id] }, | ||
} | ||
|
||
assert_equal [@app.signin_permission, support_app.signin_permission], | ||
@batch_invitation.supported_permissions | ||
end | ||
|
||
context "with no permissions selected" do | ||
should "still grant default permissions to BatchInvitation" do | ||
support_app = create(:application, name: "Support") | ||
support_app.signin_permission.update!(default: true) | ||
|
||
post :create, params: { batch_invitation_id: @batch_invitation.id } | ||
|
||
assert_equal [support_app.signin_permission], | ||
@batch_invitation.supported_permissions | ||
end | ||
end | ||
|
||
should "send an email to signon-alerts" do | ||
perform_enqueued_jobs do | ||
post :create, params: { batch_invitation_id: @batch_invitation.id } | ||
|
||
email = ActionMailer::Base.deliveries.detect do |m| | ||
m.to.any? { |to| to =~ /signon-alerts@.*\.gov\.uk/ } | ||
end | ||
assert_not_nil email | ||
assert_equal "[SIGNON] #{@user.name} created a batch of 2 users in development", email.subject | ||
end | ||
end | ||
|
||
should "redirect to the batch invitation page and show a flash message" do | ||
post :create, params: { batch_invitation_id: @batch_invitation.id } | ||
|
||
assert_match(/Scheduled invitation of 2 users/i, flash[:notice]) | ||
assert_redirected_to "/batch_invitations/#{@batch_invitation.id}" | ||
end | ||
end | ||
end |
Oops, something went wrong.