Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/controllers/api/v2/sales_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ class Api::V2::SalesController < Api::V2::BaseController
before_action(only: [:revoke_access, :undo_revoke_access]) { require_oauth_scope! :edit_sales }
before_action :set_page, only: :index

RESULTS_PER_PAGE = 10
# Each page costs the caller one HTTP request per RESULTS_PER_PAGE sales, and the callers that
# read a whole history (the CLI's `sales buyers`/`sales list --all`, the dashboard's customer
# list, any script looping `next_page_key`) pay that in a tight loop. At 10 a seller with ~60k
# sales burned ~6,000 requests to read their own sales, which by itself exhausts the edge's
# 6,000 requests/hour per-IP budget and gets the IP banned with a 1015 mid-walk. 100 matches
# Api::V2::SubscribersController and turns that walk into ~600 requests.
RESULTS_PER_PAGE = 100
# A deprecated `page` becomes the SQL offset `(@page - 1) * RESULTS_PER_PAGE`, and MySQL rejects
# an offset past its 64-bit range. The signed 64-bit ceiling holds on any server version, and the
# offset's `- 1` is why the page ceiling is one above this quotient.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const GetSales = () => (
<ApiParameter name="license_key" description="(optional) - Filter sales by license key" />
<ApiParameter
name="page_key"
description="(optional) - A key representing a page of results. It is given in the response as `next_page_key`."
description="(optional) - A key representing a page of results. It is given in the response as `next_page_key`. Each page holds up to 100 sales."
/>
</ApiParameters>
<ApiResponseFields>
Expand Down
29 changes: 25 additions & 4 deletions spec/controllers/api/v2/sales_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
end

it "serializes a full page without per-sale association lookups" do
# This spec budgets queries per association, not the shipped page size: stub a small page so
# the eleven-row-per-purchase fixtures stay cheap now that a real page is 100 sales.
stub_const("Api::V2::SalesController::RESULTS_PER_PAGE", 4)
purchases_with_upsells = (Api::V2::SalesController::RESULTS_PER_PAGE - 2).times.map do |index|
product = create(:physical_product, user: @seller, name: "Product #{index}")
variant_category = create(:variant_category, link: product, title: "Format")
Expand Down Expand Up @@ -211,7 +214,8 @@
expect(sale_json.keys).to include("preorder_authorization_time", "cancellation_date", "subscription_end_date")
end

it "returns a link to the next page if there are more than 10 sales" do
it "returns a link to the next page if there are more sales than fit on one page" do
stub_const("Api::V2::SalesController::RESULTS_PER_PAGE", 5)
per_page = Api::V2::SalesController::RESULTS_PER_PAGE
create_list(:purchase, per_page, link: @product)
expected_sales = @seller.sales.for_sales_api.order(created_at: :desc, id: :desc).to_a
Expand Down Expand Up @@ -259,7 +263,22 @@
end
end

it "returns the full page the API documents, so a large history costs few requests" do
expect(Api::V2::SalesController::RESULTS_PER_PAGE).to eq(100)
create_list(:purchase, Api::V2::SalesController::RESULTS_PER_PAGE, link: @product)
expected_total = @seller.sales.for_sales_api.count

get :index, params: @params
expect(response.parsed_body["sales"].size).to eq(Api::V2::SalesController::RESULTS_PER_PAGE)
expect(response.parsed_body["next_page_key"]).to be_present

@params[:page_key] = response.parsed_body["next_page_key"]
get :index, params: @params
expect(response.parsed_body["sales"].size).to eq(expected_total - Api::V2::SalesController::RESULTS_PER_PAGE)
end

it "returns the correct link to the next pages from second page onwards" do
stub_const("Api::V2::SalesController::RESULTS_PER_PAGE", 5)
per_page = Api::V2::SalesController::RESULTS_PER_PAGE
create_list(:purchase, (per_page * 3), link: @product)
expected_sales = @seller.sales.for_sales_api.order(created_at: :desc, id: :desc).to_a
Expand Down Expand Up @@ -462,15 +481,17 @@
end

it "accepts the largest page within the signed 64-bit offset range" do
# (922337203685477581 - 1) * 10 fits the signed 64-bit range; the next page's offset does not.
@params.merge!(page: "922337203685477581")
# The ceiling is derived from RESULTS_PER_PAGE, so compute it rather than hardcoding an
# offset that only holds for one page size: MAX_DEPRECATED_PAGE's offset fits the signed
# 64-bit range, the next page's offset does not.
@params.merge!(page: Api::V2::SalesController::MAX_DEPRECATED_PAGE.to_s)
get :index, params: @params

expect(response.code).to eq "200"
end

it "returns a 400 error for the first page past the signed 64-bit offset range" do
@params.merge!(page: "922337203685477582")
@params.merge!(page: (Api::V2::SalesController::MAX_DEPRECATED_PAGE + 1).to_s)
get :index, params: @params
expect(response.code).to eq "400"
expect(response.parsed_body).to eq({
Expand Down
Loading