From eb1b8f69de70bbf82fcf14663482c57a25161024 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 29 Feb 2024 09:21:56 -0500 Subject: [PATCH] Allow pagination `Link` headers on API accounts/statuses when pinned true (#29442) --- .../api/v1/accounts/statuses_controller.rb | 2 +- .../v1/accounts/statuses_controller_spec.rb | 48 ++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/accounts/statuses_controller.rb b/app/controllers/api/v1/accounts/statuses_controller.rb index fe4279302fc..6a994ff5419 100644 --- a/app/controllers/api/v1/accounts/statuses_controller.rb +++ b/app/controllers/api/v1/accounts/statuses_controller.rb @@ -4,7 +4,7 @@ class Api::V1::Accounts::StatusesController < Api::BaseController before_action -> { authorize_if_got_token! :read, :'read:statuses' } before_action :set_account - after_action :insert_pagination_headers, unless: -> { truthy_param?(:pinned) } + after_action :insert_pagination_headers def index cache_if_unauthenticated! diff --git a/spec/controllers/api/v1/accounts/statuses_controller_spec.rb b/spec/controllers/api/v1/accounts/statuses_controller_spec.rb index 9bf385c03dc..102e3b8e915 100644 --- a/spec/controllers/api/v1/accounts/statuses_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/statuses_controller_spec.rb @@ -18,7 +18,8 @@ describe Api::V1::Accounts::StatusesController do get :index, params: { account_id: user.account.id, limit: 1 } expect(response).to have_http_status(200) - expect(response.headers['Link'].links.size).to eq(2) + expect(links_from_header.size) + .to eq(2) end context 'with only media' do @@ -55,10 +56,45 @@ describe Api::V1::Accounts::StatusesController do Fabricate(:status_pin, account: user.account, status: Fabricate(:status, account: user.account)) end - it 'returns http success' do + it 'returns http success and includes a header link' do get :index, params: { account_id: user.account.id, pinned: true } expect(response).to have_http_status(200) + expect(links_from_header.size) + .to eq(1) + expect(links_from_header) + .to contain_exactly( + have_attributes( + href: /pinned=true/, + attr_pairs: contain_exactly(['rel', 'prev']) + ) + ) + end + end + + context 'with enough pinned statuses to paginate' do + before do + stub_const 'Api::BaseController::DEFAULT_STATUSES_LIMIT', 1 + 2.times { Fabricate(:status_pin, account: user.account) } + end + + it 'returns http success and header pagination links to prev and next' do + get :index, params: { account_id: user.account.id, pinned: true } + + expect(response).to have_http_status(200) + expect(links_from_header.size) + .to eq(2) + expect(links_from_header) + .to contain_exactly( + have_attributes( + href: /pinned=true/, + attr_pairs: contain_exactly(['rel', 'next']) + ), + have_attributes( + href: /pinned=true/, + attr_pairs: contain_exactly(['rel', 'prev']) + ) + ) end end @@ -98,4 +134,12 @@ describe Api::V1::Accounts::StatusesController do end end end + + private + + def links_from_header + response + .headers['Link'] + .links + end end