Files
fizzy/test/controllers/webhooks_controller_test.rb
T
Kevin McConnell 59dd8ca549 Make IDs more time-sortable in tests
In order for model ordering to work as expected in tests, we need to
keep two properties:

- Fixtures are all created in the past
- Models sort in the order that they were created

This allows us to do things like this:

    post cards_path, params: { ... }
    created_card = Card.last

When using UUIDv7 PKs rather than sequential integers, we have to make
sure a couple of things happen in order for this still to be true:

- Fixtures should generate deterministic IDs that translate to UUIDs
  that would have been created in the past (i.e. before today)
- Newly created objects must have enough precision in their timestamps
  so that they sort in the order they were created, and their random
  component doesn't come into play.

To solve this, we use the deterministic numeric ID as a number of
milliseconds after an early year. And we ensure that the new timestamps
we create have sub-millisecond precision.
2025-11-17 09:12:40 -05:00

116 lines
2.8 KiB
Ruby

require "test_helper"
class WebhooksControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in_as :kevin
end
test "index" do
get board_webhooks_path(boards(:writebook))
assert_response :success
end
test "show" do
webhook = webhooks(:active)
get board_webhook_path(webhook.board, webhook)
assert_response :success
end
test "new" do
get new_board_webhook_path(boards(:writebook))
assert_response :success
assert_select "form"
end
test "create with valid params" do
board = boards(:writebook)
assert_difference "Webhook.count", 1 do
post board_webhooks_path(board), params: {
webhook: {
name: "Test Webhook",
url: "https://example.com/webhook",
subscribed_actions: [ "", "card_published", "card_closed" ]
}
}
end
webhook = Webhook.last
assert_redirected_to board_webhook_path(webhook.board, webhook)
assert_equal board, webhook.board
assert_equal "Test Webhook", webhook.name
assert_equal "https://example.com/webhook", webhook.url
assert_equal [ "card_published", "card_closed" ], webhook.subscribed_actions
end
test "create with invalid params" do
board = boards(:writebook)
assert_no_difference "Webhook.count" do
post board_webhooks_path(board), params: {
webhook: {
name: "",
url: "invalid-url"
}
}
end
assert_response :unprocessable_entity
end
test "edit" do
webhook = webhooks(:active)
get edit_board_webhook_path(webhook.board, webhook)
assert_response :success
assert_select "form"
end
test "update with valid params" do
webhook = webhooks(:active)
patch board_webhook_path(webhook.board, webhook), params: {
webhook: {
name: "Updated Webhook",
subscribed_actions: [ "card_published" ]
}
}
webhook.reload
assert_redirected_to board_webhook_path(webhook.board, webhook)
assert_equal "Updated Webhook", webhook.name
assert_equal [ "card_published" ], webhook.subscribed_actions
end
test "update with invalid params" do
webhook = webhooks(:active)
patch board_webhook_path(webhook.board, webhook), params: {
webhook: {
name: ""
}
}
assert_response :unprocessable_entity
assert_no_changes -> { webhook.reload.url } do
patch board_webhook_path(webhook.board, webhook), params: {
webhook: {
name: "Updated Webhook",
url: "https://different.com/webhook"
}
}
end
assert_redirected_to board_webhook_path(webhook.board, webhook)
end
test "destroy" do
webhook = webhooks(:active)
assert_difference "Webhook.count", -1 do
delete board_webhook_path(webhook.board, webhook)
end
assert_redirected_to board_webhooks_path(webhook.board)
end
end