Skip to content

Commit

Permalink
Merge pull request #11 from HyperTHD/REDIS_PORT
Browse files Browse the repository at this point in the history
Porting Redis to Satellite
  • Loading branch information
HyperTHD authored Apr 9, 2021
2 parents 83be940 + 83f30d6 commit 636a619
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 8 deletions.
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ process.env = Object.assign(process.env, {
JWT_AUDIENCE: 'http://localhost',
JWT_ISSUER: 'http://localhost',
JWT_EXPIRES_IN: '1h',
MOCK_REDIS: '1',
});

module.exports = {
Expand All @@ -14,4 +15,5 @@ module.exports = {
testEnvironment: 'node',
coverageDirectory: '<rootDir>/coverage',
collectCoverageFrom: ['<rootDir>/src/**/*.js'],
testTimeout: 8000,
};
143 changes: 135 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"express-pino-logger": "^6.0.0",
"helmet": "4.4.1",
"http-errors": "^1.8.0",
"ioredis": "^4.26.0",
"ioredis-mock": "^5.4.1",
"jsonwebtoken": "^8.5.1",
"pino": "^6.11.2",
"pino-colada": "^2.1.0"
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ module.exports.createServiceToken = require('./service-token');
module.exports.Router = (options) => createRouter(options);
module.exports.isAuthenticated = isAuthenticated;
module.exports.isAuthorized = isAuthorized;
module.exports.Redis = require('./redis');
24 changes: 24 additions & 0 deletions src/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const logger = require('./logger');
const Redis = require('ioredis');
const MockRedis = require('ioredis-mock');

// If you need to set the Redis URL, do it in REDIS_URL
const redisUrl = process.env.REDIS_URL || 'redis://redis:6379';

// Set MOCK_REDIS=1 to mock, MOCK_REDIS= to use real redis
const useMockRedis = process.env.MOCK_REDIS;

// RedisConstructor is one of Redis or MockRedis
const RedisConstructor = useMockRedis ? MockRedis : Redis;

function createRedisClient(options) {
return new RedisConstructor(redisUrl, options);
}

// If using MockRedis, shim info() until https://github.com/stipsan/ioredis-mock/issues/841 ships
if (useMockRedis && typeof MockRedis.prototype.info !== 'function') {
logger.debug('Shimming MockRedis info() method');
MockRedis.prototype.info = () => Promise.resolve('redis_version:999.999.999');
}

module.exports = createRedisClient;
21 changes: 21 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
hash,
createError,
createServiceToken,
Redis,
} = require('./src');
const { JWT_EXPIRES_IN, JWT_ISSUER, JWT_AUDIENCE, SECRET } = process.env;

Expand Down Expand Up @@ -882,3 +883,23 @@ describe('createServiceToken()', () => {
expect(decoded.exp).toBeGreaterThan(currentDateSeconds);
});
});

describe('Redis()', () => {
let redis;

beforeEach(() => {
redis = Redis();
});

afterEach(() => {
redis.quit();
});

test('Redis ping command should return pong', (done) => {
redis.ping((err, result) => {
expect(err).toBe(null);
expect(result).toEqual('PONG');
done();
});
});
});

0 comments on commit 636a619

Please sign in to comment.