diff --git a/lib/travis/api/app/endpoint/logs.rb b/lib/travis/api/app/endpoint/logs.rb index 5dee65d9..37d38be2 100644 --- a/lib/travis/api/app/endpoint/logs.rb +++ b/lib/travis/api/app/endpoint/logs.rb @@ -8,6 +8,24 @@ class Travis::Api::App get '/:id' do |id| respond_with service(:find_log, params) end + + # Clears up the content of the log by the *job id* + # Optionally takes parameter *reason* + patch '/:id' do |id| + begin + result = self.service(:remove_log, params) + respond_with result + rescue Travis::AuthorizationDenied => ade + status 401 + { error: { message: ade.message } } + rescue Travis::JobUnfinished => jue + status 422 + { error: { message: "Job #{id} is not finished" } } + rescue => e + status 500 + { error: { message: "Unexpected error occurred: #{e.message}" } } + end + end end end end diff --git a/spec/unit/endpoint/logs_spec.rb b/spec/unit/endpoint/logs_spec.rb new file mode 100644 index 00000000..3fa502aa --- /dev/null +++ b/spec/unit/endpoint/logs_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +describe Travis::Api::App::Endpoint::Logs do + let(:user) { Factory(:user) } + let(:job) { Factory(:test, owner: user, log: Factory(:log)) } + let(:provider) { Factory(:annotation_provider) } + + describe "GET /logs/:id/" do + it "finds log successfully" do + get("/logs/#{job.log.id}", {}, "HTTP_ACCEPT" => "application/vnd.travis-ci.2+json, */*; q=0.01").should be_ok + end + end + + describe "PATCH /logs/:id/" do + before do + Travis::Services::RemoveLog.any_instance.stubs(:current_user).returns user + end + + context "user is unauthorized" do + it 'returns status 401' do + response = patch("/logs/#{job.id}") + response.status.should == 401 + JSON.parse(response.body)['error']['message'].should =~ Regexp.new("insufficient permission") + end + end + + context 'job is still running' do + it 'returns status 422' do + job.stubs(:finished?).returns false + user.stubs(:permission?).with(:push, anything).returns true + + response = patch("/logs/#{job.id}") + response.status.should == 422 + JSON.parse(response.body)['error']['message'].should =~ Regexp.new("Job .*is (not |un)finished") + end + end + end +end diff --git a/travis-api.gemspec b/travis-api.gemspec index 9e735082..b517c000 100644 --- a/travis-api.gemspec +++ b/travis-api.gemspec @@ -213,6 +213,7 @@ Gem::Specification.new do |s| "spec/unit/endpoint/endpoints_spec.rb", "spec/unit/endpoint/hooks_spec.rb", "spec/unit/endpoint/jobs_spec.rb", + "spec/unit/endpoint/logs_spec.rb", "spec/unit/endpoint/repos_spec.rb", "spec/unit/endpoint/users_spec.rb", "spec/unit/endpoint_spec.rb",