-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added unit tests for identity service to increase coverage upto 85%
- Loading branch information
1 parent
c00b5df
commit 09e6c5c
Showing
19 changed files
with
564 additions
and
52 deletions.
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
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,90 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AppController } from './app.controller'; | ||
import { | ||
HealthCheckError, | ||
HealthCheckService, | ||
HealthIndicatorResult, | ||
HttpHealthIndicator, | ||
TerminusModule, | ||
} from '@nestjs/terminus'; | ||
import { PrismaHealthIndicator } from './utils/prisma.health'; | ||
import { VaultHealthIndicator } from './utils/vault.health'; | ||
import { HealthCheckExecutor } from '@nestjs/terminus/dist/health-check/health-check-executor.service'; | ||
import { | ||
ERROR_LOGGER, | ||
getErrorLoggerProvider, | ||
} from '@nestjs/terminus/dist/health-check/error-logger/error-logger.provider'; | ||
import { getLoggerProvider, TERMINUS_LOGGER } from '@nestjs/terminus/dist/health-check/logger/logger.provider'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
import { ServiceUnavailableException } from '@nestjs/common'; | ||
|
||
describe('AppController', () => { | ||
let appController: AppController; | ||
let prismaHealthIndicator: PrismaHealthIndicator; | ||
let vaultHealthIndicator: VaultHealthIndicator; | ||
|
||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [ | ||
HttpModule, | ||
TerminusModule | ||
], | ||
controllers: [AppController], | ||
providers: [ | ||
getLoggerProvider(), | ||
getErrorLoggerProvider(), | ||
HealthCheckExecutor, | ||
HealthCheckError, | ||
HealthCheckService, | ||
HttpHealthIndicator, | ||
{ | ||
provide: PrismaHealthIndicator, | ||
useFactory: () => ({ | ||
isHealthy: jest.fn(), | ||
}), | ||
}, | ||
{ | ||
provide: VaultHealthIndicator, | ||
useFactory: () => ({ | ||
isHealthy: jest.fn(), | ||
}), | ||
}, | ||
], | ||
}).compile(); | ||
|
||
appController = module.get<AppController>(AppController); | ||
prismaHealthIndicator = module.get<PrismaHealthIndicator>(PrismaHealthIndicator); | ||
vaultHealthIndicator = module.get<VaultHealthIndicator>(VaultHealthIndicator); | ||
}); | ||
|
||
beforeEach(async () => { | ||
jest.restoreAllMocks(); | ||
}) | ||
|
||
describe('checkHealth', () => { | ||
it('should return a health check result with all services healthy', async () => { | ||
jest.spyOn(prismaHealthIndicator, 'isHealthy') | ||
.mockResolvedValue(new Promise((resolve) => { | ||
resolve({ db: { status: 'up' }} as HealthIndicatorResult); | ||
})); | ||
jest.spyOn(vaultHealthIndicator, 'isHealthy') | ||
.mockResolvedValue(new Promise((resolve) => { | ||
resolve({ vault: { status: 'up' }} as HealthIndicatorResult); | ||
})); | ||
const result = await appController.checkHealth(); | ||
expect(result.status).toEqual('ok'); | ||
expect(result.info.db.status).toEqual('up'); | ||
expect(result.info.vault.status).toEqual('up'); | ||
}); | ||
|
||
it('should return a health check result with one service unhealthy', async () => { | ||
jest.spyOn(prismaHealthIndicator, 'isHealthy') | ||
.mockRejectedValue(new HealthCheckError("Prisma health check failed", null)); | ||
jest.spyOn(vaultHealthIndicator, 'isHealthy') | ||
.mockResolvedValue(new Promise((resolve) => { | ||
resolve({ vault: { status: 'up' }} as HealthIndicatorResult); | ||
})); | ||
await expect(appController.checkHealth()).rejects.toThrow(ServiceUnavailableException); | ||
}); | ||
}); | ||
}); |
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,81 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthGuard } from './auth.guard'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
jest.mock('jwks-rsa', () => ({ | ||
__esModule: true, | ||
default: jest.fn(), | ||
})); | ||
|
||
describe('AuthGuard', () => { | ||
let guard: AuthGuard; | ||
let reflector: Reflector; | ||
let configService: ConfigService; | ||
let originalEnv: NodeJS.ProcessEnv; | ||
|
||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
AuthGuard, | ||
{ | ||
provide: Reflector, | ||
useValue: { | ||
get: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: ConfigService, | ||
useValue: { | ||
get: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
guard = module.get<AuthGuard>(AuthGuard); | ||
reflector = module.get<Reflector>(Reflector); | ||
configService = module.get<ConfigService>(ConfigService); | ||
}); | ||
|
||
beforeEach(async () => { | ||
originalEnv = { ...process.env }; | ||
process.env.ENABLE_AUTH = 'true'; | ||
jest.restoreAllMocks(); | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(guard).toBeDefined(); | ||
}); | ||
|
||
describe('canActivate', () => { | ||
it('should return true if isPublic is set to true', async () => { | ||
jest.spyOn(reflector, 'get').mockReturnValue(true); | ||
const result = await guard.canActivate({ getHandler: jest.fn() }); | ||
expect(result).toEqual(true); | ||
}); | ||
|
||
it('should return true if ENABLE_AUTH is false', async () => { | ||
process.env.ENABLE_AUTH = 'false'; | ||
jest.spyOn(reflector, 'get').mockReturnValue(false); | ||
jest.spyOn(configService, 'get').mockReturnValue('false'); | ||
const result = await guard.canActivate({ getHandler: jest.fn() }); | ||
expect(result).toEqual(true); | ||
}); | ||
|
||
it('should return false if no Bearer token found', async () => { | ||
jest.spyOn(reflector, 'get').mockReturnValue(false); | ||
jest.spyOn(configService, 'get').mockReturnValue('true'); | ||
const request = { headers: { authorization: 'InvalidToken' } }; | ||
Check failure Code scanning / CodeQL Hard-coded credentials Critical test
The hard-coded value "InvalidToken" is used as
authorization header Error loading related location Loading |
||
const result = await guard.canActivate({ getHandler: jest.fn(), switchToHttp: () => ({ getRequest: () => request }) }); | ||
expect(result).toEqual(false); | ||
}); | ||
|
||
// Add more test cases as needed to cover different scenarios | ||
}); | ||
|
||
afterEach(() => { | ||
// Restore the original process.env after the test | ||
process.env = { ...originalEnv }; | ||
}); | ||
}); |
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
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
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
1 change: 0 additions & 1 deletion
1
services/identity-service/src/did/dtos/GenerateDidResponse.dto.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.