generated from ministryofjustice/hmpps-template-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alee/decoupling auth client from redis (#11)
* Decoupling auth client from redis impl
- Loading branch information
1 parent
c8a5587
commit 1a11c21
Showing
10 changed files
with
114 additions
and
63 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,34 @@ | ||
import { RedisClient } from 'redis' | ||
import TokenStore from './tokenStore' | ||
|
||
const redisClient = { | ||
on: jest.fn(), | ||
get: jest.fn(), | ||
set: jest.fn(), | ||
} | ||
|
||
describe('tokenStore', () => { | ||
let tokenStore: TokenStore | ||
|
||
beforeEach(() => { | ||
tokenStore = new TokenStore((redisClient as unknown) as RedisClient) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it('Can retrieve token', async () => { | ||
redisClient.get.mockImplementation((key, callback) => callback(null, 'token-1')) | ||
|
||
await expect(tokenStore.getToken('user-1')).resolves.toBe('token-1') | ||
|
||
expect(redisClient.get).toHaveBeenCalledWith('user-1', expect.any(Function)) | ||
}) | ||
|
||
it('Can set token', async () => { | ||
await tokenStore.setToken('user-1', 'token-1', 10) | ||
|
||
expect(redisClient.set).toHaveBeenCalledWith('user-1', 'token-1', 'EX', 10, expect.any(Function)) | ||
}) | ||
}) |
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,38 @@ | ||
import redis from 'redis' | ||
import { promisify } from 'util' | ||
|
||
import logger from '../../log' | ||
import config from '../config' | ||
|
||
const createRedisClient = () => { | ||
return redis.createClient({ | ||
port: config.redis.port, | ||
password: config.redis.password, | ||
host: config.redis.host, | ||
tls: config.redis.tls_enabled === 'true' ? {} : false, | ||
prefix: 'systemToken:', | ||
}) | ||
} | ||
|
||
export default class TokenStore { | ||
private getRedisAsync: (key: string) => Promise<string> | ||
|
||
private setRedisAsync: (key: string, value: string, mode: string, durationSeconds: number) => Promise<void> | ||
|
||
constructor(redisClient: redis.RedisClient = createRedisClient()) { | ||
redisClient.on('error', error => { | ||
logger.error(error, `Redis error`) | ||
}) | ||
|
||
this.getRedisAsync = promisify(redisClient.get).bind(redisClient) | ||
this.setRedisAsync = promisify(redisClient.set).bind(redisClient) | ||
} | ||
|
||
public async setToken(key: string, token: string, durationSeconds: number): Promise<void> { | ||
this.setRedisAsync(key, token, 'EX', durationSeconds) | ||
} | ||
|
||
public async getToken(key: string): Promise<string> { | ||
return this.getRedisAsync(key) | ||
} | ||
} |
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