Commit dc8ce956 authored by Stan Hu's avatar Stan Hu

Merge branch '9070-refactor-specs' into 'master'

Refactor epics/issues API specs

Closes #9070

See merge request gitlab-org/gitlab-ee!9086
parents afee5539 cf745a24
...@@ -69,16 +69,6 @@ describe API::Epics do ...@@ -69,16 +69,6 @@ describe API::Epics do
describe 'GET /groups/:id/epics' do describe 'GET /groups/:id/epics' do
let(:url) { "/groups/#{group.path}/epics" } let(:url) { "/groups/#{group.path}/epics" }
def expect_paginated_array_of_items(expected)
expect(response).to have_gitlab_http_status(200)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
items = json_response.map { |i| i['id'] }
expect(items).to eq(expected)
end
it_behaves_like 'error requests' it_behaves_like 'error requests'
context 'when the request is correct' do context 'when the request is correct' do
...@@ -125,61 +115,61 @@ describe API::Epics do ...@@ -125,61 +115,61 @@ describe API::Epics do
it 'returns epics authored by the given author id' do it 'returns epics authored by the given author id' do
get api(url, user), params: { author_id: user2.id } get api(url, user), params: { author_id: user2.id }
expect_paginated_array_of_items([epic2.id]) expect_paginated_array_response([epic2.id])
end end
it 'returns epics matching given search string for title' do it 'returns epics matching given search string for title' do
get api(url, user), params: { search: epic2.title } get api(url, user), params: { search: epic2.title }
expect_paginated_array_of_items([epic2.id]) expect_paginated_array_response([epic2.id])
end end
it 'returns epics matching given search string for description' do it 'returns epics matching given search string for description' do
get api(url, user), params: { search: epic2.description } get api(url, user), params: { search: epic2.description }
expect_paginated_array_of_items([epic2.id]) expect_paginated_array_response([epic2.id])
end end
it 'returns epics matching given status' do it 'returns epics matching given status' do
get api(url, user), params: { state: :opened } get api(url, user), params: { state: :opened }
expect_paginated_array_of_items([epic2.id]) expect_paginated_array_response([epic2.id])
end end
it 'returns all epics when state set to all' do it 'returns all epics when state set to all' do
get api(url, user), params: { state: :all } get api(url, user), params: { state: :all }
expect_paginated_array_of_items([epic2.id, epic.id]) expect_paginated_array_response([epic2.id, epic.id])
end end
it 'sorts by created_at descending by default' do it 'sorts by created_at descending by default' do
get api(url, user) get api(url, user)
expect_paginated_array_of_items([epic2.id, epic.id]) expect_paginated_array_response([epic2.id, epic.id])
end end
it 'sorts ascending when requested' do it 'sorts ascending when requested' do
get api(url, user), params: { sort: :asc } get api(url, user), params: { sort: :asc }
expect_paginated_array_of_items([epic.id, epic2.id]) expect_paginated_array_response([epic.id, epic2.id])
end end
it 'sorts by updated_at descending when requested' do it 'sorts by updated_at descending when requested' do
get api(url, user), params: { order_by: :updated_at } get api(url, user), params: { order_by: :updated_at }
expect_paginated_array_of_items([epic.id, epic2.id]) expect_paginated_array_response([epic.id, epic2.id])
end end
it 'sorts by updated_at ascending when requested' do it 'sorts by updated_at ascending when requested' do
get api(url, user), params: { order_by: :updated_at, sort: :asc } get api(url, user), params: { order_by: :updated_at, sort: :asc }
expect_paginated_array_of_items([epic2.id, epic.id]) expect_paginated_array_response([epic2.id, epic.id])
end end
it 'returns an array of labeled epics' do it 'returns an array of labeled epics' do
get api(url, user), params: { labels: label.title } get api(url, user), params: { labels: label.title }
expect_paginated_array_of_items([epic2.id]) expect_paginated_array_response([epic2.id])
end end
it_behaves_like 'can admin epics' it_behaves_like 'can admin epics'
...@@ -201,7 +191,7 @@ describe API::Epics do ...@@ -201,7 +191,7 @@ describe API::Epics do
get api(url, user), params: { page: page, per_page: per_page } get api(url, user), params: { page: page, per_page: per_page }
expect(response.headers['X-Page']).to eq(page.to_s) expect(response.headers['X-Page']).to eq(page.to_s)
expect_paginated_array_of_items(expected) expect_paginated_array_response(expected)
end end
end end
......
...@@ -38,31 +38,26 @@ describe API::Issues, :mailer do ...@@ -38,31 +38,26 @@ describe API::Issues, :mailer do
end end
describe "filtering by weight" do describe "filtering by weight" do
before do let!(:issue1) { create(:issue, author: user2, project: project, weight: 1, created_at: 3.days.ago) }
create(:issue, author: user2, project: project, weight: 1) let!(:issue2) { create(:issue, author: user2, project: project, weight: 5, created_at: 2.days.ago) }
create(:issue, author: user2, project: project, weight: 3) let!(:issue3) { create(:issue, author: user2, project: project, weight: 3, created_at: 1.day.ago) }
end
let!(:issue2) { create(:issue, author: user2, project: project, weight: 5) }
it 'returns issues with specific weight' do it 'returns issues with specific weight' do
get api('/issues', user), params: { weight: 5, scope: 'all' } get api('/issues', user), params: { weight: 5, scope: 'all' }
expect_paginated_array_response(size: 1) expect_paginated_array_response(issue2.id)
expect(json_response.first['id']).to eq(issue2.id)
end end
it 'returns issues with no weight' do it 'returns issues with no weight' do
get api('/issues', user), params: { weight: 'None', scope: 'all' } get api('/issues', user), params: { weight: 'None', scope: 'all' }
expect_paginated_array_response(size: 1) expect_paginated_array_response(issue.id)
expect(json_response.first['id']).to eq(issue.id)
end end
it 'returns issues with any weight' do it 'returns issues with any weight' do
get api('/issues', user), params: { weight: 'Any', scope: 'all' } get api('/issues', user), params: { weight: 'Any', scope: 'all' }
expect_paginated_array_response(size: 3) expect_paginated_array_response([issue3.id, issue2.id, issue1.id])
end end
end end
end end
...@@ -131,11 +126,4 @@ describe API::Issues, :mailer do ...@@ -131,11 +126,4 @@ describe API::Issues, :mailer do
end end
end end
end end
def expect_paginated_array_response(size: nil)
expect(response).to have_gitlab_http_status(200)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.length).to eq(size) if size
end
end end
This diff is collapsed.
...@@ -36,4 +36,11 @@ module ApiHelpers ...@@ -36,4 +36,11 @@ module ApiHelpers
full_path full_path
end end
def expect_paginated_array_response(items)
expect(response).to have_gitlab_http_status(200)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.map { |item| item['id'] }).to eq(Array(items))
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