Skip to content

Commit

Permalink
chore: Adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sashko9807 committed Sep 2, 2024
1 parent dbc25b5 commit 28e144e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
20 changes: 13 additions & 7 deletions apps/api/src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Beneficiary, Person } from '@prisma/client'
import { Beneficiary, Person, Prisma } from '@prisma/client'

Check warning on line 1 in apps/api/src/auth/auth.service.spec.ts

View workflow job for this annotation

GitHub Actions / Run API tests

'Prisma' is defined but never used
import { mockDeep } from 'jest-mock-extended'
import { ConfigService } from '@nestjs/config'
import { HttpService } from '@nestjs/axios'
Expand Down Expand Up @@ -448,7 +448,7 @@ describe('AuthService', () => {
// Don't subscribe to marketing list
newsletter: false,
})

jest.spyOn(prismaMock.person, 'upsert').mockResolvedValue(person)
jest.spyOn(admin.users, 'create').mockResolvedValue({ id: keycloakId })
const marketingSpy = jest
Expand All @@ -463,7 +463,7 @@ describe('AuthService', () => {
})

describe('deleteUser', () => {
const corporatePerson: any = {
const corporatePerson: Awaited<ReturnType<PersonService['findOneByKeycloakId']>> = {
id: 'e43348aa-be33-4c12-80bf-2adfbf8736cd',
firstName: 'Admin',
lastName: 'Dev',
Expand All @@ -483,6 +483,10 @@ describe('AuthService', () => {
profileEnabled: false,
beneficiaries: [],
organizer: null,
deletedAt: null,
helpUsImprove: true,
company: null,
recurringDonations: [],
}

it('should delete user successfully', async () => {
Expand All @@ -497,15 +501,17 @@ describe('AuthService', () => {
.mockResolvedValueOnce('')

const adminDeleteSpy = jest.spyOn(admin.users, 'del').mockResolvedValueOnce()
const prismaDeleteSpy = jest.spyOn(prismaMock.person, 'delete').mockResolvedValueOnce(person)
const prismaDeleteSpy = jest
.spyOn(personService, 'softDelete')
.mockResolvedValueOnce(corporatePerson)
const loggerLogSpy = jest.spyOn(Logger, 'log')

await expect(service.deleteUser(keycloakId)).resolves.not.toThrow()

expect(personSpy).toHaveBeenCalledOnce()
expect(authenticateAdminSpy).toHaveBeenCalledTimes(1)
expect(adminDeleteSpy).toHaveBeenCalledWith({ id: keycloakId })
expect(prismaDeleteSpy).toHaveBeenCalledWith({ where: { keycloakId } })
expect(prismaDeleteSpy).toHaveBeenCalledWith(corporatePerson.id)
expect(loggerLogSpy).toHaveBeenCalledWith(
`User with keycloak id ${keycloakId} was successfully deleted!`,
)
Expand Down Expand Up @@ -551,7 +557,7 @@ describe('AuthService', () => {
const adminDeleteSpy = jest.spyOn(admin.users, 'del').mockResolvedValueOnce()

const prismaDeleteSpy = jest
.spyOn(prismaMock.person, 'delete')
.spyOn(personService, 'softDelete')
.mockRejectedValueOnce(new Error('Prisma Rejection!'))

const loggerLogSpy = jest.spyOn(Logger, 'error')
Expand All @@ -561,7 +567,7 @@ describe('AuthService', () => {
expect(personSpy).toHaveBeenCalledOnce()
expect(authenticateAdminSpy).toHaveBeenCalledTimes(1)
expect(adminDeleteSpy).toHaveBeenCalledWith({ id: keycloakId })
expect(prismaDeleteSpy).toHaveBeenCalledWith({ where: { keycloakId } })
expect(prismaDeleteSpy).toHaveBeenCalledWith(corporatePerson.id)
expect(loggerLogSpy).toHaveBeenCalledWith(
`Deleting user fails with reason: Prisma Rejection!`,
)
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ export class AuthService {
)
}

if (user.recurringDonations.length) {
if (user.recurringDonations?.length) {
throw new ForbiddenException(
`Account cannot be deleted due to active recurring payments. Please cancel all recurring payments before deleting this account`,
)
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/person/person.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class PersonService {
lastName: '',
address: '',
email: '',
birthday: '',
birthday: null,
personalNumber: '',
phone: '',
helpUsImprove: false,
Expand Down

0 comments on commit 28e144e

Please sign in to comment.