1
- import { Entity } from '@backstage/catalog-model' ;
1
+ import { Entity , GroupEntity } from '@backstage/catalog-model' ;
2
2
3
3
import * as Knex from 'knex' ;
4
4
import { createTracker , MockClient , Tracker } from 'knex-mock-client' ;
@@ -41,28 +41,24 @@ describe('ancestor-search-memo', () => {
41
41
] ;
42
42
43
43
const testGroups = [
44
- createGroupEntity (
45
- 'group:default/team-a' ,
46
- 'group:default/team-b' ,
47
- [ ] ,
48
- [ 'adam' ] ,
49
- ) ,
50
- createGroupEntity ( 'group:default/team-b' , 'group:default/team-c' , [ ] , [ ] ) ,
51
- createGroupEntity ( 'group:default/team-c' , '' , [ ] , [ ] ) ,
52
- createGroupEntity (
53
- 'group:default/team-d' ,
54
- 'group:default/team-e' ,
55
- [ ] ,
56
- [ 'george' ] ,
57
- ) ,
58
- createGroupEntity ( 'group:default/team-e' , 'group:default/team-f' , [ ] , [ ] ) ,
59
- createGroupEntity ( 'group:default/team-f' , '' , [ ] , [ ] ) ,
44
+ createGroupEntity ( 'team-a' , 'team-b' , [ ] , [ 'adam' ] ) ,
45
+ createGroupEntity ( 'team-b' , 'team-c' , [ ] , [ ] ) ,
46
+ createGroupEntity ( 'team-c' , '' , [ ] , [ ] ) ,
47
+ createGroupEntity ( 'team-d' , 'team-e' , [ ] , [ 'george' ] ) ,
48
+ createGroupEntity ( 'team-e' , 'team-f' , [ ] , [ ] ) ,
49
+ createGroupEntity ( 'team-f' , '' , [ ] , [ ] ) ,
60
50
] ;
61
51
52
+ const testUserGroups = [ createGroupEntity ( 'team-a' , 'team-b' , [ ] , [ 'adam' ] ) ] ;
53
+
62
54
const catalogApiMock : any = {
63
- getEntities : jest
64
- . fn ( )
65
- . mockImplementation ( ( ) => Promise . resolve ( { items : testGroups } ) ) ,
55
+ getEntities : jest . fn ( ) . mockImplementation ( ( arg : any ) => {
56
+ const hasMember = arg . filter [ 'relations.hasMember' ] ;
57
+ if ( hasMember && hasMember === 'user:default/adam' ) {
58
+ return { items : testUserGroups } ;
59
+ }
60
+ return { items : testGroups } ;
61
+ } ) ,
66
62
} ;
67
63
68
64
const catalogDBClient = Knex . knex ( { client : MockClient } ) ;
@@ -74,12 +70,16 @@ describe('ancestor-search-memo', () => {
74
70
authenticate : jest . fn ( ) . mockImplementation ( ) ,
75
71
} ;
76
72
77
- const asm = new AncestorSearchMemo (
78
- 'user:default/adam' ,
79
- tokenManagerMock ,
80
- catalogApiMock ,
81
- catalogDBClient ,
82
- ) ;
73
+ let asm : AncestorSearchMemo ;
74
+
75
+ beforeEach ( ( ) => {
76
+ asm = new AncestorSearchMemo (
77
+ 'user:default/adam' ,
78
+ tokenManagerMock ,
79
+ catalogApiMock ,
80
+ catalogDBClient ,
81
+ ) ;
82
+ } ) ;
83
83
84
84
describe ( 'getAllGroups and getAllRelations' , ( ) => {
85
85
let tracker : Tracker ;
@@ -104,7 +104,6 @@ describe('ancestor-search-memo', () => {
104
104
105
105
it ( 'should return all groups' , async ( ) => {
106
106
const allGroupsTest = await asm . getAllGroups ( ) ;
107
- // @ts -ignore
108
107
expect ( allGroupsTest ) . toEqual ( testGroups ) ;
109
108
} ) ;
110
109
@@ -137,14 +136,8 @@ describe('ancestor-search-memo', () => {
137
136
} ) ;
138
137
139
138
it ( 'should return all user groups' , async ( ) => {
140
- tracker . on
141
- . select (
142
- / s e l e c t " s o u r c e _ e n t i t y _ r e f " , " t a r g e t _ e n t i t y _ r e f " f r o m " r e l a t i o n s " w h e r e " t y p e " = ? / ,
143
- )
144
- . response ( userRelations ) ;
145
- const relations = await asm . getUserRelations ( ) ;
146
-
147
- expect ( relations ) . toEqual ( userRelations ) ;
139
+ const userGroups = await asm . getUserGroups ( ) ;
140
+ expect ( userGroups ) . toEqual ( testUserGroups ) ;
148
141
} ) ;
149
142
150
143
it ( 'should fail to return anything when there is an error getting user relations' , async ( ) => {
@@ -187,6 +180,7 @@ describe('ancestor-search-memo', () => {
187
180
asm ,
188
181
relation as Relation ,
189
182
allRelationsTest as Relation [ ] ,
183
+ 0 ,
190
184
) ,
191
185
) ;
192
186
@@ -196,6 +190,103 @@ describe('ancestor-search-memo', () => {
196
190
expect ( asm . hasEntityRef ( 'group:default/team-c' ) ) . toBeTruthy ( ) ;
197
191
expect ( asm . hasEntityRef ( 'group:default/team-d' ) ) . toBeFalsy ( ) ;
198
192
} ) ;
193
+
194
+ // maxDepth of one stops here
195
+ // |
196
+ // user:default/adam -> group:default/team-a -> group:default/team-b -> group:default/team-c
197
+ it ( 'should build the graph but stop based on the maxDepth' , async ( ) => {
198
+ const asmMaxDepth = new AncestorSearchMemo (
199
+ 'user:default/adam' ,
200
+ tokenManagerMock ,
201
+ catalogApiMock ,
202
+ catalogDBClient ,
203
+ 1 ,
204
+ ) ;
205
+
206
+ tracker . on
207
+ . select (
208
+ / s e l e c t " s o u r c e _ e n t i t y _ r e f " , " t a r g e t _ e n t i t y _ r e f " f r o m " r e l a t i o n s " w h e r e " t y p e " = ? / ,
209
+ )
210
+ . response ( userRelations ) ;
211
+ const userRelationsTest = await asmMaxDepth . getUserRelations ( ) ;
212
+
213
+ tracker . reset ( ) ;
214
+ tracker . on
215
+ . select (
216
+ / s e l e c t " s o u r c e _ e n t i t y _ r e f " , " t a r g e t _ e n t i t y _ r e f " f r o m " r e l a t i o n s " w h e r e " t y p e " = ? / ,
217
+ )
218
+ . response ( allRelations ) ;
219
+ const allRelationsTest = await asmMaxDepth . getAllRelations ( ) ;
220
+
221
+ userRelationsTest . forEach ( relation =>
222
+ asmMaxDepth . traverseRelations (
223
+ asmMaxDepth ,
224
+ relation as Relation ,
225
+ allRelationsTest as Relation [ ] ,
226
+ 0 ,
227
+ ) ,
228
+ ) ;
229
+
230
+ expect ( asmMaxDepth . hasEntityRef ( 'user:default/adam' ) ) . toBeTruthy ( ) ;
231
+ expect ( asmMaxDepth . hasEntityRef ( 'group:default/team-a' ) ) . toBeTruthy ( ) ;
232
+ expect ( asmMaxDepth . hasEntityRef ( 'group:default/team-b' ) ) . toBeTruthy ( ) ;
233
+ expect ( asmMaxDepth . hasEntityRef ( 'group:default/team-c' ) ) . toBeFalsy ( ) ;
234
+ expect ( asmMaxDepth . hasEntityRef ( 'group:default/team-d' ) ) . toBeFalsy ( ) ;
235
+ } ) ;
236
+ } ) ;
237
+
238
+ describe ( 'traverseGroups' , ( ) => {
239
+ // user:default/adam -> group:default/team-a -> group:default/team-b -> group:default/team-c
240
+ it ( 'should build a graph for a particular user' , async ( ) => {
241
+ const userGroupsTest = await asm . getUserGroups ( ) ;
242
+
243
+ const allGroupsTest = await asm . getAllGroups ( ) ;
244
+
245
+ userGroupsTest . forEach ( group =>
246
+ asm . traverseGroups (
247
+ asm ,
248
+ group as GroupEntity ,
249
+ allGroupsTest as GroupEntity [ ] ,
250
+ 0 ,
251
+ ) ,
252
+ ) ;
253
+
254
+ expect ( asm . hasEntityRef ( 'group:default/team-a' ) ) . toBeTruthy ( ) ;
255
+ expect ( asm . hasEntityRef ( 'group:default/team-b' ) ) . toBeTruthy ( ) ;
256
+ expect ( asm . hasEntityRef ( 'group:default/team-c' ) ) . toBeTruthy ( ) ;
257
+ expect ( asm . hasEntityRef ( 'group:default/team-d' ) ) . toBeFalsy ( ) ;
258
+ } ) ;
259
+
260
+ // maxDepth of one stops here
261
+ // |
262
+ // user:default/adam -> group:default/team-a -> group:default/team-b -> group:default/team-c
263
+ it ( 'should build the graph but stop based on the maxDepth' , async ( ) => {
264
+ const asmMaxDepth = new AncestorSearchMemo (
265
+ 'user:default/adam' ,
266
+ tokenManagerMock ,
267
+ catalogApiMock ,
268
+ catalogDBClient ,
269
+ 1 ,
270
+ ) ;
271
+
272
+ const userGroupsTest = await asmMaxDepth . getUserGroups ( ) ;
273
+
274
+ const allGroupsTest = await asmMaxDepth . getAllGroups ( ) ;
275
+
276
+ userGroupsTest . forEach ( group =>
277
+ asmMaxDepth . traverseGroups (
278
+ asmMaxDepth ,
279
+ group as GroupEntity ,
280
+ allGroupsTest as GroupEntity [ ] ,
281
+ 0 ,
282
+ ) ,
283
+ ) ;
284
+
285
+ expect ( asmMaxDepth . hasEntityRef ( 'group:default/team-a' ) ) . toBeTruthy ( ) ;
286
+ expect ( asmMaxDepth . hasEntityRef ( 'group:default/team-b' ) ) . toBeTruthy ( ) ;
287
+ expect ( asmMaxDepth . hasEntityRef ( 'group:default/team-c' ) ) . toBeFalsy ( ) ;
288
+ expect ( asmMaxDepth . hasEntityRef ( 'group:default/team-d' ) ) . toBeFalsy ( ) ;
289
+ } ) ;
199
290
} ) ;
200
291
201
292
describe ( 'buildUserGraph' , ( ) => {
@@ -211,6 +302,12 @@ describe('ancestor-search-memo', () => {
211
302
const asmDBSpy = jest
212
303
. spyOn ( asmUserGraph , 'doesRelationTableExist' )
213
304
. mockImplementation ( ( ) => Promise . resolve ( true ) ) ;
305
+ const userRelationsSpy = jest
306
+ . spyOn ( asmUserGraph , 'getUserRelations' )
307
+ . mockImplementation ( ( ) => Promise . resolve ( userRelations ) ) ;
308
+ const allRelationsSpy = jest
309
+ . spyOn ( asmUserGraph , 'getAllRelations' )
310
+ . mockImplementation ( ( ) => Promise . resolve ( allRelations ) ) ;
214
311
215
312
beforeAll ( ( ) => {
216
313
tracker = createTracker ( catalogDBClient ) ;
@@ -222,25 +319,16 @@ describe('ancestor-search-memo', () => {
222
319
223
320
// user:default/adam -> group:default/team-a -> group:default/team-b -> group:default/team-c
224
321
it ( 'should build the user graph using relations table' , async ( ) => {
225
- tracker . on
226
- . select (
227
- / s e l e c t " s o u r c e _ e n t i t y _ r e f " , " t a r g e t _ e n t i t y _ r e f " f r o m " r e l a t i o n s " w h e r e " t y p e " = ? / ,
228
- )
229
- . response ( userRelations ) ;
230
- tracker . reset ( ) ;
231
- tracker . on
232
- . select (
233
- / s e l e c t " s o u r c e _ e n t i t y _ r e f " , " t a r g e t _ e n t i t y _ r e f " f r o m " r e l a t i o n s " w h e r e " t y p e " = ? / ,
234
- )
235
- . response ( allRelations ) ;
236
322
await asmUserGraph . buildUserGraph ( asmUserGraph ) ;
237
323
238
324
expect ( asmDBSpy ) . toHaveBeenCalled ( ) ;
239
- expect ( asm . hasEntityRef ( 'user:default/adam' ) ) . toBeTruthy ( ) ;
240
- expect ( asm . hasEntityRef ( 'group:default/team-a' ) ) . toBeTruthy ( ) ;
241
- expect ( asm . hasEntityRef ( 'group:default/team-b' ) ) . toBeTruthy ( ) ;
242
- expect ( asm . hasEntityRef ( 'group:default/team-c' ) ) . toBeTruthy ( ) ;
243
- expect ( asm . hasEntityRef ( 'group:default/team-d' ) ) . toBeFalsy ( ) ;
325
+ expect ( userRelationsSpy ) . toHaveBeenCalled ( ) ;
326
+ expect ( allRelationsSpy ) . toHaveBeenCalled ( ) ;
327
+ expect ( asmUserGraph . hasEntityRef ( 'user:default/adam' ) ) . toBeTruthy ( ) ;
328
+ expect ( asmUserGraph . hasEntityRef ( 'group:default/team-a' ) ) . toBeTruthy ( ) ;
329
+ expect ( asmUserGraph . hasEntityRef ( 'group:default/team-b' ) ) . toBeTruthy ( ) ;
330
+ expect ( asmUserGraph . hasEntityRef ( 'group:default/team-c' ) ) . toBeTruthy ( ) ;
331
+ expect ( asmUserGraph . hasEntityRef ( 'group:default/team-d' ) ) . toBeFalsy ( ) ;
244
332
} ) ;
245
333
} ) ;
246
334
0 commit comments