Commit fda48d30 authored by Shinya Maeda's avatar Shinya Maeda

Improve api/pipelines_spec.rb

parent 7d48cb01
...@@ -28,10 +28,14 @@ describe PipelinesFinder do ...@@ -28,10 +28,14 @@ describe PipelinesFinder do
context 'when scope is finished' do context 'when scope is finished' do
let(:params) { { scope: 'finished' } } let(:params) { { scope: 'finished' } }
let!(:pipeline) { create(:ci_pipeline, project: project, status: 'success') } let!(:pipelines) do
[create(:ci_pipeline, project: project, status: 'success'),
create(:ci_pipeline, project: project, status: 'failed'),
create(:ci_pipeline, project: project, status: 'canceled')]
end
it 'returns matched pipelines' do it 'returns matched pipelines' do
is_expected.to eq([pipeline]) is_expected.to eq(pipelines.sort_by { |p| -p.id })
end end
end end
...@@ -168,7 +172,7 @@ describe PipelinesFinder do ...@@ -168,7 +172,7 @@ describe PipelinesFinder do
let(:params) { { order_by: 'user_id', sort: 'asc' } } let(:params) { { order_by: 'user_id', sort: 'asc' } }
let!(:pipelines) { create_list(:ci_pipeline, 2, project: project, user: create(:user)) } let!(:pipelines) { create_list(:ci_pipeline, 2, project: project, user: create(:user)) }
it 'sorts as user_id: :desc' do it 'sorts as user_id: :asc' do
is_expected.to eq(pipelines.sort_by { |p| p.user.id }) is_expected.to eq(pipelines.sort_by { |p| p.user.id })
end end
......
...@@ -26,55 +26,52 @@ describe API::Pipelines do ...@@ -26,55 +26,52 @@ describe API::Pipelines do
end end
context 'when parameter is passed' do context 'when parameter is passed' do
let(:user1) { create(:user) } %w[running pending].each do |target|
let(:user2) { create(:user) } context "when scope is #{target}" do
let(:project) { create(:project, :repository) } before do
create(:ci_pipeline, project: project, status: target)
before do
create(:ci_pipeline, project: project, user: user1, ref: 'v1.0.0', tag: true)
create(:ci_pipeline, project: project, user: user1, status: 'created')
create(:ci_pipeline, project: project, user: user1, status: 'pending')
create(:ci_pipeline, project: project, user: user1, status: 'running')
create(:ci_pipeline, project: project, user: user1, status: 'success')
create(:ci_pipeline, project: project, user: user2, status: 'failed')
create(:ci_pipeline, project: project, user: user2, status: 'canceled')
create(:ci_pipeline, project: project, user: user2, status: 'skipped')
create(:ci_pipeline, project: project, user: user2, yaml_errors: 'Syntax error')
end
context 'when scope is passed' do
%w[running pending].each do |target|
context "when scope is #{target}" do
it "returns matched pipelines" do
get api("/projects/#{project.id}/pipelines", user), scope: target
expect(response).to have_http_status(200)
expect(response).to include_pagination_headers
expect(json_response).not_to be_empty
json_response.each { |r| expect(r['status']).to eq(target) }
end
end end
end
context 'when scope is finished' do
it 'returns matched pipelines' do it 'returns matched pipelines' do
get api("/projects/#{project.id}/pipelines", user), scope: 'finished' get api("/projects/#{project.id}/pipelines", user), scope: target
expect(response).to have_http_status(200) expect(response).to have_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response).not_to be_empty expect(json_response).not_to be_empty
json_response.each { |r| expect(r['status']).to be_in(%w[success failed canceled]) } json_response.each { |r| expect(r['status']).to eq(target) }
end end
end end
end
context 'when scope is finished' do
before do
create(:ci_pipeline, project: project, status: 'success')
create(:ci_pipeline, project: project, status: 'failed')
create(:ci_pipeline, project: project, status: 'canceled')
end
it 'returns matched pipelines' do
get api("/projects/#{project.id}/pipelines", user), scope: 'finished'
expect(response).to have_http_status(:ok)
expect(response).to include_pagination_headers
expect(json_response).not_to be_empty
json_response.each { |r| expect(r['status']).to be_in(%w[success failed canceled]) }
end
end
context 'when scope is branches or tags' do
let!(:pipeline_branch) { create(:ci_pipeline, project: project) }
let!(:pipeline_tag) { create(:ci_pipeline, project: project, ref: 'v1.0.0', tag: true) }
context 'when scope is branches' do context 'when scope is branches' do
it 'returns matched pipelines' do it 'returns matched pipelines' do
get api("/projects/#{project.id}/pipelines", user), scope: 'branches' get api("/projects/#{project.id}/pipelines", user), scope: 'branches'
expect(response).to have_http_status(200) expect(response).to have_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response).not_to be_empty expect(json_response).not_to be_empty
expect(json_response.last['sha']).to eq(project.pipelines.where(tag: false).last.sha) expect(json_response.last['id']).to eq(pipeline_branch.id)
end end
end end
...@@ -82,51 +79,59 @@ describe API::Pipelines do ...@@ -82,51 +79,59 @@ describe API::Pipelines do
it 'returns matched pipelines' do it 'returns matched pipelines' do
get api("/projects/#{project.id}/pipelines", user), scope: 'tags' get api("/projects/#{project.id}/pipelines", user), scope: 'tags'
expect(response).to have_http_status(200) expect(response).to have_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response).not_to be_empty expect(json_response).not_to be_empty
expect(json_response.last['sha']).to eq(project.pipelines.where(tag: true).last.sha) expect(json_response.last['id']).to eq(pipeline_tag.id)
end end
end end
end
context 'when scope is invalid' do context 'when scope is invalid' do
it 'returns 400' do it 'returns 400' do
get api("/projects/#{project.id}/pipelines", user), scope: 'invalid-scope' get api("/projects/#{project.id}/pipelines", user), scope: 'invalid-scope'
expect(response).to have_http_status(400) expect(response).to have_http_status(:bad_request)
end
end end
end end
context 'when status is passed' do %w[running pending success failed canceled skipped].each do |target|
%w[running pending success failed canceled skipped].each do |target| context "when status is #{target}" do
context "when status is #{target}" do before do
it 'returns matched pipelines' do create(:ci_pipeline, project: project, status: target)
get api("/projects/#{project.id}/pipelines", user), status: target exception_status = %w[running pending success failed canceled skipped] - [target]
create(:ci_pipeline, project: project, status: exception_status.sample)
end
expect(response).to have_http_status(200) it 'returns matched pipelines' do
expect(response).to include_pagination_headers get api("/projects/#{project.id}/pipelines", user), status: target
expect(json_response).not_to be_empty
json_response.each { |r| expect(r['status']).to eq(target) } expect(response).to have_http_status(:ok)
end expect(response).to include_pagination_headers
expect(json_response).not_to be_empty
json_response.each { |r| expect(r['status']).to eq(target) }
end end
end end
end
context 'when status is invalid' do context 'when status is invalid' do
it 'returns 400' do it 'returns :bad_request' do
get api("/projects/#{project.id}/pipelines", user), status: 'invalid-status' get api("/projects/#{project.id}/pipelines", user), status: 'invalid-status'
expect(response).to have_http_status(400) expect(response).to have_http_status(:bad_request)
end
end end
end end
context 'when ref is passed' do context 'when ref is specified' do
before do
create(:ci_pipeline, project: project)
end
context 'when ref exists' do context 'when ref exists' do
it 'returns matched pipelines' do it 'returns matched pipelines' do
get api("/projects/#{project.id}/pipelines", user), ref: 'master' get api("/projects/#{project.id}/pipelines", user), ref: 'master'
expect(response).to have_http_status(200) expect(response).to have_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response).not_to be_empty expect(json_response).not_to be_empty
json_response.each { |r| expect(r['ref']).to eq('master') } json_response.each { |r| expect(r['ref']).to eq('master') }
...@@ -137,21 +142,23 @@ describe API::Pipelines do ...@@ -137,21 +142,23 @@ describe API::Pipelines do
it 'returns empty' do it 'returns empty' do
get api("/projects/#{project.id}/pipelines", user), ref: 'invalid-ref' get api("/projects/#{project.id}/pipelines", user), ref: 'invalid-ref'
expect(response).to have_http_status(200) expect(response).to have_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response).to be_empty expect(json_response).to be_empty
end end
end end
end end
context 'when name is passed' do context 'when name is specified' do
let!(:pipeline) { create(:ci_pipeline, project: project, user: user) }
context 'when name exists' do context 'when name exists' do
it 'returns matched pipelines' do it 'returns matched pipelines' do
get api("/projects/#{project.id}/pipelines", user), name: user1.name get api("/projects/#{project.id}/pipelines", user), name: user.name
expect(response).to have_http_status(200) expect(response).to have_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response.first['sha']).to eq(project.pipelines.where(user: user1).order(id: :desc).first.sha) expect(json_response.first['id']).to eq(pipeline.id)
end end
end end
...@@ -159,21 +166,23 @@ describe API::Pipelines do ...@@ -159,21 +166,23 @@ describe API::Pipelines do
it 'returns empty' do it 'returns empty' do
get api("/projects/#{project.id}/pipelines", user), name: 'invalid-name' get api("/projects/#{project.id}/pipelines", user), name: 'invalid-name'
expect(response).to have_http_status(200) expect(response).to have_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response).to be_empty expect(json_response).to be_empty
end end
end end
end end
context 'when username is passed' do context 'when username is specified' do
let!(:pipeline) { create(:ci_pipeline, project: project, user: user) }
context 'when username exists' do context 'when username exists' do
it 'returns matched pipelines' do it 'returns matched pipelines' do
get api("/projects/#{project.id}/pipelines", user), username: user1.username get api("/projects/#{project.id}/pipelines", user), username: user.username
expect(response).to have_http_status(200) expect(response).to have_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response.first['sha']).to eq(project.pipelines.where(user: user1).order(id: :desc).first.sha) expect(json_response.first['id']).to eq(pipeline.id)
end end
end end
...@@ -181,21 +190,24 @@ describe API::Pipelines do ...@@ -181,21 +190,24 @@ describe API::Pipelines do
it 'returns empty' do it 'returns empty' do
get api("/projects/#{project.id}/pipelines", user), username: 'invalid-username' get api("/projects/#{project.id}/pipelines", user), username: 'invalid-username'
expect(response).to have_http_status(200) expect(response).to have_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response).to be_empty expect(json_response).to be_empty
end end
end end
end end
context 'when yaml_errors is passed' do context 'when yaml_errors is specified' do
let!(:pipeline1) { create(:ci_pipeline, project: project, yaml_errors: 'Syntax error') }
let!(:pipeline2) { create(:ci_pipeline, project: project) }
context 'when yaml_errors is true' do context 'when yaml_errors is true' do
it 'returns matched pipelines' do it 'returns matched pipelines' do
get api("/projects/#{project.id}/pipelines", user), yaml_errors: true get api("/projects/#{project.id}/pipelines", user), yaml_errors: true
expect(response).to have_http_status(200) expect(response).to have_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response.first['id']).to eq(project.pipelines.where("yaml_errors IS NOT NULL").order(id: :desc).first.id) expect(json_response.first['id']).to eq(pipeline1.id)
end end
end end
...@@ -203,49 +215,50 @@ describe API::Pipelines do ...@@ -203,49 +215,50 @@ describe API::Pipelines do
it 'returns matched pipelines' do it 'returns matched pipelines' do
get api("/projects/#{project.id}/pipelines", user), yaml_errors: false get api("/projects/#{project.id}/pipelines", user), yaml_errors: false
expect(response).to have_http_status(200) expect(response).to have_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response.first['id']).to eq(project.pipelines.where("yaml_errors IS NULL").order(id: :desc).first.id) expect(json_response.first['id']).to eq(pipeline2.id)
end end
end end
context 'when yaml_errors is invalid' do context 'when yaml_errors is invalid' do
it 'returns 400' do it 'returns :bad_request' do
get api("/projects/#{project.id}/pipelines", user), yaml_errors: 'invalid-yaml_errors' get api("/projects/#{project.id}/pipelines", user), yaml_errors: 'invalid-yaml_errors'
expect(response).to have_http_status(400) expect(response).to have_http_status(:bad_request)
end end
end end
end end
context 'when order_by and sort are passed' do context 'when order_by and sort are specified' do
context 'when order_by and sort are valid' do context 'when order_by user_id' do
it 'sorts pipelines' do let!(:pipeline) { create_list(:ci_pipeline, 2, project: project, user: create(:user)) }
it 'sorts as user_id: :asc' do
get api("/projects/#{project.id}/pipelines", user), order_by: 'user_id', sort: 'asc' get api("/projects/#{project.id}/pipelines", user), order_by: 'user_id', sort: 'asc'
expect(response).to have_http_status(200) expect(response).to have_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response).not_to be_empty expect(json_response).not_to be_empty
pipelines = project.pipelines.order(user_id: :asc) pipeline.sort_by { |p| p.user.id }.tap do |sorted_pipeline|
json_response.each_with_index do |r, i| json_response.each_with_index { |r, i| expect(r['id']).to eq(sorted_pipeline[i].id) }
expect(r['id']).to eq(pipelines[i].id)
end end
end end
end
context 'when order_by is invalid' do context 'when sort is invalid' do
it 'returns 400' do it 'sorts as user_id: :desc' do
get api("/projects/#{project.id}/pipelines", user), order_by: 'lock_version', sort: 'asc' get api("/projects/#{project.id}/pipelines", user), order_by: 'user_id', sort: 'invalid_sort'
expect(response).to have_http_status(400) expect(response).to have_http_status(:bad_request)
end
end end
end end
context 'when sort is invalid' do context 'when order_by is invalid' do
it 'returns 400' do it 'returns :bad_request' do
get api("/projects/#{project.id}/pipelines", user), order_by: 'id', sort: 'hack' get api("/projects/#{project.id}/pipelines", user), order_by: 'lock_version', sort: 'asc'
expect(response).to have_http_status(400) expect(response).to have_http_status(:bad_request)
end end
end end
end end
......
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