Files
fizzy/test/controllers/cards_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

296 lines
8.8 KiB
Ruby

require "test_helper"
class CardsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in_as :kevin
end
test "index" do
get cards_path
assert_response :success
end
test "filtered index" do
get cards_path(filters(:jz_assignments).as_params.merge(term: "haggis"))
assert_response :success
end
test "create a new draft" do
assert_difference -> { Card.count }, 1 do
post board_cards_path(boards(:writebook))
end
card = Card.last
assert_redirected_to card_draft_path(card)
assert card.drafted?
end
test "create resumes existing draft if it exists" do
draft = boards(:writebook).cards.create!(creator: users(:kevin), status: :drafted)
assert_no_difference -> { Card.count } do
post board_cards_path(boards(:writebook))
assert_redirected_to card_draft_path(draft)
end
end
test "show redirects to draft when card is drafted" do
card = boards(:writebook).cards.create!(creator: users(:kevin), status: :drafted)
get card_path(card)
assert_redirected_to card_draft_path(card)
end
test "show renders inline code in title" do
card = cards(:logo)
card.update_column :title, "Fix the `bug` in production"
get card_path(card)
assert_select ".card__title-link" do |element|
assert_equal "Fix the <code>bug</code> in production", element.inner_html
end
end
test "edit" do
get edit_card_path(cards(:logo))
assert_response :success
end
test "edit card with invalid attachments in description" do
card = cards(:logo)
card.update! description: <<~HTML
<action-text-attachment sgid="gid://fizzy/Card/nonexistent" content-type="application/octet-stream"></action-text-attachment>
HTML
get edit_card_path(card)
assert_response :success
end
test "update" do
patch card_path(cards(:logo)), as: :turbo_stream, params: {
card: {
title: "Logo needs to change",
image: fixture_file_upload("moon.jpg", "image/jpeg"),
description: "Something more in-depth" } }
assert_response :success
card = cards(:logo).reload
assert_equal "Logo needs to change", card.title
assert_equal "moon.jpg", card.image.filename.to_s
assert_equal "Something more in-depth", card.description.to_plain_text.strip
end
test "update draft card does not render reactions" do
draft = boards(:writebook).cards.create!(creator: users(:kevin), status: :drafted)
patch card_path(draft), as: :turbo_stream, params: {
card: { image: fixture_file_upload("moon.jpg", "image/jpeg") }
}
assert_response :success
assert_no_match "reactions", response.body, "Draft card should not show reactions/boost button"
end
test "users can only see cards in boards they have access to" do
get card_path(cards(:logo))
assert_response :success
boards(:writebook).update! all_access: false
boards(:writebook).accesses.revoke_from users(:kevin)
get card_path(cards(:logo))
assert_response :not_found
end
test "admins can see delete button on any card" do
get card_path(cards(:logo))
assert_response :success
assert_match "Delete this card", response.body
end
test "card creators can see delete button on their own cards" do
logout_and_sign_in_as :david
get card_path(cards(:logo))
assert_response :success
assert_match "Delete this card", response.body
end
test "non-admins cannot see delete button on cards they did not create" do
logout_and_sign_in_as :jz
get card_path(cards(:logo))
assert_response :success
assert_no_match "Delete this card", response.body
end
test "non-admins cannot delete cards they did not create" do
logout_and_sign_in_as :jz
assert_no_difference -> { Card.count } do
delete card_path(cards(:logo))
end
assert_response :forbidden
end
test "card creators can delete their own cards" do
logout_and_sign_in_as :david
assert_difference -> { Card.count }, -1 do
delete card_path(cards(:logo))
end
assert_redirected_to boards(:writebook)
end
test "admins can delete any card" do
assert_difference -> { Card.count }, -1 do
delete card_path(cards(:logo))
end
assert_redirected_to boards(:writebook)
end
test "show card with comment containing malformed remote image attachment" do
card = cards(:logo)
card.comments.create! \
creator: users(:kevin),
body: '<action-text-attachment url="image.png" content-type="image/*" presentation="gallery"></action-text-attachment>'
get card_path(card)
assert_response :success
end
test "show as JSON" do
card = cards(:logo)
card.steps.create!(content: "First step")
card.steps.create!(content: "Second step", completed: true)
get card_path(card), as: :json
assert_response :success
assert_equal card.title, @response.parsed_body["title"]
assert_equal card.closed?, @response.parsed_body["closed"]
assert_equal card.postponed?, @response.parsed_body["postponed"]
assert_equal 2, @response.parsed_body["steps"].size
assert_equal card_comments_url(card), @response.parsed_body["comments_url"]
assert_equal card_reactions_url(card), @response.parsed_body["reactions_url"]
end
test "create as JSON" do
assert_difference -> { Card.count }, +1 do
post board_cards_path(boards(:writebook)),
params: { card: { title: "My new card", description: "Big if true" } },
as: :json
assert_response :created
end
card = Card.last
assert_equal card_path(card, format: :json), @response.headers["Location"]
assert_equal "My new card", @response.parsed_body["title"]
assert_equal "My new card", card.title
assert_equal "Big if true", card.description.to_plain_text
end
test "create as JSON with custom created_at" do
custom_time = Time.utc(2024, 1, 15, 10, 30, 0)
assert_difference -> { Card.count }, +1 do
post board_cards_path(boards(:writebook)),
params: { card: { title: "Backdated card", created_at: custom_time } },
as: :json
assert_response :created
end
assert_equal custom_time, Card.last.created_at
end
test "create as JSON with custom last_active_at" do
created_time = Time.utc(2024, 1, 15, 10, 30, 0)
last_active_time = Time.utc(2024, 6, 1, 12, 0, 0)
assert_difference -> { Card.count }, +1 do
post board_cards_path(boards(:writebook)),
params: { card: { title: "Card with activity", created_at: created_time, last_active_at: last_active_time } },
as: :json
assert_response :created
end
card = Card.last
assert_equal created_time, card.created_at
assert_equal last_active_time, card.last_active_at
end
test "create as JSON defaults last_active_at to created_at when not provided" do
created_time = Time.utc(2024, 1, 15, 10, 30, 0)
assert_difference -> { Card.count }, +1 do
post board_cards_path(boards(:writebook)),
params: { card: { title: "Backdated card without last_active_at", created_at: created_time } },
as: :json
assert_response :created
end
card = Card.last
assert_equal created_time, card.created_at
assert_equal created_time, card.last_active_at
end
test "update as JSON with custom last_active_at" do
card = cards(:logo)
custom_time = Time.utc(2024, 3, 15, 14, 0, 0)
put card_path(card, format: :json), params: { card: { last_active_at: custom_time } }
assert_response :success
assert_equal custom_time, card.reload.last_active_at
end
test "update as JSON can restore last_active_at after comments overwrite it" do
created_time = Time.utc(2024, 1, 15, 10, 30, 0)
last_active_time = Time.utc(2024, 6, 1, 12, 0, 0)
# Create a card with custom timestamps (simulating import)
post board_cards_path(boards(:writebook)),
params: { card: { title: "Imported card", created_at: created_time, last_active_at: last_active_time } },
as: :json
assert_response :created
card = Card.last
# Adding a comment overwrites last_active_at (this is expected)
card.comments.create!(creator: users(:kevin), body: "Imported comment")
assert_not_equal last_active_time, card.reload.last_active_at
# After import, restore the correct last_active_at
put card_path(card, format: :json), params: { card: { last_active_at: last_active_time } }
assert_response :success
assert_equal last_active_time, card.reload.last_active_at
end
test "update as JSON" do
card = cards(:logo)
put card_path(card, format: :json), params: { card: { title: "Update test" } }
assert_response :success
assert_equal "Update test", card.reload.title
end
test "delete as JSON" do
card = cards(:logo)
delete card_path(card, format: :json)
assert_response :no_content
assert_not Card.exists?(card.id)
end
end