diff --git a/ee/app/controllers/groups/ldaps_controller.rb b/ee/app/controllers/groups/ldaps_controller.rb index e11c33d33cd13f629ec020f3bebf78590e7af4ca..326379fbc1f82e5364aa0d1a2d896f082cbfaf6d 100644 --- a/ee/app/controllers/groups/ldaps_controller.rb +++ b/ee/app/controllers/groups/ldaps_controller.rb @@ -6,11 +6,19 @@ class Groups::LdapsController < Groups::ApplicationController before_action :check_enabled_extras! def sync + # A group can transition to pending if it is in the ready or failed + # state. If it is in the started or pending state, then that means + # it is already running. If the group doesn't validate, then it's + # likely the group will never transition out of its current state, + # so we should tell the group owner. if @group.pending_ldap_sync LdapGroupSyncWorker.perform_async(@group.id) message = 'The group sync has been scheduled' - else + elsif @group.valid? message = 'The group sync is already scheduled' + else + message = "This group is in an invalid state: #{@group.errors.full_messages.join(', ')}" + return redirect_to group_group_members_path(@group), alert: message end redirect_to group_group_members_path(@group), notice: message diff --git a/ee/changelogs/unreleased/sh-check-group-ldap-sync-validity.yml b/ee/changelogs/unreleased/sh-check-group-ldap-sync-validity.yml new file mode 100644 index 0000000000000000000000000000000000000000..add6e62b3693e4a620f4464d4a07bb260badad4d --- /dev/null +++ b/ee/changelogs/unreleased/sh-check-group-ldap-sync-validity.yml @@ -0,0 +1,5 @@ +--- +title: Notify owner that group is invalid when LDAP "Sync now" fails +merge_request: 10509 +author: +type: fixed diff --git a/ee/spec/controllers/groups/ldaps_controller_spec.rb b/ee/spec/controllers/groups/ldaps_controller_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..5f7a6b16cce5b8c874684fd60f2543f9328e6751 --- /dev/null +++ b/ee/spec/controllers/groups/ldaps_controller_spec.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Groups::LdapsController do + include LdapHelpers + + let(:group) { create(:group) } + let(:user) { create(:user) } + + before do + stub_ldap_setting(enabled: true) + group.add_owner(user) + + sign_in(user) + end + + describe 'POST #sync' do + subject do + Sidekiq::Testing.fake! do + post :sync, params: { group_id: group.to_param } + end + end + + it 'transitions to the pending state' do + subject + + expect(group.reload.ldap_sync_pending?).to be_truthy + + expect(controller).to redirect_to(group_group_members_path(group)) + end + + it 'notifies user that the group is already pending' do + group.update_columns(ldap_sync_status: 'pending') + + subject + + expect(flash[:notice]).to eq('The group sync is already scheduled') + expect(controller).to redirect_to(group_group_members_path(group)) + end + + it 'returns an error if the group does not validate' do + group.update_columns(repository_size_limit: -1) + + expect(group).not_to be_valid + + subject + + expect(flash[:alert]).to include('This group is in an invalid state') + expect(controller).to redirect_to(group_group_members_path(group)) + end + end +end