Skip to content

Commit

Permalink
Fix redis in tests
Browse files Browse the repository at this point in the history
Correct set, get, keys, and ttl functions. Also wait for client ready in global setup
  • Loading branch information
benb116 committed Dec 2, 2021
1 parent 3d9f31b commit 96b4b88
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ server/.env
nginx.conf
server/logs/
**/*.log
coverage/
10 changes: 8 additions & 2 deletions server/db/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ const connObj = {
socket: { connectTimeout: 10000 },
};
const client = createClient(connObj);
const subscriber = createClient(connObj);
const subscriber = client.duplicate();

// Redis connection is not ready right away
// Attach a promise that is resolved once the connection is created.
// Can be used elsewhere as "await client.ready"
// to hold operation until connection is established
let clientReady;
client.ready = new Promise((res) => { clientReady = res; });

connectRedis();
async function connectRedis() {
await client.connect();
await client.ping().then((out) => {
if (out === 'PONG') logger.info(`Redis connected ${REDIS_HOST}:${REDIS_PORT}`);
if (out === 'PONG' && process.env.NODE_ENV !== 'test') {
logger.info(`Redis connected ${REDIS_HOST}:${REDIS_PORT}`);
}
});
clientReady();
}
Expand Down
16 changes: 6 additions & 10 deletions server/features/user/tests/evalPassReset.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
const { promisify } = require('util');
const cryptoRandomString = require('crypto-random-string');
const { client, rediskeys } = require('../../../db/redis');
const service = require('../services/evalPassReset.service');
const { ErrorTest } = require('../../util/util');
const config = require('../../../config');

const keys = promisify(client.keys).bind(client);
const ttl = promisify(client.ttl).bind(client);

describe('evalPassReset service', () => {
test('Valid request returns confirmation and redis key', async () => {
const email = '[email protected]';
const rand = cryptoRandomString({ length: config.verificationTokenLength, type: 'url-safe' });
await client.SET(rediskeys.passReset(rand), email, { EX: config.verificationTimeout * 60 });

// Check to make sure the redis key was set
const redisOutput = await keys('passReset:*');
const redisOutput = await client.KEYS('passReset:*');
expect(redisOutput.length).toBeGreaterThan(0);
const thekey = redisOutput[0];
expect(redisOutput[0].split('passReset:')[1].length).toBe(config.verificationTokenLength);
const thettl = await ttl(thekey).then(Number);
const thettl = await client.TTL(thekey).then(Number);
expect(thettl).toBeGreaterThan(0);

const output = await service({ token: rand, password: '12345678', confirmPassword: '12345678' });
Expand All @@ -32,11 +28,11 @@ describe('evalPassReset service', () => {
await client.SET(rediskeys.passReset(rand), email, { EX: config.verificationTimeout * 60 });

// Check to make sure the redis key was set
const redisOutput = await keys('passReset:*');
const redisOutput = await client.KEYS('passReset:*');
expect(redisOutput.length).toBeGreaterThan(0);
const thekey = redisOutput[0];
expect(redisOutput[0].split('passReset:')[1].length).toBe(config.verificationTokenLength);
const thettl = await ttl(thekey).then(Number);
const thettl = await client.TTL(thekey).then(Number);
expect(thettl).toBeGreaterThan(0);

try {
Expand All @@ -58,11 +54,11 @@ describe('evalPassReset service', () => {
await client.SET(rediskeys.passReset(rand), email, { EX: config.verificationTimeout * 60 });

// Check to make sure the redis key was set
const redisOutput = await keys('passReset:*');
const redisOutput = await client.KEYS('passReset:*');
expect(redisOutput.length).toBeGreaterThan(0);
const thekey = redisOutput[0];
expect(redisOutput[0].split('passReset:')[1].length).toBe(config.verificationTokenLength);
const thettl = await ttl(thekey).then(Number);
const thettl = await client.TTL(thekey).then(Number);
expect(thettl).toBeGreaterThan(0);

try {
Expand Down
8 changes: 2 additions & 6 deletions server/features/user/tests/genPassReset.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
const { promisify } = require('util');
const service = require('../services/genPassReset.service');
const { ErrorTest } = require('../../util/util');

const { client } = require('../../../db/redis');
const config = require('../../../config');

const keys = promisify(client.keys).bind(client);
const ttl = promisify(client.ttl).bind(client);

describe('genPassReset service', () => {
test('Valid request returns confirmation and redis key', async () => {
const output = await service({ email: '[email protected]' });
expect(output).toMatch(/^.*resetPassword.*$/);

// Check to make sure the redis key was set
const redisOutput = await keys('passReset:*');
const redisOutput = await client.KEYS('passReset:*');
expect(redisOutput.length).toBeGreaterThan(0);
const thekey = redisOutput[0];
expect(redisOutput[0].split('passReset:')[1].length).toBe(config.verificationTokenLength);
const thettl = await ttl(thekey).then(Number);
const thettl = await client.TTL(thekey).then(Number);
expect(thettl).toBeGreaterThan(0);
});

Expand Down
8 changes: 2 additions & 6 deletions server/features/user/tests/genVerify.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
const { promisify } = require('util');
const service = require('../services/genVerify.service');
const { ErrorTest } = require('../../util/util');

const { client } = require('../../../db/redis');
const config = require('../../../config');

const keys = promisify(client.keys).bind(client);
const ttl = promisify(client.ttl).bind(client);

describe('genVerify service', () => {
test('Valid request returns confirmation and redis key', async () => {
const output = await service({ email: '[email protected]' });
expect(output).toStrictEqual({ needsVerification: true });

// Check to make sure the redis key was set
const redisOutput = await keys('emailVer:*');
const redisOutput = await client.KEYS('emailVer:*');
expect(redisOutput.length).toBeGreaterThan(0);
const thekey = redisOutput[0];
expect(redisOutput[0].split('emailVer:')[1].length).toBe(config.verificationTokenLength);
const thettl = await ttl(thekey).then(Number);
const thettl = await client.TTL(thekey).then(Number);
expect(thettl).toBeGreaterThan(0);
});

Expand Down
3 changes: 3 additions & 0 deletions server/jest/jestGlobalSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ const path = require('path');
const fs = require('fs');
const sequelize = require('../db');
const PopulateDB = require('../db/dbpopulate');
const { client } = require('../db/redis');

const initSQL = fs.readFileSync(path.resolve(__dirname, '../db/dbinit.sql'), 'utf8');

module.exports = async () => {
await client.ready;
await client.FLUSHALL();
await sequelize.query(initSQL);
await PopulateDB();
};
4 changes: 2 additions & 2 deletions server/jest/jestGlobalTeardown.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ const { client, subscriber } = require('../db/redis');

module.exports = async () => {
sequelize.close();
client.quit();
subscriber.quit();
await client.quit();
await subscriber.quit();
};

0 comments on commit 96b4b88

Please sign in to comment.