Commit 3e3ea895 authored by Drew Blessing's avatar Drew Blessing Committed by Drew Blessing

Controller and spec cleanup

parent 1b6711bc
...@@ -16,7 +16,7 @@ class GroupsController < Groups::ApplicationController ...@@ -16,7 +16,7 @@ class GroupsController < Groups::ApplicationController
prepend_before_action(only: [:show, :issues]) { authenticate_sessionless_user!(:rss) } prepend_before_action(only: [:show, :issues]) { authenticate_sessionless_user!(:rss) }
prepend_before_action(only: [:issues_calendar]) { authenticate_sessionless_user!(:ics) } prepend_before_action(only: [:issues_calendar]) { authenticate_sessionless_user!(:ics) }
prepend_before_action :ensure_export_enabled, only: [:export, :download_export] prepend_before_action :ensure_export_enabled, only: [:export, :download_export]
prepend_before_action :check_captcha, only: :create, if: -> { recaptcha_on_group_creation_enabled? } prepend_before_action :check_captcha, only: :create, if: -> { captcha_enabled? }
before_action :authenticate_user!, only: [:new, :create] before_action :authenticate_user!, only: [:new, :create]
before_action :group, except: [:index, :new, :create] before_action :group, except: [:index, :new, :create]
...@@ -24,7 +24,7 @@ class GroupsController < Groups::ApplicationController ...@@ -24,7 +24,7 @@ class GroupsController < Groups::ApplicationController
# Authorize # Authorize
before_action :authorize_admin_group!, only: [:edit, :update, :destroy, :projects, :transfer, :export, :download_export] before_action :authorize_admin_group!, only: [:edit, :update, :destroy, :projects, :transfer, :export, :download_export]
before_action :authorize_create_group!, only: [:new] before_action :authorize_create_group!, only: [:new]
before_action :load_recaptcha, only: [:new], if: -> { recaptcha_on_group_creation_enabled? } before_action :load_recaptcha, only: [:new, :create], if: -> { captcha_required? }
before_action :group_projects, only: [:projects, :activity, :issues, :merge_requests] before_action :group_projects, only: [:projects, :activity, :issues, :merge_requests]
before_action :event_filter, only: [:activity] before_action :event_filter, only: [:activity]
...@@ -41,7 +41,7 @@ class GroupsController < Groups::ApplicationController ...@@ -41,7 +41,7 @@ class GroupsController < Groups::ApplicationController
before_action :export_rate_limit, only: [:export, :download_export] before_action :export_rate_limit, only: [:export, :download_export]
helper_method :captcha_enabled?, :captcha_required? helper_method :captcha_required?
skip_cross_project_access_check :index, :new, :create, :edit, :update, skip_cross_project_access_check :index, :new, :create, :edit, :update,
:destroy, :projects :destroy, :projects
...@@ -322,13 +322,14 @@ class GroupsController < Groups::ApplicationController ...@@ -322,13 +322,14 @@ class GroupsController < Groups::ApplicationController
render_404 unless Feature.enabled?(:group_import_export, @group, default_enabled: true) render_404 unless Feature.enabled?(:group_import_export, @group, default_enabled: true)
end end
private
def load_recaptcha def load_recaptcha
Gitlab::Recaptcha.load_configurations! Gitlab::Recaptcha.load_configurations!
end end
def check_captcha def check_captcha
return if group_params[:parent_id].present? # Only require for top-level groups return if group_params[:parent_id].present? # Only require for top-level groups
return unless captcha_enabled? && load_recaptcha
return if verify_recaptcha return if verify_recaptcha
...@@ -338,8 +339,6 @@ class GroupsController < Groups::ApplicationController ...@@ -338,8 +339,6 @@ class GroupsController < Groups::ApplicationController
render action: 'new' render action: 'new'
end end
private
def successful_creation_hooks; end def successful_creation_hooks; end
def groups def groups
...@@ -359,15 +358,11 @@ class GroupsController < Groups::ApplicationController ...@@ -359,15 +358,11 @@ class GroupsController < Groups::ApplicationController
end end
def captcha_enabled? def captcha_enabled?
Gitlab::Recaptcha.enabled? Gitlab::Recaptcha.enabled? && Feature.enabled?(:recaptcha_on_group_creation, type: :ops)
end end
def captcha_required? def captcha_required?
recaptcha_on_group_creation_enabled? && !params[:parent_id] captcha_enabled? && !params[:parent_id]
end
def recaptcha_on_group_creation_enabled?
Feature.enabled?(:recaptcha_on_group_creation, type: :ops)
end end
end end
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
.col-sm-4 .col-sm-4
= render_if_exists 'shared/groups/invite_members' = render_if_exists 'shared/groups/invite_members'
- if captcha_enabled? && captcha_required? - if captcha_required?
.row.recaptcha .row.recaptcha
.col-sm-4 .col-sm-4
= recaptcha_tags = recaptcha_tags
......
...@@ -328,7 +328,7 @@ RSpec.describe GroupsController, factory_default: :keep do ...@@ -328,7 +328,7 @@ RSpec.describe GroupsController, factory_default: :keep do
end end
it 'displays an error when the reCAPTCHA is not solved' do it 'displays an error when the reCAPTCHA is not solved' do
allow_any_instance_of(described_class).to receive(:verify_recaptcha).and_return(false) allow(controller).to receive(:verify_recaptcha).and_return(false)
post :create, params: { group: { name: 'new_group', path: "new_group" } } post :create, params: { group: { name: 'new_group', path: "new_group" } }
...@@ -344,13 +344,23 @@ RSpec.describe GroupsController, factory_default: :keep do ...@@ -344,13 +344,23 @@ RSpec.describe GroupsController, factory_default: :keep do
expect(response).to have_gitlab_http_status(:found) expect(response).to have_gitlab_http_status(:found)
end end
it 'allows creating a sub-group without checking the captcha' do
expect(controller).not_to receive(:verify_recaptcha)
expect do
post :create, params: { group: { name: 'new_group', path: "new_group", parent_id: group.id } }
end.to change { Group.count }.by(1)
expect(response).to have_gitlab_http_status(:found)
end
context 'with feature flag switched off' do context 'with feature flag switched off' do
before do before do
stub_feature_flags(recaptcha_on_group_creation: false) stub_feature_flags(recaptcha_on_group_creation: false)
end end
it 'allows creating a group without the reCAPTCHA' do it 'allows creating a group without the reCAPTCHA' do
expect(described_class).not_to receive(:verify_recaptcha) expect(controller).not_to receive(:verify_recaptcha)
expect do expect do
post :create, params: { group: { name: 'new_group', path: "new_group" } } post :create, params: { group: { name: 'new_group', path: "new_group" } }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment