Skip to content

Commit

Permalink
add findAll(GET) method in the campaign-application.service(second tr…
Browse files Browse the repository at this point in the history
…y) (#652)

* add create campaign agreements  validation

if any of the 3 agreements for creating campaign are not checked -> throw a HttpException with status 405

* fix http error status code in campaign-application.service

http error code is now 400 in validation for all 3 agreements  in create method

* fix throw new BadRequestException

changed the error from HttpException to BadRequestException for the agreements validation in create method in campaign-application.service.ts

* injected prisma service

* add findAll camapings method

created findAll functionality in campaign-application.service and add testing for the method

* reverted commit

* fix test for findAll (campaign-applications) method

new the mocked campaign-applications in the tests have the right schema(schema of the campaign-application)

* working on "cleaning my branches"

* Revert "fix test for findAll (campaign-applications) method"

This reverts commit cb14fdf.

* add findAll(GET) method in the campaign-application.service

* fix campaign-application controller and services

remove unused providers in campaign-application.spec.ts
add validation if user is admin in findAll method in campaign-application.controller.ts
add tests for findAll method

* remove commented code
  • Loading branch information
Martbul authored Jul 1, 2024
1 parent 544c19d commit fec31a0
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Test, TestingModule } from '@nestjs/testing'
import { CampaignApplicationController } from './campaign-application.controller'
import { CampaignApplicationService } from './campaign-application.service'
import { SpyOf, autoSpy } from '@podkrepi-bg/testing'
import { ForbiddenException } from '@nestjs/common'
import { KeycloakTokenParsed, isAdmin } from '../auth/keycloak'

jest.mock('../auth/keycloak')

describe('CampaignApplicationController', () => {
let controller: CampaignApplicationController
Expand Down Expand Up @@ -43,14 +47,27 @@ describe('CampaignApplicationController', () => {
})
})

it('when findAll called it should delegate to the service findAll', () => {
it('when findAll called by a non-admin user it should throw a ForbiddenException', () => {
// arrange
const user = { sub: 'non-admin', 'allowed-origins': ['test'] } as KeycloakTokenParsed
;(isAdmin as jest.Mock).mockReturnValue(false)

// act & assert
expect(() => controller.findAll(user)).toThrow(ForbiddenException)
})

it('when findAll called by an admin user it should delegate to the service findAll', () => {
// arrange
const user = { sub: 'admin', 'allowed-origins': ['test'] } as KeycloakTokenParsed
;(isAdmin as jest.Mock).mockReturnValue(true)

// act
controller.findAll()
controller.findAll(user)

// assert
expect(service.findAll).toHaveBeenCalledWith()
})

it('when findOne called it should delegate to the service findOne', () => {
// arrange
// act
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Post, Body, Patch, Param } from '@nestjs/common'
import { Controller, Get, Post, Body, Patch, Param, ForbiddenException } from '@nestjs/common'
import { CampaignApplicationService } from './campaign-application.service'
import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto'
import { UpdateCampaignApplicationDto } from './dto/update-campaign-application.dto'
Expand All @@ -19,7 +19,10 @@ export class CampaignApplicationController {
}

@Get('list')
findAll() {
findAll(@AuthenticatedUser() user: KeycloakTokenParsed) {
if (!isAdmin(user)) {
throw new ForbiddenException('Must be admin to get all campaign-applications')
}
return this.campaignApplicationService.findAll()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Module } from '@nestjs/common'
import { CampaignApplicationService } from './campaign-application.service'
import { CampaignApplicationController } from './campaign-application.controller'

import { PrismaModule } from '../prisma/prisma.module'
@Module({
imports: [PrismaModule],
controllers: [CampaignApplicationController],
providers: [CampaignApplicationService],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import { Test, TestingModule } from '@nestjs/testing'
import { CampaignApplicationService } from './campaign-application.service'
import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto'
import { BadRequestException, HttpStatus } from '@nestjs/common'
import { BadRequestException } from '@nestjs/common'
import { CampaignApplicationState, CampaignTypeCategory } from '@prisma/client'
import { prismaMock, MockPrismaService } from '../prisma/prisma-client.mock'
import { EmailService } from '../email/email.service'

describe('CampaignApplicationService', () => {
let service: CampaignApplicationService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [CampaignApplicationService],
providers: [
CampaignApplicationService,
MockPrismaService,
{
provide: EmailService,
useValue: {
sendFromTemplate: jest.fn(() => true),
},
},
],
}).compile()

service = module.get<CampaignApplicationService>(CampaignApplicationService)
Expand All @@ -28,9 +40,10 @@ describe('CampaignApplicationService', () => {
organizerBeneficiaryRel: 'Test Relation',
goal: 'Test Goal',
amount: '1000',
toEntity: jest.fn(), // Mock implementation
toEntity: jest.fn(),
}
it('should throw an error if acceptTermsAndConditions are not accepted', () => {

it('should throw an error if acceptTermsAndConditions is false', () => {
const dto: CreateCampaignApplicationDto = {
...baseDto,
acceptTermsAndConditions: false,
Expand All @@ -43,7 +56,7 @@ describe('CampaignApplicationService', () => {
)
})

it('should throw an error if transparencyTermsAccepted are not accepted', () => {
it('should throw an error if transparencyTermsAccepted is false', () => {
const dto: CreateCampaignApplicationDto = {
...baseDto,
acceptTermsAndConditions: true,
Expand All @@ -56,7 +69,7 @@ describe('CampaignApplicationService', () => {
)
})

it('should throw an error if personalInformationProcessingAccepted is not accepted', () => {
it('should throw an error if personalInformationProcessingAccepted is false', () => {
const dto: CreateCampaignApplicationDto = {
...baseDto,
acceptTermsAndConditions: true,
Expand All @@ -69,7 +82,7 @@ describe('CampaignApplicationService', () => {
)
})

it('should add a new campaign application if all agreements are accepted', () => {
it('should add a new campaign-application if all agreements are true', () => {
const dto: CreateCampaignApplicationDto = {
...baseDto,
acceptTermsAndConditions: true,
Expand All @@ -80,4 +93,81 @@ describe('CampaignApplicationService', () => {
expect(service.create(dto)).toBe('This action adds a new campaignApplication')
})
})
})

describe('findAll', () => {
it('should return an array of campaign-applications', async () => {
const mockCampaigns = [
{
id: 'testId',
createdAt: new Date('2022-04-08T06:36:33.661Z'),
updatedAt: new Date('2022-04-08T06:36:33.662Z'),
description: 'Test description',
organizerId: 'testOrganizerId1',
organizerName: 'Test Organizer1',
organizerEmail: '[email protected]',
beneficiary: 'test beneficary',
organizerPhone: '123456789',
organizerBeneficiaryRel: 'Test Relation',
campaignName: 'Test Campaign',
goal: 'Test Goal',
history: 'test history',
amount: '1000',
campaignGuarantee: 'test campaignGuarantee',
otherFinanceSources: 'test otherFinanceSources',
otherNotes: 'test otherNotes',
state: CampaignApplicationState.review,
category: CampaignTypeCategory.medical,
ticketURL: 'testsodifhso',
archived: false,
},
{
id: 'testId2',
createdAt: new Date('2022-04-08T06:36:33.661Z'),
updatedAt: new Date('2022-04-08T06:36:33.662Z'),
description: 'Test description',
organizerId: 'testOrganizerId2',
organizerName: 'Test Organizer2',
organizerEmail: '[email protected]',
beneficiary: 'test beneficary2',
organizerPhone: '123456789',
organizerBeneficiaryRel: 'Test Relation2',
campaignName: 'Test Campaign2',
goal: 'Test Goal2',
history: 'test history2',
amount: '2000',
campaignGuarantee: 'test campaignGuarantee2',
otherFinanceSources: 'test otherFinanceSources2',
otherNotes: 'test otherNotes2',
state: CampaignApplicationState.review,
category: CampaignTypeCategory.medical,
ticketURL: 'testsodifhso2',
archived: false,
},
]

prismaMock.campaignApplication.findMany.mockResolvedValue(mockCampaigns)

const result = await service.findAll()

expect(result).toEqual(mockCampaigns)
expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1)
})

it('should return an empty array if no campaign-applications are found', async () => {
prismaMock.campaignApplication.findMany.mockResolvedValue([])

const result = await service.findAll()

expect(result).toEqual([])
expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1)
})

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

await expect(service.findAll()).rejects.toThrow(errorMessage)
expect(prismaMock.campaignApplication.findMany).toHaveBeenCalledTimes(1)
})
})
})
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { BadRequestException, HttpException, HttpStatus, Injectable } from '@nestjs/common'

Check warning on line 1 in apps/api/src/campaign-application/campaign-application.service.ts

View workflow job for this annotation

GitHub Actions / Run API tests

'HttpException' is defined but never used
import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto'
import { UpdateCampaignApplicationDto } from './dto/update-campaign-application.dto'
import { PrismaService } from '../prisma/prisma.service'

@Injectable()
export class CampaignApplicationService {
constructor(private prisma: PrismaService) {}
async getCampaignByIdWithPersonIds(id: string): Promise<UpdateCampaignApplicationDto> {
throw new Error('Method not implemented.')
}
Expand All @@ -20,7 +22,7 @@ export class CampaignApplicationService {
}

findAll() {
return `This action returns all campaignApplication`
return this.prisma.campaignApplication.findMany()
}

findOne(id: string) {
Expand Down

0 comments on commit fec31a0

Please sign in to comment.