summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLin Jen-Shin <godfat@godfat.org>2017-06-27 15:20:11 +0800
committerLin Jen-Shin <godfat@godfat.org>2017-06-27 15:20:11 +0800
commit01960fce252433986d1bcd19c4fee0380921dfdb (patch)
tree0f7c12b0fa938f4217d4e899f9d23068a12ed72e
parentad3843aee14abb36e0f4a7ede7210f26ed4879c9 (diff)
downloadgitlab-ce-01960fce252433986d1bcd19c4fee0380921dfdb.tar.gz
Fix optional args for POST :id/variables
Always use declared_params(include_missing: false) so that we don't give nils for optional arguments
-rw-r--r--lib/api/variables.rb2
-rw-r--r--spec/requests/api/variables_spec.rb11
2 files changed, 12 insertions, 1 deletions
diff --git a/lib/api/variables.rb b/lib/api/variables.rb
index 381c4ef50b0..10374995497 100644
--- a/lib/api/variables.rb
+++ b/lib/api/variables.rb
@@ -45,7 +45,7 @@ module API
optional :protected, type: String, desc: 'Whether the variable is protected'
end
post ':id/variables' do
- variable = user_project.variables.create(declared(params, include_parent_namespaces: false).to_h)
+ variable = user_project.variables.create(declared_params(include_missing: false))
if variable.valid?
present variable, with: Entities::Variable
diff --git a/spec/requests/api/variables_spec.rb b/spec/requests/api/variables_spec.rb
index 83673864fe7..e0975024b80 100644
--- a/spec/requests/api/variables_spec.rb
+++ b/spec/requests/api/variables_spec.rb
@@ -82,6 +82,17 @@ describe API::Variables do
expect(json_response['protected']).to be_truthy
end
+ it 'creates variable with optional attributes' 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).to have_http_status(201)
+ expect(json_response['key']).to eq('TEST_VARIABLE_2')
+ expect(json_response['value']).to eq('VALUE_2')
+ expect(json_response['protected']).to be_falsey
+ end
+
it 'does not allow to duplicate variable key' do
expect do
post api("/projects/#{project.id}/variables", user), key: variable.key, value: 'VALUE_2'