Haraka 3.1.4 → 3.1.5

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.
@@ -26,151 +26,107 @@ const queue_dir = path.resolve(__dirname, '../test-queue')
26
26
 
27
27
  // ── Helpers ───────────────────────────────────────────────────────────────────
28
28
 
29
- const ensureQueueDir = () =>
29
+ const ensureQueueDir = () => fs.promises.mkdir(queue_dir, { recursive: true })
30
+
31
+ const cleanQueueDir = async () => {
32
+ if (!fs.existsSync(queue_dir)) return
33
+ for (const file of fs.readdirSync(queue_dir)) {
34
+ const full = path.resolve(queue_dir, file)
35
+ if (fs.lstatSync(full).isDirectory()) throw new Error(`unexpected subdirectory: ${full}`)
36
+ fs.unlinkSync(full)
37
+ }
38
+ }
39
+
40
+ /** Creates a mock HMailItem, resolving with it or rejecting on error. */
41
+ const mockHMailItem = (ctx, opts = {}) =>
30
42
  new Promise((resolve, reject) => {
31
- fs.exists(queue_dir, (exists) => {
32
- if (exists) return resolve()
33
- fs.mkdir(queue_dir, (err) => (err ? reject(err) : resolve()))
34
- })
43
+ util_hmailitem.newMockHMailItem(ctx, reject, opts, resolve)
35
44
  })
36
45
 
37
- const cleanQueueDir = () =>
46
+ /**
47
+ * Intercepts `HMailItem.prototype[method]`, calls `assertion(this)` when invoked,
48
+ * then triggers the action under test via `trigger()`.
49
+ */
50
+ const interceptAndAssert = (method, assertion, trigger) =>
38
51
  new Promise((resolve, reject) => {
39
- fs.exists(queue_dir, (exists) => {
40
- if (!exists) return resolve()
52
+ const orig = HMailItem.prototype[method]
53
+ HMailItem.prototype[method] = function () {
41
54
  try {
42
- for (const file of fs.readdirSync(queue_dir)) {
43
- const full = path.resolve(queue_dir, file)
44
- if (fs.lstatSync(full).isDirectory()) return reject(new Error(`unexpected subdirectory: ${full}`))
45
- fs.unlinkSync(full)
46
- }
55
+ assertion(this)
47
56
  resolve()
48
- } catch (err) {
49
- reject(err)
57
+ } catch (e) {
58
+ reject(e)
59
+ } finally {
60
+ HMailItem.prototype[method] = orig
50
61
  }
51
- })
52
- })
53
-
54
- /** Creates a mock HMailItem, resolving with it or rejecting on error. */
55
- const mockHMailItem = (ctx, opts = {}) =>
56
- new Promise((resolve, reject) => {
57
- util_hmailitem.newMockHMailItem(ctx, reject, opts, resolve)
62
+ }
63
+ trigger()
58
64
  })
59
65
 
60
66
  // ── Tests ─────────────────────────────────────────────────────────────────────
61
67
 
68
+ // [method, dsn_status, optional setup fn, trigger fn, test name]
69
+ const testCases = [
70
+ {
71
+ name: 'get_mx=DENY triggers bounce with dsn_status 5.1.2',
72
+ method: 'bounce',
73
+ status: '5.1.2',
74
+ setup: (h) => {
75
+ h.domain = h.todo.domain
76
+ },
77
+ trigger: (h) => HMailItem.prototype.get_mx_respond.apply(h, [constants.deny, {}]),
78
+ },
79
+ {
80
+ name: 'get_mx=DENYSOFT triggers temp_fail with dsn_status 4.1.2',
81
+ method: 'temp_fail',
82
+ status: '4.1.2',
83
+ setup: (h) => {
84
+ h.domain = h.todo.domain
85
+ },
86
+ trigger: (h) => HMailItem.prototype.get_mx_respond.apply(h, [constants.denysoft, {}]),
87
+ },
88
+ {
89
+ name: 'get_mx_error({code:NXDOMAIN}) triggers bounce with dsn_status 5.1.2',
90
+ method: 'bounce',
91
+ status: '5.1.2',
92
+ trigger: (h) => HMailItem.prototype.get_mx_error.apply(h, [{ code: dns.NXDOMAIN }]),
93
+ },
94
+ {
95
+ name: "get_mx_error({code:'SOME-OTHER-ERR'}) triggers temp_fail with dsn_status 4.1.0",
96
+ method: 'temp_fail',
97
+ status: '4.1.0',
98
+ trigger: (h) => HMailItem.prototype.get_mx_error.apply(h, [{ code: 'SOME-OTHER-ERR' }, {}]),
99
+ },
100
+ {
101
+ name: 'found_mx with empty exchange triggers bounce with dsn_status 5.1.2',
102
+ method: 'bounce',
103
+ status: '5.1.2',
104
+ trigger: (h) => HMailItem.prototype.found_mx.apply(h, [[{ priority: 0, exchange: '' }]]),
105
+ },
106
+ {
107
+ name: 'try_deliver with empty mxlist triggers temp_fail with dsn_status 5.1.2',
108
+ method: 'temp_fail',
109
+ status: '5.1.2',
110
+ setup: (h) => {
111
+ h.mxlist = []
112
+ },
113
+ trigger: (h) => HMailItem.prototype.try_deliver.apply(h, []),
114
+ },
115
+ ]
116
+
62
117
  describe('outbound_bounce_net_errors', () => {
63
118
  beforeEach(ensureQueueDir)
64
119
  afterEach(cleanQueueDir)
65
120
 
66
- it('get_mx=DENY triggers bounce with dsn_status 5.1.2', async () => {
67
- const mock_hmail = await mockHMailItem(outbound_context)
68
- await new Promise((resolve, reject) => {
69
- const orig = HMailItem.prototype.bounce
70
- HMailItem.prototype.bounce = function (err, opts) {
71
- try {
72
- assert.equal(this.todo.rcpt_to[0].dsn_status, '5.1.2', 'dsn status')
73
- resolve()
74
- } catch (e) {
75
- reject(e)
76
- } finally {
77
- HMailItem.prototype.bounce = orig
78
- }
79
- }
80
- mock_hmail.domain = mock_hmail.todo.domain
81
- HMailItem.prototype.get_mx_respond.apply(mock_hmail, [constants.deny, {}])
82
- })
83
- })
84
-
85
- it('get_mx=DENYSOFT triggers temp_fail with dsn_status 4.1.2', async () => {
86
- const mock_hmail = await mockHMailItem(outbound_context)
87
- await new Promise((resolve, reject) => {
88
- const orig = HMailItem.prototype.temp_fail
89
- HMailItem.prototype.temp_fail = function (err, opts) {
90
- try {
91
- assert.equal(this.todo.rcpt_to[0].dsn_status, '4.1.2', 'dsn status')
92
- resolve()
93
- } catch (e) {
94
- reject(e)
95
- } finally {
96
- HMailItem.prototype.temp_fail = orig
97
- }
98
- }
99
- mock_hmail.domain = mock_hmail.todo.domain
100
- HMailItem.prototype.get_mx_respond.apply(mock_hmail, [constants.denysoft, {}])
101
- })
102
- })
103
-
104
- it('get_mx_error({code:NXDOMAIN}) triggers bounce with dsn_status 5.1.2', async () => {
105
- const mock_hmail = await mockHMailItem(outbound_context)
106
- await new Promise((resolve, reject) => {
107
- const orig = HMailItem.prototype.bounce
108
- HMailItem.prototype.bounce = function (err, opts) {
109
- try {
110
- assert.equal(this.todo.rcpt_to[0].dsn_status, '5.1.2', 'dsn status')
111
- resolve()
112
- } catch (e) {
113
- reject(e)
114
- } finally {
115
- HMailItem.prototype.bounce = orig
116
- }
117
- }
118
- HMailItem.prototype.get_mx_error.apply(mock_hmail, [{ code: dns.NXDOMAIN }])
119
- })
120
- })
121
-
122
- it("get_mx_error({code:'SOME-OTHER-ERR'}) triggers temp_fail with dsn_status 4.1.0", async () => {
123
- const mock_hmail = await mockHMailItem(outbound_context)
124
- await new Promise((resolve, reject) => {
125
- const orig = HMailItem.prototype.temp_fail
126
- HMailItem.prototype.temp_fail = function (err, opts) {
127
- try {
128
- assert.equal(this.todo.rcpt_to[0].dsn_status, '4.1.0', 'dsn status')
129
- resolve()
130
- } catch (e) {
131
- reject(e)
132
- } finally {
133
- HMailItem.prototype.temp_fail = orig
134
- }
135
- }
136
- HMailItem.prototype.get_mx_error.apply(mock_hmail, [{ code: 'SOME-OTHER-ERR' }, {}])
121
+ for (const { name, method, status, setup, trigger } of testCases) {
122
+ it(name, async () => {
123
+ const mock_hmail = await mockHMailItem(outbound_context)
124
+ if (setup) setup(mock_hmail)
125
+ await interceptAndAssert(
126
+ method,
127
+ (h) => assert.equal(h.todo.rcpt_to[0].dsn_status, status, 'dsn_status'),
128
+ () => trigger(mock_hmail),
129
+ )
137
130
  })
138
- })
139
-
140
- it("found_mx(null, [{priority:0,exchange:''}]) triggers bounce with dsn_status 5.1.2", async () => {
141
- const mock_hmail = await mockHMailItem(outbound_context)
142
- await new Promise((resolve, reject) => {
143
- const orig = HMailItem.prototype.bounce
144
- HMailItem.prototype.bounce = function (err, opts) {
145
- try {
146
- assert.equal(this.todo.rcpt_to[0].dsn_status, '5.1.2', 'dsn status')
147
- resolve()
148
- } catch (e) {
149
- reject(e)
150
- } finally {
151
- HMailItem.prototype.bounce = orig
152
- }
153
- }
154
- HMailItem.prototype.found_mx.apply(mock_hmail, [[{ priority: 0, exchange: '' }]])
155
- })
156
- })
157
-
158
- it('try_deliver with empty mxlist triggers temp_fail with dsn_status 5.1.2', async () => {
159
- const mock_hmail = await mockHMailItem(outbound_context)
160
- mock_hmail.mxlist = []
161
- await new Promise((resolve, reject) => {
162
- const orig = HMailItem.prototype.temp_fail
163
- HMailItem.prototype.temp_fail = function (err, opts) {
164
- try {
165
- assert.equal(this.todo.rcpt_to[0].dsn_status, '5.1.2', 'dsn status')
166
- resolve()
167
- } catch (e) {
168
- reject(e)
169
- } finally {
170
- HMailItem.prototype.temp_fail = orig
171
- }
172
- }
173
- HMailItem.prototype.try_deliver.apply(mock_hmail, [])
174
- })
175
- })
131
+ }
176
132
  })