Skip to content

Commit

Permalink
Tmp — do not use fakeredis for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
XVincentX committed Nov 3, 2017
1 parent 412ba07 commit 4deacf9
Show file tree
Hide file tree
Showing 28 changed files with 832 additions and 879 deletions.
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ jobs:
"node-6":
docker:
- image: circleci/node:6
- image: redis:alpine
working_directory: ~/repo

steps:
Expand All @@ -13,6 +14,7 @@ jobs:
"node-8":
docker:
- image: circleci/node:8
- image: redis:alpine
working_directory: ~/repo

steps:
Expand Down
24 changes: 1 addition & 23 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const logger = require('./logger').db;
const redisCommands = require('redis-commands');
require('util.promisify/shim')(); // NOTE: shim for native node 8.0 uril.promisify
const util = require('util');
let db;

module.exports = function () {
Expand All @@ -11,26 +8,7 @@ module.exports = function () {
const config = require('./config');
const redisOptions = config.systemConfig.db && config.systemConfig.db.redis;

// special mode, will emulate all redis commands.
// designed for demo and test scenarious to avoid having real Redis instance
const emulate = process.argv[2] === 'emulate' || redisOptions.emulate;

if (emulate) {
const redis = require('fakeredis');
promisify(redis.RedisClient.prototype, redisCommands.list.filter(c => c.toLowerCase() !== 'multi'));
promisify(redis.Multi.prototype, ['exec', 'execAtomic']);
db = redis.createClient(redisOptions);
} else {
db = new (require('ioredis'))(redisOptions);
}

function promisify (obj, methods) {
methods.forEach((method) => {
if (obj[method]) {
obj[method] = util.promisify(obj[method]);
}
});
}
db = new (require('ioredis'))(redisOptions);

db.on('ready', function () {
logger.debug('Redis is ready');
Expand Down
8 changes: 3 additions & 5 deletions test/conditions.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const EgContextBase = require('../lib/gateway/context');
require('../lib/conditions').init();
Expand Down Expand Up @@ -158,8 +156,8 @@ describe('req.matchEGCondition', function () {
conditions: [{
name: 'oneOf',
conditions: [
{ name: 'pathExact', path: '/foo/bar' },
{ name: 'not', condition: { name: 'always' } }
{ name: 'pathExact', path: '/foo/bar' },
{ name: 'not', condition: { name: 'always' } }
]
},
{
Expand All @@ -168,7 +166,7 @@ describe('req.matchEGCondition', function () {
name: 'oneOf',
conditions: [
control,
{ name: 'pathExact', path: '/path/path/path' }
{ name: 'pathExact', path: '/path/path/path' }
]
}
}
Expand Down
2 changes: 0 additions & 2 deletions test/config-https-sni.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const assert = require('assert');
const fs = require('fs');
Expand Down
2 changes: 1 addition & 1 deletion test/config/system.config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
db:
redis:
host: localhost
port: 5555
port: 6379
emulate: true
namespace: EG-TEST
crypto:
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/policy-seq-oauth2-expression-log-ratelimit-proxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const session = require('supertest-session');
const should = require('should');
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/hot-reload/system.config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
db:
redis:
host: localhost
port: 5555
port: 6379
emulate: true
namespace: EG-TEST
crypto:
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/round-robin/system.config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
db:
redis:
host: localhost
port: 5555
port: 6379
emulate: true
namespace: EG-TEST
crypto:
Expand Down
2 changes: 0 additions & 2 deletions test/oauth/authorizationcode.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const session = require('supertest-session');
const should = require('should');
Expand Down
2 changes: 0 additions & 2 deletions test/oauth/client.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const session = require('supertest-session');
const should = require('should');
Expand Down
2 changes: 0 additions & 2 deletions test/oauth/implicit.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const session = require('supertest-session');
const should = require('should');
Expand Down
2 changes: 0 additions & 2 deletions test/oauth/password.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const session = require('supertest-session');
const should = require('should');
Expand Down
2 changes: 0 additions & 2 deletions test/policies/basic-auth-policy.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const request = require('supertest');
const should = require('should');
Expand Down
3 changes: 1 addition & 2 deletions test/policies/keyauth/keyauth.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const headerName = 'Authorization';

const request = require('supertest');
Expand Down
2 changes: 0 additions & 2 deletions test/policies/oauth/consumer-and-token-headers.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const session = require('supertest-session');
const should = require('should');
Expand Down
2 changes: 0 additions & 2 deletions test/policies/oauth/oauth-policy.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const session = require('supertest-session');
const should = require('should');
Expand Down
2 changes: 0 additions & 2 deletions test/policies/rate-limiter/rate-limit.by.host.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const testHelper = require('../../common/routing.helper');
const config = require('../../../lib/config');
Expand Down
2 changes: 0 additions & 2 deletions test/policies/rate-limiter/rate-limit.by.ip.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const testHelper = require('../../common/routing.helper');
const config = require('../../../lib/config');
Expand Down
2 changes: 0 additions & 2 deletions test/policies/rate-limiter/rate-limit.for.specific.host.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const testHelper = require('../../common/routing.helper');
const config = require('../../../lib/config');
Expand Down
6 changes: 2 additions & 4 deletions test/routing/multi-step-policy-execution.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const testHelper = require('../common/routing.helper');
const sinon = require('sinon');
Expand Down Expand Up @@ -35,7 +33,7 @@ describe('default config with multi step (multi action) policy', () => {
apiEndpoints: {
test_default: {}
},
policies: [ 'test', 'test-return' ],
policies: ['test', 'test-return'],
pipelines: {
pipeline1: {
apiEndpoints: ['test_default'],
Expand All @@ -54,7 +52,7 @@ describe('default config with multi step (multi action) policy', () => {
}
}
};
helper.setup({config, plugins});
helper.setup({ config, plugins });
});

after('cleanup', (done) => {
Expand Down
76 changes: 46 additions & 30 deletions test/routing/path-general.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const testHelper = require('../common/routing.helper');
const Config = require('../../lib/config/config');
Expand All @@ -23,15 +21,17 @@ describe('path resolution for specific and general domains', () => {
describe('paths configuration without wildcards paths:/admin host:' + host, () => {
const helper = testHelper();
before('setup', () => {
const plugins = {policies: [{
name: 'routeTest',
policy: () => (req, res) => {
res.json({ result: 'test', hostname: req.hostname, url: req.url, apiEndpoint: req.egContext.apiEndpoint });
}
}]};
const plugins = {
policies: [{
name: 'routeTest',
policy: () => (req, res) => {
res.json({ result: 'test', hostname: req.hostname, url: req.url, apiEndpoint: req.egContext.apiEndpoint });
}
}]
};
config.gatewayConfig = configTemplate;
config.gatewayConfig.apiEndpoints.test.paths = '/admin';
helper.setup({config, plugins});
helper.setup({ config, plugins });
});

after('cleanup', (done) => {
Expand Down Expand Up @@ -65,15 +65,19 @@ describe('path resolution for specific and general domains', () => {

describe('paths configuration with /admin/*', () => {
const helper = testHelper();
const plugins = {policies: [{name: 'routeTest',
policy: () => (req, res) => {
res.json({ result: 'test', hostname: req.hostname, url: req.url, apiEndpoint: req.egContext.apiEndpoint });
}}]};
const plugins = {
policies: [{
name: 'routeTest',
policy: () => (req, res) => {
res.json({ result: 'test', hostname: req.hostname, url: req.url, apiEndpoint: req.egContext.apiEndpoint });
}
}]
};

before('setup', () => {
config.gatewayConfig = configTemplate;
config.gatewayConfig.apiEndpoints.test.paths = '/admin/*';
helper.setup({config, plugins});
helper.setup({ config, plugins });
});

after('cleanup', (done) => {
Expand Down Expand Up @@ -107,15 +111,19 @@ describe('path resolution for specific and general domains', () => {

describe('paths with one named parameter /admin/:id', () => {
const helper = testHelper();
const plugins = {policies: [{name: 'routeTest',
policy: () => (req, res) => {
res.json({ result: 'test', hostname: req.hostname, url: req.url, apiEndpoint: req.egContext.apiEndpoint });
}}]};
const plugins = {
policies: [{
name: 'routeTest',
policy: () => (req, res) => {
res.json({ result: 'test', hostname: req.hostname, url: req.url, apiEndpoint: req.egContext.apiEndpoint });
}
}]
};

before('setup', () => {
config.gatewayConfig = configTemplate;
config.gatewayConfig.apiEndpoints.test.paths = '/admin/:id';
helper.setup({config, plugins});
helper.setup({ config, plugins });
});

after('cleanup', (done) => {
Expand All @@ -137,7 +145,7 @@ describe('path resolution for specific and general domains', () => {
}));
});

[ '/student', '/adm', '/admin', '/admin/', '/admin/1/rt' ].forEach(function (url) {
['/student', '/adm', '/admin', '/admin/', '/admin/1/rt'].forEach(function (url) {
it('should not serve url: ' + url, helper.validate404({
setup: {
host,
Expand All @@ -149,15 +157,19 @@ describe('path resolution for specific and general domains', () => {

describe('paths with one named parameter /admin/:group/:id', () => {
const helper = testHelper();
const plugins = {policies: [{name: 'routeTest',
policy: () => (req, res) => {
res.json({ result: 'test', hostname: req.hostname, url: req.url, apiEndpoint: req.egContext.apiEndpoint });
}}]};
const plugins = {
policies: [{
name: 'routeTest',
policy: () => (req, res) => {
res.json({ result: 'test', hostname: req.hostname, url: req.url, apiEndpoint: req.egContext.apiEndpoint });
}
}]
};

before('setup', () => {
config.gatewayConfig = configTemplate;
config.gatewayConfig.apiEndpoints.test.paths = '/admin/:group/:id';
helper.setup({config, plugins});
helper.setup({ config, plugins });
});

after('cleanup', (done) => {
Expand Down Expand Up @@ -191,15 +203,19 @@ describe('path resolution for specific and general domains', () => {

describe('paths configuration with wildcard after slash or directory ["/admin","/admin/*"]', () => {
const helper = testHelper();
const plugins = {policies: [{name: 'routeTest',
policy: () => (req, res) => {
res.json({ result: 'test', hostname: req.hostname, url: req.url, apiEndpoint: req.egContext.apiEndpoint });
}}]};
const plugins = {
policies: [{
name: 'routeTest',
policy: () => (req, res) => {
res.json({ result: 'test', hostname: req.hostname, url: req.url, apiEndpoint: req.egContext.apiEndpoint });
}
}]
};

before('setup', () => {
config.gatewayConfig = configTemplate;
config.gatewayConfig.apiEndpoints.test.paths = ['/admin', '/admin/*'];
helper.setup({config, plugins});
helper.setup({ config, plugins });
});

after('cleanup', (done) => {
Expand Down
2 changes: 0 additions & 2 deletions test/services/applications.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const mock = require('mock-require');
mock('redis', require('fakeredis'));

const should = require('should');
const uuid = require('uuid');
Expand Down
Loading

0 comments on commit 4deacf9

Please sign in to comment.