From f616ec5c8db05374d7daf05dc2fcca0d5f7e91bc Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Wed, 12 Mar 2025 16:12:59 +0100 Subject: [PATCH] test: Update getFlows test for user model --- packages/backend/src/models/user.test.js | 79 +++++++++++++++++------- 1 file changed, 58 insertions(+), 21 deletions(-) diff --git a/packages/backend/src/models/user.test.js b/packages/backend/src/models/user.test.js index 7a12abb7..6b85064d 100644 --- a/packages/backend/src/models/user.test.js +++ b/packages/backend/src/models/user.test.js @@ -1181,14 +1181,24 @@ describe('User model', () => { }); }); - describe('getFlows', () => { - let currentUser, currentUserRole, folder, flowOne, flowTwo; + describe.only('getFlows', () => { + let currentUser, + currentUserRole, + anotherUser, + folder, + anotherUserFolder, + flowOne, + flowTwo, + flowThree; beforeEach(async () => { currentUser = await createUser(); currentUserRole = await currentUser.$relatedQuery('role'); + anotherUser = await createUser(); + folder = await createFolder({ userId: currentUser.id }); + anotherUserFolder = await createFolder({ userId: anotherUser.id }); flowOne = await createFlow({ userId: currentUser.id, @@ -1201,11 +1211,16 @@ describe('User model', () => { name: 'Flow Two', }); + flowThree = await createFlow({ + userId: anotherUser.id, + name: 'Flow Three', + }); + await createPermission({ action: 'read', subject: 'Flow', roleId: currentUserRole.id, - conditions: ['isCreator'], + conditions: [], }); currentUser = await currentUser.$query().withGraphFetched({ @@ -1214,44 +1229,66 @@ describe('User model', () => { }); }); - it('should return flows filtered by folderId', async () => { - const flows = await currentUser.getFlows({ folderId: folder.id }); - - expect(flows).toHaveLength(1); - expect(flows[0].id).toBe(flowOne.id); - }); - it('should return flows filtered by name', async () => { - const flows = await currentUser.getFlows({ name: 'Flow Two' }); + const flows = await currentUser.getFlows({ name: 'Flow Two' }, [ + folder.id, + ]); expect(flows).toHaveLength(1); expect(flows[0].id).toBe(flowTwo.id); }); + it('should return flows with specific folder ID', async () => { + const flows = await currentUser.getFlows({ folderId: folder.id }, [ + folder.id, + ]); + + expect(flows.length).toBe(1); + expect(flows[0].id).toBe(flowOne.id); + }); + it('should return flows filtered by folderId and name', async () => { - const flows = await currentUser.getFlows({ - folderId: folder.id, - name: 'Flow One', - }); + const flows = await currentUser.getFlows( + { + folderId: folder.id, + name: 'Flow One', + }, + [folder.id] + ); expect(flows).toHaveLength(1); expect(flows[0].id).toBe(flowOne.id); }); it('should return all flows if no filters are provided', async () => { - const flows = await currentUser.getFlows({}); + const flows = await currentUser.getFlows({}, [folder.id]); - expect(flows).toHaveLength(2); + expect(flows).toHaveLength(3); expect(flows.map((flow) => flow.id)).toEqual( - expect.arrayContaining([flowOne.id, flowTwo.id]) + expect.arrayContaining([flowOne.id, flowTwo.id, flowThree.id]) ); }); it('should return uncategorized flows if the folderId is null', async () => { - const flows = await currentUser.getFlows({ folderId: 'null' }); + const flows = await currentUser.getFlows({ folderId: 'null' }, [ + folder.id, + ]); - expect(flows).toHaveLength(1); - expect(flows[0].id).toBe(flowTwo.id); + expect(flows).toHaveLength(2); + expect(flows.map((flow) => flow.id)).toEqual( + expect.arrayContaining([flowTwo.id, flowThree.id]) + ); + }); + + it('should return other users flow as uncategorized flows if the folderId is null', async () => { + const flows = await currentUser.getFlows({ folderId: 'null' }, [ + folder.id, + ]); + + expect(flows).toHaveLength(2); + expect(flows.map((flow) => flow.id)).toEqual( + expect.arrayContaining([flowTwo.id, flowThree.id]) + ); }); });