2018-11-07 17:29:37 +03:00
|
|
|
const should = require('should');
|
|
|
|
const supertest = require('supertest');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const url = require('url');
|
2019-09-20 18:02:45 +03:00
|
|
|
const configUtils = require('../../utils/configUtils');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../core/shared/config');
|
2020-03-30 18:26:47 +03:00
|
|
|
const models = require('../../../core/server/models');
|
2019-09-20 18:02:45 +03:00
|
|
|
const testUtils = require('../../utils');
|
2018-11-07 17:29:37 +03:00
|
|
|
const localUtils = require('./utils');
|
2019-09-20 18:02:45 +03:00
|
|
|
|
2019-02-04 17:49:59 +03:00
|
|
|
describe('Authors Content API', function () {
|
2020-11-30 17:25:22 +03:00
|
|
|
let request;
|
|
|
|
|
|
|
|
before(async function () {
|
2021-11-18 11:55:35 +03:00
|
|
|
await localUtils.startGhost();
|
2020-11-30 17:25:22 +03:00
|
|
|
request = supertest.agent(config.get('url'));
|
|
|
|
await testUtils.initFixtures('owner:post', 'users:no-owner', 'user:inactive', 'posts', 'api_keys');
|
2018-11-07 17:29:37 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
configUtils.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
const validKey = localUtils.getValidKey();
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can request authors', async function () {
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`authors/?key=${validKey}`))
|
2018-11-07 17:29:37 +03:00
|
|
|
.set('Origin', testUtils.API.getURL())
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse.authors);
|
|
|
|
localUtils.API.checkResponse(jsonResponse, 'authors');
|
|
|
|
jsonResponse.authors.should.have.length(3);
|
|
|
|
|
|
|
|
// We don't expose the email address, status and other attrs.
|
|
|
|
localUtils.API.checkResponse(jsonResponse.authors[0], 'author', ['url'], null, null);
|
|
|
|
|
|
|
|
// Default order 'name asc' check
|
|
|
|
jsonResponse.authors[0].name.should.eql('Ghost');
|
|
|
|
jsonResponse.authors[2].name.should.eql('Slimer McEctoplasm');
|
|
|
|
|
|
|
|
should.exist(res.body.authors[0].url);
|
|
|
|
should.exist(url.parse(res.body.authors[0].url).protocol);
|
|
|
|
should.exist(url.parse(res.body.authors[0].url).host);
|
|
|
|
|
|
|
|
// Public api returns all authors, but no status! Locked/Inactive authors can still have written articles.
|
|
|
|
const response = await models.Author.findPage(Object.assign({status: 'all'}, testUtils.context.internal));
|
|
|
|
_.map(response.data, model => model.toJSON()).length.should.eql(3);
|
2018-11-07 17:29:37 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can request authors including post count', async function () {
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`authors/?key=${validKey}&include=count.posts&order=count.posts ASC`))
|
2018-11-07 17:29:37 +03:00
|
|
|
.set('Origin', testUtils.API.getURL())
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
2018-11-07 17:29:37 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const jsonResponse = res.body;
|
2018-11-07 17:29:37 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
should.exist(jsonResponse.authors);
|
|
|
|
jsonResponse.authors.should.have.length(3);
|
2018-11-07 17:29:37 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
// We don't expose the email address.
|
|
|
|
localUtils.API.checkResponse(jsonResponse.authors[0], 'author', ['count', 'url'], null, null);
|
2018-11-07 17:29:37 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
// Each user should have the correct count and be more than 0
|
|
|
|
_.find(jsonResponse.authors, {slug: 'joe-bloggs'}).count.posts.should.eql(4);
|
|
|
|
_.find(jsonResponse.authors, {slug: 'slimer-mcectoplasm'}).count.posts.should.eql(1);
|
|
|
|
_.find(jsonResponse.authors, {slug: 'ghost'}).count.posts.should.eql(7);
|
2018-11-07 17:29:37 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const ids = jsonResponse.authors
|
|
|
|
.filter(author => (author.slug !== 'ghost'))
|
|
|
|
.map(user => user.id);
|
2018-11-07 17:29:37 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
ids.should.eql([
|
|
|
|
testUtils.DataGenerator.Content.users[3].id,
|
|
|
|
testUtils.DataGenerator.Content.users[0].id
|
|
|
|
]);
|
2018-11-07 17:29:37 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can request single author', async function () {
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`authors/slug/ghost/?key=${validKey}`))
|
2018-11-07 17:29:37 +03:00
|
|
|
.set('Origin', testUtils.API.getURL())
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
|
|
|
|
should.exist(jsonResponse.authors);
|
|
|
|
jsonResponse.authors.should.have.length(1);
|
|
|
|
|
|
|
|
// We don't expose the email address.
|
|
|
|
localUtils.API.checkResponse(jsonResponse.authors[0], 'author', ['url'], null, null);
|
2018-11-07 17:29:37 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can request author by id including post count', async function () {
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`authors/1/?key=${validKey}&include=count.posts`))
|
2018-11-07 17:29:37 +03:00
|
|
|
.set('Origin', testUtils.API.getURL())
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
|
|
|
|
should.exist(jsonResponse.authors);
|
|
|
|
jsonResponse.authors.should.have.length(1);
|
|
|
|
|
|
|
|
// We don't expose the email address.
|
|
|
|
localUtils.API.checkResponse(jsonResponse.authors[0], 'author', ['count', 'url'], null, null);
|
2018-11-07 17:29:37 +03:00
|
|
|
});
|
|
|
|
});
|