Files
fizzy/test/controllers/searches_controller_test.rb
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

102 lines
4.1 KiB
Ruby

require "test_helper"
class SearchesControllerTest < ActionDispatch::IntegrationTest
include SearchTestHelper
setup do
@board.update!(all_access: true)
@card = @board.cards.create!(title: "Layout is broken", description: "Look at this mess.", status: "published", creator: @user)
@comment_card = @board.cards.create!(title: "Some card", status: "published", creator: @user)
@comment_card.comments.create!(body: "overflowing text issue", creator: @user)
@comment2_card = @board.cards.create!(title: "Just haggis", description: "More haggis", status: "published", creator: @user)
@comment2_card.comments.create!(body: "I love haggis", creator: @user)
untenanted { sign_in_as @user }
end
test "search" do
# Search query is blank
get search_path(q: "", script_name: "/#{@account.external_account_id}")
assert @query.nil?
# Searching by card title
get search_path(q: "broken", script_name: "/#{@account.external_account_id}")
assert_select "li .search__title", text: /Layout is broken/
assert_select "li .search__excerpt", text: /Look at this mess/
# Searching by comment
get search_path(q: "overflowing", script_name: "/#{@account.external_account_id}")
assert_select "li .search__title", text: /Some card/
assert_select "li .search__excerpt--comment", text: /overflowing text issue/
# Searching for a term that appears in a card and in a comment
get search_path(q: "haggis", script_name: "/#{@account.external_account_id}")
assert_select "li .search__title", text: /Just haggis/, count: 2 # card title shows up in two entries
assert_select "li .search__excerpt", text: /More haggis/ # one entry for the card description
assert_select "li .search__excerpt--comment", text: /I love haggis/ # one entry for the comment
assert_match(/<mark class="circled-text"><span><\/span>haggis<\/mark>/, response.body)
# Searching by card id
get search_path(q: @card.id, script_name: "/#{@account.external_account_id}")
assert_select "form[data-controller='auto-submit']"
# Searching with non-existent card id
get search_path(q: "999999", script_name: "/#{@account.external_account_id}")
assert_select "form[data-controller='auto-submit']", count: 0
assert_select ".search__blank-slate", text: "No matches"
end
test "search as JSON" do
get search_path(q: "broken", script_name: "/#{@account.external_account_id}"), as: :json
assert_response :success
body = @response.parsed_body
assert_kind_of Array, body
assert_equal 1, body.size
assert_equal "Layout is broken", body.first["title"]
end
test "search by card ID as JSON returns array" do
get search_path(q: @card.id, script_name: "/#{@account.external_account_id}"), as: :json
assert_response :success
body = @response.parsed_body
assert_kind_of Array, body
assert_equal 1, body.size
assert_equal @card.id, body.first["id"]
end
test "search as JSON deduplicates cards with multiple search hits" do
get search_path(q: "haggis", script_name: "/#{@account.external_account_id}"), as: :json
assert_response :success
body = @response.parsed_body
assert_kind_of Array, body
assert_equal 1, body.size
assert_equal @comment2_card.id, body.first["id"]
end
test "search highlights matched terms with proper HTML marks" do
@board.cards.create!(title: "Testing search highlighting", status: "published", creator: @user)
get search_path(q: "highlighting", script_name: "/#{@account.external_account_id}")
assert_response :success
end
test "search preserves highlight marks but escapes surrounding HTML" do
@board.cards.create!(
title: "<b>Bold</b> testing content",
status: "published",
creator: @user
)
get search_path(q: "testing", script_name: "/#{@account.external_account_id}")
assert_response :success
# Should escape <b> tags
assert response.body.include?("&lt;b&gt;")
# But should preserve highlight marks around "testing"
assert_match(/<mark class="circled-text"><span><\/span>testing<\/mark>/, response.body)
end
end