2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const Promise = require('bluebird');
|
2015-06-27 18:42:10 +03:00
|
|
|
|
2020-04-29 18:44:27 +03: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 18:42:10 +03:00
|
|
|
|
|
|
|
describe('{{#get}} helper', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let fn;
|
|
|
|
let inverse;
|
2018-10-17 10:23:59 +03:00
|
|
|
let locals = {};
|
2015-06-27 18:42:10 +03:00
|
|
|
|
2017-06-08 22:34:20 +03:00
|
|
|
before(function () {
|
|
|
|
models.init();
|
|
|
|
});
|
|
|
|
|
2015-06-27 18:42:10 +03:00
|
|
|
beforeEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
fn = sinon.spy();
|
|
|
|
inverse = sinon.spy();
|
2018-10-17 10:23:59 +03:00
|
|
|
|
2019-09-10 12:37:04 +03:00
|
|
|
locals = {root: {_locals: {apiVersion: 'v2'}}, globalProp: {foo: 'bar'}};
|
2015-06-27 18:42:10 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2015-06-27 18:42:10 +03:00
|
|
|
});
|
|
|
|
|
2018-11-07 17:29:37 +03:00
|
|
|
describe('authors v2', function () {
|
2019-09-10 12:37:04 +03:00
|
|
|
let browseAuthorsStub;
|
2018-11-07 17:29:37 +03:00
|
|
|
const meta = {pagination: {}};
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
locals = {root: {_locals: {apiVersion: 'v2'}}};
|
|
|
|
|
2019-09-10 12:37:04 +03:00
|
|
|
browseAuthorsStub = sinon.stub(api.v2, 'authorsPublic').get(() => {
|
2018-11-07 17:29:37 +03:00
|
|
|
return {
|
2019-01-21 19:53:44 +03:00
|
|
|
browse: sinon.stub().resolves({authors: [], meta: meta})
|
2018-11-07 17:29:37 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-10 12:37:04 +03:00
|
|
|
it('browse authors', function (done) {
|
2018-11-07 17:29:37 +03:00
|
|
|
helpers.get.call(
|
|
|
|
{},
|
|
|
|
'authors',
|
|
|
|
{hash: {}, data: locals, fn: fn, inverse: inverse}
|
|
|
|
).then(function () {
|
2019-08-09 17:25:12 +03:00
|
|
|
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 12:37:04 +03:00
|
|
|
let browseAuthorsStub;
|
2019-08-09 17:25:12 +03:00
|
|
|
const meta = {pagination: {}};
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
locals = {root: {_locals: {apiVersion: 'canary'}}};
|
|
|
|
|
2019-09-10 12:37:04 +03:00
|
|
|
browseAuthorsStub = sinon.stub(api.canary, 'authorsPublic').get(() => {
|
2019-08-09 17:25:12 +03:00
|
|
|
return {
|
|
|
|
browse: sinon.stub().resolves({authors: [], meta: meta})
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-10 12:37:04 +03:00
|
|
|
it('browse authors', function (done) {
|
2019-08-09 17:25:12 +03:00
|
|
|
helpers.get.call(
|
|
|
|
{},
|
|
|
|
'authors',
|
|
|
|
{hash: {}, data: locals, fn: fn, inverse: inverse}
|
|
|
|
).then(function () {
|
2019-09-03 10:03:31 +03:00
|
|
|
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 12:37:04 +03:00
|
|
|
let browseAuthorsStub;
|
2019-09-03 10:03:31 +03:00
|
|
|
const meta = {pagination: {}};
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
locals = {root: {_locals: {apiVersion: 'v3'}}};
|
|
|
|
|
2019-09-10 12:37:04 +03:00
|
|
|
browseAuthorsStub = sinon.stub(api.v3, 'authorsPublic').get(() => {
|
2019-09-03 10:03:31 +03:00
|
|
|
return {
|
|
|
|
browse: sinon.stub().resolves({authors: [], meta: meta})
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-10 12:37:04 +03:00
|
|
|
it('browse authors', function (done) {
|
2019-09-03 10:03:31 +03:00
|
|
|
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 18:42:10 +03:00
|
|
|
describe('general error handling', function () {
|
|
|
|
it('should return an error for an unknown resource', function (done) {
|
|
|
|
helpers.get.call(
|
|
|
|
{},
|
|
|
|
'magic',
|
2018-10-17 10:23:59 +03:00
|
|
|
{hash: {}, data: locals, fn: fn, inverse: inverse}
|
2015-06-27 18:42:10 +03:00
|
|
|
).then(function () {
|
2016-02-08 00:27:01 +03: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 18:42:10 +03: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 12:37:04 +03:00
|
|
|
{hash: {slug: 'thing!'}, data: locals, fn: fn, inverse: inverse}
|
2015-06-27 18:42:10 +03:00
|
|
|
).then(function () {
|
2016-02-08 00:27:01 +03: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 18:42:10 +03: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 10:23:59 +03:00
|
|
|
'posts',
|
|
|
|
{data: locals}
|
2015-06-27 18:42:10 +03:00
|
|
|
).then(function () {
|
2016-02-08 00:27:01 +03:00
|
|
|
fn.called.should.be.false();
|
|
|
|
inverse.called.should.be.false();
|
2015-06-27 18:42:10 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
2015-10-22 13:12:21 +03:00
|
|
|
|
|
|
|
describe('path resolution', function () {
|
2020-04-29 18:44:27 +03: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 13:12:21 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2019-09-10 12:37:04 +03:00
|
|
|
browseStub = sinon.stub().resolves();
|
|
|
|
readStub = sinon.stub().resolves();
|
|
|
|
sinon.stub(api.v2, 'postsPublic').get(() => {
|
|
|
|
return {
|
|
|
|
browse: browseStub,
|
|
|
|
read: readStub
|
|
|
|
};
|
|
|
|
});
|
2015-10-22 13:12:21 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve post.tags alias', function (done) {
|
|
|
|
helpers.get.call(
|
2018-10-17 10:23:59 +03:00
|
|
|
resource,
|
2015-10-22 13:12:21 +03:00
|
|
|
'posts',
|
2018-10-17 10:23:59 +03:00
|
|
|
{hash: {filter: 'tags:[{{post.tags}}]'}, data: locals, fn: fn, inverse: inverse}
|
2015-10-22 13:12:21 +03:00
|
|
|
).then(function () {
|
2016-02-08 00:27:01 +03: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 13:12:21 +03: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 10:23:59 +03:00
|
|
|
resource,
|
2015-10-22 13:12:21 +03:00
|
|
|
'posts',
|
2018-10-17 10:23:59 +03:00
|
|
|
{hash: {filter: 'author:{{post.author}}'}, data: locals, fn: fn, inverse: inverse}
|
2015-10-22 13:12:21 +03:00
|
|
|
).then(function () {
|
2016-02-08 00:27:01 +03: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 13:12:21 +03: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 10:23:59 +03:00
|
|
|
resource,
|
2015-10-22 13:12:21 +03:00
|
|
|
'posts',
|
2018-10-17 10:23:59 +03:00
|
|
|
{hash: {filter: 'id:-{{post.id}}'}, data: locals, fn: fn, inverse: inverse}
|
2015-10-22 13:12:21 +03:00
|
|
|
).then(function () {
|
2017-03-21 11:24:11 +03: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 13:12:21 +03:00
|
|
|
|
2017-03-21 11:24:11 +03:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
2015-10-22 13:12:21 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle arrays the same as handlebars', function (done) {
|
|
|
|
helpers.get.call(
|
2018-10-17 10:23:59 +03:00
|
|
|
resource,
|
2015-10-22 13:12:21 +03:00
|
|
|
'posts',
|
2018-10-17 10:23:59 +03:00
|
|
|
{hash: {filter: 'tags:{{post.tags.[0].slug}}'}, data: locals, fn: fn, inverse: inverse}
|
2015-10-22 13:12:21 +03:00
|
|
|
).then(function () {
|
2016-02-08 00:27:01 +03: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 22:00:58 +03:00
|
|
|
browseStub.firstCall.args[0].filter.should.eql('tags:test');
|
2015-10-22 13:12:21 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
2018-02-14 20:33:07 +03:00
|
|
|
it('should handle dates', function (done) {
|
|
|
|
helpers.get.call(
|
2018-10-17 10:23:59 +03:00
|
|
|
resource,
|
2018-02-14 20:33:07 +03:00
|
|
|
'posts',
|
2019-08-19 14:41:09 +03:00
|
|
|
{hash: {filter: 'published_at:<=\'{{post.published_at}}\''}, data: locals, fn: fn, inverse: inverse}
|
2018-02-14 20:33:07 +03: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 13:12:21 +03:00
|
|
|
it('should output nothing if path does not resolve', function (done) {
|
|
|
|
helpers.get.call(
|
2018-10-17 10:23:59 +03:00
|
|
|
resource,
|
2015-10-22 13:12:21 +03:00
|
|
|
'posts',
|
2018-10-17 10:23:59 +03:00
|
|
|
{hash: {filter: 'id:{{post.thing}}'}, data: locals, fn: fn, inverse: inverse}
|
2015-10-22 13:12:21 +03:00
|
|
|
).then(function () {
|
2017-03-21 11:24:11 +03: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 13:12:21 +03:00
|
|
|
|
2017-03-21 11:24:11 +03:00
|
|
|
done();
|
|
|
|
}).catch(done);
|
2015-10-22 13:12:21 +03:00
|
|
|
});
|
2019-02-04 18:19:00 +03: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 13:12:21 +03:00
|
|
|
});
|
2015-06-27 18:42:10 +03:00
|
|
|
});
|