diff --git a/lib/travis/api/app/endpoint/repos.rb b/lib/travis/api/app/endpoint/repos.rb index 1f28645c..286632ac 100644 --- a/lib/travis/api/app/endpoint/repos.rb +++ b/lib/travis/api/app/endpoint/repos.rb @@ -64,8 +64,12 @@ class Travis::Api::App settings.merge(payload['settings']) # TODO: I would like to have better API here, but leaving this # for testing to not waste too much time before I can play with it - settings.save - respond_with({ settings: settings.obfuscated }, version: :v2) + if settings.save + respond_with({ settings: settings.obfuscated }, version: :v2) + else + status 422 + respond_with(settings, type: :validation_error, version: :v2) + end else status 404 end diff --git a/spec/integration/v2/repositories_spec.rb b/spec/integration/v2/repositories_spec.rb index 166f4b86..5c51cdc3 100644 --- a/spec/integration/v2/repositories_spec.rb +++ b/spec/integration/v2/repositories_spec.rb @@ -34,10 +34,23 @@ describe 'Repos' do body['settings']['build_pushes'].should == false end + it 'returns errors when settings are not valid' do + json = { 'settings' => { 'maximum_number_of_builds' => 'this is not a number' } }.to_json + response = patch "repos/#{repo.id}/settings", json, headers + + repo.reload.settings['maximum_number_of_builds'].should == 0 + + body = JSON.parse(response.body) + body['message'].should == 'Validation failed' + body['errors'].should == [{ + 'field' => 'maximum_number_of_builds', + 'code' => 'not_a_number' + }] + end + it 'allows to get settings' do response = get "repos/#{repo.id}/settings", {}, headers - settings = Repository::Settings.defaults - JSON.parse(response.body).should == { 'settings' => settings } + JSON.parse(response.body)['settings'].should have_key('build_pushes') end end