2020-04-29 16:44:27 +01:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const Promise = require('bluebird');
|
2015-06-27 16:42:10 +01:00
|
|
|
|
2020-04-29 16:44:27 +01:00
|
|
|
// Stuff we are testing
|
|
|
|
const helpers = require('../../../core/frontend/helpers');
|
|
|
|
|
|
|
|
const models = require('../../../core/server/models');
|
|
|
|
const api = require('../../../core/server/api');
|
2015-06-27 16:42:10 +01:00
|
|
|
|
|
|
|
describe('{{#get}} helper', function () {
|
2020-04-29 16:44:27 +01:00
|
|
|
let fn;
|
|
|
|
let inverse;
|
2018-10-17 09:23:59 +02:00
|
|
|
let locals = {};
|
2015-06-27 16:42:10 +01:00
|
|
|
|
2017-06-08 21:34:20 +02:00
|
|
|
before(function () {
|
|
|
|
models.init();
|
|
|
|
});
|
|
|
|
|
2015-06-27 16:42:10 +01:00
|
|
|
beforeEach(function () {
|
2019-01-21 17:53:44 +01:00
|
|
|
fn = sinon.spy();
|
|
|
|
inverse = sinon.spy();
|
2018-10-17 09:23:59 +02:00
|
|
|
|
2019-09-10 11:37:04 +02:00
|
|
|
locals = {root: {_locals: {apiVersion: 'v2'}}, globalProp: {foo: 'bar'}};
|
2015-06-27 16:42:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 17:53:44 +01:00
|
|
|
sinon.restore();
|
2015-06-27 16:42:10 +01:00
|
|
|
});
|
|
|
|
|
2018-11-07 15:29:37 +01:00
|
|
|
describe('authors v2', function () {
|
2019-09-10 11:37:04 +02:00
|
|
|
let browseAuthorsStub;
|
2018-11-07 15:29:37 +01:00
|
|
|
const meta = {pagination: {}};
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
locals = {root: {_locals: {apiVersion: 'v2'}}};
|
|
|
|
|
2019-09-10 11:37:04 +02:00
|
|
|
browseAuthorsStub = sinon.stub(api.v2, 'authorsPublic').get(() => {
|
2018-11-07 15:29:37 +01:00
|
|
|
return {
|
2019-01-21 17:53:44 +01:00
|
|
|
browse: sinon.stub().resolves({authors: [], meta: meta})
|
2018-11-07 15:29:37 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-10 11:37:04 +02:00
|
|
|
it('browse authors', function (done) {
|
2018-11-07 15:29:37 +01:00
|
|
|
helpers.get.call(
|
|
|
|
{},
|
|
|
|
'authors',
|
|
|
|
{hash: {}, data: locals, fn: fn, inverse: inverse}
|
|
|
|
).then(function () {
|
2019-08-09 19:55:12 +05:30
|
|
|
fn.called.should.be.true();
|
|
|
|
fn.firstCall.args[0].should.be.an.Object().with.property('authors');
|
|
|
|
fn.firstCall.args[0].authors.should.eql([]);
|
|
|
|
inverse.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('authors canary', function () {
|
2019-09-10 11:37:04 +02:00
|
|
|
let browseAuthorsStub;
|
2019-08-09 19:55:12 +05:30
|
|
|
const meta = {pagination: {}};
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
locals = {root: {_locals: {apiVersion: 'canary'}}};
|
|
|
|
|
2019-09-10 11:37:04 +02:00
|
|
|
browseAuthorsStub = sinon.stub(api.canary, 'authorsPublic').get(() => {
|
2019-08-09 19:55:12 +05:30
|
|
|
return {
|
|
|
|
browse: sinon.stub().resolves({authors: [], meta: meta})
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-10 11:37:04 +02:00
|
|
|
it('browse authors', function (done) {
|
2019-08-09 19:55:12 +05:30
|
|
|
helpers.get.call(
|
|
|
|
{},
|
|
|
|
'authors',
|
|
|
|
{hash: {}, data: locals, fn: fn, inverse: inverse}
|
|
|
|
).then(function () {
|
2019-09-03 12:33:31 +05:30
|
|
|
fn.called.should.be.true();
|
|
|
|
fn.firstCall.args[0].should.be.an.Object().with.property('authors');
|
|
|
|
fn.firstCall.args[0].authors.should.eql([]);
|
|
|
|
inverse.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('authors v3', function () {
|
2019-09-10 11:37:04 +02:00
|
|
|
let browseAuthorsStub;
|
2019-09-03 12:33:31 +05:30
|
|
|
const meta = {pagination: {}};
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
locals = {root: {_locals: {apiVersion: 'v3'}}};
|
|
|
|
|
2019-09-10 11:37:04 +02:00
|
|
|
browseAuthorsStub = sinon.stub(api.v3, 'authorsPublic').get(() => {
|
2019-09-03 12:33:31 +05:30
|
|
|
return {
|
|
|
|
browse: sinon.stub().resolves({authors: [], meta: meta})
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-10 11:37:04 +02:00
|
|
|
it('browse authors', function (done) {
|
2019-09-03 12:33:31 +05:30
|
|
|
helpers.get.call(
|
|
|
|
{},
|
|
|
|
'authors',
|
|
|
|
{hash: {}, data: locals, fn: fn, inverse: inverse}
|
|
|
|
).then(function () {
|
|
|
|
fn.called.should.be.true();
|
|
|
|
fn.firstCall.args[0].should.be.an.Object().with.property('authors');
|
|
|
|
fn.firstCall.args[0].authors.should.eql([]);
|
|
|
|
inverse.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-06-27 16:42:10 +01:00
|
|
|
describe('general error handling', function () {
|
|
|
|
it('should return an error for an unknown resource', function (done) {
|
|
|
|
helpers.get.call(
|
|
|
|
{},
|
|
|
|
'magic',
|
2018-10-17 09:23:59 +02:00
|
|
|
{hash: {}, data: locals, fn: fn, inverse: inverse}
|
2015-06-27 16:42:10 +01:00
|
|
|
).then(function () {
|
2016-02-07 21:27:01 +00:00
|
|
|
fn.called.should.be.false();
|
|
|
|
inverse.calledOnce.should.be.true();
|
|
|
|
inverse.firstCall.args[1].should.be.an.Object().and.have.property('data');
|
|
|
|
inverse.firstCall.args[1].data.should.be.an.Object().and.have.property('error');
|
2015-06-27 16:42:10 +01:00
|
|
|
inverse.firstCall.args[1].data.error.should.eql('Invalid resource given to get helper');
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle error from the API', function (done) {
|
|
|
|
helpers.get.call(
|
|
|
|
{},
|
|
|
|
'posts',
|
2019-09-10 11:37:04 +02:00
|
|
|
{hash: {slug: 'thing!'}, data: locals, fn: fn, inverse: inverse}
|
2015-06-27 16:42:10 +01:00
|
|
|
).then(function () {
|
2016-02-07 21:27:01 +00:00
|
|
|
fn.called.should.be.false();
|
|
|
|
inverse.calledOnce.should.be.true();
|
|
|
|
inverse.firstCall.args[1].should.be.an.Object().and.have.property('data');
|
|
|
|
inverse.firstCall.args[1].data.should.be.an.Object().and.have.property('error');
|
2015-06-27 16:42:10 +01:00
|
|
|
inverse.firstCall.args[1].data.error.should.match(/^Validation/);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show warning for call without any options', function (done) {
|
|
|
|
helpers.get.call(
|
|
|
|
{},
|
2018-10-17 09:23:59 +02:00
|
|
|
'posts',
|
|
|
|
{data: locals}
|
2015-06-27 16:42:10 +01:00
|
|
|
).then(function () {
|
2016-02-07 21:27:01 +00:00
|
|
|
fn.called.should.be.false();
|
|
|
|
inverse.called.should.be.false();
|
2015-06-27 16:42:10 +01:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
2015-10-22 11:12:21 +01:00
|
|
|
|
|
|
|
describe('path resolution', function () {
|
2020-04-29 16:44:27 +01:00
|
|
|
let browseStub;
|
|
|
|
let readStub;
|
|
|
|
const pubDate = new Date();
|
|
|
|
|
|
|
|
const resource = {
|
|
|
|
post: {id: 3, title: 'Test 3', author: {slug: 'cameron'}, tags: [{slug: 'test'}, {slug: 'magic'}], published_at: pubDate}
|
|
|
|
};
|
2015-10-22 11:12:21 +01:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2019-09-10 11:37:04 +02:00
|
|
|
browseStub = sinon.stub().resolves();
|
|
|
|
readStub = sinon.stub().resolves();
|
|
|
|
sinon.stub(api.v2, 'postsPublic').get(() => {
|
|
|
|
return {
|
|
|
|
browse: browseStub,
|
|
|
|
read: readStub
|
|
|
|
};
|
|
|
|
});
|
2015-10-22 11:12:21 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve post.tags alias', function (done) {
|
|
|
|
helpers.get.call(
|
2018-10-17 09:23:59 +02:00
|
|
|
resource,
|
2015-10-22 11:12:21 +01:00
|
|
|
'posts',
|
2018-10-17 09:23:59 +02:00
|
|
|
{hash: {filter: 'tags:[{{post.tags}}]'}, data: locals, fn: fn, inverse: inverse}
|
2015-10-22 11:12:21 +01:00
|
|
|
).then(function () {
|
2016-02-07 21:27:01 +00:00
|
|
|
browseStub.firstCall.args.should.be.an.Array().with.lengthOf(1);
|
|
|
|
browseStub.firstCall.args[0].should.be.an.Object().with.property('filter');
|
2015-10-22 11:12:21 +01:00
|
|
|
browseStub.firstCall.args[0].filter.should.eql('tags:[test,magic]');
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve post.author alias', function (done) {
|
|
|
|
helpers.get.call(
|
2018-10-17 09:23:59 +02:00
|
|
|
resource,
|
2015-10-22 11:12:21 +01:00
|
|
|
'posts',
|
2018-10-17 09:23:59 +02:00
|
|
|
{hash: {filter: 'author:{{post.author}}'}, data: locals, fn: fn, inverse: inverse}
|
2015-10-22 11:12:21 +01:00
|
|
|
).then(function () {
|
2016-02-07 21:27:01 +00:00
|
|
|
browseStub.firstCall.args.should.be.an.Array().with.lengthOf(1);
|
|
|
|
browseStub.firstCall.args[0].should.be.an.Object().with.property('filter');
|
2015-10-22 11:12:21 +01:00
|
|
|
browseStub.firstCall.args[0].filter.should.eql('author:cameron');
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve basic path', function (done) {
|
|
|
|
helpers.get.call(
|
2018-10-17 09:23:59 +02:00
|
|
|
resource,
|
2015-10-22 11:12:21 +01:00
|
|
|
'posts',
|
2018-10-17 09:23:59 +02:00
|
|
|
{hash: {filter: 'id:-{{post.id}}'}, data: locals, fn: fn, inverse: inverse}
|
2015-10-22 11:12:21 +01:00
|
|
|
).then(function () {
|
2017-03-21 08:24:11 +00:00
|
|
|
browseStub.firstCall.args.should.be.an.Array().with.lengthOf(1);
|
|
|
|
browseStub.firstCall.args[0].should.be.an.Object().with.property('filter');
|
|
|
|
browseStub.firstCall.args[0].filter.should.eql('id:-3');
|
2015-10-22 11:12:21 +01:00
|
|
|
|
2017-03-21 08:24:11 +00:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
2015-10-22 11:12:21 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle arrays the same as handlebars', function (done) {
|
|
|
|
helpers.get.call(
|
2018-10-17 09:23:59 +02:00
|
|
|
resource,
|
2015-10-22 11:12:21 +01:00
|
|
|
'posts',
|
2018-10-17 09:23:59 +02:00
|
|
|
{hash: {filter: 'tags:{{post.tags.[0].slug}}'}, data: locals, fn: fn, inverse: inverse}
|
2015-10-22 11:12:21 +01:00
|
|
|
).then(function () {
|
2016-02-07 21:27:01 +00:00
|
|
|
browseStub.firstCall.args.should.be.an.Array().with.lengthOf(1);
|
|
|
|
browseStub.firstCall.args[0].should.be.an.Object().with.property('filter');
|
2017-03-23 19:00:58 +00:00
|
|
|
browseStub.firstCall.args[0].filter.should.eql('tags:test');
|
2015-10-22 11:12:21 +01:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2018-02-14 17:33:07 +00:00
|
|
|
it('should handle dates', function (done) {
|
|
|
|
helpers.get.call(
|
2018-10-17 09:23:59 +02:00
|
|
|
resource,
|
2018-02-14 17:33:07 +00:00
|
|
|
'posts',
|
2019-08-19 12:41:09 +01:00
|
|
|
{hash: {filter: 'published_at:<=\'{{post.published_at}}\''}, data: locals, fn: fn, inverse: inverse}
|
2018-02-14 17:33:07 +00:00
|
|
|
).then(function () {
|
|
|
|
browseStub.firstCall.args.should.be.an.Array().with.lengthOf(1);
|
|
|
|
browseStub.firstCall.args[0].should.be.an.Object().with.property('filter');
|
|
|
|
browseStub.firstCall.args[0].filter.should.eql(`published_at:<='${pubDate.toISOString()}'`);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2015-10-22 11:12:21 +01:00
|
|
|
it('should output nothing if path does not resolve', function (done) {
|
|
|
|
helpers.get.call(
|
2018-10-17 09:23:59 +02:00
|
|
|
resource,
|
2015-10-22 11:12:21 +01:00
|
|
|
'posts',
|
2018-10-17 09:23:59 +02:00
|
|
|
{hash: {filter: 'id:{{post.thing}}'}, data: locals, fn: fn, inverse: inverse}
|
2015-10-22 11:12:21 +01:00
|
|
|
).then(function () {
|
2017-03-21 08:24:11 +00:00
|
|
|
browseStub.firstCall.args.should.be.an.Array().with.lengthOf(1);
|
|
|
|
browseStub.firstCall.args[0].should.be.an.Object().with.property('filter');
|
|
|
|
browseStub.firstCall.args[0].filter.should.eql('id:');
|
2015-10-22 11:12:21 +01:00
|
|
|
|
2017-03-21 08:24:11 +00:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
2015-10-22 11:12:21 +01:00
|
|
|
});
|
2019-02-04 15:19:00 +00:00
|
|
|
|
|
|
|
it('should resolve global props', function (done) {
|
|
|
|
helpers.get.call(
|
|
|
|
resource,
|
|
|
|
'posts',
|
|
|
|
{hash: {filter: 'slug:{{@globalProp.foo}}'}, data: locals, fn: fn, inverse: inverse}
|
|
|
|
).then(function () {
|
|
|
|
browseStub.firstCall.args.should.be.an.Array().with.lengthOf(1);
|
|
|
|
browseStub.firstCall.args[0].should.be.an.Object().with.property('filter');
|
|
|
|
browseStub.firstCall.args[0].filter.should.eql('slug:bar');
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
2015-10-22 11:12:21 +01:00
|
|
|
});
|
2015-06-27 16:42:10 +01:00
|
|
|
});
|