Files
fizzy/test/controllers/accounts/exports_controller_test.rb
T
Jeremy Daer 723c8180b4 Add JSON response format for full SDK/CLI coverage (#2675)
* Add JSON response format to void-response controller actions

Add respond_to blocks with JSON format to 14 controllers that
previously only rendered HTML or Turbo Stream responses. JSON
callers get head :no_content (or :created/:ok for push
subscriptions, distinguishing new vs existing).

Covers: board involvements, board/account entropies, column
reordering, card readings, card publishing, user roles, user
avatars, account settings, notification settings, join codes
(with 422 error branch), and push subscriptions.

* Add steps index endpoint with JSON view

Add index action to Cards::StepsController so SDK/CLI callers
can list all steps for a card. Reuses the existing _step.json
partial.

* Add JSON views for paginated card list endpoints

Add show.json.jbuilder for stream, not-now, and closed column
endpoints. No controller changes needed — set_page_and_extract_
portion_from and fresh_when already work format-agnostically.

* Add JSON search endpoint with distinct-card pagination

JSON search paginates distinct Card records (via the existing
Card.mentioning scope with .distinct) rather than Search::Record
objects. This ensures page boundaries, Link headers, and counts
reflect unique cards — no short pages from post-pagination dedup.

ID-match searches return a uniform single-element Card[] array
through the same pagination path.

* Add JSON views for settings and configuration endpoints

Add show.json.jbuilder for account settings (name), join codes
(code, usage_count, usage_limit, url, active), and notification
settings (bundle_email_frequency). Tests for show and update
were added in the void-response commit.

* Add JSON response format to data exports

Create returns 201 with export id, status, and created_at so
callers can poll. Show returns any-status exports for JSON (not
just completed) with a download_url when ready, or 404 for
missing/other-user exports.

* Extend access token JSON API

Add index.json and _access_token.json partial for listing tokens.
Add JSON format to destroy. Include id and created_at in the
create response alongside the existing token/description/permission
fields.

* Fix ambiguous precedence warning in export JSON view

* Assert X-Total-Count pagination header in streams JSON test

* Address review feedback

- Guard steps index against HTML requests (redirect to card)
- Normalize created_at to UTC in access token and export JSON
- Add deterministic ordering (.latest) to search JSON pagination

* Return 406 for HTML requests to steps index instead of redirect

* Add wrap_parameters for flat SDK JSON payload compatibility

Five controllers infer the wrong wrapper key from their class
name, so flat JSON bodies like {"role":"admin"} fail with 400.
Add explicit wrap_parameters declarations:

- Users::RolesController → :user
- Notifications::SettingsController → :user_settings
- Account::JoinCodesController → :account_join_code
- Account::SettingsController → :account
- Boards::EntropiesController → :board

Add integration tests that send flat (unwrapped) JSON payloads
for all seven param-bearing endpoints to prove SDK compatibility.

* Address PR review feedback

- Use 201 Created (not 204) for all create actions: publishes, readings,
  left/right positions, push subscriptions
- Simplify push subscription to always return :created (no 200/201 variance)
- Remove superfluous respond_to block from steps#index
- Merge exports#show into single respond_to block
- Move export download_url conditional out of inline args
- Ensure join code active is explicitly boolean
- Remove extra parens from search controller assignment
- Add blank lines between format blocks for readability

* Return JSON bodies on create actions

Create actions for boards, cards, columns, comments, and steps now
render the resource as JSON (via their show views) instead of returning
empty 201 responses with only a Location header. SDKs expect a JSON
body they can deserialize into the created resource.

* Return JSON bodies on reaction create actions

Card and comment reaction creates now render the reaction as JSON
instead of returning empty 201 responses, consistent with the other
resource-creating endpoints.

* Allow access tokens API without account scope

Access tokens are identity-level (not account-scoped), so the
controller needs to work with bearer token auth and no account slug
in the URL. Skip require_account so the bearer token auth path
can proceed.

Also fix involvement test to send params in JSON body (not query
string) to match real SDK usage.
2026-03-09 11:39:49 -07:00

140 lines
3.8 KiB
Ruby

require "test_helper"
class Account::ExportsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in_as :jason
end
test "create creates an export record and enqueues job" do
assert_difference -> { Account::Export.count }, 1 do
assert_enqueued_with(job: DataExportJob) do
post account_exports_path
end
end
assert_redirected_to account_settings_path
assert_equal "Export started. You'll receive an email when it's ready.", flash[:notice]
end
test "create associates export with current user" do
post account_exports_path
export = Account::Export.last
assert_equal users(:jason), export.user
assert_equal Current.account, export.account
assert export.pending?
end
test "create rejects request when current export limit is reached" do
Account::ExportsController::CURRENT_EXPORT_LIMIT.times do
Account::Export.create!(account: Current.account, user: users(:jason))
end
assert_no_difference -> { Account::Export.count } do
post account_exports_path
end
assert_response :too_many_requests
end
test "create allows request when exports are older than one day" do
Account::ExportsController::CURRENT_EXPORT_LIMIT.times do
Account::Export.create!(account: Current.account, user: users(:jason), created_at: 2.days.ago)
end
assert_difference -> { Account::Export.count }, 1 do
post account_exports_path
end
assert_redirected_to account_settings_path
end
test "show displays completed export with download link" do
export = Account::Export.create!(account: Current.account, user: users(:jason))
export.build
get account_export_path(export)
assert_response :success
assert_select "a#download-link"
end
test "show displays a warning if the export is missing" do
get account_export_path("not-really-an-export")
assert_response :success
assert_select "h2", "Download Expired"
end
test "show does not allow access to another user's export" do
export = Account::Export.create!(account: Current.account, user: users(:kevin))
export.build
get account_export_path(export)
assert_response :success
assert_select "h2", "Download Expired"
end
test "create as JSON" do
assert_difference -> { Account::Export.count }, 1 do
assert_enqueued_with(job: DataExportJob) do
post account_exports_path, as: :json
end
end
assert_response :created
body = @response.parsed_body
assert body["id"].present?
assert_equal "pending", body["status"]
assert_nil body["download_url"]
end
test "show as JSON with completed export" do
export = Account::Export.create!(account: Current.account, user: users(:jason))
export.build
get account_export_path(export), as: :json
assert_response :success
body = @response.parsed_body
assert_equal export.id, body["id"]
assert_equal "completed", body["status"]
assert body["download_url"].present?
end
test "show as JSON with pending export" do
export = Account::Export.create!(account: Current.account, user: users(:jason))
get account_export_path(export), as: :json
assert_response :success
body = @response.parsed_body
assert_equal "pending", body["status"]
assert_nil body["download_url"]
end
test "show as JSON with missing export" do
get account_export_path("nonexistent"), as: :json
assert_response :not_found
end
test "create is forbidden for non-admin members" do
logout_and_sign_in_as :david
post account_exports_path
assert_response :forbidden
end
test "show is forbidden for non-admin members" do
logout_and_sign_in_as :david
export = Account::Export.create!(account: Current.account, user: users(:jason))
export.build
get account_export_path(export)
assert_response :forbidden
end
end