2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const Promise = require('bluebird');
|
2021-10-06 12:52:46 +03:00
|
|
|
const {SafeString} = require('../../../../core/frontend/services/rendering');
|
2015-06-27 18:42:10 +03:00
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
// Stuff we are testing
|
2021-10-06 12:52:46 +03:00
|
|
|
const get = require('../../../../core/frontend/helpers/get');
|
2020-04-29 18:44:27 +03:00
|
|
|
|
2021-10-06 12:52:46 +03:00
|
|
|
const models = require('../../../../core/server/models');
|
2022-01-21 15:36:07 +03:00
|
|
|
|
2021-12-16 14:59:39 +03:00
|
|
|
const proxy = require('../../../../core/frontend/services/proxy');
|
2015-06-27 18:42:10 +03:00
|
|
|
|
2022-01-21 15:36:07 +03:00
|
|
|
const API_VERSION = 'canary';
|
|
|
|
|
|
|
|
const api = require('../../../../core/server/api')[API_VERSION];
|
|
|
|
|
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
|
|
|
|
2022-01-21 15:36:07 +03:00
|
|
|
locals = {root: {_locals: {apiVersion: API_VERSION}}, 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
|
|
|
});
|
|
|
|
|
2021-07-01 19:55:44 +03:00
|
|
|
describe('context preparation', function () {
|
|
|
|
let browsePostsStub;
|
|
|
|
const meta = {pagination: {}};
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
locals = {root: {_locals: {apiVersion: 'canary'}}};
|
|
|
|
|
2022-01-21 15:36:07 +03:00
|
|
|
browsePostsStub = sinon.stub(api, 'postsPublic').get(() => {
|
2021-07-01 19:55:44 +03:00
|
|
|
return {
|
|
|
|
browse: sinon.stub().resolves({posts: [{feature_image_caption: '<a href="#">A link</a>'}], meta: meta})
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('converts html strings to SafeString', function (done) {
|
2021-10-04 18:30:54 +03:00
|
|
|
get.call(
|
2021-07-01 19:55:44 +03:00
|
|
|
{},
|
|
|
|
'posts',
|
|
|
|
{hash: {}, data: locals, fn: fn, inverse: inverse}
|
|
|
|
).then(function () {
|
|
|
|
fn.called.should.be.true();
|
|
|
|
fn.firstCall.args[0].should.be.an.Object().with.property('posts');
|
|
|
|
|
|
|
|
fn.firstCall.args[0].posts[0].feature_image_caption.should.be.an.instanceOf(SafeString);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-09 17:25:12 +03:00
|
|
|
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 () {
|
2022-01-21 15:36:07 +03:00
|
|
|
locals = {root: {_locals: {apiVersion: API_VERSION}}};
|
2019-08-09 17:25:12 +03:00
|
|
|
|
2022-01-21 15:36:07 +03:00
|
|
|
browseAuthorsStub = sinon.stub(api, '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) {
|
2021-10-04 18:30:54 +03:00
|
|
|
get.call(
|
2019-09-03 10:03:31 +03:00
|
|
|
{},
|
|
|
|
'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) {
|
2021-10-04 18:30:54 +03:00
|
|
|
get.call(
|
2015-06-27 18:42:10 +03:00
|
|
|
{},
|
|
|
|
'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) {
|
2021-10-04 18:30:54 +03:00
|
|
|
get.call(
|
2015-06-27 18:42:10 +03:00
|
|
|
{},
|
|
|
|
'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) {
|
2021-10-04 18:30:54 +03:00
|
|
|
get.call(
|
2015-06-27 18:42:10 +03:00
|
|
|
{},
|
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();
|
2022-01-21 15:36:07 +03:00
|
|
|
sinon.stub(api, 'postsPublic').get(() => {
|
2019-09-10 12:37:04 +03:00
|
|
|
return {
|
|
|
|
browse: browseStub,
|
|
|
|
read: readStub
|
|
|
|
};
|
|
|
|
});
|
2015-10-22 13:12:21 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve post.tags alias', function (done) {
|
2021-10-04 18:30:54 +03:00
|
|
|
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) {
|
2021-10-04 18:30:54 +03:00
|
|
|
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) {
|
2021-10-04 18:30:54 +03:00
|
|
|
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) {
|
2021-10-04 18:30:54 +03:00
|
|
|
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) {
|
2021-10-04 18:30:54 +03:00
|
|
|
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) {
|
2021-10-04 18:30:54 +03:00
|
|
|
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) {
|
2021-10-04 18:30:54 +03:00
|
|
|
get.call(
|
2019-02-04 18:19:00 +03:00
|
|
|
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
|
|
|
});
|
2021-12-16 14:59:39 +03:00
|
|
|
|
|
|
|
describe('limit=all max', function () {
|
|
|
|
let browseStub;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
browseStub = sinon.stub().resolves();
|
2022-01-21 15:36:07 +03:00
|
|
|
|
|
|
|
sinon.stub(api, 'postsPublic').get(() => {
|
2021-12-16 14:59:39 +03:00
|
|
|
return {
|
|
|
|
browse: browseStub
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Behaves normally without config', async function () {
|
2022-01-21 15:36:07 +03:00
|
|
|
locals = {root: {_locals: {apiVersion: API_VERSION}}};
|
2021-12-16 14:59:39 +03:00
|
|
|
await get.call(
|
|
|
|
{},
|
|
|
|
'posts',
|
|
|
|
{hash: {limit: 'all'}, data: locals, fn: fn, inverse: inverse}
|
|
|
|
);
|
|
|
|
browseStub.firstCall.args[0].limit.should.eql('all');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Replaces "all" with "getHelperLimitAllMax" config, if present', async function () {
|
|
|
|
sinon.stub(proxy.config, 'get').withArgs('getHelperLimitAllMax').returns(2);
|
|
|
|
|
2022-01-21 15:36:07 +03:00
|
|
|
locals = {root: {_locals: {apiVersion: API_VERSION}}};
|
2021-12-16 14:59:39 +03:00
|
|
|
await get.call(
|
|
|
|
{},
|
|
|
|
'posts',
|
|
|
|
{hash: {limit: 'all'}, data: locals, fn: fn, inverse: inverse}
|
|
|
|
);
|
|
|
|
browseStub.firstCall.args[0].limit.should.eql(2);
|
|
|
|
});
|
|
|
|
});
|
2022-03-03 18:18:05 +03:00
|
|
|
|
|
|
|
describe('auth', function () {
|
|
|
|
/**
|
|
|
|
* @type sinon.SinonStub<any[], any>
|
|
|
|
*/
|
|
|
|
let browseStub;
|
|
|
|
let member;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
browseStub = sinon.stub().resolves();
|
|
|
|
member = {uuid: 'test'};
|
|
|
|
|
|
|
|
sinon.stub(api, 'postsPublic').get(() => {
|
|
|
|
return {
|
|
|
|
browse: browseStub
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should pass the member context', async function () {
|
|
|
|
locals = {root: {_locals: {apiVersion: API_VERSION}}, member};
|
|
|
|
await get.call(
|
|
|
|
{},
|
|
|
|
'posts',
|
|
|
|
{hash: {}, data: locals, fn: fn, inverse: inverse}
|
|
|
|
);
|
|
|
|
browseStub.firstCall.args[0].context.member.should.eql(member);
|
|
|
|
});
|
|
|
|
});
|
2015-06-27 18:42:10 +03:00
|
|
|
});
|