@tellescope/sdk 1.255.8 → 1.255.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/lib/cjs/enduser.d.ts +1 -0
  2. package/lib/cjs/enduser.d.ts.map +1 -1
  3. package/lib/cjs/sdk.d.ts +3 -2
  4. package/lib/cjs/sdk.d.ts.map +1 -1
  5. package/lib/cjs/tests/api_tests/webhook_error_handling.test.d.ts +6 -0
  6. package/lib/cjs/tests/api_tests/webhook_error_handling.test.d.ts.map +1 -0
  7. package/lib/cjs/tests/api_tests/webhook_error_handling.test.js +324 -0
  8. package/lib/cjs/tests/api_tests/webhook_error_handling.test.js.map +1 -0
  9. package/lib/cjs/tests/tests.d.ts.map +1 -1
  10. package/lib/cjs/tests/tests.js +156 -152
  11. package/lib/cjs/tests/tests.js.map +1 -1
  12. package/lib/esm/sdk.d.ts +2 -2
  13. package/lib/esm/tests/api_tests/custom_dashboards.test.d.ts.map +1 -1
  14. package/lib/esm/tests/api_tests/custom_dashboards.test.js +259 -33
  15. package/lib/esm/tests/api_tests/custom_dashboards.test.js.map +1 -1
  16. package/lib/esm/tests/api_tests/medication_added_trigger.test.d.ts.map +1 -1
  17. package/lib/esm/tests/api_tests/medication_added_trigger.test.js +206 -136
  18. package/lib/esm/tests/api_tests/medication_added_trigger.test.js.map +1 -1
  19. package/lib/esm/tests/api_tests/security/F-0005-ai-conversations-rbac.test.d.ts +10 -0
  20. package/lib/esm/tests/api_tests/security/F-0005-ai-conversations-rbac.test.d.ts.map +1 -1
  21. package/lib/esm/tests/api_tests/security/F-0005-ai-conversations-rbac.test.js +184 -41
  22. package/lib/esm/tests/api_tests/security/F-0005-ai-conversations-rbac.test.js.map +1 -1
  23. package/lib/esm/tests/api_tests/webhook_error_handling.test.d.ts +6 -0
  24. package/lib/esm/tests/api_tests/webhook_error_handling.test.d.ts.map +1 -0
  25. package/lib/esm/tests/api_tests/webhook_error_handling.test.js +317 -0
  26. package/lib/esm/tests/api_tests/webhook_error_handling.test.js.map +1 -0
  27. package/lib/esm/tests/tests.d.ts.map +1 -1
  28. package/lib/esm/tests/tests.js +181 -165
  29. package/lib/esm/tests/tests.js.map +1 -1
  30. package/lib/tsconfig.tsbuildinfo +1 -1
  31. package/package.json +8 -8
  32. package/src/tests/api_tests/webhook_error_handling.test.ts +190 -0
  33. package/src/tests/tests.ts +2 -0
  34. package/test_generated.pdf +0 -0
@@ -0,0 +1,190 @@
1
+ require('source-map-support').install();
2
+
3
+ import express from "express"
4
+ import bodyParser from "body-parser"
5
+ import crypto from "crypto"
6
+ import http from "http"
7
+
8
+ import { Session } from "../../sdk"
9
+ import {
10
+ assert,
11
+ async_test,
12
+ log_header,
13
+ wait,
14
+ } from "@tellescope/testing"
15
+ import { setup_tests } from "../setup"
16
+
17
+ const host = process.env.API_URL || 'http://localhost:8080' as const
18
+
19
+ const TEST_SECRET = "this is a test secret for verifying integrity of web hooks"
20
+ const sha256 = (s: string) => crypto.createHash('sha256').update(s).digest('hex')
21
+
22
+ // DNS resolution for the reserved .invalid TLD always fails => axios ENOTFOUND (no err.response)
23
+ const ENOTFOUND_URL = `http://webhook-error-handling-test.invalid/hook`
24
+
25
+ const POLL_TIMEOUT_MS = 10_000
26
+ const POLL_INTERVAL_MS = 500
27
+
28
+ const find_log_for_url = async (sdk: Session, url: string) => {
29
+ const start = Date.now()
30
+ while (Date.now() - start < POLL_TIMEOUT_MS) {
31
+ const logs = await sdk.api.webhook_logs.getSome({ limit: 50, sort: 'newFirst' as any }) as Array<any>
32
+ const match = logs.find(l => l.url === url)
33
+ if (match) return match
34
+ await wait(undefined, POLL_INTERVAL_MS)
35
+ }
36
+ return null
37
+ }
38
+
39
+ const get_failure_count = async (sdk: Session) => {
40
+ const configs = await sdk.api.webhooks.getSome() as Array<any>
41
+ return (configs[0]?.deliveryFailureCount ?? 0) as number
42
+ }
43
+
44
+ // the $inc on the webhook config is fire-and-forget — poll until it lands
45
+ const wait_for_failure_count = async (sdk: Session, expected: number) => {
46
+ const start = Date.now()
47
+ let count = -1
48
+ while (Date.now() - start < POLL_TIMEOUT_MS) {
49
+ count = await get_failure_count(sdk)
50
+ if (count === expected) return count
51
+ await wait(undefined, POLL_INTERVAL_MS)
52
+ }
53
+ return count
54
+ }
55
+
56
+ // Main test function that can be called independently
57
+ export const webhook_error_handling_tests = async ({ sdk, sdkNonAdmin } : { sdk: Session, sdkNonAdmin: Session }) => {
58
+ log_header("Webhook Error Handling Tests")
59
+
60
+ // local receiver: error paths for failure scenarios, a healthy path that verifies integrity
61
+ const app = express()
62
+ app.use(bodyParser.json({ limit: '25mb' }))
63
+
64
+ let successIntegrityVerified = false
65
+ app.post('/fail-400', (_, res) => { res.status(400).json({ error: 'bad request' }) })
66
+ app.post('/fail-500', (_, res) => { res.status(500).json({ error: 'server error' }) })
67
+ app.post('/success', (req, res) => {
68
+ const { records, timestamp, integrity } = req.body
69
+ successIntegrityVerified = (
70
+ sha256((records ?? []).map((r: { id: string }) => r.id).join('') + timestamp + TEST_SECRET) === integrity
71
+ )
72
+ res.status(200).json({})
73
+ })
74
+
75
+ const server: http.Server = await new Promise(resolve => {
76
+ const s = app.listen(0, '127.0.0.1', () => resolve(s))
77
+ })
78
+ const receiverBase = `http://127.0.0.1:${(server.address() as any).port}`
79
+
80
+ const enduserIds: string[] = []
81
+ const trigger_delivery = async (label: string) => {
82
+ const enduser = await sdk.api.endusers.createOne({ email: `webhook-error-${label}-${Date.now()}@tellescope.com` })
83
+ enduserIds.push(enduser.id)
84
+ }
85
+
86
+ try {
87
+ await async_test(
88
+ 'configure webhook',
89
+ () => sdk.api.webhooks.configure({ url: ENOTFOUND_URL, secret: TEST_SECRET }),
90
+ { shouldError: false, onResult: () => true },
91
+ )
92
+ await sdk.api.webhooks.update({ subscriptionUpdates: { endusers: { create: true, update: false, delete: false } } })
93
+
94
+ const initialCount = await get_failure_count(sdk)
95
+
96
+ // Scenario 1: ENOTFOUND — no HTTP response at all
97
+ await trigger_delivery('enotfound')
98
+ const enotfoundLog = await find_log_for_url(sdk, ENOTFOUND_URL)
99
+ assert(!!enotfoundLog, 'no webhook_logs entry for ENOTFOUND delivery', 'ENOTFOUND failure logged to webhook_logs')
100
+ assert(
101
+ typeof enotfoundLog?.response === 'string' && enotfoundLog.response.length > 0,
102
+ `expected string error message response, got: ${JSON.stringify(enotfoundLog?.response)}`,
103
+ 'ENOTFOUND log has error message as response'
104
+ )
105
+ assert(enotfoundLog?.responseCode === undefined, 'unexpected responseCode for ENOTFOUND', 'ENOTFOUND log has no responseCode')
106
+ assert(
107
+ await wait_for_failure_count(sdk, initialCount + 1) === initialCount + 1,
108
+ 'deliveryFailureCount not incremented after ENOTFOUND',
109
+ 'deliveryFailureCount incremented after ENOTFOUND'
110
+ )
111
+
112
+ // Scenario 2a: HTTP 400 from the receiver
113
+ await sdk.api.webhooks.update({ url: `${receiverBase}/fail-400` })
114
+ await trigger_delivery('http400')
115
+ const log400 = await find_log_for_url(sdk, `${receiverBase}/fail-400`)
116
+ assert(log400?.responseCode === 400, `expected responseCode 400, got ${log400?.responseCode}`, 'HTTP 400 responseCode recorded')
117
+ assert(
118
+ await wait_for_failure_count(sdk, initialCount + 2) === initialCount + 2,
119
+ 'deliveryFailureCount not incremented after HTTP 400',
120
+ 'deliveryFailureCount incremented after HTTP 400'
121
+ )
122
+
123
+ // Scenario 2b: HTTP 500 from the receiver
124
+ await sdk.api.webhooks.update({ url: `${receiverBase}/fail-500` })
125
+ await trigger_delivery('http500')
126
+ const log500 = await find_log_for_url(sdk, `${receiverBase}/fail-500`)
127
+ assert(log500?.responseCode === 500, `expected responseCode 500, got ${log500?.responseCode}`, 'HTTP 500 responseCode recorded')
128
+ assert(
129
+ await wait_for_failure_count(sdk, initialCount + 3) === initialCount + 3,
130
+ 'deliveryFailureCount not incremented after HTTP 500',
131
+ 'deliveryFailureCount incremented after HTTP 500'
132
+ )
133
+
134
+ // Scenario 3 (timeout): skipped — WEBHOOK_TIMEOUT_MS is a constant (15s), not env-overridable,
135
+ // and a 15s+ non-responding receiver would make this suite unacceptably slow.
136
+ // Covered by security/F-0155-webhook-timeout.test.ts via send_automation_webhook.
137
+
138
+ // Scenario 4: success path unchanged — 200 logged, counter untouched
139
+ await sdk.api.webhooks.update({ url: `${receiverBase}/success` })
140
+ await trigger_delivery('success')
141
+ const successLog = await find_log_for_url(sdk, `${receiverBase}/success`)
142
+ assert(successLog?.responseCode === 200, `expected responseCode 200, got ${successLog?.responseCode}`, 'success responseCode recorded')
143
+ assert(successIntegrityVerified, 'integrity check failed on success delivery', 'success delivery integrity verified')
144
+ await wait(undefined, 1000) // allow any (erroneous) counter update to land before checking
145
+ assert(
146
+ await get_failure_count(sdk) === initialCount + 3,
147
+ 'deliveryFailureCount changed on successful delivery',
148
+ 'deliveryFailureCount untouched on success'
149
+ )
150
+
151
+ // deliveryFailureCount is server-managed: not settable through the public update endpoint
152
+ const configs = await sdk.api.webhooks.getSome() as Array<any>
153
+ const webhookId = configs[0]?.id
154
+ await sdk.api.webhooks.updateOne(webhookId, { deliveryFailureCount: 9999 } as any).catch(() => {}) // may reject (readonly)
155
+ assert(
156
+ await get_failure_count(sdk) === initialCount + 3,
157
+ 'customer was able to overwrite deliveryFailureCount',
158
+ 'deliveryFailureCount not customer-settable'
159
+ )
160
+ } finally {
161
+ // stop deliveries for subsequent tests and shut down the local receiver
162
+ await sdk.api.webhooks.update({ subscriptionUpdates: { endusers: { create: false, update: false, delete: false } } }).catch(() => {})
163
+ server.close()
164
+ for (const id of enduserIds) {
165
+ try { await sdk.api.endusers.deleteOne(id) } catch {}
166
+ }
167
+ }
168
+ }
169
+
170
+ // Allow running this test file independently
171
+ if (require.main === module) {
172
+ console.log(`🌐 Using API URL: ${host}`)
173
+ const sdk = new Session({ host })
174
+ const sdkNonAdmin = new Session({ host })
175
+
176
+ const runTests = async () => {
177
+ await setup_tests(sdk, sdkNonAdmin)
178
+ await webhook_error_handling_tests({ sdk, sdkNonAdmin })
179
+ }
180
+
181
+ runTests()
182
+ .then(() => {
183
+ console.log("✅ Webhook error handling test suite completed successfully")
184
+ process.exit(0)
185
+ })
186
+ .catch((error) => {
187
+ console.error("❌ Webhook error handling test suite failed:", error)
188
+ process.exit(1)
189
+ })
190
+ }
@@ -136,6 +136,7 @@ import { date_string_validation_tests } from "./api_tests/date_string_validation
136
136
  import { enduser_session_invalidation_tests } from "./api_tests/enduser_session_invalidation.test";
137
137
  import { enduser_cross_access_isolation_tests } from "./api_tests/enduser_cross_access_isolation.test";
138
138
  import { calendar_event_webhook_template_tests } from "./api_tests/calendar_event_webhook_template.test";
139
+ import { webhook_error_handling_tests } from "./api_tests/webhook_error_handling.test";
139
140
 
140
141
  const UniquenessViolationMessage = 'Uniqueness Violation'
141
142
 
@@ -15054,6 +15055,7 @@ const ip_address_form_tests = async () => {
15054
15055
  await invite_user_enumeration_tests({ sdk, sdkNonAdmin })
15055
15056
  await handle_incoming_communication_cross_tenant_tests({ sdk, sdkNonAdmin })
15056
15057
  await calendar_event_webhook_template_tests({ sdk, sdkNonAdmin })
15058
+ await webhook_error_handling_tests({ sdk, sdkNonAdmin })
15057
15059
  await outbound_chat_sent_trigger_tests({ sdk })
15058
15060
  await enduser_login_rate_limits_tests({ sdk, sdkNonAdmin })
15059
15061
  await data_sync_redaction_bypass_tests({ sdk, sdkNonAdmin })
Binary file