Commit 937567b7 authored by Tomasz Maczukin's avatar Tomasz Maczukin

Add create feature to variables API

parent c5177dd5
...@@ -41,6 +41,24 @@ module API ...@@ -41,6 +41,24 @@ module API
present variables.first, with: Entities::Variable present variables.first, with: Entities::Variable
end end
# Create a new variable in project
#
# Parameters:
# id (required) - The ID of a project
# key (required) - The key of variable being created
# value (required) - The value of variable being created
# Example Request:
# POST /projects/:id/variables
post ':id/variables' do
required_attributes! [:key, :value]
variable = user_project.variables.create(key: params[:key], value: params[:value])
return render_validation_error!(variable) unless variable.valid?
variable.save!
present variable, with: Entities::Variable
end
# Update existing variable of a project # Update existing variable of a project
# #
# Parameters: # Parameters:
...@@ -75,6 +93,8 @@ module API ...@@ -75,6 +93,8 @@ module API
return not_found!('Variable') unless variable return not_found!('Variable') unless variable
variable.destroy variable.destroy
present variable, with: Entities::Variable
end end
end end
end end
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
FactoryGirl.define do FactoryGirl.define do
factory :ci_variable, class: Ci::Variable do factory :ci_variable, class: Ci::Variable do
id 1 id 10
key 'TEST_VARIABLE_1' key 'TEST_VARIABLE_1'
value 'VALUE_1' value 'VALUE_1'
......
...@@ -79,6 +79,44 @@ describe API::API, api: true do ...@@ -79,6 +79,44 @@ describe API::API, api: true do
end end
end end
describe 'POST /projects/:id/variables' do
context 'authorized user with proper permissions' do
it 'should create variable' do
expect do
post api("/projects/#{project.id}/variables", user), key: 'TEST_VARIABLE_2', value: 'VALUE_2'
end.to change{project.variables.count}.by(1)
expect(response.status).to eq(201)
expect(json_response['key']).to eq('TEST_VARIABLE_2')
expect(json_response['value']).to eq('VALUE_2')
end
it 'should not allow to duplicate variable key' do
expect do
post api("/projects/#{project.id}/variables", user), key: 'TEST_VARIABLE_1', value: 'VALUE_2'
end.to change{project.variables.count}.by(0)
expect(response.status).to eq(400)
end
end
context 'authorized user with invalid permissions' do
it 'should not create variable' do
post api("/projects/#{project.id}/variables", user2)
expect(response.status).to eq(403)
end
end
context 'unauthorized user' do
it 'should not create variable' do
post api("/projects/#{project.id}/variables")
expect(response.status).to eq(401)
end
end
end
describe 'PUT /projects/:id/variables/:variable_id' do describe 'PUT /projects/:id/variables/:variable_id' do
context 'authorized user with proper permissions' do context 'authorized user with proper permissions' do
it 'should update variable data' do it 'should update variable data' 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