Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New DB API: Storage #554

Merged
merged 10 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// best practices
"complexity": [2, 8],
"default-case": 2,
"guard-for-in": 2,
"guard-for-in": 0,
"no-alert": 1,
"no-floating-decimal": 1,
"no-self-compare": 2,
Expand Down
6 changes: 2 additions & 4 deletions lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const _ = require('lodash'),
passport = require('passport'),
users = require('./services/users'),
db = require('./services/db'),
references = require('./services/references'),
session = require('express-session'),
flash = require('express-flash'),
Expand All @@ -15,6 +14,7 @@ const _ = require('lodash'),
ADMIN: 'admin',
WRITE: 'write'
};
var db = require('./services/db');

/**
* Check the auth level to see if a user
Expand Down Expand Up @@ -130,7 +130,6 @@ function serializeUser(user, done) {

function deserializeUser(uid, done) {
return db.get(`/_users/${uid}`)
.then(JSON.parse)
.then(function (user) {
done(null, user);
})
Expand Down Expand Up @@ -166,7 +165,6 @@ function verify(properties) {
if (!req.user) {
// first time logging in! update the user data
return db.get(uid)
.then(JSON.parse)
.then(function (data) {
// only update the user data if the property doesn't exist (name might have been changed through the kiln UI)
return _.defaults(data, {
Expand All @@ -185,7 +183,6 @@ function verify(properties) {
} else {
// already authenticated. just grab the user data
return db.get(uid)
.then(JSON.parse)
.then((data) => done(null, data))
.catch(() => done(null, false, { message: 'User not found!' })); // no user found
}
Expand Down Expand Up @@ -595,3 +592,4 @@ module.exports.serializeUser = serializeUser;
module.exports.deserializeUser = deserializeUser;
module.exports.onLogin = onLogin;
module.exports.onLogout = onLogout;
module.exports.setDb = mock => db = mock;
18 changes: 10 additions & 8 deletions lib/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ const filename = __filename.split('/').pop().split('.').shift(),
passportGoogle = require('passport-google-oauth'),
passportSlack = require('passport-slack'),
passportAPIKey = require('passport-http-header-token'),
db = require('./services/db'),
responses = require('./responses');
responses = require('./responses'),
storage = require('../test/fixtures/mocks/storage');

describe(_.startCase(filename), function () {
let sandbox;
let sandbox, db;

beforeEach(function () {
sandbox = sinon.sandbox.create();
Expand All @@ -26,7 +26,9 @@ describe(_.startCase(filename), function () {
sandbox.stub(responses, 'unauthorized');
// ldap is called directly, so we can't stub it
sandbox.stub(passportAPIKey, 'Strategy');
sandbox.stub(db);

db = storage();
lib.setDb(db);
});

afterEach(function () {
Expand Down Expand Up @@ -130,7 +132,7 @@ describe(_.startCase(filename), function () {
var done = sinon.spy(),
returnedUser = {username: 'person', provider: 'google'};

db.get.returns(Promise.resolve(JSON.stringify(returnedUser)));
db.get.returns(Promise.resolve(returnedUser));
fn('person', done)
.then(function () {
sinon.assert.calledOnce(done);
Expand Down Expand Up @@ -238,14 +240,14 @@ describe(_.startCase(filename), function () {
});

it('errors if user PUT fails on initial auth', function (done) {
db.get.returns(Promise.resolve(JSON.stringify({})));
db.get.returns(Promise.resolve({}));
db.put.returns(Promise.reject(new Error('some db error')));

fn(properties, siteStub)({ user: false }, null, null, profile, expectError(done));
});

it('assigns name and image if user found on initial auth', function (done) {
db.get.returns(Promise.resolve(JSON.stringify({})));
db.get.returns(Promise.resolve({}));
db.put.returns(Promise.resolve());

fn(properties, siteStub)({ user: false }, null, null, profile, expectData(done));
Expand All @@ -258,7 +260,7 @@ describe(_.startCase(filename), function () {
});

it('grabs user data if user found on subsequent auth', function (done) {
db.get.returns(Promise.resolve(JSON.stringify(userData)));
db.get.returns(Promise.resolve(userData));

fn(properties, siteStub)({ user: true }, null, null, profile, expectData(done));
});
Expand Down
9 changes: 4 additions & 5 deletions lib/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const path = require('path'),
components = require('./services/components'),
siteService = require('./services/sites'),
references = require('./services/references'),
db = require('./services/db'),
highland = require('highland'),
{ encode } = require('./services/buffer');
var log = require('./services/logger').setup({
var db = require('./services/db'),
log = require('./services/logger').setup({
file: __filename,
action: 'bootstrap'
}),
Expand Down Expand Up @@ -332,6 +332,5 @@ module.exports = function (runBootstrap = true) {
module.exports.bootstrapPath = bootstrapPath;

// For testing
module.exports.setLog = function (fakeLogger) {
log = fakeLogger;
};
module.exports.setLog = mock => log = mock;
module.exports.setDb = mock => db = mock;
116 changes: 8 additions & 108 deletions lib/bootstrap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ const _ = require('lodash'),
path = require('path'),
filename = __filename.split('/').pop().split('.').shift(),
lib = require('./bootstrap'),
db = require('./services/db'),
siteService = require('./services/sites'),
expect = require('chai').expect,
sinon = require('sinon');
sinon = require('sinon'),
storage = require('../test/fixtures/mocks/storage');

describe(_.startCase(filename), function () {
let sandbox,
let sandbox, db,
bootstrapFake, // eslint-disable-line
sitesFake,
fakeLog;
Expand Down Expand Up @@ -43,8 +42,9 @@ describe(_.startCase(filename), function () {
lib.setLog(fakeLog);
sandbox.stub(siteService);
siteService.sites.returns(_.cloneDeep(sitesFake));

return db.clear();
db = storage();
lib.setDb(db);
// return db.clear();
});

afterEach(function () {
Expand All @@ -53,7 +53,7 @@ describe(_.startCase(filename), function () {

after(function () {
// clean up
return db.clear();
// return db.clear();
});

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


it('missing bootstrap', function (done) {
fn('./jfkdlsa', sitesFake[0])
.then(done.bind(null, 'should throw'))
Expand All @@ -102,116 +101,17 @@ describe(_.startCase(filename), function () {
});

it('reads from bootstrap', function () {
// this is doing IO, so give it more time.
this.slow(50);
this.timeout(400);

return fn('./test/fixtures/config/bootstrap', sitesFake[0]).then(function () {
function expectKittehs(results) {
expect(results).to.deep.equal({
src: 'http://placekitten.com/400/600',
alt: 'adorable kittens'
});
}

return db.get(sitesFake[0].prefix + '/_components/image/instances/0')
.then(JSON.parse)
.then(expectKittehs);
sinon.assert.calledOnce(db.batch);
});
});

it('reads from bootstrap without bootstrap in path', function () {
// this is doing IO, so give it more time.
this.slow(50);
this.timeout(400);

return fn('./test/fixtures/config', sitesFake[0]);
});

it('reads from bootstrap with extension in path', function () {
// this is doing IO, so give it more time.
this.slow(50);
this.timeout(400);

return fn('./test/fixtures/config/bootstrap.yaml', sitesFake[0]);
});

it('reads uris from bootstrap', function () {
// this is doing IO, so give it more time.
this.slow(50);
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}));
}).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'
});
});
});

it('reads pages from bootstrap', function () {
// this is doing IO, so give it more time.
this.slow(50);
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}));
}).then(JSON.parse).then(function (results) {
expect(results).to.deep.equal({
'example1.com/_pages/0': JSON.stringify({
layout: 'example1.com/a/b',
url: 'http://example1.com/x/y',
body: 'example1.com/c/d',
head: ['example1.com/e/f']
})
});
});
});

it('reads users from bootstrap', function () {
// this is doing IO, so give it more time.
this.slow(50);
this.timeout(400);

return fn('./test/fixtures/config/bootstrap.yaml', sitesFake[0]).then(function () {
return db.pipeToPromise(db.list({prefix: '/_users', limit: -1}));
}).then(JSON.parse).then(function (results) {
expect(results).to.deep.equal({
'/_users/Zm9vQGJhci5jb21AYmF6': JSON.stringify({
username: '[email protected]',
provider: 'baz',
auth: 'foobarbaz'
})
});
});
});

it('reads components from bootstrap', function () {
// this is doing IO, so give it more time.
this.slow(50);
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}));
}).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"}',

// note the prefix added
'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"}',

// base data of components
'example1.com/_components/image2': '{"src":"http://placekitten.com/400/600","alt":"adorable kittens"}'
});
});
});
});
});
5 changes: 3 additions & 2 deletions lib/render.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var uriRoutes, renderers,
db = require('./services/db'),
log = require('./services/logger').setup({
file: __filename,
action: 'render'
Expand All @@ -11,7 +12,6 @@ const _ = require('lodash'),
components = require('./services/components'),
references = require('./services/references'),
clayUtils = require('clayutils'),
db = require('./services/db'),
composer = require('./services/composer'),
responses = require('./responses'),
mapLayoutToPageData = require('./utils/layout-to-page-data');
Expand Down Expand Up @@ -91,7 +91,7 @@ function renderComponent(req, res, hrStart) {
* @return {Object}
*/
function getDBObject(uri) {
return db.get(uri).then(JSON.parse);
return db.get(uri);
}

/**
Expand Down Expand Up @@ -362,3 +362,4 @@ module.exports.registerRenderers = registerRenderers;
module.exports.resetUriRouteHandlers = resetUriRouteHandlers;
module.exports.setUriRouteHandlers = setUriRouteHandlers;
module.exports.assumePublishedUnlessEditing = assumePublishedUnlessEditing;
module.exports.setDb = mock => db = mock;
11 changes: 6 additions & 5 deletions lib/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
const _ = require('lodash'),
bluebird = require('bluebird'),
filename = __filename.split('/').pop().split('.').shift(),
db = require('./services/db'),
lib = require('./' + filename),
expect = require('chai').expect,
sinon = require('sinon'),
schema = require('./schema'),
components = require('./services/components'),
composer = require('./services/composer'),
createMockReq = require('../test/fixtures/mocks/req'),
createMockRes = require('../test/fixtures/mocks/res');
createMockRes = require('../test/fixtures/mocks/res'),
storage = require('../test/fixtures/mocks/storage');


describe(_.startCase(filename), function () {
let sandbox,
let sandbox, db,
mockSite = 'mockSite';

/**
Expand All @@ -40,7 +40,7 @@ describe(_.startCase(filename), function () {
* @returns {Promise.string}
*/
function resolveString(str) {
return bluebird.resolve(JSON.stringify(str));
return bluebird.resolve(str);
}

/**
Expand Down Expand Up @@ -74,7 +74,8 @@ describe(_.startCase(filename), function () {
beforeEach(function () {
sandbox = sinon.sandbox.create();

sandbox.stub(db);
db = storage();
lib.setDb(db);
sandbox.stub(components);
sandbox.stub(composer);
sandbox.stub(schema);
Expand Down
Loading