Commit 4afb42c6 authored by Małgorzata Ksionek's avatar Małgorzata Ksionek

Add specs for new groups controller behaviour

parent de135860
......@@ -16,7 +16,7 @@ class GroupsController < Groups::ApplicationController
prepend_before_action(only: [:show, :issues]) { authenticate_sessionless_user!(:rss) }
prepend_before_action(only: [:issues_calendar]) { authenticate_sessionless_user!(:ics) }
prepend_before_action :ensure_export_enabled, only: [:export, :download_export]
prepend_before_action :check_captcha, only: :create
prepend_before_action :check_captcha, only: :create, if: -> { recaptcha_on_group_creation_enabled? }
before_action :authenticate_user!, only: [:new, :create]
before_action :group, except: [:index, :new, :create]
......@@ -24,7 +24,7 @@ class GroupsController < Groups::ApplicationController
# Authorize
before_action :authorize_admin_group!, only: [:edit, :update, :destroy, :projects, :transfer, :export, :download_export]
before_action :authorize_create_group!, only: [:new]
before_action :load_recaptcha, only: [:new]
before_action :load_recaptcha, only: [:new], if: -> { recaptcha_on_group_creation_enabled? }
before_action :group_projects, only: [:projects, :activity, :issues, :merge_requests]
before_action :event_filter, only: [:activity]
......@@ -327,8 +327,8 @@ class GroupsController < Groups::ApplicationController
end
def check_captcha
return unless captcha_enabled? && load_recaptcha
return if group_params[:parent_id].present? # Only require for top-level groups
return unless captcha_enabled? && load_recaptcha
return if verify_recaptcha
......@@ -363,7 +363,11 @@ class GroupsController < Groups::ApplicationController
end
def captcha_required?
!params[:parent_id]
recaptcha_on_group_creation_enabled? && !params[:parent_id]
end
def recaptcha_on_group_creation_enabled?
Feature.enabled?(:recaptcha_on_group_creation, type: :ops)
end
end
......
---
name: recaptcha_on_group_creation
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/56707
rollout_issue_url:
milestone: '13.10'
type: ops
group: group::access
default_enabled: false
......@@ -312,6 +312,38 @@ RSpec.describe GroupsController, factory_default: :keep do
end
end
end
context 'when creating a group with captcha protection' do
before do
sign_in(user)
stub_application_setting(recaptcha_enabled: true)
end
after do
# Avoid test ordering issue and ensure `verify_recaptcha` returns true
unless Recaptcha.configuration.skip_verify_env.include?('test')
Recaptcha.configuration.skip_verify_env << 'test'
end
end
it 'displays an error when the reCAPTCHA is not solved' do
allow_any_instance_of(described_class).to receive(:verify_recaptcha).and_return(false)
post :create, params: { group: { name: 'new_group', path: "new_group" } }
expect(response).to render_template(:new)
expect(flash[:alert]).to eq(_('There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.'))
end
it 'allows creating a group when the reCAPTCHA is solved' do
expect do
post :create, params: { group: { name: 'new_group', path: "new_group" } }
end.to change { Group.count }.by(1)
expect(response).to have_gitlab_http_status(:found)
end
end
end
describe 'GET #index' do
......
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