From f597066d168ea40536987fe618be54832c07c3c3 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Fri, 8 Nov 2024 13:38:10 +0000 Subject: [PATCH] test(user): write tests for authorizedFlows --- packages/backend/src/models/user.test.js | 60 ++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/packages/backend/src/models/user.test.js b/packages/backend/src/models/user.test.js index 1ce70f8e..e02473d3 100644 --- a/packages/backend/src/models/user.test.js +++ b/packages/backend/src/models/user.test.js @@ -13,6 +13,9 @@ import Subscription from './subscription.ee.js'; import UsageData from './usage-data.ee.js'; import User from './user.js'; import { createUser } from '../../test/factories/user.js'; +import { createRole } from '../../test/factories/role.js'; +import { createPermission } from '../../test/factories/permission.js'; +import { createFlow } from '../../test/factories/flow.js'; describe('User model', () => { it('tableName should return correct name', () => { @@ -245,4 +248,61 @@ describe('User model', () => { expect(token).toBe(undefined); }); }); + + describe('authorizedFlows', () => { + it('should return user flows with isCreator condition', async () => { + const userRole = await createRole({ name: 'User' }); + + await createPermission({ + roleId: userRole.id, + subject: 'Flow', + action: 'read', + conditions: ['isCreator'], + }); + + const user = await createUser({ roleId: userRole.id }); + + const userWithRoleAndPermissions = await user + .$query() + .withGraphFetched({ role: true, permissions: true }); + + const userFlow = await createFlow({ userId: user.id }); + await createFlow(); + + expect(await userWithRoleAndPermissions.authorizedFlows).toStrictEqual([ + userFlow, + ]); + }); + + it('should return all flows without isCreator condition', async () => { + const userRole = await createRole({ name: 'User' }); + + await createPermission({ + roleId: userRole.id, + subject: 'Flow', + action: 'read', + conditions: [], + }); + + const user = await createUser({ roleId: userRole.id }); + + const userWithRoleAndPermissions = await user + .$query() + .withGraphFetched({ role: true, permissions: true }); + + const userFlow = await createFlow({ userId: user.id }); + const anotherUserFlow = await createFlow(); + + expect(await userWithRoleAndPermissions.authorizedFlows).toStrictEqual([ + userFlow, + anotherUserFlow, + ]); + }); + + it('should throw an authorization error without Flow read permission', async () => { + const user = new User(); + + expect(() => user.authorizedFlows).toThrowError('NotAuthorized'); + }); + }); });