Skip to content

Commit

Permalink
V5 (#505)
Browse files Browse the repository at this point in the history
* update node for circle

* remove log service

* updating route names

* auth test

* generic changes + auth & users

* fixing API tests

* stashing test fixes

* more test updates

* fixing an underscore reference

* fixing tests and upgrade to sinon 4.x

* lint errors

* Renderers Terminate Response (#506)

* moving response termination to renderer

* linting

* addressing feedback

* Deprecate publish (#507)

* moving response termination to renderer

* linting

* initial work

* addressing comments

* Remove memory check (#508)

* moving response termination to renderer

* linting

* addressing feedback

* control service stops checking for memory leak

* lint fixes

* 5.0.0-rc3

* forgot to save

* removed commented out code && winston logging module references)

* upping engines version
  • Loading branch information
jonwinton authored Feb 15, 2018
1 parent a0221b6 commit 9bb18af
Show file tree
Hide file tree
Showing 72 changed files with 959 additions and 2,189 deletions.
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ machine:
timezone:
America/New_York
node:
version: 6.11.0
version: 8.9.4
test:
post:
- cp -r ./coverage/* $CIRCLE_ARTIFACTS
Expand Down
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ module.exports.schedule = require('./lib/services/schedule');
module.exports.pages = require('./lib/services/pages');
module.exports.sites = require('./lib/services/sites');
module.exports.references = require('./lib/services/references');
module.exports.log = require('./lib/services/log');
24 changes: 13 additions & 11 deletions lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function isProtectedRoute(req) {
function getAuthUrl(site) {
var base = references.uriToUrl(site.prefix, null, site.port);

return _.last(base) === '/' ? `${base}auth` : `${base}/auth`;
return _.last(base) === '/' ? `${base}_auth` : `${base}/_auth`;
}

/**
Expand Down Expand Up @@ -129,7 +129,7 @@ function serializeUser(user, done) {
}

function deserializeUser(uid, done) {
return db.get(`/users/${uid}`)
return db.get(`/_users/${uid}`)
.then(JSON.parse)
.then(function (user) {
done(null, user);
Expand Down Expand Up @@ -161,7 +161,7 @@ function verify(properties) {
}

// get UID
uid = `/users/${users.encode(`${username.toLowerCase()}`, provider)}`;
uid = `/_users/${users.encode(`${username.toLowerCase()}`, provider)}`;

if (!req.user) {
// first time logging in! update the user data
Expand All @@ -179,7 +179,9 @@ function verify(properties) {
.then(() => done(null, data))
.catch(e => done(e));
})
.catch(() => done(null, false, { message: 'User not found!' })); // no user found
.catch(() => {
done(null, false, { message: 'User not found!' });
}); // no user found
} else {
// already authenticated. just grab the user data
return db.get(uid)
Expand Down Expand Up @@ -388,23 +390,23 @@ function addAuthRoutes(router, site) {
// note: always call apikey auth first
if (provider === 'google') {
// google needs to send a special scope argument
router.get(`/auth/${provider}`, passport.authenticate(`${provider}-${site.slug}`, { scope: [
router.get(`/_auth/${provider}`, passport.authenticate(`${provider}-${site.slug}`, { scope: [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email'
] }));
} else if (provider === 'ldap') {
router.get(`/auth/${provider}`, checkCredentials, passport.authenticate(`${provider}-${site.slug}`, {
router.get(`/_auth/${provider}`, checkCredentials, passport.authenticate(`${provider}-${site.slug}`, {
// passport options
failureRedirect: `${getAuthUrl(site)}/login`,
failureFlash: true,
successReturnToOrRedirect: getPathOrBase(site) }));
} else {
router.get(`/auth/${provider}`, passport.authenticate(`${provider}-${site.slug}`));
router.get(`/_auth/${provider}`, passport.authenticate(`${provider}-${site.slug}`));
}

if (provider !== 'ldap') {
// ldap handles everything in /auth/ldap and doesn't use the callback at all
router.get(`/auth/${provider}/callback`, passport.authenticate(`${provider}-${site.slug}`, {
// ldap handles everything in /_auth/ldap and doesn't use the callback at all
router.get(`/_auth/${provider}/callback`, passport.authenticate(`${provider}-${site.slug}`, {
failureRedirect: `${getAuthUrl(site)}/login`,
failureFlash: true,
successReturnToOrRedirect: getPathOrBase(site) })); // redirect to previous page or site root
Expand Down Expand Up @@ -560,8 +562,8 @@ function init(router, providers, site, sessionStore) {
// add authorization routes
// note: these (and the provider routes) are added here,
// rather than as route controllers in lib/routes/
router.get('/auth/login', onLogin(tpl, site, currentProviders));
router.get('/auth/logout', onLogout(site));
router.get('/_auth/login', onLogin(tpl, site, currentProviders));
router.get('/_auth/logout', onLogout(site));
_.each(providers, module.exports.addAuthRoutes(router, site)); // allow mocking this in tests

// handle de-authentication errors. This occurs when a user is logged in
Expand Down
30 changes: 14 additions & 16 deletions lib/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const filename = __filename.split('/').pop().split('.').shift(),
expect = require('chai').expect,
sinon = require('sinon'),
passport = require('passport'),
winston = require('winston'),
passportTwitter = require('passport-twitter'),
passportGoogle = require('passport-google-oauth'),
passportSlack = require('passport-slack'),
Expand All @@ -19,8 +18,7 @@ describe(_.startCase(filename), function () {

beforeEach(function () {
sandbox = sinon.sandbox.create();
sandbox.stub(winston);
sandbox.stub(passport, 'authenticate', () => (req, res, next) => next());
sandbox.stub(passport, 'authenticate').callsFake(() => (req, res, next) => next());
sandbox.stub(passport, 'use');
sandbox.stub(passportTwitter, 'Strategy');
sandbox.stub(passportGoogle, 'OAuth2Strategy');
Expand Down Expand Up @@ -136,7 +134,7 @@ describe(_.startCase(filename), function () {
fn('person', done)
.then(function () {
sinon.assert.calledOnce(done);
sinon.assert.calledWith(db.get, '/users/person');
sinon.assert.calledWith(db.get, '/_users/person');
sinon.assert.calledWith(done, null, returnedUser);
});
});
Expand All @@ -149,7 +147,7 @@ describe(_.startCase(filename), function () {
fn('person', done)
.then(function () {
sinon.assert.calledOnce(done);
sinon.assert.calledWith(db.get, '/users/person');
sinon.assert.calledWith(db.get, '/_users/person');
sinon.assert.calledWith(done, userError);
});
});
Expand All @@ -159,11 +157,11 @@ describe(_.startCase(filename), function () {
const fn = lib[this.title];

it('adds initial slash (after the site path) if site has a path', function () {
expect(fn({ path: '/foo', prefix: 'domain.com/foo', port: '80'}, 'twitter')).to.equal('http://domain.com/foo/auth/twitter/callback');
expect(fn({ path: '/foo', prefix: 'domain.com/foo', port: '80'}, 'twitter')).to.equal('http://domain.com/foo/_auth/twitter/callback');
});

it('does not add slash if site has no path', function () {
expect(fn({ prefix: 'domain.com/', port: '80'}, 'twitter')).to.equal('http://domain.com/auth/twitter/callback');
expect(fn({ prefix: 'domain.com/', port: '80'}, 'twitter')).to.equal('http://domain.com/_auth/twitter/callback');
});
});

Expand Down Expand Up @@ -270,7 +268,7 @@ describe(_.startCase(filename), function () {
const fn = lib[this.title];

it('calls verify with a slightly different function signature', function (done) {
sandbox.stub(lib, 'verify', () => (req, token, tokenSecret, profile, cb) => cb()); // eslint-disable-line
sandbox.stub(lib, 'verify').callsFake(() => (req, token, tokenSecret, profile, cb) => cb()); // eslint-disable-line

fn({})({}, {}, function () {
expect(lib.verify.called).to.equal(true);
Expand Down Expand Up @@ -411,25 +409,25 @@ describe(_.startCase(filename), function () {

it('adds google auth and callback routes', function () {
fn(router, {})('google');
expect(_.includes(paths, '/auth/google')).to.equal(true);
expect(_.includes(paths, '/auth/google/callback')).to.equal(true);
expect(_.includes(paths, '/_auth/google')).to.equal(true);
expect(_.includes(paths, '/_auth/google/callback')).to.equal(true);
});

it('adds twitter auth and callback routes', function () {
fn(router, {})('twitter');
expect(_.includes(paths, '/auth/twitter')).to.equal(true);
expect(_.includes(paths, '/auth/twitter/callback')).to.equal(true);
expect(_.includes(paths, '/_auth/twitter')).to.equal(true);
expect(_.includes(paths, '/_auth/twitter/callback')).to.equal(true);
});

it('adds slack auth and callback routes', function () {
fn(router, {})('slack');
expect(_.includes(paths, '/auth/slack')).to.equal(true);
expect(_.includes(paths, '/auth/slack/callback')).to.equal(true);
expect(_.includes(paths, '/_auth/slack')).to.equal(true);
expect(_.includes(paths, '/_auth/slack/callback')).to.equal(true);
});

it('adds ldap auth route', function () {
fn(router, {})('ldap');
expect(_.includes(paths, '/auth/ldap')).to.equal(true);
expect(_.includes(paths, '/_auth/ldap')).to.equal(true);
});
});

Expand Down Expand Up @@ -543,7 +541,7 @@ describe(_.startCase(filename), function () {
var res = { redirect: sandbox.spy() };

fn({ prefix: 'domain.com' })({ logout: _.noop }, res);
expect(res.redirect.calledWith('http://domain.com/auth/login')).to.equal(true);
expect(res.redirect.calledWith('http://domain.com/_auth/login')).to.equal(true);
});
});

Expand Down
26 changes: 9 additions & 17 deletions lib/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ function getConfig(dir) {
/**
* Save an thing (any thing) to the database, and log that we did so.
*
* If we don't like this logging, we could add levels to winston, or add dev or debug levels to the config.
*
* For the current stage, it's most useful as info.
*
* @param {string} name
* @param {object} thing
* @returns {object}
Expand Down Expand Up @@ -126,7 +122,7 @@ function saveObjects(dbPath, list, save) {
* @param {object} site
*/
function applyPrefixToPages(bootstrap, site) {
bootstrap.pages = _.transform(bootstrap.pages, function (obj, value, key) {
bootstrap._pages = _.transform(bootstrap._pages, function (obj, value, key) {
const list = references.listDeepObjects(value);

list.push(value);
Expand All @@ -153,10 +149,10 @@ function applyPrefixToPages(bootstrap, site) {
* @param {object} site
*/
function applyPrefixToComponents(bootstrap, site) {
bootstrap.components = _.transform(bootstrap.components, function (obj, value, key) {
bootstrap._components = _.transform(bootstrap._components, function (obj, value, key) {
_.each(references.listDeepObjects(value, '_ref'), function (refObj) {
if (refObj._ref[0] === '/') {
refObj._ref = site.prefix + refObj._ref;
refObj._ref = `${site.prefix}${refObj._ref}`;
}
});

Expand All @@ -169,7 +165,7 @@ function applyPrefixToComponents(bootstrap, site) {
* @param {object} site
*/
function applyPrefixToUris(bootstrap, site) {
bootstrap.uris = _.transform(bootstrap.uris, function (obj, value, key) {
bootstrap._uris = _.transform(bootstrap._uris, function (obj, value, key) {
if (value[0] === '/') {
value = site.prefix + value;
}
Expand Down Expand Up @@ -212,21 +208,17 @@ function load(data, site) {
prefix = site.prefix,
compData = applyPrefix(data, site);

promiseListsOps = saveObjects(prefix + '/lists/', compData.lists, getPutOperation);
promiseUrisOps = saveBase64Strings(prefix + '/uris/', compData.uris, getPutOperation);
_.each(compData.uris, function (page, uri) {
promiseListsOps = saveObjects(`${prefix}/_lists/`, compData._lists, getPutOperation);
promiseUrisOps = saveBase64Strings(`${prefix}/_uris/`, compData._uris, getPutOperation);
_.each(compData._uris, function (page, uri) {
log('info', `bootstrapped: ${uri} => ${page}`);
});
promiseComponentsOps = saveWithInstances(`${prefix}/components/`, compData.components, function (name, item) {
promiseComponentsOps = saveWithInstances(`${prefix}/_components/`, compData._components, function (name, item) {
return bluebird.join(
components.getPutOperations(name, _.cloneDeep(item))
).then(_.flatten);
});
promisePagesOps = saveObjects(prefix + '/pages/', compData.pages, function (name, item) {
return [
getPutOperation(name, item)
];
});
promisePagesOps = saveObjects(`${prefix}/_pages/`, compData._pages, (name, item) => [ getPutOperation(name, item)]);

return bluebird.join(promiseComponentsOps, promisePagesOps, promiseUrisOps, promiseListsOps, function (componentsOps, pagesOps, urisOps, listsOps) {
const flatComponentsOps = _.flatten(componentsOps),
Expand Down
31 changes: 15 additions & 16 deletions lib/bootstrap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ describe(_.startCase(filename), function () {
fakeLog = sandbox.stub();

lib.setLog(fakeLog);
sandbox.stub(db, 'formatBatchOperations').returns([]);
sandbox.stub(siteService);
siteService.sites.returns(_.cloneDeep(sitesFake));

Expand All @@ -61,11 +60,11 @@ describe(_.startCase(filename), function () {
const fn = lib;

beforeEach(function () {
sandbox.stub(path, 'resolve', _.identity);
sandbox.stub(path, 'resolve').callsFake(_.identity);
sandbox.stub(files, 'isDirectory');
sandbox.stub(files, 'getYaml');
sandbox.stub(files, 'getComponents');
sandbox.stub(files, 'getComponentPath', _.identity);
sandbox.stub(files, 'getComponentPath').callsFake(_.identity);
});

it('bootstraps', function () {
Expand Down Expand Up @@ -115,7 +114,7 @@ describe(_.startCase(filename), function () {
});
}

return db.get(sitesFake[0].prefix + '/components/image/instances/0')
return db.get(sitesFake[0].prefix + '/_components/image/instances/0')
.then(JSON.parse)
.then(expectKittehs);
});
Expand Down Expand Up @@ -143,13 +142,13 @@ describe(_.startCase(filename), function () {
this.timeout(400);

return fn('./test/fixtures/config/bootstrap.yaml', sitesFake[0]).then(function () {
return db.pipeToPromise(db.list({prefix: sitesFake[0].prefix + '/uris', limit: -1}));
return db.pipeToPromise(db.list({prefix: sitesFake[0].prefix + '/_uris', limit: -1}));
}).then(JSON.parse).then(function (results) {
expect(results).to.deep.equal({
'example1.com/uris/YQ==': 'b',
'example1.com/uris/Yw==': 'example1.com/d',
'example1.com/uris/ZXhhbXBsZTEuY29tL2U=': 'f',
'example1.com/uris/ZXhhbXBsZTEuY29tL2c=': 'example1.com/h'
'example1.com/_uris/YQ==': 'b',
'example1.com/_uris/Yw==': 'example1.com/d',
'example1.com/_uris/ZXhhbXBsZTEuY29tL2U=': 'f',
'example1.com/_uris/ZXhhbXBsZTEuY29tL2c=': 'example1.com/h'
});
});
});
Expand All @@ -160,10 +159,10 @@ describe(_.startCase(filename), function () {
this.timeout(400);

return fn('./test/fixtures/config/bootstrap.yaml', sitesFake[0]).then(function () {
return db.pipeToPromise(db.list({prefix: 'example1.com/pages', limit: -1}));
return db.pipeToPromise(db.list({prefix: 'example1.com/_pages', limit: -1}));
}).then(JSON.parse).then(function (results) {
expect(results).to.deep.equal({
'example1.com/pages/0': JSON.stringify({
'example1.com/_pages/0': JSON.stringify({
layout: 'example1.com/a/b',
url: 'http://example1.com/x/y',
body: 'example1.com/c/d',
Expand All @@ -179,20 +178,20 @@ describe(_.startCase(filename), function () {
this.timeout(400);

return fn('./test/fixtures/config/bootstrap.yaml', sitesFake[0]).then(function () {
return db.pipeToPromise(db.list({prefix: 'example1.com/components', limit: -1}));
return db.pipeToPromise(db.list({prefix: 'example1.com/_components', limit: -1}));
}).then(JSON.parse).then(function (results) {
expect(results).to.deep.include({
// instance data of components (prefixes all the way down)
'example1.com/components/image/instances/0': '{"src":"http://placekitten.com/400/600","alt":"adorable kittens"}',
'example1.com/_components/image/instances/0': '{"src":"http://placekitten.com/400/600","alt":"adorable kittens"}',

// note the prefix added
'example1.com/components/image/instances/1': '{"_ref":"example1.com/components/image2"}',
'example1.com/_components/image/instances/1': '{"_ref":"example1.com/_components/image2"}',

// note the prefix NOT added
'example1.com/components/image/instances/2': '{"_ref":"localhost/components/what"}',
'example1.com/_components/image/instances/2': '{"_ref":"localhost/_components/what"}',

// base data of components
'example1.com/components/image2': '{"src":"http://placekitten.com/400/600","alt":"adorable kittens"}'
'example1.com/_components/image2': '{"src":"http://placekitten.com/400/600","alt":"adorable kittens"}'
});
});
});
Expand Down
Loading

0 comments on commit 9bb18af

Please sign in to comment.