-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Persisting created new campaignApplication to db #657
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9cb5827
finish create functionality and testing on it
Martbul c8c270f
finish create functionality and testing on it
Martbul 81fcc96
restructured mocking of data in service.spec and controller.spec
Martbul 0bb1358
finish mocking data changes
Martbul 1aab97f
updated controller,spec mockis
Martbul 383040c
fix module providers
Martbul 509b20a
hardcoded testing values
Martbul 15849d5
delete unused imports & recover unit test
Martbul 33777b4
remove campaingApplication data sanitization
Martbul b16e032
importing moduls instead of providing services
Martbul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { KeycloakTokenParsed } from './keycloak' | ||
|
||
export const mockUser = { | ||
exp: 1719866987, | ||
iat: 1719866687, | ||
jti: '607c488f-6e18-4455-8384-161cec4f1940', | ||
iss: 'http://localhost:8180/auth/realms/webapp', | ||
aud: 'account', | ||
sub: '5795ea9e-ac11-436b-b97d-7b03dbd863f2', | ||
typ: 'Bearer', | ||
azp: 'jwt-headless', | ||
session_state: 'def317ff-0043-4509-ade3-926dd155085e', | ||
'allowed-origins': ['*'], | ||
realm_access: { roles: ['default-roles-webapp', 'offline_access', 'uma_authorization'] }, | ||
resource_access: { | ||
account: { roles: ['manage-account', 'manage-account-links', 'view-profile'] }, | ||
}, | ||
scope: 'openid profile email', | ||
sid: 'def317ff-0043-4509-ade3-926dd155085e', | ||
email_verified: 'true', | ||
name: 'asdasd sdfsdfsdfs', | ||
preferred_username: 'martbul01@gmail.com', | ||
given_name: 'asdasd', | ||
family_name: 'sdfsdfsdfs', | ||
email: 'martbul01@gmail.com', | ||
} as KeycloakTokenParsed | ||
|
||
export const mockUserAdmin = { | ||
exp: 1719866987, | ||
iat: 1719866687, | ||
jti: '607c488f-6e18-4455-8384-161cec4f1940', | ||
iss: 'http://localhost:8180/auth/realms/webapp', | ||
aud: 'account', | ||
sub: '5795ea9e-ac11-436b-b97d-7b03dbd863f2', | ||
typ: 'Bearer', | ||
azp: 'jwt-headless', | ||
session_state: 'def317ff-0043-4509-ade3-926dd155085e', | ||
'allowed-origins': ['*'], | ||
realm_access: { roles: ['default-roles-webapp', 'offline_access', 'uma_authorization'] }, | ||
resource_access: { account: { roles: ['manage-account', 'account-view-supporters'] } }, | ||
scope: 'openid profile email', | ||
sid: 'def317ff-0043-4509-ade3-926dd155085e', | ||
email_verified: 'true', | ||
name: 'asdasd sdfsdfsdfs', | ||
preferred_username: 'martbul01@gmail.com', | ||
given_name: 'asdasd', | ||
family_name: 'sdfsdfsdfs', | ||
email: 'martbul01@gmail.com', | ||
} as KeycloakTokenParsed |
87 changes: 87 additions & 0 deletions
87
apps/api/src/campaign-application/__mocks__/campaign-application-mocks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { CampaignApplicationState, CampaignTypeCategory } from '@prisma/client' | ||
import { CreateCampaignApplicationDto } from '../dto/create-campaign-application.dto' | ||
|
||
export const mockNewCampaignApplication = { | ||
campaignName: 'Test Campaign', | ||
organizerName: 'Test Organizer', | ||
organizerEmail: 'testemail@gmail.com', | ||
organizerPhone: '123456789', | ||
beneficiary: 'Test beneficary', | ||
organizerBeneficiaryRel: 'Test organizerBeneficiaryRel', | ||
goal: 'Test goal', | ||
history: 'Test history', | ||
amount: '1000', | ||
description: 'Test description', | ||
campaignGuarantee: 'Test guarantee', | ||
otherFinanceSources: 'Test otherFinanceSources', | ||
otherNotes: 'Test otherNotes', | ||
category: CampaignTypeCategory.medical, | ||
} | ||
|
||
const dto: CreateCampaignApplicationDto = { | ||
...mockNewCampaignApplication, | ||
acceptTermsAndConditions: true, | ||
transparencyTermsAccepted: true, | ||
personalInformationProcessingAccepted: true, | ||
toEntity: new CreateCampaignApplicationDto().toEntity, | ||
} | ||
|
||
export const mockCampaigns = [ | ||
{ | ||
id: '1', | ||
createdAt: new Date('2022-04-08T06:36:33.661Z'), | ||
updatedAt: new Date('2022-04-08T06:36:33.662Z'), | ||
description: 'Test description1', | ||
organizerId: 'testOrganizerId1', | ||
organizerName: 'Test Organizer1', | ||
organizerEmail: 'organizer1@example.com', | ||
beneficiary: 'test beneficary1', | ||
organizerPhone: '123456789', | ||
organizerBeneficiaryRel: 'Test Relation1', | ||
campaignName: 'Test Campaign1', | ||
goal: 'Test Goal1', | ||
history: 'test history1', | ||
amount: '1000', | ||
campaignGuarantee: 'test campaignGuarantee1', | ||
otherFinanceSources: 'test otherFinanceSources1', | ||
otherNotes: 'test otherNotes1', | ||
state: CampaignApplicationState.review, | ||
category: CampaignTypeCategory.medical, | ||
ticketURL: 'testsodifhso1', | ||
archived: false, | ||
}, | ||
{ | ||
id: '2', | ||
createdAt: new Date('2022-04-08T06:36:33.661Z'), | ||
updatedAt: new Date('2022-04-08T06:36:33.662Z'), | ||
description: 'Test description2', | ||
organizerId: 'testOrganizerId2', | ||
organizerName: 'Test Organizer2', | ||
organizerEmail: 'organizer2@example.com', | ||
beneficiary: 'test beneficary2', | ||
organizerPhone: '123456789', | ||
organizerBeneficiaryRel: 'Test Relation2', | ||
campaignName: 'Test Campaign2', | ||
goal: 'Test Goal2', | ||
history: 'test history2', | ||
amount: '1000', | ||
campaignGuarantee: 'test campaignGuarantee2', | ||
otherFinanceSources: 'test otherFinanceSources2', | ||
otherNotes: 'test otherNotes2', | ||
state: CampaignApplicationState.review, | ||
category: CampaignTypeCategory.medical, | ||
ticketURL: 'testsodifhso2', | ||
archived: false, | ||
}, | ||
] | ||
|
||
export const mockCreatedCampaignApplication = { | ||
id: 'mockCampaignApplicationId', | ||
createdAt: new Date('2022-04-08T06:36:33.661Z'), | ||
updatedAt: new Date('2022-04-08T06:36:33.662Z'), | ||
...mockNewCampaignApplication, | ||
organizerId: 'mockOrganizerId', | ||
state: CampaignApplicationState.review, | ||
ticketURL: null, | ||
archived: false, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,37 @@ | ||
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') | ||
import { autoSpy } from '@podkrepi-bg/testing' | ||
import { CreateCampaignApplicationDto } from './dto/create-campaign-application.dto' | ||
import { KeycloakTokenParsed } from '../auth/keycloak' | ||
import { ForbiddenException, NotFoundException } from '@nestjs/common' | ||
import { PersonService } from '../person/person.service' | ||
import { mockUser, mockUserAdmin } from './../auth/__mocks__' | ||
import { mockNewCampaignApplication } from './__mocks__/campaign-application-mocks' | ||
|
||
describe('CampaignApplicationController', () => { | ||
let controller: CampaignApplicationController | ||
let service: SpyOf<CampaignApplicationService> | ||
let service: CampaignApplicationService | ||
let personService: PersonService | ||
|
||
const mockCreateNewCampaignApplication = { | ||
...mockNewCampaignApplication, | ||
acceptTermsAndConditions: true, | ||
transparencyTermsAccepted: true, | ||
personalInformationProcessingAccepted: true, | ||
toEntity: new CreateCampaignApplicationDto().toEntity, | ||
} as CreateCampaignApplicationDto | ||
|
||
beforeEach(async () => { | ||
service = autoSpy(CampaignApplicationService) | ||
personService = autoSpy(PersonService) | ||
|
||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [CampaignApplicationController], | ||
providers: [{ provide: CampaignApplicationService, useValue: service }], | ||
providers: [ | ||
{ provide: CampaignApplicationService, useValue: service }, | ||
{ provide: PersonService, useValue: personService }, | ||
], | ||
}).compile() | ||
|
||
controller = module.get<CampaignApplicationController>(CampaignApplicationController) | ||
|
@@ -26,63 +41,63 @@ describe('CampaignApplicationController', () => { | |
expect(controller).toBeDefined() | ||
}) | ||
|
||
it('when create called it should delegate to the service create', () => { | ||
// arrange | ||
// act | ||
controller.create({ | ||
acceptTermsAndConditions: true, | ||
personalInformationProcessingAccepted: true, | ||
transparencyTermsAccepted: true, | ||
title: 'new ', | ||
toEntity: jest.fn(), | ||
}) | ||
|
||
// assert | ||
expect(service.create).toHaveBeenCalledWith({ | ||
acceptTermsAndConditions: true, | ||
personalInformationProcessingAccepted: true, | ||
transparencyTermsAccepted: true, | ||
title: 'new ', | ||
toEntity: expect.any(Function), | ||
}) | ||
it('when create called it should delegate to the service create', async () => { | ||
// Arrange | ||
jest.spyOn(personService, 'findOneByKeycloakId').mockResolvedValue(mockUser) | ||
|
||
// Act | ||
await controller.create(mockCreateNewCampaignApplication, mockUser) | ||
|
||
// Assert | ||
expect(service.create).toHaveBeenCalledWith(mockCreateNewCampaignApplication, mockUser) | ||
}) | ||
|
||
it('when create called with wrong user it should throw NotFoundException', async () => { | ||
jest.spyOn(personService, 'findOneByKeycloakId').mockResolvedValue(null) | ||
|
||
// Act & Assert | ||
await expect(controller.create(mockCreateNewCampaignApplication, mockUser)).rejects.toThrow( | ||
NotFoundException, | ||
) | ||
}) | ||
|
||
it('when findAll called by a non-admin user it should throw a ForbiddenException', () => { | ||
// arrange | ||
jest.mock('../auth/keycloak', () => ({ | ||
isAdmin: jest.fn().mockReturnValue(false), | ||
})) | ||
|
||
// Arrange | ||
const user = { sub: 'non-admin', 'allowed-origins': ['test'] } as KeycloakTokenParsed | ||
;(isAdmin as jest.Mock).mockReturnValue(false) | ||
|
||
// act & assert | ||
// 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(user) | ||
|
||
// assert | ||
expect(service.findAll).toHaveBeenCalledWith() | ||
jest.mock('../auth/keycloak', () => ({ | ||
isAdmin: jest.fn().mockImplementation((user: KeycloakTokenParsed) => { | ||
return user.resource_access?.account?.roles.includes('account-view-supporters') | ||
}), | ||
})) | ||
|
||
// Act & Assert | ||
expect(() => controller.findAll(mockUserAdmin)).not.toThrow(ForbiddenException) | ||
controller.findAll(mockUserAdmin) | ||
expect(service.findAll).toHaveBeenCalled() | ||
}) | ||
|
||
it('when findOne called it should delegate to the service findOne', () => { | ||
// arrange | ||
// act | ||
// Act | ||
controller.findOne('id') | ||
|
||
// assert | ||
// Assert | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. avoid such unnecessary changes - they make the code review that much harder |
||
expect(service.findOne).toHaveBeenCalledWith('id') | ||
}) | ||
|
||
it('when update called it should delegate to the service update', () => { | ||
// arrange | ||
// act | ||
// Act | ||
controller.update('1', {}, { sub: 'test', 'allowed-origins': ['test'] }) | ||
|
||
// assert | ||
// Assert | ||
expect(service.update).toHaveBeenCalledWith('1', {}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is deleted but the logic is still in use. Was this by design?