refactor(webhooks): address PR review on auth-pass

- webhook_receiver: always run hmac.compare_digest (drop the
  `not provided or` short-circuit) so the constant-time path is
  taken regardless of whether the Authorization header is present.
- client/webhooks: modernise the new `auth_data` type hint to
  `dict[str, str] | None` per CLAUDE.md.
- tests/client: rename `test_create_webhook_with_auth_headers` →
  `test_create_webhook_with_static_headers` and use
  `auth_method="header"` (NC's webhook_listeners only supports
  "none" and "header"; the previous "bearer" value was invalid).
- auth/webhook_routes: extract `_register_preset_webhooks` from
  `enable_webhook_preset` so the auth-threading behaviour is
  testable without standing up a Starlette app + auth middleware.
- tests/unit: new test_webhook_routes_register covering the helper
  with secret set / unset, and verifying ids round-trip in order.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-04-30 04:02:26 +02:00
co-authored by Claude Opus 4.7
parent 224428fca5
commit f5f05b7c84
5 changed files with 136 additions and 24 deletions
+11 -8
View File
@@ -135,8 +135,11 @@ async def test_create_webhook_with_filter(webhooks_client, mocker):
@pytest.mark.unit
async def test_create_webhook_with_auth_headers(webhooks_client, mocker):
"""Test creating a webhook with authentication headers."""
async def test_create_webhook_with_static_headers(webhooks_client, mocker):
"""Static request headers (the ``headers`` field) ride on every delivery
independently of ``authData``. NC's webhook_listeners app accepts only
``authMethod="none"`` or ``"header"`` — pre-existing tests previously
referenced ``"bearer"`` which is not a valid value."""
mock_response = mocker.Mock()
mock_response.json.return_value = {
"ocs": {
@@ -144,7 +147,7 @@ async def test_create_webhook_with_auth_headers(webhooks_client, mocker):
"id": 125,
"uri": "http://example.com/webhook",
"event": "OCP\\Files\\Events\\Node\\NodeCreatedEvent",
"authMethod": "bearer",
"authMethod": "header",
}
}
}
@@ -156,17 +159,17 @@ async def test_create_webhook_with_auth_headers(webhooks_client, mocker):
webhook_data = await webhooks_client.create_webhook(
event="OCP\\Files\\Events\\Node\\NodeCreatedEvent",
uri="http://example.com/webhook",
auth_method="bearer",
headers={"Authorization": "Bearer secret-token"},
auth_method="header",
headers={"X-Trace-Id": "trace-123"},
)
assert webhook_data["id"] == 125
assert webhook_data["authMethod"] == "bearer"
assert webhook_data["authMethod"] == "header"
mock_make_request.assert_called_once()
call_args = mock_make_request.call_args
assert call_args[1]["json"]["authMethod"] == "bearer"
assert call_args[1]["json"]["headers"] == {"Authorization": "Bearer secret-token"}
assert call_args[1]["json"]["authMethod"] == "header"
assert call_args[1]["json"]["headers"] == {"X-Trace-Id": "trace-123"}
@pytest.mark.unit