summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/groups/variables_controller.rb16
-rw-r--r--app/models/group.rb2
-rw-r--r--config/routes/group.rb6
-rw-r--r--spec/controllers/groups/variables_controller_spec.rb79
4 files changed, 96 insertions, 7 deletions
diff --git a/app/controllers/groups/variables_controller.rb b/app/controllers/groups/variables_controller.rb
index 10038ff3ad9..3c303b64b65 100644
--- a/app/controllers/groups/variables_controller.rb
+++ b/app/controllers/groups/variables_controller.rb
@@ -31,6 +31,16 @@ module Groups
end
end
+ def save_multiple
+ respond_to do |format|
+ format.json do
+ return head :ok if @group.update(variables_params)
+
+ head :bad_request
+ end
+ end
+ end
+
def destroy
if variable.destroy
redirect_to group_settings_ci_cd_path(group),
@@ -49,8 +59,12 @@ module Groups
params.require(:variable).permit(*variable_params_attributes)
end
+ def variables_params
+ params.permit(variables_attributes: [*variable_params_attributes])
+ end
+
def variable_params_attributes
- %i[key value protected]
+ %i[id key value protected _destroy]
end
def variable
diff --git a/app/models/group.rb b/app/models/group.rb
index 5b7f1b38612..29df4144d03 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -31,6 +31,8 @@ class Group < Namespace
has_many :uploads, as: :model, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
+ accepts_nested_attributes_for :variables, allow_destroy: true
+
validate :visibility_level_allowed_by_projects
validate :visibility_level_allowed_by_sub_groups
validate :visibility_level_allowed_by_parent
diff --git a/config/routes/group.rb b/config/routes/group.rb
index 24c76bc55ab..cdf2647415d 100644
--- a/config/routes/group.rb
+++ b/config/routes/group.rb
@@ -27,7 +27,11 @@ constraints(GroupUrlConstrainer.new) do
resource :ci_cd, only: [:show], controller: 'ci_cd'
end
- resources :variables, only: [:index, :show, :update, :create, :destroy]
+ resources :variables, only: [:index, :show, :update, :create, :destroy] do
+ collection do
+ post :save_multiple
+ end
+ end
resources :children, only: [:index]
diff --git a/spec/controllers/groups/variables_controller_spec.rb b/spec/controllers/groups/variables_controller_spec.rb
index 8ea98cd9e8f..294e712f45d 100644
--- a/spec/controllers/groups/variables_controller_spec.rb
+++ b/spec/controllers/groups/variables_controller_spec.rb
@@ -29,13 +29,9 @@ describe Groups::VariablesController do
end
describe 'POST #update' do
- let(:variable) { create(:ci_group_variable) }
+ let!(:variable) { create(:ci_group_variable, group: group) }
context 'updating a variable with valid characters' do
- before do
- group.variables << variable
- end
-
it 'shows a success flash message' do
post :update, group_id: group,
id: variable.id, variable: { key: variable.key, value: 'two' }
@@ -53,4 +49,77 @@ describe Groups::VariablesController do
end
end
end
+
+ describe 'POST #save_multiple' do
+ let!(:variable) { create(:ci_group_variable, group: group) }
+
+ context 'with invalid new variable parameters' do
+ subject do
+ post :save_multiple,
+ group_id: group,
+ variables_attributes: [{ id: variable.id, key: variable.key, value: 'other_value' },
+ { key: '..?', value: 'dummy_value' }],
+ format: :json
+ end
+
+ it 'does not update the existing variable' do
+ expect { subject }.not_to change { variable.reload.value }
+ end
+
+ it 'does not create the new variable' do
+ expect { subject }.not_to change { group.variables.count }
+ end
+
+ it 'returns a bad request response' do
+ subject
+
+ expect(response).to have_gitlab_http_status(:bad_request)
+ end
+ end
+
+ context 'with valid new variable parameters' do
+ subject do
+ post :save_multiple,
+ group_id: group,
+ variables_attributes: [{ id: variable.id, key: variable.key, value: 'other_value' },
+ { key: 'new_key', value: 'dummy_value' }],
+ format: :json
+ end
+
+ it 'updates the existing variable' do
+ expect { subject }.to change { variable.reload.value }.to('other_value')
+ end
+
+ it 'creates the new variable' do
+ expect { subject }.to change { group.variables.count }.by(1)
+ end
+
+ it 'returns a successful response' do
+ subject
+
+ expect(response).to have_gitlab_http_status(:ok)
+ end
+ end
+
+ context 'with a deleted variable' do
+ subject do
+ post :save_multiple,
+ group_id: group,
+ variables_attributes: [{ id: variable.id, key: variable.key,
+ value: variable.value, _destroy: 'true' }],
+ format: :json
+ end
+
+ it 'destroys the variable' do
+ expect { subject }.to change { group.variables.count }.by(-1)
+ expect { variable.reload }.to raise_error ActiveRecord::RecordNotFound
+ end
+
+ it 'returns a successful response' do
+ subject
+
+ expect(response).to have_gitlab_http_status(:ok)
+ end
+ end
+ end
end