|
1 | | -import { test } from "node:test"; |
2 | | -import { properties, RULE_IDS } from "../lib/const.ts"; |
3 | | -import { actorPropertyRequired } from "../lib/messages.ts"; |
4 | | -import { createKeyRequiredEdgeCaseTests } from "../lib/test-templates.ts"; |
5 | | -import lintTest from "../lib/test.ts"; |
| 1 | +import { RULE_IDS } from "../lib/const.ts"; |
| 2 | +import { |
| 3 | + createRequiredDispatcherRuleTests, |
| 4 | + createRequiredEdgeCaseTests, |
| 5 | + runTests, |
| 6 | +} from "../lib/test-templates.ts"; |
6 | 7 | import * as rule from "../rules/actor-assertion-method-required.ts"; |
7 | 8 |
|
8 | 9 | const ruleName = RULE_IDS.actorAssertionMethodRequired; |
9 | | - |
10 | | -test( |
11 | | - `${ruleName}: ✅ Good - \`setActorDispatcher\` called on non-Federation object`, |
12 | | - lintTest({ |
13 | | - code: ` |
14 | | - federation.setActorDispatcher("/users/{identifier}", async (ctx, identifier) => { |
15 | | - return new Person({ |
16 | | - name: "John Doe", |
17 | | - }); |
18 | | - }); |
19 | | - `, |
20 | | - rule, |
21 | | - ruleName, |
22 | | - federationSetup: ` |
23 | | - const federation = { |
24 | | - setActorDispatcher: () => {} |
25 | | - }; |
26 | | - `, |
27 | | - }), |
28 | | -); |
29 | | - |
30 | | -test( |
31 | | - `${ruleName}: ✅ Good - key pairs dispatcher NOT configured, property missing (no error)`, |
32 | | - lintTest({ |
33 | | - code: ` |
34 | | - federation.setActorDispatcher("/users/{identifier}", async (ctx, identifier) => { |
35 | | - return new Person({ |
36 | | - id: ctx.getActorUri(identifier), |
37 | | - name: "John Doe", |
38 | | - }); |
39 | | - }); |
40 | | - `, |
41 | | - rule, |
42 | | - ruleName, |
43 | | - }), |
44 | | -); |
45 | | - |
46 | | -test( |
47 | | - `${ruleName}: ✅ Good - key pairs dispatcher configured BEFORE setActorDispatcher, property present`, |
48 | | - lintTest({ |
49 | | - code: ` |
50 | | - federation.setKeyPairsDispatcher(async (ctx, identifier) => { |
51 | | - return []; |
52 | | - }); |
53 | | -
|
54 | | - federation.setActorDispatcher("/users/{identifier}", async (ctx, identifier) => { |
55 | | - return new Person({ |
56 | | - id: ctx.getActorUri(identifier), |
57 | | - assertionMethod: ctx.getActorKeyPairs(identifier), |
58 | | - name: "John Doe", |
59 | | - }); |
60 | | - }); |
61 | | - `, |
62 | | - rule, |
63 | | - ruleName, |
64 | | - }), |
65 | | -); |
66 | | - |
67 | | -test( |
68 | | - `${ruleName}: ✅ Good - key pairs dispatcher configured BEFORE setActorDispatcher (chained), property present`, |
69 | | - lintTest({ |
70 | | - code: ` |
71 | | - federation |
72 | | - .setActorDispatcher("/users/{identifier}", async (ctx, identifier) => { |
73 | | - return new Person({ |
74 | | - id: ctx.getActorUri(identifier), |
75 | | - assertionMethod: ctx.getActorKeyPairs(identifier), |
76 | | - name: "John Doe", |
77 | | - }); |
78 | | - }) |
79 | | - .setKeyPairsDispatcher(async (ctx, identifier) => []); |
80 | | - `, |
81 | | - rule, |
82 | | - ruleName, |
83 | | - }), |
84 | | -); |
85 | | - |
86 | | -test( |
87 | | - `${ruleName}: ✅ Good - key pairs dispatcher configured AFTER setActorDispatcher, property present`, |
88 | | - lintTest({ |
89 | | - code: ` |
90 | | - federation.setActorDispatcher("/users/{identifier}", async (ctx, identifier) => { |
91 | | - return new Person({ |
92 | | - id: ctx.getActorUri(identifier), |
93 | | - assertionMethod: ctx.getActorKeyPairs(identifier), |
94 | | - name: "John Doe", |
95 | | - }); |
96 | | - }); |
97 | | -
|
98 | | - federation.setKeyPairsDispatcher(async (ctx, identifier) => { |
99 | | - return []; |
100 | | - }); |
101 | | - `, |
102 | | - rule, |
103 | | - ruleName, |
104 | | - }), |
105 | | -); |
106 | | - |
107 | | -test( |
108 | | - `${ruleName}: ✅ Good - key pairs dispatcher configured AFTER setActorDispatcher (chained), property present`, |
109 | | - lintTest({ |
110 | | - code: ` |
111 | | - federation |
112 | | - .setActorDispatcher("/users/{identifier}", async (ctx, identifier) => { |
113 | | - return new Person({ |
114 | | - id: ctx.getActorUri(identifier), |
115 | | - assertionMethod: ctx.getActorKeyPairs(identifier), |
116 | | - name: "John Doe", |
117 | | - }); |
118 | | - }) |
119 | | - .setKeyPairsDispatcher(async (ctx, identifier) => []); |
120 | | - `, |
121 | | - rule, |
122 | | - ruleName, |
123 | | - }), |
124 | | -); |
125 | | - |
126 | | -test( |
127 | | - `${ruleName}: ❌ Bad - key pairs dispatcher configured BEFORE, property missing`, |
128 | | - lintTest({ |
129 | | - code: ` |
130 | | - federation.setKeyPairsDispatcher(async (ctx, identifier) => { |
131 | | - return []; |
132 | | - }); |
133 | | -
|
134 | | - federation.setActorDispatcher("/users/{identifier}", async (ctx, identifier) => { |
135 | | - return new Person({ |
136 | | - id: ctx.getActorUri(identifier), |
137 | | - name: "John Doe", |
138 | | - }); |
139 | | - }); |
140 | | - `, |
141 | | - rule, |
142 | | - ruleName, |
143 | | - expectedError: actorPropertyRequired(properties.assertionMethod), |
144 | | - }), |
145 | | -); |
146 | | - |
147 | | -test( |
148 | | - `${ruleName}: ❌ Bad - key pairs dispatcher configured BEFORE (chained), property missing`, |
149 | | - lintTest({ |
150 | | - code: ` |
151 | | - federation |
152 | | - .setActorDispatcher("/users/{identifier}", async (ctx, identifier) => { |
153 | | - return new Person({ |
154 | | - id: ctx.getActorUri(identifier), |
155 | | - name: "John Doe", |
156 | | - }); |
157 | | - }) |
158 | | - .setKeyPairsDispatcher(async (ctx, identifier) => []); |
159 | | - `, |
160 | | - rule, |
161 | | - ruleName, |
162 | | - expectedError: actorPropertyRequired(properties.assertionMethod), |
163 | | - }), |
164 | | -); |
165 | | - |
166 | | -test( |
167 | | - `${ruleName}: ❌ Bad - key pairs dispatcher configured AFTER, property missing`, |
168 | | - lintTest({ |
169 | | - code: ` |
170 | | - federation.setActorDispatcher("/users/{identifier}", async (ctx, identifier) => { |
171 | | - return new Person({ |
172 | | - id: ctx.getActorUri(identifier), |
173 | | - name: "John Doe", |
174 | | - }); |
175 | | - }); |
176 | | -
|
177 | | - federation.setKeyPairsDispatcher(async (ctx, identifier) => { |
178 | | - return []; |
179 | | - }); |
180 | | - `, |
181 | | - rule, |
182 | | - ruleName, |
183 | | - expectedError: actorPropertyRequired(properties.assertionMethod), |
184 | | - }), |
185 | | -); |
186 | | - |
187 | | -test( |
188 | | - `${ruleName}: ❌ Bad - key pairs dispatcher configured AFTER (chained), property missing`, |
189 | | - lintTest({ |
190 | | - code: ` |
191 | | - federation |
192 | | - .setActorDispatcher("/users/{identifier}", async (ctx, identifier) => { |
193 | | - return new Person({ |
194 | | - id: ctx.getActorUri(identifier), |
195 | | - name: "John Doe", |
196 | | - }); |
197 | | - }) |
198 | | - .setKeyPairsDispatcher(async (ctx, identifier) => []); |
199 | | - `, |
200 | | - rule, |
201 | | - ruleName, |
202 | | - expectedError: actorPropertyRequired(properties.assertionMethod), |
203 | | - }), |
204 | | -); |
205 | | - |
206 | | -// Edge case tests |
207 | 10 | const config = { rule, ruleName }; |
208 | | -const edgeCases = createKeyRequiredEdgeCaseTests("assertionMethod", config); |
209 | | -test( |
210 | | - `${ruleName}: ✅ Edge - ternary with property in both branches`, |
211 | | - edgeCases["ternary with property in both branches"], |
212 | | -); |
213 | | -test( |
214 | | - `${ruleName}: ❌ Edge - ternary missing property in consequent`, |
215 | | - edgeCases["ternary missing property in consequent"], |
216 | | -); |
217 | | -test( |
218 | | - `${ruleName}: ❌ Edge - ternary missing property in alternate`, |
219 | | - edgeCases["ternary missing property in alternate"], |
220 | | -); |
221 | | -test( |
222 | | - `${ruleName}: ❌ Edge - ternary missing property in both branches`, |
223 | | - edgeCases["ternary missing property in both branches"], |
224 | | -); |
225 | | -test( |
226 | | - `${ruleName}: ✅ Edge - nested ternary with property`, |
227 | | - edgeCases["nested ternary with property"], |
228 | | -); |
229 | | -test( |
230 | | - `${ruleName}: ✅ Edge - if/else with property in both branches`, |
231 | | - edgeCases["if else with property in both branches"], |
232 | | -); |
233 | | -test( |
234 | | - `${ruleName}: ❌ Edge - if/else missing property in if block`, |
235 | | - edgeCases["if else missing property in if block"], |
236 | | -); |
237 | | -test( |
238 | | - `${ruleName}: ❌ Edge - if/else missing property in else block`, |
239 | | - edgeCases["if else missing property in else block"], |
240 | | -); |
241 | | -test( |
242 | | - `${ruleName}: ❌ Edge - if/else missing property in both blocks`, |
243 | | - edgeCases["if else missing property in both blocks"], |
244 | | -); |
245 | | -test( |
246 | | - `${ruleName}: ✅ Edge - nested if with property`, |
247 | | - edgeCases["nested if with property"], |
248 | | -); |
249 | | -test( |
250 | | - `${ruleName}: ✅ Edge - if else if else with property in all branches`, |
251 | | - edgeCases["if else if else with property in all branches"], |
252 | | -); |
253 | | -test( |
254 | | - `${ruleName}: ❌ Edge - if else if else missing property in else if`, |
255 | | - edgeCases["if else if else missing property in else if"], |
256 | | -); |
257 | | -test( |
258 | | - `${ruleName}: ✅ Edge - if else if with final return property in all paths`, |
259 | | - edgeCases["if else if with final return property in all paths"], |
260 | | -); |
261 | | -test( |
262 | | - `${ruleName}: ❌ Edge - if else if with final return missing property in final return`, |
263 | | - edgeCases["if else if with final return missing property in final return"], |
| 11 | + |
| 12 | +runTests( |
| 13 | + ruleName, |
| 14 | + createRequiredDispatcherRuleTests("assertionMethod", config), |
264 | 15 | ); |
| 16 | +runTests(ruleName, createRequiredEdgeCaseTests("assertionMethod", config)); |
0 commit comments