Skip to content

Commit

Permalink
Check archived property on publish (#644)
Browse files Browse the repository at this point in the history
* Updating tests for checkForArchived

* Added checkForArchived

* export checkForArchived for testing

* checkForArchived tests

* Bracket spacing

Co-Authored-By: james-owen <[email protected]>

* checkArchived description

* Styling
  • Loading branch information
james-owen authored Mar 27, 2019
1 parent b3281fe commit 91942c6
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 18 deletions.
55 changes: 39 additions & 16 deletions lib/services/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ function _checkForDynamicPage(uri, { _dynamic }) {
return bluebird.resolve(_dynamic);
}

/**
* Checks the archive property on a page's meta object, to determine whether
* it can be published. Archived pages should not be published. Otherwise, an
* initial object is passed to the processing chain in processPublishRules.
*
* @param {String} uri
* @returns {Promise}
*/
function _checkForArchived(uri) {
return db.getMeta(replaceVersion(uri))
.then(meta => meta.archived)
.then(archived => archived
? bluebird.reject({ val: '', errors: { _checkForArchived: 'The page is archived and cannot be published' } })
: bluebird.resolve({ val: '', errors: {} })
);
}

/**
* Allow functions to add data to the meta object.
* Functions are defined on the site controller and
Expand All @@ -127,7 +144,7 @@ function addToMeta(meta, modifiers = [], uri, data) {
}

/**
* Always do these actins when publishing a page
* Always do these actions when publishing a page
*
* @param {String} url
* @param {String} uri
Expand Down Expand Up @@ -166,21 +183,26 @@ function publishPageAtUrl(url, uri, data, locals, site) { // eslint-disable-line
* @returns {Promise}
*/
function processPublishRules(publishingChain, uri, pageData, locals) {
return bluebird.reduce(publishingChain, (acc, fn, index) => {
if (!acc.val) {
return bluebird.try(() => fn(uri, pageData, locals))
.then(val => {
acc.val = val;
return acc;
})
.catch(e => {
acc.errors[fn.name || index] = e.message;
return bluebird.resolve(acc);
});
}

return acc;
}, { val: '', errors: {}});
// check whether page is archived, then run the rest of the publishing chain
return _checkForArchived(uri)
.then(initialObj => {
return bluebird.reduce(publishingChain, (acc, fn, index) => {
if (!acc.val) {
return bluebird.try(() => fn(uri, pageData, locals))
.then(val => {
acc.val = val;
return acc;
})
.catch(e => {
acc.errors[fn.name || index] = e.message;
return bluebird.resolve(acc);
});
}
return acc;
}, initialObj);
})
.catch(err => bluebird.resolve(err)); // err object constructed in _checkForArchived function above
}

/**
Expand All @@ -197,7 +219,7 @@ function validatePublishRules(uri, { val, errors }) {
let err, errMsg;

if (!val) {
errMsg = `Unable to determine a url for publishing ${replaceVersion(uri)}`;
errMsg = `Error publishing page: ${replaceVersion(uri)}`;
log('error', errMsg, { publishRuleErrors: errors });
err = new Error(errMsg);
err.status = 400;
Expand Down Expand Up @@ -235,6 +257,7 @@ module.exports.resolvePublishUrl = resolvePublishUrl;
// For testing
module.exports._checkForUrlProperty = _checkForUrlProperty;
module.exports._checkForDynamicPage = _checkForDynamicPage;
module.exports._checkForArchived = _checkForArchived;
module.exports.processPublishRules = processPublishRules;
module.exports.validatePublishRules = validatePublishRules;
module.exports.addToMeta = addToMeta;
Expand Down
58 changes: 56 additions & 2 deletions lib/services/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,32 @@ describe(_.startCase(filename), function () {
rule1.returns(Promise.reject(new Error(0)));
rule2.returns(Promise.resolve('foo.com/bar/baz'));

return fn([ () => Promise.reject(new Error(0)), rule2, rule3 ])
const uri = 'http://example.com/uri@published';

db.getMeta.returns(Promise.resolve({ archived: false }));

return fn([ () => Promise.reject(new Error(0)), rule2, rule3 ], uri)
.then(() => {
sinon.assert.notCalled(rule3);
sinon.assert.calledOnce(rule2);
});
});

it('does not process if archived is true', function () {
rule1.returns(Promise.reject(new Error(0)));
rule2.returns(Promise.resolve('foo.com/bar/baz'));

const uri = 'http://example.com/uri@published';

db.getMeta.returns(Promise.resolve({ archived: true }));

return fn([ rule1, rule2, rule3 ], uri)
.then(() => {
sinon.assert.notCalled(rule1);
sinon.assert.notCalled(rule2);
sinon.assert.notCalled(rule3);
});
});
});

describe('validatePublishRules', function () {
Expand All @@ -76,7 +96,7 @@ describe(_.startCase(filename), function () {
return fn('someuri', {val: '', errors })
.catch(err => {
expect(err.status).to.equal(400);
sinon.assert.calledWith(fakeLog, 'error', 'Unable to determine a url for publishing someuri', {
sinon.assert.calledWith(fakeLog, 'error', 'Error publishing page: someuri', {
publishRuleErrors: errors
});
});
Expand All @@ -92,6 +112,8 @@ describe(_.startCase(filename), function () {
modifyFn.returnsArg(0);
db.get.returns(Promise.resolve(JSON.stringify(fakePage)));
meta.getMeta.returns(Promise.resolve({}));
db.getMeta.returns(Promise.resolve({ archived: false }));

return lib.resolvePublishUrl('some/uri', {}, {})(_.cloneDeep(fakePage))
.then(resp => expect(resp).to.eql({ data: fakePage, meta: { url: 'http://some.url', urlHistory: ['http://some.url'] }}));
});
Expand All @@ -104,6 +126,7 @@ describe(_.startCase(filename), function () {
modifyFn.returnsArg(0);
db.get.returns(Promise.resolve(fakePage));
meta.getMeta.returns(Promise.resolve({}));
db.getMeta.returns(Promise.resolve({ archived: false }));

return lib.resolvePublishUrl('some/uri', {}, fakeSite)(_.cloneDeep(fakePage))
.then(resp => {
Expand All @@ -123,6 +146,7 @@ describe(_.startCase(filename), function () {
meta.getMeta.returns(Promise.resolve({
urlHistory: fakeHistory
}));
db.getMeta.returns(Promise.resolve({ archived: false }));

return lib.resolvePublishUrl('some/uri', {}, fakeSite)(_.cloneDeep(fakePage))
.then(() => {
Expand All @@ -141,6 +165,7 @@ describe(_.startCase(filename), function () {
meta.getMeta.returns(Promise.resolve({
urlHistory: fakeHistory
}));
db.getMeta.returns(Promise.resolve({ archived: false }));

return lib.resolvePublishUrl('some/uri', {}, fakeSite)(_.cloneDeep(fakePage))
.then(resp => {
Expand All @@ -155,6 +180,7 @@ describe(_.startCase(filename), function () {
pubRule.returns(fakeUrl);
modifyFn.returnsArg(0);
meta.getMeta.returns(Promise.resolve({}));
db.getMeta.returns(Promise.resolve({ archived: false }));

return lib.resolvePublishUrl('some/uri', {}, fakeSite)(_.cloneDeep(fakePage))
.then(resp => {
Expand All @@ -166,6 +192,8 @@ describe(_.startCase(filename), function () {
const fakePage = {page: 'data', _dynamic: true},
locals = {};

db.getMeta.returns(Promise.resolve({ archived: false }));

return lib.resolvePublishUrl('some/uri', locals, fakeSite)(_.cloneDeep(fakePage))
.then(resp => {
expect(resp.meta._dynamic).to.be.true;
Expand Down Expand Up @@ -237,4 +265,30 @@ describe(_.startCase(filename), function () {
});
});
});

describe('_checkForArchived', function () {
const fn = lib[this.title];

it('rejects with an error object if the meta.archived property is true', function () {
const uri = 'http://example.com/uri@published';

db.getMeta.returns(Promise.resolve({ archived: true }));

return fn(uri)
.catch(err => {
const expectedErr = { val:'', errors: { _checkForArchived: 'The page is archived and cannot be published' } };

expect(err).to.eql(expectedErr);
});
});

it('resolves with an empty error object if the meta.archived property is false', function () {
const uri = 'http://example.com/uri@published';

db.getMeta.returns(Promise.resolve({ archived: false }));

return fn(uri)
.then(resp => expect(resp).to.eql({ val:'', errors: {} }));
});
});
});

0 comments on commit 91942c6

Please sign in to comment.