2017-07-18 08:40:35 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe API::GroupVariables do
|
|
|
|
let(:group) { create(:group) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
describe 'GET /groups/:id/variables' do
|
|
|
|
let!(:variable) { create(:ci_group_variable, group: group) }
|
|
|
|
|
|
|
|
context 'authorized user with proper permissions' do
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
group.add_maintainer(user)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns group variables' do
|
|
|
|
get api("/groups/#{group.id}/variables", user)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-07-18 08:40:35 -04:00
|
|
|
expect(json_response).to be_a(Array)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized user with invalid permissions' do
|
|
|
|
it 'does not return group variables' do
|
|
|
|
get api("/groups/#{group.id}/variables", user)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(403)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
|
|
|
it 'does not return group variables' do
|
|
|
|
get api("/groups/#{group.id}/variables")
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(401)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /groups/:id/variables/:key' do
|
|
|
|
let!(:variable) { create(:ci_group_variable, group: group) }
|
|
|
|
|
|
|
|
context 'authorized user with proper permissions' do
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
group.add_maintainer(user)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns group variable details' do
|
|
|
|
get api("/groups/#{group.id}/variables/#{variable.key}", user)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-07-18 08:40:35 -04:00
|
|
|
expect(json_response['value']).to eq(variable.value)
|
|
|
|
expect(json_response['protected']).to eq(variable.protected?)
|
2019-05-06 09:11:42 -04:00
|
|
|
expect(json_response['variable_type']).to eq(variable.variable_type)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with 404 Not Found if requesting non-existing variable' do
|
|
|
|
get api("/groups/#{group.id}/variables/non_existing_variable", user)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized user with invalid permissions' do
|
|
|
|
it 'does not return group variable details' do
|
|
|
|
get api("/groups/#{group.id}/variables/#{variable.key}", user)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(403)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
|
|
|
it 'does not return group variable details' do
|
|
|
|
get api("/groups/#{group.id}/variables/#{variable.key}")
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(401)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /groups/:id/variables' do
|
|
|
|
context 'authorized user with proper permissions' do
|
|
|
|
let!(:variable) { create(:ci_group_variable, group: group) }
|
|
|
|
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
group.add_maintainer(user)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates variable' do
|
|
|
|
expect do
|
2019-02-26 19:22:51 -05:00
|
|
|
post api("/groups/#{group.id}/variables", user), params: { key: 'TEST_VARIABLE_2', value: 'PROTECTED_VALUE_2', protected: true }
|
2017-08-09 05:52:22 -04:00
|
|
|
end.to change {group.variables.count}.by(1)
|
2017-07-18 08:40:35 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-07-18 08:40:35 -04:00
|
|
|
expect(json_response['key']).to eq('TEST_VARIABLE_2')
|
2019-02-26 19:22:51 -05:00
|
|
|
expect(json_response['value']).to eq('PROTECTED_VALUE_2')
|
2017-07-18 08:40:35 -04:00
|
|
|
expect(json_response['protected']).to be_truthy
|
2019-05-06 09:11:42 -04:00
|
|
|
expect(json_response['variable_type']).to eq('env_var')
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates variable with optional attributes' do
|
|
|
|
expect do
|
2019-05-06 09:11:42 -04:00
|
|
|
post api("/groups/#{group.id}/variables", user), params: { variable_type: 'file', key: 'TEST_VARIABLE_2', value: 'VALUE_2' }
|
2017-08-09 05:52:22 -04:00
|
|
|
end.to change {group.variables.count}.by(1)
|
2017-07-18 08:40:35 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-07-18 08:40:35 -04:00
|
|
|
expect(json_response['key']).to eq('TEST_VARIABLE_2')
|
|
|
|
expect(json_response['value']).to eq('VALUE_2')
|
|
|
|
expect(json_response['protected']).to be_falsey
|
2019-05-06 09:11:42 -04:00
|
|
|
expect(json_response['variable_type']).to eq('file')
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow to duplicate variable key' do
|
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/groups/#{group.id}/variables", user), params: { key: variable.key, value: 'VALUE_2' }
|
2017-08-09 05:52:22 -04:00
|
|
|
end.to change {group.variables.count}.by(0)
|
2017-07-18 08:40:35 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(400)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized user with invalid permissions' do
|
|
|
|
it 'does not create variable' do
|
|
|
|
post api("/groups/#{group.id}/variables", user)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(403)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
|
|
|
it 'does not create variable' do
|
|
|
|
post api("/groups/#{group.id}/variables")
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(401)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PUT /groups/:id/variables/:key' do
|
|
|
|
let!(:variable) { create(:ci_group_variable, group: group) }
|
|
|
|
|
|
|
|
context 'authorized user with proper permissions' do
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
group.add_maintainer(user)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates variable data' do
|
2018-02-05 12:42:58 -05:00
|
|
|
initial_variable = group.variables.reload.first
|
2017-07-18 08:40:35 -04:00
|
|
|
value_before = initial_variable.value
|
|
|
|
|
2019-05-06 09:11:42 -04:00
|
|
|
put api("/groups/#{group.id}/variables/#{variable.key}", user), params: { variable_type: 'file', value: 'VALUE_1_UP', protected: true }
|
2017-07-18 08:40:35 -04:00
|
|
|
|
2018-02-05 12:42:58 -05:00
|
|
|
updated_variable = group.variables.reload.first
|
2017-07-18 08:40:35 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-07-18 08:40:35 -04:00
|
|
|
expect(value_before).to eq(variable.value)
|
|
|
|
expect(updated_variable.value).to eq('VALUE_1_UP')
|
|
|
|
expect(updated_variable).to be_protected
|
2019-05-06 09:11:42 -04:00
|
|
|
expect(json_response['variable_type']).to eq('file')
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with 404 Not Found if requesting non-existing variable' do
|
|
|
|
put api("/groups/#{group.id}/variables/non_existing_variable", user)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized user with invalid permissions' do
|
|
|
|
it 'does not update variable' do
|
|
|
|
put api("/groups/#{group.id}/variables/#{variable.key}", user)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(403)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
|
|
|
it 'does not update variable' do
|
|
|
|
put api("/groups/#{group.id}/variables/#{variable.key}")
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(401)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /groups/:id/variables/:key' do
|
|
|
|
let!(:variable) { create(:ci_group_variable, group: group) }
|
|
|
|
|
|
|
|
context 'authorized user with proper permissions' do
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
group.add_maintainer(user)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes variable' do
|
|
|
|
expect do
|
|
|
|
delete api("/groups/#{group.id}/variables/#{variable.key}", user)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(204)
|
2017-08-09 05:52:22 -04:00
|
|
|
end.to change {group.variables.count}.by(-1)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with 404 Not Found if requesting non-existing variable' do
|
|
|
|
delete api("/groups/#{group.id}/variables/non_existing_variable", user)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
2017-08-24 12:03:39 -04:00
|
|
|
|
|
|
|
it_behaves_like '412 response' do
|
|
|
|
let(:request) { api("/groups/#{group.id}/variables/#{variable.key}", user) }
|
|
|
|
end
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized user with invalid permissions' do
|
|
|
|
it 'does not delete variable' do
|
|
|
|
delete api("/groups/#{group.id}/variables/#{variable.key}", user)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(403)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
|
|
|
it 'does not delete variable' do
|
|
|
|
delete api("/groups/#{group.id}/variables/#{variable.key}")
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(401)
|
2017-07-18 08:40:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|