From 95e96245529c361d25e79621d3812c4c212ecd19 Mon Sep 17 00:00:00 2001 From: andes-it Date: Fri, 25 Oct 2024 09:23:23 +0000 Subject: [PATCH] chore: nx format:write update dirty files --- .../endorsement-list.controller.spec.ts | 86 ++- .../endorsement-list.service.spec.ts | 649 +++++++++--------- .../endorsementList.service.ts | 7 +- 3 files changed, 385 insertions(+), 357 deletions(-) diff --git a/apps/services/endorsements/api/src/app/modules/endorsementList/endorsement-list.controller.spec.ts b/apps/services/endorsements/api/src/app/modules/endorsementList/endorsement-list.controller.spec.ts index 375613afc1b4..5e4c6cd613ca 100644 --- a/apps/services/endorsements/api/src/app/modules/endorsementList/endorsement-list.controller.spec.ts +++ b/apps/services/endorsements/api/src/app/modules/endorsementList/endorsement-list.controller.spec.ts @@ -47,20 +47,23 @@ describe('EndorsementListController', () => { it.each([ ['GET', '/endorsement-list'], - ['POST', '/endorsement-list'], + ['POST', '/endorsement-list'], ['GET', '/endorsement-list/12345'], ['PUT', '/endorsement-list/12345/close'], ['PUT', '/endorsement-list/12345/lock'], - ])('%s %s should return 401 when user is not authenticated', async (method, path) => { - const res = await (server as any)[method.toLowerCase()](path) - - expect(res.status).toBe(401) - expect(res.body).toMatchObject({ - status: 401, - title: 'Unauthorized', - type: 'https://httpstatuses.org/401', - }) - }) + ])( + '%s %s should return 401 when user is not authenticated', + async (method, path) => { + const res = await (server as any)[method.toLowerCase()](path) + + expect(res.status).toBe(401) + expect(res.body).toMatchObject({ + status: 401, + title: 'Unauthorized', + type: 'https://httpstatuses.org/401', + }) + }, + ) }) describe('With auth', () => { @@ -76,13 +79,17 @@ describe('EndorsementListController', () => { server = request(app.getHttpServer()) const endorsementListService = app.get(EndorsementListService) - - jest.spyOn(endorsementListService, 'findListsByTags').mockResolvedValue({ - data: [mockEndorsementList], - totalCount: 1, - pageInfo: { hasNextPage: false }, - }) - jest.spyOn(endorsementListService, 'create').mockResolvedValue(mockEndorsementList) + + jest + .spyOn(endorsementListService, 'findListsByTags') + .mockResolvedValue({ + data: [mockEndorsementList], + totalCount: 1, + pageInfo: { hasNextPage: false }, + }) + jest + .spyOn(endorsementListService, 'create') + .mockResolvedValue(mockEndorsementList) }) afterAll(async () => { @@ -110,7 +117,9 @@ describe('EndorsementListController', () => { title: 'New List', description: 'New description', openedDate: new Date(), - closedDate: new Date(new Date().setMonth(new Date().getMonth() + 1)), + closedDate: new Date( + new Date().setMonth(new Date().getMonth() + 1), + ), } const res = await server.post('/endorsement-list').send(newList) @@ -133,10 +142,16 @@ describe('EndorsementListController', () => { server = request(app.getHttpServer()) const endorsementListService = app.get(EndorsementListService) - - jest.spyOn(endorsementListService, 'findSingleList').mockResolvedValue(mockEndorsementList) - jest.spyOn(endorsementListService, 'lock').mockResolvedValue(mockEndorsementList) - jest.spyOn(endorsementListService, 'unlock').mockResolvedValue(mockEndorsementList) + + jest + .spyOn(endorsementListService, 'findSingleList') + .mockResolvedValue(mockEndorsementList) + jest + .spyOn(endorsementListService, 'lock') + .mockResolvedValue(mockEndorsementList) + jest + .spyOn(endorsementListService, 'unlock') + .mockResolvedValue(mockEndorsementList) }) afterAll(async () => { @@ -182,17 +197,20 @@ describe('EndorsementListController', () => { ['GET', '/endorsement-list'], ['POST', '/endorsement-list'], ['PUT', '/endorsement-list/12345/lock'], - ])('%s %s should return 403 when user lacks required scope', async (method, path) => { - const res = await server[method.toLowerCase()](path) + ])( + '%s %s should return 403 when user lacks required scope', + async (method, path) => { + const res = await server[method.toLowerCase()](path) - expect(res.status).toBe(403) - expect(res.body).toMatchObject({ - status: 403, - title: 'Forbidden', - detail: 'Forbidden resource', - type: 'https://httpstatuses.org/403', - }) - }) + expect(res.status).toBe(403) + expect(res.body).toMatchObject({ + status: 403, + title: 'Forbidden', + detail: 'Forbidden resource', + type: 'https://httpstatuses.org/403', + }) + }, + ) }) }) -}) \ No newline at end of file +}) diff --git a/apps/services/endorsements/api/src/app/modules/endorsementList/endorsement-list.service.spec.ts b/apps/services/endorsements/api/src/app/modules/endorsementList/endorsement-list.service.spec.ts index e30ca9c0c6ea..72710bc11633 100644 --- a/apps/services/endorsements/api/src/app/modules/endorsementList/endorsement-list.service.spec.ts +++ b/apps/services/endorsements/api/src/app/modules/endorsementList/endorsement-list.service.spec.ts @@ -412,359 +412,368 @@ describe('EndorsementListService', () => { expect(mockEmailService.sendEmail).toHaveBeenCalled() }) }) - - describe('findListsGenericQuery', () => { - const mockQuery = { limit: 10 } + describe('findListsGenericQuery', () => { + const mockQuery = { limit: 10 } - it('should return paginated results', async () => { - const mockPaginatedResult = { - data: [mockEndorsementList], - totalCount: 1, - pageInfo: { hasNextPage: false }, - } - mockEndorsementListModel.findAll.mockResolvedValue([mockEndorsementList]) - mockEndorsementListModel.count.mockResolvedValue(1) + it('should return paginated results', async () => { + const mockPaginatedResult = { + data: [mockEndorsementList], + totalCount: 1, + pageInfo: { hasNextPage: false }, + } + mockEndorsementListModel.findAll.mockResolvedValue([ + mockEndorsementList, + ]) + mockEndorsementListModel.count.mockResolvedValue(1) - const result = await service.findListsGenericQuery(mockQuery) - expect(result).toHaveProperty('data') - expect(result).toHaveProperty('totalCount') - expect(result).toHaveProperty('pageInfo') - }) + const result = await service.findListsGenericQuery(mockQuery) + expect(result).toHaveProperty('data') + expect(result).toHaveProperty('totalCount') + expect(result).toHaveProperty('pageInfo') + }) - it('should apply provided where conditions', async () => { - const whereCondition = { adminLock: false } // Example condition - const query = { limit: 10 } // Mock query - - // Mock the findAll response - mockEndorsementListModel.findAll.mockResolvedValue([mockEndorsementList]) - mockEndorsementListModel.count.mockResolvedValue(1) - - // Call the service method with a query and where condition - await service.findListsGenericQuery(query, whereCondition) - - // Expect the findAll method to be called with the correct 'where' condition - expect(mockEndorsementListModel.findAll).toHaveBeenCalledWith( - expect.objectContaining({ - where: whereCondition, // Ensure that the 'where' condition is passed correctly - }), - ) - }) - }) + it('should apply provided where conditions', async () => { + const whereCondition = { adminLock: false } // Example condition + const query = { limit: 10 } // Mock query - describe('findListsByTags', () => { - const mockQuery = { limit: 10 } - const mockTags = [EndorsementTag.GENERAL_PETITION] + // Mock the findAll response + mockEndorsementListModel.findAll.mockResolvedValue([ + mockEndorsementList, + ]) + mockEndorsementListModel.count.mockResolvedValue(1) - it('should return lists filtered by tags for admin user', async () => { - const mockPaginatedResult = { - data: [mockEndorsementList], - totalCount: 1, - pageInfo: { hasNextPage: false }, - } - mockEndorsementListModel.findAll.mockResolvedValue([mockEndorsementList]) - mockEndorsementListModel.count.mockResolvedValue(1) - - await service.findListsByTags(mockTags, mockQuery, mockAdminUser) - expect(mockEndorsementListModel.findAll).toHaveBeenCalledWith( - expect.objectContaining({ - where: { - tags: { [Op.overlap]: mockTags }, - adminLock: { [Op.or]: [true, false] }, - }, - }), - ) - }) + // Call the service method with a query and where condition + await service.findListsGenericQuery(query, whereCondition) - it('should return lists filtered by tags for regular user', async () => { - const tags = ['generalTag'] - const query = { limit: 10 } - - // Mock the findAll response to return some lists - mockEndorsementListModel.findAll.mockResolvedValue([mockEndorsementList]) - - // Mock the count response to avoid the error with pagination - mockEndorsementListModel.count.mockResolvedValue(1) // Ensuring count resolves with a promise - - // Call the service method with tags, query, and the regular user (mockUser) - await service.findListsByTags(tags, query, mockUser) - - // Ensure that the query for regular users has 'adminLock: false' - expect(mockEndorsementListModel.findAll).toHaveBeenCalledWith( - expect.objectContaining({ - where: { - tags: { [Op.overlap]: tags }, - adminLock: false, // Ensure that adminLock is false for regular users - }, - }), - ) - - // Verify that count was called - expect(mockEndorsementListModel.count).toHaveBeenCalledWith( - expect.objectContaining({ - where: { - tags: { [Op.overlap]: tags }, - adminLock: false, // The same condition should apply to count - }, - }), - ) + // Expect the findAll method to be called with the correct 'where' condition + expect(mockEndorsementListModel.findAll).toHaveBeenCalledWith( + expect.objectContaining({ + where: whereCondition, // Ensure that the 'where' condition is passed correctly + }), + ) + }) }) - }) - describe('findOpenListsTaggedGeneralPetition', () => { - const mockQuery = { limit: 10 } + describe('findListsByTags', () => { + const mockQuery = { limit: 10 } + const mockTags = [EndorsementTag.GENERAL_PETITION] - it('should return open general petition lists', async () => { - const mockPaginatedResult = { - data: [mockEndorsementList], - totalCount: 1, - pageInfo: { hasNextPage: false }, - } - mockEndorsementListModel.findAll.mockResolvedValue([mockEndorsementList]) - mockEndorsementListModel.count.mockResolvedValue(1) + it('should return lists filtered by tags for admin user', async () => { + const mockPaginatedResult = { + data: [mockEndorsementList], + totalCount: 1, + pageInfo: { hasNextPage: false }, + } + mockEndorsementListModel.findAll.mockResolvedValue([ + mockEndorsementList, + ]) + mockEndorsementListModel.count.mockResolvedValue(1) - const result = await service.findOpenListsTaggedGeneralPetition(mockQuery) - expect(result).toHaveProperty('data') - expect(mockEndorsementListModel.findAll).toHaveBeenCalled() - }) + await service.findListsByTags(mockTags, mockQuery, mockAdminUser) + expect(mockEndorsementListModel.findAll).toHaveBeenCalledWith( + expect.objectContaining({ + where: { + tags: { [Op.overlap]: mockTags }, + adminLock: { [Op.or]: [true, false] }, + }, + }), + ) + }) - it('should throw NotFoundException when no lists found', async () => { - const query = { limit: 10 } - - // Mock findAll to return an empty array (no results) - mockEndorsementListModel.findAll.mockResolvedValue([]) - - // Mock count to return 0, indicating no lists found - mockEndorsementListModel.count.mockResolvedValue(0) - - // Ensure the service throws NotFoundException when no lists are found - await expect(service.findOpenListsTaggedGeneralPetition(query)).rejects.toThrow(NotFoundException) - }) - - }) + it('should return lists filtered by tags for regular user', async () => { + const tags = ['generalTag'] + const query = { limit: 10 } - describe('emailPDF', () => { - const mockListId = 'test-id' - const mockEmail = 'test@example.com' - const mockEndorsementWithEndorsements = { - ...mockEndorsementList, - endorsements: [ - { - created: new Date(), - meta: { - fullName: 'Test User', - locality: 'Test City', - }, - }, - ], - } + // Mock the findAll response to return some lists + mockEndorsementListModel.findAll.mockResolvedValue([ + mockEndorsementList, + ]) + + // Mock the count response to avoid the error with pagination + mockEndorsementListModel.count.mockResolvedValue(1) // Ensuring count resolves with a promise + + // Call the service method with tags, query, and the regular user (mockUser) + await service.findListsByTags(tags, query, mockUser) + + // Ensure that the query for regular users has 'adminLock: false' + expect(mockEndorsementListModel.findAll).toHaveBeenCalledWith( + expect.objectContaining({ + where: { + tags: { [Op.overlap]: tags }, + adminLock: false, // Ensure that adminLock is false for regular users + }, + }), + ) - beforeEach(() => { - mockEndorsementListModel.findOne.mockResolvedValue( - mockEndorsementWithEndorsements, - ) - mockNationalRegistryService.getName.mockResolvedValue({ - fulltNafn: 'Test Owner', + // Verify that count was called + expect(mockEndorsementListModel.count).toHaveBeenCalledWith( + expect.objectContaining({ + where: { + tags: { [Op.overlap]: tags }, + adminLock: false, // The same condition should apply to count + }, + }), + ) }) }) - it('should successfully send PDF email', async () => { - const result = await service.emailPDF(mockListId, mockEmail) + describe('findOpenListsTaggedGeneralPetition', () => { + const mockQuery = { limit: 10 } - expect(result.success).toBe(true) - expect(mockEmailService.sendEmail).toHaveBeenCalledWith( - expect.objectContaining({ - to: expect.arrayContaining([ - expect.objectContaining({ - address: mockEmail, - }), - ]), - }), - ) - }) + it('should return open general petition lists', async () => { + const mockPaginatedResult = { + data: [mockEndorsementList], + totalCount: 1, + pageInfo: { hasNextPage: false }, + } + mockEndorsementListModel.findAll.mockResolvedValue([ + mockEndorsementList, + ]) + mockEndorsementListModel.count.mockResolvedValue(1) + + const result = await service.findOpenListsTaggedGeneralPetition( + mockQuery, + ) + expect(result).toHaveProperty('data') + expect(mockEndorsementListModel.findAll).toHaveBeenCalled() + }) - it('should handle email sending failure', async () => { - mockEmailService.sendEmail.mockRejectedValue(new Error('Email failed')) + it('should throw NotFoundException when no lists found', async () => { + const query = { limit: 10 } - const result = await service.emailPDF(mockListId, mockEmail) - expect(result.success).toBe(false) - }) + // Mock findAll to return an empty array (no results) + mockEndorsementListModel.findAll.mockResolvedValue([]) - it('should throw NotFoundException when list not found', async () => { - mockEndorsementListModel.findOne.mockResolvedValue(null) + // Mock count to return 0, indicating no lists found + mockEndorsementListModel.count.mockResolvedValue(0) - await expect(service.emailPDF(mockListId, mockEmail)).rejects.toThrow( - NotFoundException, - ) + // Ensure the service throws NotFoundException when no lists are found + await expect( + service.findOpenListsTaggedGeneralPetition(query), + ).rejects.toThrow(NotFoundException) + }) }) - }) - describe('exportList', () => { - const exportParams = { - listId: 'test-id', - user: mockUser, - fileType: 'pdf' as const, - } + describe('emailPDF', () => { + const mockListId = 'test-id' + const mockEmail = 'test@example.com' + const mockEndorsementWithEndorsements = { + ...mockEndorsementList, + endorsements: [ + { + created: new Date(), + meta: { + fullName: 'Test User', + locality: 'Test City', + }, + }, + ], + } + + beforeEach(() => { + mockEndorsementListModel.findOne.mockResolvedValue( + mockEndorsementWithEndorsements, + ) + mockNationalRegistryService.getName.mockResolvedValue({ + fulltNafn: 'Test Owner', + }) + }) - beforeEach(() => { - mockEndorsementListModel.findOne.mockResolvedValue(mockEndorsementList) - mockNationalRegistryService.getName.mockResolvedValue({ - fulltNafn: 'Test Owner', + it('should successfully send PDF email', async () => { + const result = await service.emailPDF(mockListId, mockEmail) + + expect(result.success).toBe(true) + expect(mockEmailService.sendEmail).toHaveBeenCalledWith( + expect.objectContaining({ + to: expect.arrayContaining([ + expect.objectContaining({ + address: mockEmail, + }), + ]), + }), + ) }) - }) - it('should handle PDF export', async () => { - const result = await service.exportList( - exportParams.listId, - exportParams.user, - exportParams.fileType, - ) + it('should handle email sending failure', async () => { + mockEmailService.sendEmail.mockRejectedValue(new Error('Email failed')) - expect(result.url).toBe('https://test-url.com') - expect(mockAwsService.uploadFile).toHaveBeenCalled() - expect(mockAwsService.getPresignedUrl).toHaveBeenCalled() - }) + const result = await service.emailPDF(mockListId, mockEmail) + expect(result.success).toBe(false) + }) + + it('should throw NotFoundException when list not found', async () => { + mockEndorsementListModel.findOne.mockResolvedValue(null) - it('should handle CSV export', async () => { - const result = await service.exportList( - exportParams.listId, - exportParams.user, - 'csv', - ) - - expect(result.url).toBe('https://test-url.com') - expect(mockAwsService.uploadFile).toHaveBeenCalledWith( - expect.any(Buffer), - expect.objectContaining({ - key: expect.stringContaining('.csv'), - }), - expect.objectContaining({ - ContentType: 'text/csv', - }), - ) + await expect(service.emailPDF(mockListId, mockEmail)).rejects.toThrow( + NotFoundException, + ) + }) }) - it('should handle AWS upload failure', async () => { - mockAwsService.uploadFile.mockRejectedValue(new Error('Upload failed')) + describe('exportList', () => { + const exportParams = { + listId: 'test-id', + user: mockUser, + fileType: 'pdf' as const, + } + + beforeEach(() => { + mockEndorsementListModel.findOne.mockResolvedValue(mockEndorsementList) + mockNationalRegistryService.getName.mockResolvedValue({ + fulltNafn: 'Test Owner', + }) + }) - await expect( - service.exportList( + it('should handle PDF export', async () => { + const result = await service.exportList( exportParams.listId, exportParams.user, exportParams.fileType, - ), - ).rejects.toThrow('Error uploading file to S3') - }) - }) + ) - describe('emailCreated and emailLock', () => { - beforeEach(() => { - mockNationalRegistryService.getName.mockResolvedValue({ - fulltNafn: 'Test Owner', + expect(result.url).toBe('https://test-url.com') + expect(mockAwsService.uploadFile).toHaveBeenCalled() + expect(mockAwsService.getPresignedUrl).toHaveBeenCalled() }) - }) - it('should send creation email successfully', async () => { - const result = await service.emailCreated(mockEndorsementList) - expect(result.success).toBe(true) - expect(mockEmailService.sendEmail).toHaveBeenCalled() - }) + it('should handle CSV export', async () => { + const result = await service.exportList( + exportParams.listId, + exportParams.user, + 'csv', + ) - it('should send lock email successfully', async () => { - const result = await service.emailLock(mockEndorsementList) - expect(result.success).toBe(true) - expect(mockEmailService.sendEmail).toHaveBeenCalled() - }) + expect(result.url).toBe('https://test-url.com') + expect(mockAwsService.uploadFile).toHaveBeenCalledWith( + expect.any(Buffer), + expect.objectContaining({ + key: expect.stringContaining('.csv'), + }), + expect.objectContaining({ + ContentType: 'text/csv', + }), + ) + }) - it('should handle missing owner email', async () => { - const listWithoutEmail = { - ...mockEndorsementList, - meta: {}, - } + it('should handle AWS upload failure', async () => { + mockAwsService.uploadFile.mockRejectedValue(new Error('Upload failed')) - await expect(service.emailLock(listWithoutEmail)).rejects.toThrow( - NotFoundException, - ) + await expect( + service.exportList( + exportParams.listId, + exportParams.user, + exportParams.fileType, + ), + ).rejects.toThrow('Error uploading file to S3') + }) }) - it('should handle email service failures', async () => { - mockEmailService.sendEmail.mockRejectedValue(new Error('Email failed')) + describe('emailCreated and emailLock', () => { + beforeEach(() => { + mockNationalRegistryService.getName.mockResolvedValue({ + fulltNafn: 'Test Owner', + }) + }) - const result = await service.emailCreated(mockEndorsementList) - expect(result.success).toBe(false) - }) - }) + it('should send creation email successfully', async () => { + const result = await service.emailCreated(mockEndorsementList) + expect(result.success).toBe(true) + expect(mockEmailService.sendEmail).toHaveBeenCalled() + }) + it('should send lock email successfully', async () => { + const result = await service.emailLock(mockEndorsementList) + expect(result.success).toBe(true) + expect(mockEmailService.sendEmail).toHaveBeenCalled() + }) -describe('List query functionality', () => { - it('should handle pagination parameters', async () => { - const query = { - limit: 10, - after: 'cursor-after-value', // Simulate an 'after' cursor - } - - // Mock findAll and count methods - mockEndorsementListModel.findAll.mockResolvedValue([mockEndorsementList]) - mockEndorsementListModel.count.mockResolvedValue(1) - - // Call the service method - await service.findListsGenericQuery(query) - - // Ensure that pagination params like 'limit' and 'after' cursor are passed - expect(mockEndorsementListModel.findAll).toHaveBeenCalledWith( - expect.objectContaining({ - limit: 10, - where: expect.anything(), // The `where` condition should still be there - // No 'offset', but check 'after' or 'before' is being handled correctly by pagination logic - }), - ) - }) - + it('should handle missing owner email', async () => { + const listWithoutEmail = { + ...mockEndorsementList, + meta: {}, + } + await expect(service.emailLock(listWithoutEmail)).rejects.toThrow( + NotFoundException, + ) + }) - it('should handle invalid cursor values', async () => { - const query = { - limit: 10, - after: 'invalid-cursor' - } - mockEndorsementListModel.findAll.mockResolvedValue([]) - mockEndorsementListModel.count.mockResolvedValue(0) + it('should handle email service failures', async () => { + mockEmailService.sendEmail.mockRejectedValue(new Error('Email failed')) - const result = await service.findListsGenericQuery(query) - expect(result.pageInfo.hasNextPage).toBe(false) - }) -}) + const result = await service.emailCreated(mockEndorsementList) + expect(result.success).toBe(false) + }) + }) + + describe('List query functionality', () => { + it('should handle pagination parameters', async () => { + const query = { + limit: 10, + after: 'cursor-after-value', // Simulate an 'after' cursor + } + + // Mock findAll and count methods + mockEndorsementListModel.findAll.mockResolvedValue([ + mockEndorsementList, + ]) + mockEndorsementListModel.count.mockResolvedValue(1) + + // Call the service method + await service.findListsGenericQuery(query) + + // Ensure that pagination params like 'limit' and 'after' cursor are passed + expect(mockEndorsementListModel.findAll).toHaveBeenCalledWith( + expect.objectContaining({ + limit: 10, + where: expect.anything(), // The `where` condition should still be there + // No 'offset', but check 'after' or 'before' is being handled correctly by pagination logic + }), + ) + }) -// describe('Core list operations', () => { - describe('open', () => { - it('should handle opening a list with new date', async () => { - const newDate = { closedDate: new Date('2025-01-01') } - const list = { ...mockEndorsementList, update: jest.fn() } - list.update.mockResolvedValue({ ...list, ...newDate }) + it('should handle invalid cursor values', async () => { + const query = { + limit: 10, + after: 'invalid-cursor', + } + mockEndorsementListModel.findAll.mockResolvedValue([]) + mockEndorsementListModel.count.mockResolvedValue(0) - const result = await service.open(list, newDate) - expect(result.closedDate).toEqual(newDate.closedDate) + const result = await service.findListsGenericQuery(query) + expect(result.pageInfo.hasNextPage).toBe(false) + }) }) - }) - describe('updateEndorsementList', () => { - it('should update multiple fields', async () => { - const updates = { - title: 'Updated Title', - description: 'Updated Description', - openedDate: new Date('2024-01-01'), - closedDate: new Date('2024-12-31') - } - const list = { ...mockEndorsementList, update: jest.fn() } - list.update.mockResolvedValue({ ...list, ...updates }) + // describe('Core list operations', () => { + describe('open', () => { + it('should handle opening a list with new date', async () => { + const newDate = { closedDate: new Date('2025-01-01') } + const list = { ...mockEndorsementList, update: jest.fn() } + list.update.mockResolvedValue({ ...list, ...newDate }) - const result = await service.updateEndorsementList(list, updates) - expect(result.title).toBe(updates.title) - expect(result.description).toBe(updates.description) + const result = await service.open(list, newDate) + expect(result.closedDate).toEqual(newDate.closedDate) + }) }) - }) + describe('updateEndorsementList', () => { + it('should update multiple fields', async () => { + const updates = { + title: 'Updated Title', + description: 'Updated Description', + openedDate: new Date('2024-01-01'), + closedDate: new Date('2024-12-31'), + } + const list = { ...mockEndorsementList, update: jest.fn() } + list.update.mockResolvedValue({ ...list, ...updates }) + const result = await service.updateEndorsementList(list, updates) + expect(result.title).toBe(updates.title) + expect(result.description).toBe(updates.description) + }) + }) describe('updateEndorsementList', () => { it('should update multiple fields', async () => { @@ -802,66 +811,68 @@ describe('List query functionality', () => { }) }) - it('should handle presigned URL errors gracefully', async () => { const listId = 'test-id' const fileType = 'pdf' - + // Mock the file upload to succeed mockAwsService.uploadFile.mockResolvedValue(undefined) - + // Simulate an error when generating the presigned URL - mockAwsService.getPresignedUrl.mockRejectedValue(new Error('Presigned URL generation failed')) - + mockAwsService.getPresignedUrl.mockRejectedValue( + new Error('Presigned URL generation failed'), + ) + // Mock fetchEndorsementList to return a valid endorsement list (bypassing the NotFoundException) mockEndorsementListModel.findOne.mockResolvedValue(mockEndorsementList) - mockNationalRegistryService.getName.mockResolvedValue({ fulltNafn: 'Test Owner' }) - + mockNationalRegistryService.getName.mockResolvedValue({ + fulltNafn: 'Test Owner', + }) + // Expect the service to throw an error when presigned URL generation fails - await expect(service.exportList(listId, mockUser, fileType)).rejects.toThrow('Presigned URL generation failed') - + await expect( + service.exportList(listId, mockUser, fileType), + ).rejects.toThrow('Presigned URL generation failed') + // Ensure the file upload was attempted expect(mockAwsService.uploadFile).toHaveBeenCalled() - + // Ensure the presigned URL generation was attempted and failed expect(mockAwsService.getPresignedUrl).toHaveBeenCalled() }) - it('should handle file size limits', async () => { const listId = 'test-id' const fileType = 'csv' - + // Simulate a list with a large number of endorsements const largeEndorsementList = { ...mockEndorsementList, endorsements: Array(1000).fill({ created: new Date(), - meta: { fullName: 'Test User', locality: 'Test City' } - }) + meta: { fullName: 'Test User', locality: 'Test City' }, + }), } - + // Mock findOne to return a large endorsement list mockEndorsementListModel.findOne.mockResolvedValue(largeEndorsementList) - + // Mock file upload to succeed mockAwsService.uploadFile.mockResolvedValue(undefined) - + // Mock presigned URL generation to succeed mockAwsService.getPresignedUrl.mockResolvedValue('https://test-url.com') - + // Call the exportList method and expect success const result = await service.exportList(listId, mockUser, fileType) - + // Check that a presigned URL was generated and returned expect(result.url).toBeDefined() - + // Ensure the file upload was attempted expect(mockAwsService.uploadFile).toHaveBeenCalled() - + // Ensure the presigned URL generation was successful expect(mockAwsService.getPresignedUrl).toHaveBeenCalled() }) - }) - diff --git a/apps/services/endorsements/api/src/app/modules/endorsementList/endorsementList.service.ts b/apps/services/endorsements/api/src/app/modules/endorsementList/endorsementList.service.ts index d80037a4ba99..8dc95c1e471c 100644 --- a/apps/services/endorsements/api/src/app/modules/endorsementList/endorsementList.service.ts +++ b/apps/services/endorsements/api/src/app/modules/endorsementList/endorsementList.service.ts @@ -252,21 +252,20 @@ export class EndorsementListService { closedDate: { [Op.gt]: dateOb }, adminLock: false, } - + const paginatedResult = await this.findListsGenericQuery(query, where) - + // Check if no results were found and throw NotFoundException if (paginatedResult.totalCount === 0) { throw new NotFoundException() } - + return paginatedResult } catch (error) { this.logger.warn('findOpenListsTaggedGeneralPetition not found') throw new NotFoundException() } } - async findSingleOpenListTaggedGeneralPetition( listId: string,