v3 add spec for build cancel - not yet passing
This commit is contained in:
parent
faf60a5f7f
commit
2e110bc455
167
spec/v3/services/build/cancel_spec.rb
Normal file
167
spec/v3/services/build/cancel_spec.rb
Normal file
|
@ -0,0 +1,167 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Travis::API::V3::Services::Build::Cancel do
|
||||||
|
let(:repo) { Travis::API::V3::Models::Repository.where(owner_name: 'svenfuchs', name: 'minimal').first }
|
||||||
|
let(:build) { repo.builds.first }
|
||||||
|
let(:sidekiq_payload) { JSON.load(Sidekiq::Client.last['args'].last[:payload]).deep_symbolize_keys }
|
||||||
|
let(:sidekiq_params) { Sidekiq::Client.last['args'].last.deep_symbolize_keys }
|
||||||
|
# before { build.cancel.each(&:delete) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
Travis::Features.stubs(:owner_active?).returns(true)
|
||||||
|
@original_sidekiq = Sidekiq::Client
|
||||||
|
Sidekiq.send(:remove_const, :Client) # to avoid a warning
|
||||||
|
Sidekiq::Client = []
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
Sidekiq.send(:remove_const, :Client) # to avoid a warning
|
||||||
|
Sidekiq::Client = @original_sidekiq
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "not authenticated" do
|
||||||
|
before { post("/v3/build/#{build.id}/cancel") }
|
||||||
|
example { expect(last_response.status).to be == 403 }
|
||||||
|
example { expect(JSON.load(body)).to be == {
|
||||||
|
"@type" => "error",
|
||||||
|
"error_type" => "login_required",
|
||||||
|
"error_message" => "login required"
|
||||||
|
}}
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "missing build, authenticated" do
|
||||||
|
let(:token) { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) }
|
||||||
|
let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }}
|
||||||
|
before { post("/v3/build/9999999999/cancel", {}, headers) }
|
||||||
|
|
||||||
|
example { expect(last_response.status).to be == 404 }
|
||||||
|
example { expect(JSON.load(body)).to be == {
|
||||||
|
"@type" => "error",
|
||||||
|
"error_type" => "not_found",
|
||||||
|
"error_message" => "build not found (or insufficient access)",
|
||||||
|
"resource_type" => "build"
|
||||||
|
}}
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "existing repository, no push access" do
|
||||||
|
let(:token) { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) }
|
||||||
|
let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }}
|
||||||
|
before { post("/v3/build/#{build.id}/cancel", {}, headers) }
|
||||||
|
|
||||||
|
example { expect(last_response.status).to be == 403 }
|
||||||
|
example { expect(JSON.load(body).to_s).to include(
|
||||||
|
"@type",
|
||||||
|
"error_type",
|
||||||
|
"error_message",
|
||||||
|
"insufficient access",
|
||||||
|
"resource_type",
|
||||||
|
"build",
|
||||||
|
"permission",
|
||||||
|
"build_cancellation")
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "private repository, no access" do
|
||||||
|
let(:token) { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) }
|
||||||
|
let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }}
|
||||||
|
before { repo.update_attribute(:private, true) }
|
||||||
|
before { post("/v3/build/#{build.id}/cancel", {}, headers) }
|
||||||
|
after { build.update_attribute(:private, false) }
|
||||||
|
|
||||||
|
example { expect(last_response.status).to be == 404 }
|
||||||
|
example { expect(JSON.load(body)).to be == {
|
||||||
|
"@type" => "error",
|
||||||
|
"error_type" => "not_found",
|
||||||
|
"error_message" => "build not found (or insufficient access)",
|
||||||
|
"resource_type" => "build"
|
||||||
|
}}
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "existing repository, push access" do
|
||||||
|
let(:params) {{}}
|
||||||
|
let(:token) { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) }
|
||||||
|
let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }}
|
||||||
|
before { Travis::API::V3::Models::Permission.create(repository: repo, user: repo.owner, push: true) }
|
||||||
|
before { post("/v3/build/#{build.id}/cancel", params, headers) }
|
||||||
|
|
||||||
|
example { expect(last_response.status).to be == 202 }
|
||||||
|
example { expect(JSON.load(body).to_s).to include(
|
||||||
|
"@type",
|
||||||
|
"pending",
|
||||||
|
"build",
|
||||||
|
"@href",
|
||||||
|
"@representation",
|
||||||
|
"minimal",
|
||||||
|
"request",
|
||||||
|
"user",
|
||||||
|
"resource_type",
|
||||||
|
"request")
|
||||||
|
}
|
||||||
|
|
||||||
|
example { expect(sidekiq_payload).to be == {
|
||||||
|
build: { id: build.id }
|
||||||
|
}}
|
||||||
|
|
||||||
|
example { expect(Sidekiq::Client.last['queue']).to be == 'build_cancellations' }
|
||||||
|
example { expect(Sidekiq::Client.last['class']).to be == 'Travis::Sidekiq::CancellationRequest' }
|
||||||
|
|
||||||
|
describe "setting id has no effect" do
|
||||||
|
let(:params) {{ id: 42 }}
|
||||||
|
example { expect(sidekiq_payload).to be == {
|
||||||
|
build: { id: build.id }
|
||||||
|
}}
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "passing the token in params" do
|
||||||
|
let(:params) {{ request: { token: 'foo-bar' }}}
|
||||||
|
example { expect(sidekiq_params[:credentials]).to be == {
|
||||||
|
token: 'foo-bar'
|
||||||
|
}}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
describe "existing repository, application with full access" do
|
||||||
|
let(:app_name) { 'travis-example' }
|
||||||
|
let(:app_secret) { '12345678' }
|
||||||
|
let(:sign_opts) { "a=#{app_name}" }
|
||||||
|
let(:signature) { OpenSSL::HMAC.hexdigest('sha256', app_secret, sign_opts) }
|
||||||
|
let(:headers) {{ 'HTTP_AUTHORIZATION' => "signature #{sign_opts}:#{signature}" }}
|
||||||
|
before { Travis.config.applications = { app_name => { full_access: true, secret: app_secret }}}
|
||||||
|
before { post("/v3/build/#{build.id}/cancel", params, headers) }
|
||||||
|
|
||||||
|
# describe 'without setting user' do
|
||||||
|
# let(:params) {{}}
|
||||||
|
# example { expect(last_response.status).to be == 400 }
|
||||||
|
# example { expect(JSON.load(body)).to be == {
|
||||||
|
# "@type" => "error",
|
||||||
|
# "error_type" => "wrong_params",
|
||||||
|
# "error_message" => "missing user"
|
||||||
|
# }}
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# describe 'setting user' do
|
||||||
|
# let(:params) {{ user: { id: repo.owner.id } }}
|
||||||
|
# example { expect(last_response.status).to be == 202 }
|
||||||
|
# example { expect(sidekiq_payload).to be == {
|
||||||
|
# repository: { id: repo.id, owner_name: 'svenfuchs', name: 'minimal' },
|
||||||
|
# user: { id: repo.owner.id },
|
||||||
|
# message: nil,
|
||||||
|
# branch: 'master',
|
||||||
|
# config: {}
|
||||||
|
# }}
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# describe 'setting branch' do
|
||||||
|
# let(:params) {{ user: { id: repo.owner.id }, branch: 'example' }}
|
||||||
|
# example { expect(last_response.status).to be == 202 }
|
||||||
|
# example { expect(sidekiq_payload).to be == {
|
||||||
|
# repository: { id: repo.id, owner_name: 'svenfuchs', name: 'minimal' },
|
||||||
|
# user: { id: repo.owner.id },
|
||||||
|
# message: nil,
|
||||||
|
# branch: 'example',
|
||||||
|
# config: {}
|
||||||
|
# }}
|
||||||
|
# end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user