From 2e91a9bd3487ad5137688dd5260a5f216c5a4ef8 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 15 Mar 2024 06:17:45 -0400 Subject: [PATCH] Add `include_pagination_headers` matcher to check `Link` header in api specs (#29596) --- spec/requests/api/v1/blocks_spec.rb | 14 ++++++-------- spec/requests/api/v1/bookmarks_spec.rb | 11 ++++++++--- spec/requests/api/v1/favourites_spec.rb | 14 ++++++-------- spec/requests/api/v1/followed_tags_spec.rb | 14 ++++++-------- spec/requests/api/v1/mutes_spec.rb | 9 +++++---- spec/requests/api/v1/notifications_spec.rb | 11 ++++++++--- spec/requests/api/v1/timelines/home_spec.rb | 9 +++++---- spec/requests/api/v1/timelines/public_spec.rb | 9 +++++---- spec/requests/api/v1/timelines/tag_spec.rb | 9 +++++---- spec/support/matchers/api_pagination.rb | 13 +++++++++++++ 10 files changed, 67 insertions(+), 46 deletions(-) create mode 100644 spec/support/matchers/api_pagination.rb diff --git a/spec/requests/api/v1/blocks_spec.rb b/spec/requests/api/v1/blocks_spec.rb index 62543157c3..c6c2d56f36 100644 --- a/spec/requests/api/v1/blocks_spec.rb +++ b/spec/requests/api/v1/blocks_spec.rb @@ -38,16 +38,14 @@ RSpec.describe 'Blocks' do expect(body_as_json.size).to eq(params[:limit]) end - it 'sets the correct pagination header for the prev path' do + it 'sets correct link header pagination' do subject - expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_blocks_url(limit: params[:limit], since_id: blocks.last.id)) - end - - it 'sets the correct pagination header for the next path' do - subject - - expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_blocks_url(limit: params[:limit], max_id: blocks[1].id)) + expect(response) + .to include_pagination_headers( + prev: api_v1_blocks_url(limit: params[:limit], since_id: blocks.last.id), + next: api_v1_blocks_url(limit: params[:limit], max_id: blocks.second.id) + ) end end diff --git a/spec/requests/api/v1/bookmarks_spec.rb b/spec/requests/api/v1/bookmarks_spec.rb index 18f4fddc29..dc32820c89 100644 --- a/spec/requests/api/v1/bookmarks_spec.rb +++ b/spec/requests/api/v1/bookmarks_spec.rb @@ -42,9 +42,14 @@ RSpec.describe 'Bookmarks' do it 'paginates correctly', :aggregate_failures do subject - expect(body_as_json.size).to eq(params[:limit]) - expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_bookmarks_url(limit: params[:limit], min_id: bookmarks.last.id)) - expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_bookmarks_url(limit: params[:limit], max_id: bookmarks[1].id)) + expect(body_as_json.size) + .to eq(params[:limit]) + + expect(response) + .to include_pagination_headers( + prev: api_v1_bookmarks_url(limit: params[:limit], min_id: bookmarks.last.id), + next: api_v1_bookmarks_url(limit: params[:limit], max_id: bookmarks.second.id) + ) end end diff --git a/spec/requests/api/v1/favourites_spec.rb b/spec/requests/api/v1/favourites_spec.rb index 2d8a42e716..b988ac99db 100644 --- a/spec/requests/api/v1/favourites_spec.rb +++ b/spec/requests/api/v1/favourites_spec.rb @@ -45,16 +45,14 @@ RSpec.describe 'Favourites' do expect(body_as_json.size).to eq(params[:limit]) end - it 'sets the correct pagination header for the prev path' do + it 'sets the correct pagination headers' do subject - expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_favourites_url(limit: params[:limit], min_id: favourites.last.id)) - end - - it 'sets the correct pagination header for the next path' do - subject - - expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_favourites_url(limit: params[:limit], max_id: favourites[1].id)) + expect(response) + .to include_pagination_headers( + prev: api_v1_favourites_url(limit: params[:limit], min_id: favourites.last.id), + next: api_v1_favourites_url(limit: params[:limit], max_id: favourites.second.id) + ) end end diff --git a/spec/requests/api/v1/followed_tags_spec.rb b/spec/requests/api/v1/followed_tags_spec.rb index 52ed1ba4bb..3d2d82d5db 100644 --- a/spec/requests/api/v1/followed_tags_spec.rb +++ b/spec/requests/api/v1/followed_tags_spec.rb @@ -49,16 +49,14 @@ RSpec.describe 'Followed tags' do expect(body_as_json.size).to eq(params[:limit]) end - it 'sets the correct pagination header for the prev path' do + it 'sets the correct pagination headers' do subject - expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_followed_tags_url(limit: params[:limit], since_id: tag_follows.last.id)) - end - - it 'sets the correct pagination header for the next path' do - subject - - expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_followed_tags_url(limit: params[:limit], max_id: tag_follows.last.id)) + expect(response) + .to include_pagination_headers( + prev: api_v1_followed_tags_url(limit: params[:limit], since_id: tag_follows.last.id), + next: api_v1_followed_tags_url(limit: params[:limit], max_id: tag_follows.last.id) + ) end end end diff --git a/spec/requests/api/v1/mutes_spec.rb b/spec/requests/api/v1/mutes_spec.rb index b2782a0c22..019bf16584 100644 --- a/spec/requests/api/v1/mutes_spec.rb +++ b/spec/requests/api/v1/mutes_spec.rb @@ -44,10 +44,11 @@ RSpec.describe 'Mutes' do it 'sets the correct pagination headers', :aggregate_failures do subject - headers = response.headers['Link'] - - expect(headers.find_link(%w(rel prev)).href).to eq(api_v1_mutes_url(limit: params[:limit], since_id: mutes.last.id.to_s)) - expect(headers.find_link(%w(rel next)).href).to eq(api_v1_mutes_url(limit: params[:limit], max_id: mutes.last.id.to_s)) + expect(response) + .to include_pagination_headers( + prev: api_v1_mutes_url(limit: params[:limit], since_id: mutes.last.id), + next: api_v1_mutes_url(limit: params[:limit], max_id: mutes.last.id) + ) end end diff --git a/spec/requests/api/v1/notifications_spec.rb b/spec/requests/api/v1/notifications_spec.rb index 222ff67fc8..55d3cdac94 100644 --- a/spec/requests/api/v1/notifications_spec.rb +++ b/spec/requests/api/v1/notifications_spec.rb @@ -98,9 +98,14 @@ RSpec.describe 'Notifications' do notifications = user.account.notifications - expect(body_as_json.size).to eq(params[:limit]) - expect(response.headers['Link'].find_link(%w(rel prev)).href).to eq(api_v1_notifications_url(limit: params[:limit], min_id: notifications.last.id.to_s)) - expect(response.headers['Link'].find_link(%w(rel next)).href).to eq(api_v1_notifications_url(limit: params[:limit], max_id: notifications[2].id.to_s)) + expect(body_as_json.size) + .to eq(params[:limit]) + + expect(response) + .to include_pagination_headers( + prev: api_v1_notifications_url(limit: params[:limit], min_id: notifications.last.id), + next: api_v1_notifications_url(limit: params[:limit], max_id: notifications[2].id) + ) end end diff --git a/spec/requests/api/v1/timelines/home_spec.rb b/spec/requests/api/v1/timelines/home_spec.rb index e57e9643bf..2bebe8cf45 100644 --- a/spec/requests/api/v1/timelines/home_spec.rb +++ b/spec/requests/api/v1/timelines/home_spec.rb @@ -55,10 +55,11 @@ describe 'Home', :sidekiq_inline do it 'sets the correct pagination headers', :aggregate_failures do subject - headers = response.headers['Link'] - - expect(headers.find_link(%w(rel prev)).href).to eq(api_v1_timelines_home_url(limit: 1, min_id: ana.statuses.first.id.to_s)) - expect(headers.find_link(%w(rel next)).href).to eq(api_v1_timelines_home_url(limit: 1, max_id: ana.statuses.first.id.to_s)) + expect(response) + .to include_pagination_headers( + prev: api_v1_timelines_home_url(limit: params[:limit], min_id: ana.statuses.first.id), + next: api_v1_timelines_home_url(limit: params[:limit], max_id: ana.statuses.first.id) + ) end end end diff --git a/spec/requests/api/v1/timelines/public_spec.rb b/spec/requests/api/v1/timelines/public_spec.rb index c436262407..30d1bc00cb 100644 --- a/spec/requests/api/v1/timelines/public_spec.rb +++ b/spec/requests/api/v1/timelines/public_spec.rb @@ -83,10 +83,11 @@ describe 'Public' do it 'sets the correct pagination headers', :aggregate_failures do subject - headers = response.headers['Link'] - - expect(headers.find_link(%w(rel prev)).href).to eq(api_v1_timelines_public_url(limit: 1, min_id: media_status.id.to_s)) - expect(headers.find_link(%w(rel next)).href).to eq(api_v1_timelines_public_url(limit: 1, max_id: media_status.id.to_s)) + expect(response) + .to include_pagination_headers( + prev: api_v1_timelines_public_url(limit: params[:limit], min_id: media_status.id), + next: api_v1_timelines_public_url(limit: params[:limit], max_id: media_status.id) + ) end end end diff --git a/spec/requests/api/v1/timelines/tag_spec.rb b/spec/requests/api/v1/timelines/tag_spec.rb index a118af13e2..8611341702 100644 --- a/spec/requests/api/v1/timelines/tag_spec.rb +++ b/spec/requests/api/v1/timelines/tag_spec.rb @@ -71,10 +71,11 @@ RSpec.describe 'Tag' do it 'sets the correct pagination headers', :aggregate_failures do subject - headers = response.headers['Link'] - - expect(headers.find_link(%w(rel prev)).href).to eq(api_v1_timelines_tag_url(limit: 1, min_id: love_status.id.to_s)) - expect(headers.find_link(%w(rel next)).href).to eq(api_v1_timelines_tag_url(limit: 1, max_id: love_status.id.to_s)) + expect(response) + .to include_pagination_headers( + prev: api_v1_timelines_tag_url(limit: params[:limit], min_id: love_status.id), + next: api_v1_timelines_tag_url(limit: params[:limit], max_id: love_status.id) + ) end end diff --git a/spec/support/matchers/api_pagination.rb b/spec/support/matchers/api_pagination.rb new file mode 100644 index 0000000000..81e27e44b8 --- /dev/null +++ b/spec/support/matchers/api_pagination.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +RSpec::Matchers.define :include_pagination_headers do |links| + match do |response| + links.map do |key, value| + response.headers['Link'].find_link(['rel', key.to_s]).href == value + end.all? + end + + failure_message do |header| + "expected that #{header} would have the same values as #{links}." + end +end