-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add redis health indicator and improved unit test
- Loading branch information
Showing
8 changed files
with
322 additions
and
84 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,69 @@ | ||
import { RedisModuleOptions } from '@nestjs-modules/ioredis'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { config } from 'dotenv'; | ||
import { getConfigValue } from '../utils/helper'; | ||
|
||
// Load environment variables for CLI usage | ||
config(); | ||
|
||
export class RedisConfig { | ||
constructor(private readonly configService?: ConfigService) {} | ||
|
||
configureOptions(): RedisModuleOptions { | ||
return { | ||
type: 'single', | ||
options: { | ||
host: getConfigValue<string>( | ||
'REDIS_HOST', | ||
'localhost', | ||
this.configService, | ||
), | ||
port: Number( | ||
getConfigValue<string>('REDIS_PORT', '6379', this.configService), | ||
), | ||
username: getConfigValue<string>( | ||
'REDIS_USERNAME', | ||
'', | ||
this.configService, | ||
), | ||
password: getConfigValue<string>( | ||
'REDIS_PASSWORD', | ||
'', | ||
this.configService, | ||
), | ||
db: Number(getConfigValue<string>('REDIS_DB', '0', this.configService)), | ||
maxRetriesPerRequest: Number( | ||
getConfigValue<string>('REDIS_MAX_RETRIES', '3', this.configService), | ||
), | ||
connectTimeout: Number( | ||
getConfigValue<string>( | ||
'REDIS_CONNECT_TIMEOUT', | ||
'10000', | ||
this.configService, | ||
), | ||
), | ||
retryStrategy: (times: number) => | ||
Math.min( | ||
times * | ||
Number( | ||
getConfigValue<string>( | ||
'REDIS_RETRY_DELAY', | ||
'50', | ||
this.configService, | ||
), | ||
), | ||
Number( | ||
getConfigValue<string>( | ||
'REDIS_RETRY_DELAY_MAX', | ||
'2000', | ||
this.configService, | ||
), | ||
), | ||
), | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
export default (configService?: ConfigService) => | ||
new RedisConfig(configService).configureOptions(); |
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,59 +1,31 @@ | ||
import { EntityManager, EntityRepository } from '@mikro-orm/core'; | ||
import { getRepositoryToken } from '@mikro-orm/nestjs'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CacheModule } from '../cache/cache.module'; | ||
import { CacheService } from '../cache/cache.service'; | ||
import { MiscModule } from '../misc/misc.module'; | ||
import { User } from '../user/entities/user.entity'; | ||
import { UserTransformer } from '../user/transformer/user.transformer'; | ||
import { UserService } from '../user/user.service'; | ||
import { AuthService } from './auth.service'; | ||
import { JwtStrategy } from './strategies/jwt.strategy'; | ||
|
||
const mockCacheService = { | ||
get: jest.fn(), | ||
set: jest.fn(), | ||
del: jest.fn(), | ||
delAll: jest.fn(), | ||
const mockUserService = { | ||
findByEmailWithRoleAndPermissions: jest.fn().mockResolvedValue({ | ||
id: 1, | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
password: '$2b$10$q81aKunjGaLbPvt5biUjFeSXLKhXVsMtsNxF8.Nwjx8I5l7OcU7sy', | ||
isActive: true, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
roles: [], | ||
permissions: [], | ||
}), | ||
updateLoginDate: jest.fn(), | ||
}; | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
let mockUserRepository: Partial<EntityRepository<User>>; | ||
let mockEntityManager: Partial<EntityManager>; | ||
let mockConfigService: Partial<ConfigService>; | ||
|
||
beforeEach(async () => { | ||
mockUserRepository = { | ||
findOne: jest.fn().mockReturnValue({ | ||
id: 1, | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
password: | ||
'$2b$10$q81aKunjGaLbPvt5biUjFeSXLKhXVsMtsNxF8.Nwjx8I5l7OcU7sy', | ||
isActive: true, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}), | ||
create: jest.fn().mockImplementation((dto) => ({ | ||
...dto, | ||
id: 1, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
})), | ||
assign: jest.fn().mockImplementation((user, dto) => { | ||
Object.assign(user, dto); | ||
}), | ||
}; | ||
|
||
mockEntityManager = { | ||
persistAndFlush: jest.fn(), | ||
flush: jest.fn(), | ||
removeAndFlush: jest.fn(), | ||
}; | ||
|
||
mockConfigService = { | ||
get: jest.fn((key: string) => { | ||
if (key === 'JWT_SECRET') { | ||
|
@@ -70,25 +42,20 @@ describe('AuthService', () => { | |
signOptions: { expiresIn: '1h' }, | ||
}), | ||
MiscModule, | ||
CacheModule, | ||
], | ||
providers: [ | ||
AuthService, | ||
UserService, | ||
UserTransformer, | ||
{ | ||
provide: UserService, | ||
useValue: mockUserService, | ||
}, | ||
{ | ||
provide: JwtStrategy, | ||
useFactory: (configService: ConfigService) => | ||
new JwtStrategy(configService), | ||
inject: [ConfigService], | ||
}, | ||
{ provide: ConfigService, useValue: mockConfigService }, | ||
{ provide: EntityManager, useValue: mockEntityManager }, | ||
{ provide: getRepositoryToken(User), useValue: mockUserRepository }, | ||
{ | ||
provide: CacheService, | ||
useValue: mockCacheService, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
|
@@ -108,8 +75,8 @@ describe('AuthService', () => { | |
isActive: true, | ||
createdAt: expect.any(Date), | ||
AccessToken: expect.any(String), | ||
roles: undefined, | ||
permissions: undefined, | ||
roles: [], | ||
permissions: [], | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import { RedisModule } from '@nestjs-modules/ioredis'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
import { Module } from '@nestjs/common'; | ||
import { TerminusModule } from '@nestjs/terminus'; | ||
import { HealthController } from './health.controller'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
import { RedisHealthIndicator } from './redis-health-indicator.service'; | ||
|
||
@Module({ | ||
imports: [TerminusModule, HttpModule], | ||
imports: [TerminusModule, HttpModule, RedisModule], | ||
controllers: [HealthController], | ||
providers: [RedisHealthIndicator], | ||
}) | ||
export class HealthModule {} |
Oops, something went wrong.