-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
429 lines (398 loc) · 8.61 KB
/
writer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package main
import (
"fmt"
"sort"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWriter_DOT(t *testing.T) {
testCases := map[string]struct {
inputModel string
expectedOutput string
}{
`with_union`: { // https://github.com/openfga/openfga/blob/main/docs/list_objects/example/example.md
inputModel: `
model
schema 1.1
type user
type group
relations
define member: [user, group#member]
type folder
relations
define viewer: [user]
type document
relations
define parent: [folder]
define editor: [user]
define viewer: [user, user:*, group#member] or editor or viewer from parent`,
expectedOutput: `digraph {
graph [
rankdir=BT
];
// Node definitions.
2 [label="document#editor"];
3 [label=user];
4 [label="document#parent"];
5 [label=folder];
6 [label="document#viewer"];
7 [label="user:*"];
8 [label="group#member"];
9 [label="folder#viewer"];
// Edge definitions.
2 -> 6 [
label=6
style=dashed
];
3 -> 2 [label=1];
3 -> 6 [label=3];
3 -> 8 [label=9];
3 -> 9 [label=8];
5 -> 4 [label=2];
7 -> 6 [label=4];
8 -> 6 [label=5];
8 -> 8 [label=10];
9 -> 6 [
label=7
headlabel="(document#parent)"
];
}`,
},
`with_intersection`: { // https://github.com/openfga/openfga/blob/main/docs/list_objects/example_with_intersection_or_exclusion/example.md
inputModel: `
model
schema 1.1
type user
type document
relations
define a: [user]
define b: [user]
define c: a and b`,
expectedOutput: `digraph {
graph [
rankdir=BT
];
// Node definitions.
2 [label="document#a"];
3 [label=user];
4 [label="document#b"];
5 [label="document#c"];
// Edge definitions.
2 -> 5 [
label=3
style=dashed
];
3 -> 2 [label=1];
3 -> 4 [label=2];
4 -> 5 [
label=4
style=dashed
];
}`,
},
`with_exclusion`: { // https://github.com/openfga/openfga/blob/main/docs/list_objects/example_with_intersection_or_exclusion/example.md
inputModel: `
model
schema 1.1
type user
type document
relations
define a: [user]
define b: [user]
define c: a but not b`,
expectedOutput: `digraph {
graph [
rankdir=BT
];
// Node definitions.
2 [label="document#a"];
3 [label=user];
4 [label="document#b"];
5 [label="document#c"];
// Edge definitions.
2 -> 5 [
label=3
style=dashed
];
3 -> 2 [label=1];
3 -> 4 [label=2];
4 -> 5 [
label=4
style=dashed
];
}`,
},
`with_conditions`: {
inputModel: `
model
schema 1.1
type user
type document
relations
define admin: [user with condition1]
define writer: [user with condition2]
define viewer: [user:* with condition3]
condition condition1(x: int) {
x < 100
}
condition condition2(x: int) {
x < 100
}
condition condition3(x: int) {
x < 100
}`,
expectedOutput: `digraph {
graph [
rankdir=BT
];
// Node definitions.
2 [label="document#admin"];
3 [label=" user[with condition1]"];
4 [label="document#viewer"];
5 [label=" user[with condition3]:*"];
6 [label="document#writer"];
7 [label=" user[with condition2]"];
// Edge definitions.
3 -> 2 [label=1];
5 -> 4 [label=2];
7 -> 6 [label=3];
}`,
},
`multigraph`: {
inputModel: `
model
schema 1.1
type user
type state
relations
define can_view: [user]
type transition
relations
define start: [state]
define end: [state]
define can_apply: [user] and can_view from start and can_view from end`,
expectedOutput: `digraph {
graph [
rankdir=BT
];
// Node definitions.
0 [label=state];
2 [label="state#can_view"];
3 [label=user];
6 [label="transition#can_apply"];
7 [label="transition#end"];
8 [label="transition#start"];
// Edge definitions.
0 -> 7 [label=5];
0 -> 8 [label=6];
2 -> 6 [
label=3
headlabel="(transition#start)"
];
2 -> 6 [
label=4
headlabel="(transition#end)"
];
3 -> 2 [label=1];
3 -> 6 [label=2];
}`,
},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
actualDOT, _ := Writer(test.inputModel)
actualSorted := getSorted(actualDOT)
expectedSorted := getSorted(test.expectedOutput)
diff := cmp.Diff(expectedSorted, actualSorted)
require.Empty(t, diff, "expectedDefinitiveCycle %s, got %s", test.expectedOutput, actualDOT)
})
}
}
func TestWriter_Cycles(t *testing.T) {
testCases := map[string]struct {
model string
expectedPossibleCycles int
expectedDefinitiveCycles int
}{
`computed_userset_1_definitive_cycle`: {
model: `
model
schema 1.1
type resource
relations
define a: b
define b: a`,
expectedDefinitiveCycles: 1,
},
`computed_userset_2`: {
model: `
model
schema 1.1
type resource
relations
define x: y
define y: z
define z: x`,
expectedDefinitiveCycles: 1,
},
`union_1`: {
model: `
model
schema 1.1
type user
type resource
relations
define x: [user] or y
define y: [user] or z
define z: [user] or x`,
expectedDefinitiveCycles: 1,
},
`union_2`: {
model: `
model
schema 1.1
type user
type resource
relations
define x: [user] or y
define y: [user] or z
define z: [user] or x`,
expectedDefinitiveCycles: 1,
},
`union_3`: {
model: `
model
schema 1.1
type user
type resource
relations
define member: [user] or memberA or memberB or memberC
define memberA: [user] or member or memberB or memberC
define memberB: [user] or member or memberA or memberC
define memberC: [user] or member or memberA or memberB`,
expectedDefinitiveCycles: 20,
},
`union_4`: {
model: `
model
schema 1.1
type user
type resource
relations
define admin: [user] or member or super_admin or owner
define member: [user] or owner or admin or super_admin
define super_admin: [user] or admin or member or owner
define owner: [user]`,
expectedDefinitiveCycles: 5,
},
`union_5`: {
model: `
model
schema 1.1
type user
type resource
relations
define admin: [user] or member or super_admin or owner
define member: [user] or owner or admin or super_admin
define super_admin: [user] or admin or member or owner
define owner: [user]`,
expectedDefinitiveCycles: 5,
},
`union_6_no_cycles`: {
model: `
model
schema 1.1
type user
type document
relations
define editor: [user]
define viewer: [document#viewer] or editor`,
},
`intersection_and_union`: {
model: `
model
schema 1.1
type user
type resource
relations
define x: [user] and y
define y: [user] and z
define z: [user] or x`,
expectedDefinitiveCycles: 1,
},
`exclusion_and_union`: {
model: `
model
schema 1.1
type user
type resource
relations
define x: [user] but not y
define y: [user] but not z
define z: [user] or x`,
expectedDefinitiveCycles: 1,
},
`many_circular_computed_relations`: {
model: `
model
schema 1.1
type user
type canvas
relations
define can_edit: editor or owner
define editor: [user, account#member]
define owner: [user]
define viewer: [user, account#member]
type account
relations
define admin: [user] or member or super_admin or owner
define member: [user] or owner or admin or super_admin
define owner: [user]
define super_admin: [user] or admin or member`,
expectedDefinitiveCycles: 5,
},
`scenario_1`: {
model: `
model
schema 1.1
type user
type document
relations
define viewer: [user, document#viewer] or editor
define editor: [user, document#viewer]`,
expectedPossibleCycles: 1,
},
`scenario_2`: {
model: `
model
schema 1.1
type user
type document
relations
define editor1: [user, document#viewer1]
define viewer2: [document#viewer1] or editor1
define viewer1: [user] or viewer2
define can_view: viewer1 or editor1`,
expectedPossibleCycles: 2,
},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
_, cycleInfo := Writer(test.model)
assert.Equal(t, test.expectedPossibleCycles, cycleInfo.possibleCycles)
assert.Equal(t, test.expectedDefinitiveCycles, cycleInfo.definitiveCycles)
fmt.Println(cycleInfo.cycles)
})
}
}
// getSorted assumes the input has multiple lines and returns the sorted version of it.
func getSorted(input string) string {
lines := strings.FieldsFunc(input, func(r rune) bool {
return r == '\n'
})
sort.Strings(lines)
return strings.Join(lines, "\n")
}