|
1 | 1 | import { LinkCreateArgs } from 'shared'; |
2 | 2 | import { AccessDeniedAdminOnlyException, HttpException, NotFoundException } from '../../src/utils/errors.utils'; |
3 | | -import { batmanAppAdmin, supermanAdmin, wonderwomanGuest } from '../test-data/users.test-data'; |
| 3 | +import { batmanAppAdmin, flashAdmin, supermanAdmin, wonderwomanGuest } from '../test-data/users.test-data'; |
4 | 4 | import { createTestLinkType, createTestOrganization, createTestUser, resetUsers } from '../test-utils'; |
5 | 5 | import prisma from '../../src/prisma/prisma'; |
6 | 6 | import { testLink1 } from '../test-data/organizations.test-data'; |
@@ -285,4 +285,77 @@ describe('Organization Tests', () => { |
285 | 285 | expect(updatedOrg.sponsorshipNotificationsSlackChannelId).toEqual('sponsorshipNotifId'); |
286 | 286 | }); |
287 | 287 | }); |
| 288 | + |
| 289 | + describe('Get Finance Delegates', () => { |
| 290 | + it('Succeeds and returns empty array when no delegates', async () => { |
| 291 | + const delegates = await OrganizationsService.getFinanceDelegates(orgId); |
| 292 | + |
| 293 | + expect(delegates).not.toBeNull(); |
| 294 | + expect(delegates.length).toBe(0); |
| 295 | + }); |
| 296 | + |
| 297 | + it('Succeeds and returns all finance delegates', async () => { |
| 298 | + const testBatman = await createTestUser(batmanAppAdmin, orgId); |
| 299 | + const testSuperman = await createTestUser(supermanAdmin, orgId); |
| 300 | + |
| 301 | + await OrganizationsService.setFinanceDelegates(testBatman, orgId, [testSuperman.userId]); |
| 302 | + |
| 303 | + const delegates = await OrganizationsService.getFinanceDelegates(orgId); |
| 304 | + |
| 305 | + expect(delegates).not.toBeNull(); |
| 306 | + expect(delegates.length).toBe(1); |
| 307 | + expect(delegates[0].userId).toBe(testSuperman.userId); |
| 308 | + expect(delegates[0].firstName).toBe('Clark'); |
| 309 | + expect(delegates[0].lastName).toBe('Kent'); |
| 310 | + }); |
| 311 | + }); |
| 312 | + |
| 313 | + describe('Set Finance Delegates', () => { |
| 314 | + it('Fails if user is not admin', async () => { |
| 315 | + const testWonderwoman = await createTestUser(wonderwomanGuest, orgId); |
| 316 | + const testSuperman = await createTestUser(supermanAdmin, orgId); |
| 317 | + |
| 318 | + await expect(OrganizationsService.setFinanceDelegates(testWonderwoman, orgId, [testSuperman.userId])).rejects.toThrow( |
| 319 | + new AccessDeniedAdminOnlyException('set finance delegates') |
| 320 | + ); |
| 321 | + }); |
| 322 | + |
| 323 | + it('Fails if one or more user IDs do not exist', async () => { |
| 324 | + const testBatman = await createTestUser(batmanAppAdmin, orgId); |
| 325 | + |
| 326 | + await expect(OrganizationsService.setFinanceDelegates(testBatman, orgId, ['nonexistent-user-id'])).rejects.toThrow( |
| 327 | + new NotFoundException('User', 'one or more user IDs') |
| 328 | + ); |
| 329 | + }); |
| 330 | + |
| 331 | + it('Succeeds and sets finance delegates', async () => { |
| 332 | + const testBatman = await createTestUser(batmanAppAdmin, orgId); |
| 333 | + const testSuperman = await createTestUser(supermanAdmin, orgId); |
| 334 | + const testFlash = await createTestUser(flashAdmin, orgId); |
| 335 | + |
| 336 | + const delegates = await OrganizationsService.setFinanceDelegates(testBatman, orgId, [ |
| 337 | + testSuperman.userId, |
| 338 | + testFlash.userId |
| 339 | + ]); |
| 340 | + |
| 341 | + expect(delegates).not.toBeNull(); |
| 342 | + expect(delegates.length).toBe(2); |
| 343 | + expect(delegates.some((delegate) => delegate.userId === testSuperman.userId)).toBeTruthy(); |
| 344 | + expect(delegates.some((delegate) => delegate.userId === testFlash.userId)).toBeTruthy(); |
| 345 | + }); |
| 346 | + |
| 347 | + it('Succeeds and replaces existing finance delegates', async () => { |
| 348 | + const testBatman = await createTestUser(batmanAppAdmin, orgId); |
| 349 | + const testSuperman = await createTestUser(supermanAdmin, orgId); |
| 350 | + const testFlash = await createTestUser(flashAdmin, orgId); |
| 351 | + |
| 352 | + await OrganizationsService.setFinanceDelegates(testBatman, orgId, [testSuperman.userId]); |
| 353 | + |
| 354 | + const updatedDelegates = await OrganizationsService.setFinanceDelegates(testBatman, orgId, [testFlash.userId]); |
| 355 | + |
| 356 | + expect(updatedDelegates).not.toBeNull(); |
| 357 | + expect(updatedDelegates.length).toBe(1); |
| 358 | + expect(updatedDelegates[0].userId).toBe(testFlash.userId); |
| 359 | + }); |
| 360 | + }); |
288 | 361 | }); |
0 commit comments