diff --git a/app/controllers/batch_invitation_permissions_controller.rb b/app/controllers/batch_invitation_permissions_controller.rb new file mode 100644 index 000000000..af73635c1 --- /dev/null +++ b/app/controllers/batch_invitation_permissions_controller.rb @@ -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 diff --git a/app/controllers/batch_invitations_controller.rb b/app/controllers/batch_invitations_controller.rb index d3f8686e6..b51a54d87 100644 --- a/app/controllers/batch_invitations_controller.rb +++ b/app/controllers/batch_invitations_controller.rb @@ -1,10 +1,8 @@ require "csv" class BatchInvitationsController < ApplicationController - include UserPermissionsControllerMethods before_action :authenticate_user! - helper_method :applications_and_permissions helper_method :recent_batch_invitations layout "admin_layout", only: %w[show] @@ -16,8 +14,6 @@ def new def create @batch_invitation = BatchInvitation.new(user: current_user, organisation_id: params[:batch_invitation][:organisation_id]) - @batch_invitation.supported_permission_ids = params[:user][:supported_permission_ids] if params[:user] - grant_default_permissions(@batch_invitation) authorize @batch_invitation unless file_uploaded? @@ -64,9 +60,7 @@ def create @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) + redirect_to new_batch_invitation_permissions_path(@batch_invitation) end def show @@ -91,12 +85,6 @@ def file_uploaded? end end - def grant_default_permissions(batch_invitation) - SupportedPermission.default.each do |default_permission| - batch_invitation.grant_permission(default_permission) - end - end - def batch_users_error_message(batch_user) e = batch_user.errors.first diff --git a/app/helpers/batch_invitations_helper.rb b/app/helpers/batch_invitations_helper.rb index 9f49e651d..30778c38a 100644 --- a/app/helpers/batch_invitations_helper.rb +++ b/app/helpers/batch_invitations_helper.rb @@ -1,4 +1,12 @@ module BatchInvitationsHelper + def batch_invite_status_link(batch_invitation, &block) + if !batch_invitation.has_permissions? + link_to(new_batch_invitation_permissions_path(batch_invitation), alt: "Edit this batch's permissions", &block) + else + link_to(batch_invitation_path(batch_invitation), alt: "View this batch", &block) + end + end + def batch_invite_status_message(batch_invitation) if batch_invitation.in_progress? "In progress. " \ @@ -7,6 +15,8 @@ def batch_invite_status_message(batch_invitation) "users processed." elsif batch_invitation.all_successful? "#{batch_invitation.batch_invitation_users.count} users processed." + elsif !batch_invitation.has_permissions? + "Batch invitation doesn't have any permissions yet." else "#{pluralize(batch_invitation.batch_invitation_users.failed.count, 'error')} out of " \ "#{batch_invitation.batch_invitation_users.count} " \ diff --git a/app/models/batch_invitation.rb b/app/models/batch_invitation.rb index 3fb2112ea..29cc916f1 100644 --- a/app/models/batch_invitation.rb +++ b/app/models/batch_invitation.rb @@ -12,12 +12,16 @@ class BatchInvitation < ApplicationRecord validates :outcome, inclusion: { in: [nil, "success", "fail"] } validates :user_id, presence: true + def has_permissions? + batch_invitation_application_permissions.exists? + end + def in_progress? - outcome.nil? + outcome.nil? && has_permissions? end def all_successful? - batch_invitation_users.failed.count.zero? + !outcome.nil? && batch_invitation_users.failed.count.zero? end def enqueue diff --git a/app/policies/batch_invitation_policy.rb b/app/policies/batch_invitation_policy.rb index d08587d6a..dc8e90d69 100644 --- a/app/policies/batch_invitation_policy.rb +++ b/app/policies/batch_invitation_policy.rb @@ -6,6 +6,7 @@ def new? end alias_method :create?, :new? alias_method :show?, :new? + alias_method :manage_permissions?, :new? def assign_organisation_from_csv? current_user.govuk_admin? diff --git a/app/views/batch_invitation_permissions/new.html.erb b/app/views/batch_invitation_permissions/new.html.erb new file mode 100644 index 000000000..346f0a3e1 --- /dev/null +++ b/app/views/batch_invitation_permissions/new.html.erb @@ -0,0 +1,13 @@ +<% content_for :title, "Manage permissions for new users" %> + +
+

Manage permissions for new users

+
+ +
+ <%= 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 %> +
diff --git a/app/views/batch_invitations/new.html.erb b/app/views/batch_invitations/new.html.erb index e95b89019..6efdc0db3 100644 --- a/app/views/batch_invitations/new.html.erb +++ b/app/views/batch_invitations/new.html.erb @@ -17,7 +17,7 @@ <% recent_batch_invitations.each do |batch_invitation| %> - <%= link_to(batch_invitation_path(batch_invitation), alt: "View this batch") do %> + <%= batch_invite_status_link(batch_invitation) do %> <%= batch_invitation.batch_invitation_users.count %> users by <%= batch_invitation.user.name %> at <%= batch_invitation.created_at.to_fs(:govuk_date) %> <% end %> @@ -66,10 +66,6 @@ Winston Churchill,winston@example.com <% end %> -

Permissions for the users

- - <%= render partial: "shared/user_permissions", locals: { user_object: User.new } %> - - <%= f.submit "Create users and send emails", :class => 'btn btn-success' %> + <%= f.submit "Manage permissions for new users", :class => 'btn btn-success' %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 6ee78fad9..464d26df1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -54,7 +54,12 @@ resources :applications, only: [:index] end - resources :batch_invitations, only: %i[new create show] + resources :batch_invitations, only: %i[new create show] do + resource :permissions, + only: %i[new create], + controller: :batch_invitation_permissions + end + resources :bulk_grant_permission_sets, only: %i[new create show] resources :organisations, only: %i[index edit update] resources :suspensions, only: %i[edit update] diff --git a/test/controllers/batch_invitation_permissions_controller_test.rb b/test/controllers/batch_invitation_permissions_controller_test.rb new file mode 100644 index 000000000..59e82e838 --- /dev/null +++ b/test/controllers/batch_invitation_permissions_controller_test.rb @@ -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 diff --git a/test/controllers/batch_invitations_controller_test.rb b/test/controllers/batch_invitations_controller_test.rb index 1d9ca2a24..d516c80d8 100644 --- a/test/controllers/batch_invitations_controller_test.rb +++ b/test/controllers/batch_invitations_controller_test.rb @@ -21,7 +21,7 @@ def users_csv(filename = "users.csv") context "some batches created recently" do setup do - @bi = create(:batch_invitation) + @bi = create(:batch_invitation, :in_progress) create(:batch_invitation_user, batch_invitation: @bi) end @@ -43,12 +43,11 @@ def users_csv(filename = "users.csv") context "POST create" do should "create a BatchInvitation and BatchInvitationUsers" do - app = create(:application) - post :create, params: { batch_invitation: { user_names_and_emails: users_csv }, user: { supported_permission_ids: [app.signin_permission.id] } } + create(:application) + post :create, params: { batch_invitation: { user_names_and_emails: users_csv } } bi = BatchInvitation.last assert_not_nil bi - assert_equal [app.signin_permission], bi.supported_permissions expected_names_and_emails = [["Arthur Dent", "a@hhg.com"], ["Tricia McMillan", "t@hhg.com"]] assert_equal( expected_names_and_emails, @@ -57,9 +56,7 @@ def users_csv(filename = "users.csv") end should "store the organisation to invite users to" do - post :create, - params: { user: { supported_permission_ids: [] }, - batch_invitation: { user_names_and_emails: users_csv, organisation_id: 3 } } + post :create, params: { batch_invitation: { user_names_and_emails: users_csv, organisation_id: 3 } } bi = BatchInvitation.last @@ -70,8 +67,7 @@ def users_csv(filename = "users.csv") should "store organisation info from the uploaded CSV when logged in as an admin" do @user.update!(role: Roles::Admin.role_name) post :create, - params: { user: { supported_permission_ids: [] }, - batch_invitation: { user_names_and_emails: users_csv("users_with_orgs.csv"), organisation_id: 3 } } + params: { batch_invitation: { user_names_and_emails: users_csv("users_with_orgs.csv"), organisation_id: 3 } } bi = BatchInvitation.last @@ -85,8 +81,7 @@ def users_csv(filename = "users.csv") should "store organisation info from the uploaded CSV when logged in as a superadmin" do @user.update!(role: Roles::Superadmin.role_name) post :create, - params: { user: { supported_permission_ids: [] }, - batch_invitation: { user_names_and_emails: users_csv("users_with_orgs.csv"), organisation_id: 3 } } + params: { batch_invitation: { user_names_and_emails: users_csv("users_with_orgs.csv"), organisation_id: 3 } } bi = BatchInvitation.last @@ -97,34 +92,15 @@ def users_csv(filename = "users.csv") assert_equal "cabinet-office", bi.batch_invitation_users[2].organisation_slug end - should "queue a job to do the processing" do - assert_enqueued_jobs 2 do - post :create, params: { batch_invitation: { user_names_and_emails: users_csv }, user: { supported_permission_ids: [] } } - end - end - - should "send an email to signon-alerts" do - perform_enqueued_jobs do - post :create, params: { batch_invitation: { user_names_and_emails: users_csv }, user: { supported_permission_ids: [] } } - - 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: { user_names_and_emails: users_csv }, user: { supported_permission_ids: [] } } + should "redirect to the batch invitation permissions page and show a flash message" do + post :create, params: { batch_invitation: { user_names_and_emails: users_csv } } - assert_match(/Scheduled invitation of 2 users/i, flash[:notice]) - assert_redirected_to "/batch_invitations/#{BatchInvitation.last.id}" + assert_redirected_to "/batch_invitations/#{BatchInvitation.last.id}/permissions/new" end context "no file uploaded" do should "redisplay the form and show a flash message" do - post :create, params: { batch_invitation: { user_names_and_emails: nil }, user: { supported_permission_ids: [] } } + post :create, params: { batch_invitation: { user_names_and_emails: nil } } assert_template :new assert_match(/You must upload a file/i, flash[:alert]) @@ -133,7 +109,7 @@ def users_csv(filename = "users.csv") context "the CSV contains one or more email addresses that aren't valid" do should "redisplay the form and show a flash message" do - post :create, params: { batch_invitation: { user_names_and_emails: users_csv("users_with_non_valid_emails.csv") }, user: { supported_permission_ids: [] } } + post :create, params: { batch_invitation: { user_names_and_emails: users_csv("users_with_non_valid_emails.csv") } } assert_template :new assert_match(/One or more emails were invalid/i, flash[:alert]) @@ -142,7 +118,7 @@ def users_csv(filename = "users.csv") context "the CSV has all the fields, but not in the expected order" do should "process the fields by name" do - post :create, params: { batch_invitation: { user_names_and_emails: users_csv("reversed_users.csv") }, user: { supported_permission_ids: [] } } + post :create, params: { batch_invitation: { user_names_and_emails: users_csv("reversed_users.csv") } } bi = BatchInvitation.last assert_not_nil bi.batch_invitation_users.find_by(email: "a@hhg.com") @@ -152,7 +128,7 @@ def users_csv(filename = "users.csv") context "the CSV has no data rows" do should "redisplay the form and show a flash message" do - post :create, params: { batch_invitation: { user_names_and_emails: users_csv("empty_users.csv") }, user: { supported_permission_ids: [] } } + post :create, params: { batch_invitation: { user_names_and_emails: users_csv("empty_users.csv") } } assert_template :new assert_match(/no rows/i, flash[:alert]) @@ -161,7 +137,7 @@ def users_csv(filename = "users.csv") context "the CSV format is invalid" do should "redisplay the form and show a flash message" do - post :create, params: { batch_invitation: { user_names_and_emails: users_csv("invalid_users.csv") }, user: { supported_permission_ids: [] } } + post :create, params: { batch_invitation: { user_names_and_emails: users_csv("invalid_users.csv") } } assert_template :new assert_match(/Couldn't understand that file/i, flash[:alert]) @@ -170,7 +146,7 @@ def users_csv(filename = "users.csv") context "the CSV has no headers?" do should "redisplay the form and show a flash message" do - post :create, params: { batch_invitation: { user_names_and_emails: users_csv("no_headers_users.csv") }, user: { supported_permission_ids: [] } } + post :create, params: { batch_invitation: { user_names_and_emails: users_csv("no_headers_users.csv") } } assert_template :new assert_match(/must have headers/i, flash[:alert]) @@ -179,40 +155,44 @@ def users_csv(filename = "users.csv") end context "GET show" do - setup do - @bi = create(:batch_invitation) - @user1 = create(:batch_invitation_user, name: "A", email: "a@m.com", batch_invitation: @bi) - @user2 = create(:batch_invitation_user, name: "B", email: "b@m.com", batch_invitation: @bi) - end + context "processing in progress" do + setup do + @bi = create(:batch_invitation, :in_progress) + @user1 = create(:batch_invitation_user, name: "A", email: "a@m.com", batch_invitation: @bi) + @user2 = create(:batch_invitation_user, name: "B", email: "b@m.com", batch_invitation: @bi) + end - should "list the users being created" do - get :show, params: { id: @bi.id } - assert_select "table tbody tr", 2 - assert_select "table td", "a@m.com" - assert_select "table td", "b@m.com" - end + should "list the users being created" do + get :show, params: { id: @bi.id } + assert_select "table tbody tr", 2 + assert_select "table td", "a@m.com" + assert_select "table td", "b@m.com" + end - should "include a meta refresh" do - get :show, params: { id: @bi.id } - assert_select 'head meta[http-equiv=refresh][content="3"]' - end + should "include a meta refresh" do + get :show, params: { id: @bi.id } + assert_select 'head meta[http-equiv=refresh][content="3"]' + end - should "show the state of the processing" do - @user1.update_column(:outcome, "failed") - get :show, params: { id: @bi.id } - assert_select "section.gem-c-notice", /In progress/i - assert_select "section.gem-c-notice", /1 of 2 users processed/i - end + should "show the state of the processing" do + @user1.update_column(:outcome, "failed") + get :show, params: { id: @bi.id } + assert_select "section.gem-c-notice", /In progress/i + assert_select "section.gem-c-notice", /1 of 2 users processed/i + end - should "show the outcome for each user" do - @user1.update_column(:outcome, "failed") - get :show, params: { id: @bi.id } - assert_select "td", /Failed/i + should "show the outcome for each user" do + @user1.update_column(:outcome, "failed") + get :show, params: { id: @bi.id } + assert_select "td", /Failed/i + end end context "processing complete" do setup do - @bi.update_column(:outcome, "success") + @bi = create(:batch_invitation, outcome: "success") + create(:batch_invitation_user, name: "A", email: "a@m.com", batch_invitation: @bi) + create(:batch_invitation_user, name: "B", email: "b@m.com", batch_invitation: @bi) get :show, params: { id: @bi.id } end @@ -224,5 +204,23 @@ def users_csv(filename = "users.csv") assert_select "head meta[http-equiv=refresh]", count: 0 end end + + context "batch invitation doesn't have any permissions yet" do + setup do + @bi = create(:batch_invitation) + create(:batch_invitation_user, name: "A", email: "a@m.com", batch_invitation: @bi) + create(:batch_invitation_user, name: "B", email: "b@m.com", batch_invitation: @bi) + get :show, params: { id: @bi.id } + end + + should "explain the problem with the batch invitation" do + assert_select "div.gem-c-error-alert", + /Batch invitation doesn't have any permissions yet./ + end + + should "not include the meta refresh" do + assert_select "head meta[http-equiv=refresh]", count: 0 + end + end end end diff --git a/test/factories/batch_invitation.rb b/test/factories/batch_invitation.rb index 9b00d1dbd..db6ed6a83 100644 --- a/test/factories/batch_invitation.rb +++ b/test/factories/batch_invitation.rb @@ -4,5 +4,20 @@ trait :with_organisation do association :organisation, factory: :organisation end + + trait :in_progress do + outcome { nil } + + has_permissions + end + + trait :has_permissions do + after(:create) do |batch_invitation| + unless batch_invitation.has_permissions? + batch_invitation.supported_permissions << create(:supported_permission) + batch_invitation.save! + end + end + end end end diff --git a/test/helpers/batch_invitations_helper_test.rb b/test/helpers/batch_invitations_helper_test.rb index 32d87109b..9c9b5b132 100644 --- a/test/helpers/batch_invitations_helper_test.rb +++ b/test/helpers/batch_invitations_helper_test.rb @@ -1,6 +1,53 @@ require "test_helper" class BatchInvitationsHelperTest < ActionView::TestCase + context "#batch_invite_status_message" do + should "state number of users processed so far when still in progress" do + batch_invitation = create(:batch_invitation, :in_progress) + create(:batch_invitation_user, outcome: "failed", batch_invitation:) + create(:batch_invitation_user, outcome: "skipped", batch_invitation:) + create(:batch_invitation_user, outcome: "success", batch_invitation:) + create(:batch_invitation_user, outcome: nil, batch_invitation:) + + assert_equal "In progress. 3 of 4 users processed.", + batch_invite_status_message(batch_invitation) + end + + should "state number of users processed when all were successful" do + batch_invitation = create( + :batch_invitation, + :has_permissions, + outcome: "success", + ) + create(:batch_invitation_user, outcome: "skipped", batch_invitation:) + create(:batch_invitation_user, outcome: "success", batch_invitation:) + + assert_equal "2 users processed.", + batch_invite_status_message(batch_invitation) + end + + should "state number of failures if any users have failed to process" do + batch_invitation = create( + :batch_invitation, + :has_permissions, + outcome: "success", + ) + create(:batch_invitation_user, outcome: "failed", batch_invitation:) + create(:batch_invitation_user, outcome: "skipped", batch_invitation:) + create(:batch_invitation_user, outcome: "success", batch_invitation:) + + assert_equal "1 error out of 3 users processed.", + batch_invite_status_message(batch_invitation) + end + + should "explain the problem for a batch invitation that has no permissions" do + batch_invitation = create(:batch_invitation) + + assert_equal "Batch invitation doesn't have any permissions yet.", + batch_invite_status_message(batch_invitation) + end + end + context "#batch_invite_organisation_for_user" do context "when the batch invitation user raises an invalid slug error when asked for organisation_id" do setup do @@ -46,4 +93,18 @@ class BatchInvitationsHelperTest < ActionView::TestCase end end end + + context "#batch_invite_status_link" do + should "link to show the batch when it has permissions" do + batch_invitation = create(:batch_invitation, :has_permissions, outcome: "success") + + assert_includes batch_invite_status_link(batch_invitation) {}, batch_invitation_path(batch_invitation) + end + + should "link to show edit the permissions when it has no permissions" do + batch_invitation = create(:batch_invitation) + + assert_includes batch_invite_status_link(batch_invitation) {}, new_batch_invitation_permissions_path(batch_invitation) + end + end end diff --git a/test/integration/batch_inviting_users_test.rb b/test/integration/batch_inviting_users_test.rb index fea5ca3ea..518d1be28 100644 --- a/test/integration/batch_inviting_users_test.rb +++ b/test/integration/batch_inviting_users_test.rb @@ -88,6 +88,8 @@ class BatchInvitingUsersTest < ActionDispatch::IntegrationTest visit new_batch_invitation_path path = Rails.root.join("test/fixtures/users.csv") attach_file("Choose a CSV file of users with names and email addresses", path) + click_button "Manage permissions for new users" + uncheck "Has access to #{support_app.name}?" check "Has access to #{@application.name}?" unselect "reader", from: "Permissions for #{@application.name}" @@ -124,11 +126,12 @@ def perform_batch_invite_with_user(user, application, organisation:, fixture_fil visit new_batch_invitation_path path = Rails.root.join("test/fixtures", fixture_file) attach_file("Choose a CSV file of users with names and email addresses", path) - check "Has access to #{application.name}?" if organisation select organisation.name, from: "Organisation" end + click_button "Manage permissions for new users" + check "Has access to #{application.name}?" click_button "Create users and send emails" assert_response_contains("Creating a batch of users") diff --git a/test/models/batch_invitation_test.rb b/test/models/batch_invitation_test.rb index 08a4dfa2f..f211d9f20 100644 --- a/test/models/batch_invitation_test.rb +++ b/test/models/batch_invitation_test.rb @@ -26,6 +26,73 @@ class BatchInvitationTest < ActiveSupport::TestCase @bi.save! end + context "#has_permissions?" do + should "be false when BatchInvitation has no batch_invitation_application_permissions" do + invitation = create(:batch_invitation) + + assert_not invitation.has_permissions? + end + + should "be true when BatchInvitation has any batch_invitation_application_permissions at all" do + invitation = create(:batch_invitation, supported_permissions: [@app.signin_permission]) + + assert invitation.has_permissions? + end + end + + context "#in_progress?" do + should "be false when BatchInvitation has an outcome" do + @bi.update_column(:outcome, "success") + + assert_not @bi.in_progress? + end + + should "be true when BatchInvitation does not have an outcome yet" do + @bi.update_column(:outcome, nil) + + assert @bi.in_progress? + end + + should "be false when BatchInvitation does not have any permissions yet" do + invitation = create(:batch_invitation, outcome: nil) + + assert_not invitation.in_progress? + end + end + + context "#all_successful?" do + should "be false when at least one BatchInvitationUser has failed" do + @bi.update_column(:outcome, "success") + @user_a.update_column(:outcome, "failed") + + assert_not @bi.all_successful? + end + + should "be true when no BatchInvitationUsers have failed" do + @bi.update_column(:outcome, "success") + @user_a.update_column(:outcome, "success") + @user_b.update_column(:outcome, "success") + + assert @bi.all_successful? + end + + should "be true even if outcome is 'fail' as long as no BatchInvitationUsers have failed" do + @bi.update_column(:outcome, "fail") + @user_a.update_column(:outcome, "success") + @user_b.update_column(:outcome, "success") + + assert @bi.all_successful? + end + + should "be false when BatchInvitation is still in progress" do + @bi.update_column(:outcome, nil) + @user_a.update_column(:outcome, "success") + @user_b.update_column(:outcome, "success") + + assert_not @bi.all_successful? + end + end + context "perform" do should "create the users and assign them permissions" do @bi.reload.perform