mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
REST API v1: tokens, read endpoints, squawks, webhooks
- Personal tokens (Profile → API tokens) act as their owner; integration tokens (Admin → Integrations) reach chosen aircraft and scopes, and their writes name the person they're for (SAM-On-Behalf-Of). Tokens are stored hashed, shown once, revocable, with last use recorded. - GET aircraft, components, due list, inspections, ADs, logbook entries and squawks; POST/PATCH squawks with external_ref links to the other system. - Idempotency-Key for safe retries, paging, JSON errors, rate limits. - Record history keeps the token with each change (in the hashed payload). - Webhooks (squawk created/updated/resolved, entry signed), HMAC-signed, retried, never sent to the server itself; deliveries listed for admins. - The outbound address check is shared by AirTrail and webhooks; reporting a squawk is announced the same way from the form and the API. - docs/guide/api.md: the API reference. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ec0f43019f
commit
bffdea937c
45 files changed
+1577
-51
No files matched your search
@@ -0,0 +1,103 @@
|
||||
require "test_helper"
|
||||
|
||||
# Making and revoking API tokens: personal ones on Profile, integrations
|
||||
# (with their webhooks) under Admin.
|
||||
class ApiManagementTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
@admin = create_user("admin@example.com", admin: true)
|
||||
@pilot = create_user("pilot@example.com")
|
||||
@a = create_aircraft(owner: @admin, pilot: @pilot, tail_number: "N12345")
|
||||
end
|
||||
|
||||
test "a personal token is shown once, listed by its first characters, and can be revoked" do
|
||||
sign_in @pilot
|
||||
get edit_profile_path
|
||||
assert_select "#api form[action='#{api_tokens_path}']"
|
||||
post api_tokens_path, params: { api_token: { name: "My script", scopes: %w[read squawks:write bogus] } }
|
||||
assert_response :success
|
||||
token = @pilot.api_tokens.last
|
||||
shown = css_select("input[readonly]").first["value"]
|
||||
assert_equal token, ApiToken.authenticate(shown)
|
||||
assert_equal %w[read squawks:write], token.scopes
|
||||
|
||||
get edit_profile_path
|
||||
assert_select "#api", text: /My script/
|
||||
assert_no_match shown, response.body, "not shown again"
|
||||
delete api_token_path(token)
|
||||
assert token.reload.revoked?
|
||||
assert_nil ApiToken.authenticate(shown)
|
||||
end
|
||||
|
||||
test "you can't revoke someone else's token" do
|
||||
other = ApiToken.create!(name: "theirs", kind: "personal", user: @admin, scopes: %w[read])
|
||||
sign_in @pilot
|
||||
delete api_token_path(other)
|
||||
assert_response :not_found
|
||||
assert_not other.reload.revoked?
|
||||
end
|
||||
|
||||
test "an admin makes an integration with a webhook; the token and secret are shown once" do
|
||||
sign_in @admin
|
||||
get admin_integrations_path
|
||||
assert_response :success
|
||||
post admin_integrations_path, params: { api_token: { name: "Redmine", scopes: %w[read squawks:write], aircraft_ids: [ @a.id ] },
|
||||
webhook: { url: "https://redmine.example.com/sam", events: %w[squawk.created entry.signed] } }
|
||||
assert_response :success
|
||||
integration = ApiToken.integration.last
|
||||
assert_equal [ "Redmine", [ @a.id ], @admin ], [ integration.name, integration.aircraft_ids, integration.user ]
|
||||
token, secret = css_select("input[readonly]").map { _1["value"] }
|
||||
assert_equal integration, ApiToken.authenticate(token)
|
||||
endpoint = integration.webhook_endpoints.sole
|
||||
assert_equal [ secret, %w[squawk.created entry.signed] ], [ endpoint.secret, endpoint.events ]
|
||||
|
||||
get admin_integration_path(integration)
|
||||
assert_response :success
|
||||
assert_no_match token, response.body
|
||||
assert_no_match secret, response.body
|
||||
end
|
||||
|
||||
test "editing an integration changes its reach and webhook; a new secret replaces the old" do
|
||||
integration = ApiToken.create!(name: "Redmine", kind: "integration", user: @admin, scopes: %w[read], aircraft_ids: [])
|
||||
sign_in @admin
|
||||
patch admin_integration_path(integration), params: { api_token: { name: "Redmine", scopes: %w[read], aircraft_ids: [ @a.id ] },
|
||||
webhook: { url: "https://redmine.example.com/sam", events: %w[squawk.resolved] } }
|
||||
assert_redirected_to admin_integration_path(integration)
|
||||
assert_equal [ @a.id ], integration.reload.aircraft_ids
|
||||
old = integration.webhook_endpoints.sole.secret
|
||||
post new_secret_admin_integration_path(integration)
|
||||
assert_not_equal old, integration.webhook_endpoints.sole.reload.secret
|
||||
patch admin_integration_path(integration), params: { api_token: { name: "Redmine", scopes: %w[read], aircraft_ids: [ @a.id ] }, webhook: { url: "" } }
|
||||
assert_empty integration.webhook_endpoints.reload, "clearing the address removes the webhook"
|
||||
end
|
||||
|
||||
test "an invalid integration shows the form again" do
|
||||
sign_in @admin
|
||||
post admin_integrations_path, params: { api_token: { name: "", scopes: [] }, webhook: { url: "not a url", events: [] } }
|
||||
assert_response :unprocessable_entity
|
||||
assert_equal 0, ApiToken.count
|
||||
end
|
||||
|
||||
test "revoking an integration stops its token and webhook" do
|
||||
integration = ApiToken.create!(name: "Redmine", kind: "integration", user: @admin, scopes: %w[read], aircraft_ids: [ @a.id ])
|
||||
endpoint = integration.webhook_endpoints.create!(url: "https://redmine.example.com/", secret: "s" * 32, events: %w[squawk.created])
|
||||
sign_in @admin
|
||||
delete admin_integration_path(integration)
|
||||
assert integration.reload.revoked?
|
||||
assert_not endpoint.reload.enabled
|
||||
end
|
||||
|
||||
test "integrations are for admins only" do
|
||||
sign_in @pilot
|
||||
get admin_integrations_path
|
||||
assert_redirected_to root_path
|
||||
post admin_integrations_path, params: { api_token: { name: "x", scopes: %w[read] } }
|
||||
assert_equal 0, ApiToken.count
|
||||
end
|
||||
|
||||
test "the API reference is in the user guide" do
|
||||
sign_in @pilot
|
||||
get help_page_path("api")
|
||||
assert_response :success
|
||||
assert_select "h1", "REST API"
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user