@haraka/test-fixtures 0.0.0-stage → 2.0.0

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.
package/index.js ADDED
@@ -0,0 +1,41 @@
1
+ import * as constants from '@haraka/constants'
2
+
3
+ /** @typedef {import('./lib/plugin.js').LoadedPlugin} LoadedPlugin */
4
+ /** @typedef {import('./lib/connection.js').Connection} Connection */
5
+ /** @typedef {import('./lib/transaction.js').Transaction} Transaction */
6
+ /** @typedef {import('./lib/hooks.js').HookResult} HookResult */
7
+ /** @typedef {import('./lib/stub.js').Stub} Stub */
8
+
9
+ // constants become globals on import, so test files can use OK, DENY, ...
10
+ // at module top level (e.g. in a `cases` table) before constructing a Plugin
11
+ constants.import(globalThis)
12
+
13
+ export { default as results, default as result_store } from '@haraka/results'
14
+
15
+ export * as assertions from './lib/assertions.js'
16
+ export * as connection from './lib/connection.js'
17
+ export * as dns from './lib/dns.js'
18
+ export * as helpers from './lib/helpers.js'
19
+ export * as hooks from './lib/hooks.js'
20
+ export * as line_socket from './lib/line_socket.js'
21
+ export { default as logger } from './lib/logger.js'
22
+ export { default as plugin } from './lib/plugin.js'
23
+ export * as transaction from './lib/transaction.js'
24
+
25
+ export { stub } from './lib/stub.js'
26
+
27
+ export { callHook, makePlugin, makeConnection, getResult } from './lib/helpers.js'
28
+ export {
29
+ callConnect,
30
+ callHelo,
31
+ callEhlo,
32
+ callMail,
33
+ callRcpt,
34
+ callRcptOk,
35
+ callData,
36
+ callDataPost,
37
+ callQueue,
38
+ callQueueOk,
39
+ callDisconnect,
40
+ } from './lib/hooks.js'
41
+ export { assertCont, assertOk, assertDeny, assertResult } from './lib/assertions.js'
@@ -0,0 +1,111 @@
1
+ import assert from 'node:assert/strict'
2
+
3
+ import * as constants from '@haraka/constants'
4
+
5
+ const denyCodes = new Set([constants.deny, constants.denysoft, constants.denydisconnect, constants.denysoftdisconnect])
6
+
7
+ // Accept either a raw rc value or the {rc, msg} result object returned by
8
+ // callXxx helpers, so callers can write either:
9
+ // const r = await callMail(...); assertDeny(r, /pattern/)
10
+ // const { rc } = await callMail(...); assertDeny(rc)
11
+ const split = (rcOrResult) => {
12
+ if (rcOrResult && typeof rcOrResult === 'object' && 'rc' in rcOrResult) {
13
+ return { rc: rcOrResult.rc, msg: rcOrResult.msg }
14
+ }
15
+ return { rc: rcOrResult, msg: undefined }
16
+ }
17
+
18
+ const matchMsg = (msg, expected) => {
19
+ if (expected === undefined) return
20
+ const haystack = String(msg ?? '')
21
+ if (expected instanceof RegExp) assert.match(haystack, expected)
22
+ else assert.ok(haystack.includes(expected), `expected msg to include '${expected}', got '${haystack}'`)
23
+ }
24
+
25
+ /**
26
+ * Assert the hook continued (no return code, or explicit CONT).
27
+ *
28
+ * assertCont(rc)
29
+ * assertCont(result)
30
+ *
31
+ * @param {unknown} rcOrResult a return code, or the { rc, msg } from a call helper
32
+ */
33
+ export const assertCont = (rcOrResult) => {
34
+ const { rc } = split(rcOrResult)
35
+ if (rc !== undefined && rc !== constants.cont) {
36
+ assert.fail(`expected CONT (or undefined), got ${rc}`)
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Assert the hook returned OK. Optional msg matcher.
42
+ *
43
+ * assertOk(rc)
44
+ * assertOk(result)
45
+ * assertOk(result, /accepted/)
46
+ *
47
+ * @param {unknown} rcOrResult a return code, or the { rc, msg } from a call helper
48
+ * @param {string | RegExp} [msgMatcher]
49
+ */
50
+ export const assertOk = (rcOrResult, msgMatcher) => {
51
+ const { rc, msg } = split(rcOrResult)
52
+ assert.equal(rc, constants.ok, `expected OK, got ${rc}`)
53
+ matchMsg(msg, msgMatcher)
54
+ }
55
+
56
+ /**
57
+ * Assert the hook denied. By default accepts any DENY* code; pass a
58
+ * specific code (DENY, DENYSOFT, DENYDISCONNECT, DENYSOFTDISCONNECT)
59
+ * to require an exact match.
60
+ *
61
+ * assertDeny(rc)
62
+ * assertDeny(result)
63
+ * assertDeny(result, /no thanks/)
64
+ * assertDeny(result, /no thanks/, DENYSOFT)
65
+ *
66
+ * @param {unknown} rcOrResult a return code, or the { rc, msg } from a call helper
67
+ * @param {string | RegExp} [msgMatcher]
68
+ * @param {number} [code]
69
+ */
70
+ export const assertDeny = (rcOrResult, msgMatcher, code) => {
71
+ const { rc, msg } = split(rcOrResult)
72
+ if (code !== undefined) {
73
+ assert.equal(rc, code, `expected code ${code}, got ${rc}`)
74
+ } else if (!denyCodes.has(rc)) {
75
+ assert.fail(`expected a DENY* code, got ${rc}`)
76
+ }
77
+ matchMsg(msg, msgMatcher)
78
+ }
79
+
80
+ /**
81
+ * Assert that connection.results.get(plugin)[kind] contains an entry
82
+ * matching `matcher` (string substring or RegExp). Without a matcher,
83
+ * just asserts the bucket is non-empty.
84
+ *
85
+ * assertResult(conn, plugin, 'pass')
86
+ * assertResult(conn, plugin, 'pass', /^connect:/)
87
+ * assertResult(conn, 'access', 'fail', 'blacklist')
88
+ *
89
+ * @param {{ results: import('@haraka/results').default }} connection
90
+ * @param {string | { name: string }} pluginOrName
91
+ * @param {string} kind
92
+ * @param {string | RegExp} [matcher]
93
+ */
94
+ export const assertResult = (connection, pluginOrName, kind, matcher) => {
95
+ const name = typeof pluginOrName === 'string' ? pluginOrName : pluginOrName.name
96
+ const result = connection.results.get(name)
97
+ assert.ok(result, `no results for plugin '${name}'`)
98
+ const bucket = result[kind]
99
+ assert.ok(Array.isArray(bucket), `result kind '${kind}' is not an array on plugin '${name}'`)
100
+ if (matcher === undefined) {
101
+ assert.ok(bucket.length, `expected results.${kind} to be non-empty for plugin '${name}'`)
102
+ return
103
+ }
104
+ const found = bucket.some((entry) =>
105
+ matcher instanceof RegExp ? matcher.test(String(entry)) : String(entry).includes(matcher),
106
+ )
107
+ assert.ok(
108
+ found,
109
+ `no entry matching ${matcher} in results.${kind} for plugin '${name}' (got: ${JSON.stringify(bucket)})`,
110
+ )
111
+ }