2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe API::GroupVariables do
|
2021-03-05 16:19:46 +05:30
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:variable) { create(:ci_group_variable, group: group) }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-03-05 16:19:46 +05:30
|
|
|
let(:access_level) {}
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_user(user, access_level) if access_level
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-03-05 16:19:46 +05:30
|
|
|
describe 'GET /groups/:id/variables' do
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'authorized user with proper permissions' do
|
2021-03-05 16:19:46 +05:30
|
|
|
let(:access_level) { :owner }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
it 'returns group variables' do
|
|
|
|
get api("/groups/#{group.id}/variables", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(json_response).to be_a(Array)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized user with invalid permissions' do
|
2021-03-05 16:19:46 +05:30
|
|
|
let(:access_level) { :maintainer }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'does not return group variables' do
|
|
|
|
get api("/groups/#{group.id}/variables", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
|
|
|
it 'does not return group variables' do
|
|
|
|
get api("/groups/#{group.id}/variables")
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /groups/:id/variables/:key' do
|
|
|
|
context 'authorized user with proper permissions' do
|
2021-03-05 16:19:46 +05:30
|
|
|
let(:access_level) { :owner }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
it 'returns group variable details' do
|
|
|
|
get api("/groups/#{group.id}/variables/#{variable.key}", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(json_response['value']).to eq(variable.value)
|
|
|
|
expect(json_response['protected']).to eq(variable.protected?)
|
2019-07-31 22:56:46 +05:30
|
|
|
expect(json_response['variable_type']).to eq(variable.variable_type)
|
2021-04-29 21:17:54 +05:30
|
|
|
expect(json_response['environment_scope']).to eq(variable.environment_scope)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with 404 Not Found if requesting non-existing variable' do
|
|
|
|
get api("/groups/#{group.id}/variables/non_existing_variable", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized user with invalid permissions' do
|
2021-03-05 16:19:46 +05:30
|
|
|
let(:access_level) { :maintainer }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'does not return group variable details' do
|
|
|
|
get api("/groups/#{group.id}/variables/#{variable.key}", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
|
|
|
it 'does not return group variable details' do
|
|
|
|
get api("/groups/#{group.id}/variables/#{variable.key}")
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /groups/:id/variables' do
|
|
|
|
context 'authorized user with proper permissions' do
|
2021-03-05 16:19:46 +05:30
|
|
|
let(:access_level) { :owner }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
it 'creates variable' do
|
|
|
|
expect do
|
2020-04-08 14:13:33 +05:30
|
|
|
post api("/groups/#{group.id}/variables", user), params: { key: 'TEST_VARIABLE_2', value: 'PROTECTED_VALUE_2', protected: true, masked: true }
|
2018-03-17 18:26:18 +05:30
|
|
|
end.to change {group.variables.count}.by(1)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(json_response['key']).to eq('TEST_VARIABLE_2')
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(json_response['value']).to eq('PROTECTED_VALUE_2')
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(json_response['protected']).to be_truthy
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(json_response['masked']).to be_truthy
|
2019-07-31 22:56:46 +05:30
|
|
|
expect(json_response['variable_type']).to eq('env_var')
|
2021-04-29 21:17:54 +05:30
|
|
|
expect(json_response['environment_scope']).to eq('*')
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates variable with optional attributes' do
|
|
|
|
expect do
|
2019-07-31 22:56:46 +05:30
|
|
|
post api("/groups/#{group.id}/variables", user), params: { variable_type: 'file', key: 'TEST_VARIABLE_2', value: 'VALUE_2' }
|
2018-03-17 18:26:18 +05:30
|
|
|
end.to change {group.variables.count}.by(1)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(json_response['key']).to eq('TEST_VARIABLE_2')
|
|
|
|
expect(json_response['value']).to eq('VALUE_2')
|
|
|
|
expect(json_response['protected']).to be_falsey
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(json_response['masked']).to be_falsey
|
2019-07-31 22:56:46 +05:30
|
|
|
expect(json_response['variable_type']).to eq('file')
|
2021-04-29 21:17:54 +05:30
|
|
|
expect(json_response['environment_scope']).to eq('*')
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow to duplicate variable key' do
|
|
|
|
expect do
|
2019-02-15 15:39:39 +05:30
|
|
|
post api("/groups/#{group.id}/variables", user), params: { key: variable.key, value: 'VALUE_2' }
|
2018-03-17 18:26:18 +05:30
|
|
|
end.to change {group.variables.count}.by(0)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized user with invalid permissions' do
|
2021-03-05 16:19:46 +05:30
|
|
|
let(:access_level) { :maintainer }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'does not create variable' do
|
|
|
|
post api("/groups/#{group.id}/variables", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
|
|
|
it 'does not create variable' do
|
|
|
|
post api("/groups/#{group.id}/variables")
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PUT /groups/:id/variables/:key' do
|
|
|
|
context 'authorized user with proper permissions' do
|
2021-03-05 16:19:46 +05:30
|
|
|
let(:access_level) { :owner }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
it 'updates variable data' do
|
2018-03-17 18:26:18 +05:30
|
|
|
initial_variable = group.variables.reload.first
|
2017-09-10 17:25:29 +05:30
|
|
|
value_before = initial_variable.value
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
put api("/groups/#{group.id}/variables/#{variable.key}", user), params: { variable_type: 'file', value: 'VALUE_1_UP', protected: true, masked: true }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
updated_variable = group.variables.reload.first
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(value_before).to eq(variable.value)
|
|
|
|
expect(updated_variable.value).to eq('VALUE_1_UP')
|
|
|
|
expect(updated_variable).to be_protected
|
2019-07-31 22:56:46 +05:30
|
|
|
expect(json_response['variable_type']).to eq('file')
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(json_response['masked']).to be_truthy
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with 404 Not Found if requesting non-existing variable' do
|
|
|
|
put api("/groups/#{group.id}/variables/non_existing_variable", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
it 'responds with 400 if the update fails' do
|
|
|
|
put api("/groups/#{group.id}/variables/#{variable.key}", user), params: { value: 'shrt', masked: true }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
expect(variable.reload.masked).to eq(false)
|
|
|
|
expect(json_response['message']).to eq('value' => ['is invalid'])
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized user with invalid permissions' do
|
2021-03-05 16:19:46 +05:30
|
|
|
let(:access_level) { :maintainer }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'does not update variable' do
|
|
|
|
put api("/groups/#{group.id}/variables/#{variable.key}", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
|
|
|
it 'does not update variable' do
|
|
|
|
put api("/groups/#{group.id}/variables/#{variable.key}")
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /groups/:id/variables/:key' do
|
|
|
|
context 'authorized user with proper permissions' do
|
2021-03-05 16:19:46 +05:30
|
|
|
let(:access_level) { :owner }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
it 'deletes variable' do
|
|
|
|
expect do
|
|
|
|
delete api("/groups/#{group.id}/variables/#{variable.key}", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2018-03-17 18:26:18 +05:30
|
|
|
end.to change {group.variables.count}.by(-1)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with 404 Not Found if requesting non-existing variable' do
|
|
|
|
delete api("/groups/#{group.id}/variables/non_existing_variable", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '412 response' do
|
|
|
|
let(:request) { api("/groups/#{group.id}/variables/#{variable.key}", user) }
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized user with invalid permissions' do
|
2021-03-05 16:19:46 +05:30
|
|
|
let(:access_level) { :maintainer }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'does not delete variable' do
|
|
|
|
delete api("/groups/#{group.id}/variables/#{variable.key}", user)
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
|
|
|
it 'does not delete variable' do
|
|
|
|
delete api("/groups/#{group.id}/variables/#{variable.key}")
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|