Skip to content

Commit

Permalink
chore: add tests for campaign application file fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
gparlakov committed Sep 29, 2024
1 parent 1e30672 commit d12e2f8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('CampaignApplicationService', () => {
const mockS3Service = {
uploadObject: jest.fn(),
deleteObject: jest.fn(),
streamFile: jest.fn().mockResolvedValue(1234),
}

const mockEmailService = {
Expand Down Expand Up @@ -290,6 +291,18 @@ describe('CampaignApplicationService', () => {

expect(result).toEqual(mockSingleCampaignApplication)
expect(prismaMock.campaignApplication.findUnique).toHaveBeenCalledTimes(1)
expect(prismaMock.campaignApplication.findUnique).toHaveBeenCalledWith({
where: { id: 'id' },
include: {
documents: {
select: {
id: true,
filename: true,
mimetype: true,
},
},
},
})
})

it('should throw a NotFoundException if no campaign-application is found', async () => {
Expand Down Expand Up @@ -422,4 +435,72 @@ describe('CampaignApplicationService', () => {
})
})
})

describe('getFile', () => {
it('should return a single campaign-application file', async () => {
prismaMock.campaignApplication.findFirst.mockResolvedValue(mockSingleCampaignApplication)
prismaMock.campaignApplicationFile.findFirst.mockResolvedValue({
id: '123',
filename: 'my-file',
} as File)

const result = await service.getFile('id', false, mockPerson)

expect(result).toEqual({
filename: 'my-file',
stream: 1234,
})
expect(prismaMock.campaignApplication.findFirst).toHaveBeenCalledTimes(1)
expect(prismaMock.campaignApplication.findFirst).toHaveBeenCalledWith({
where: {
documents: {
some: {
id: 'id',
},
},
},
})

expect(prismaMock.campaignApplicationFile.findFirst).toHaveBeenNthCalledWith(1, {
where: { id: 'id' },
})
})

it('should throw a NotFoundException if no campaign-application is found', async () => {
prismaMock.campaignApplication.findUnique.mockResolvedValue(null)

await expect(service.getFile('id', false, mockPerson)).rejects.toThrow(
new NotFoundException('File does not exist'),
)
})

it('should handle errors and throw an exception', async () => {
const errorMessage = 'error'
prismaMock.campaignApplication.findFirst.mockRejectedValue(new Error(errorMessage))

await expect(service.getFile('id', false, mockPerson)).rejects.toThrow(errorMessage)
})

it('should not allow non-admin users to see files belonging to other users', async () => {
prismaMock.campaignApplication.findFirst.mockResolvedValue(mockSingleCampaignApplication)
await expect(
service.getFile('id', false, { ...mockPerson, organizer: { id: 'different-id' } }),
).rejects.toThrow(
new ForbiddenException('User is not admin or organizer of the campaignApplication'),
)
})

it('should allow admin users to see files belonging to other users', async () => {
prismaMock.campaignApplication.findFirst.mockResolvedValue(mockSingleCampaignApplication)
prismaMock.campaignApplicationFile.findFirst.mockResolvedValue({
id: '123',
filename: 'my-file',
} as File)
await expect(
service.getFile('id', true, { ...mockPerson, organizer: { id: 'different-id' } }),
).resolves.not.toThrow(
new ForbiddenException('User is not admin or organizer of the campaignApplication'),
)
})
})
})
3 changes: 1 addition & 2 deletions apps/api/src/email/template.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export class CreateCampaignApplicationAdminEmailDto extends EmailTemplate<{
name = TemplateType.createCampaignApplicationAdmin
}


export class CreateCampaignApplicationOrganizerEmailDto extends EmailTemplate<{
campaignApplicationName: string
editLink?: string
Expand All @@ -122,4 +121,4 @@ export class CreateCampaignApplicationOrganizerEmailDto extends EmailTemplate<{
firstName: string
}> {
name = TemplateType.createCampaignApplicationOrganizer
}
}

0 comments on commit d12e2f8

Please sign in to comment.