2018-10-12 20:44:06 +03:00
|
|
|
const should = require('should');
|
|
|
|
const supertest = require('supertest');
|
|
|
|
const sinon = require('sinon');
|
2019-09-20 18:02:45 +03:00
|
|
|
const testUtils = require('../../utils');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../core/shared/config');
|
2020-03-30 18:26:47 +03:00
|
|
|
const mailService = require('../../../core/server/services/mail');
|
2018-10-12 20:44:06 +03:00
|
|
|
const localUtils = require('./utils');
|
|
|
|
|
2019-02-04 17:16:24 +03:00
|
|
|
describe('Invites API', function () {
|
2020-11-30 17:25:22 +03:00
|
|
|
let request;
|
2018-10-12 20:44:06 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
before(async function () {
|
|
|
|
await testUtils.startGhost();
|
|
|
|
request = supertest.agent(config.get('url'));
|
|
|
|
await localUtils.doAuth(request, 'invites');
|
2018-10-12 20:44:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(mailService.GhostMailer.prototype, 'send').resolves('Mail is disabled');
|
2018-10-12 20:44:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2018-10-12 20:44:06 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can fetch all invites', async function () {
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery('invites/'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.invites);
|
|
|
|
jsonResponse.invites.should.have.length(2);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse, 'invites');
|
|
|
|
localUtils.API.checkResponse(jsonResponse.invites[0], 'invite');
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
jsonResponse.invites[0].status.should.eql('sent');
|
|
|
|
jsonResponse.invites[0].email.should.eql('test1@ghost.org');
|
|
|
|
jsonResponse.invites[0].role_id.should.eql(testUtils.roles.ids.admin);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
jsonResponse.invites[1].status.should.eql('sent');
|
|
|
|
jsonResponse.invites[1].email.should.eql('test2@ghost.org');
|
|
|
|
jsonResponse.invites[1].role_id.should.eql(testUtils.roles.ids.author);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
mailService.GhostMailer.prototype.send.called.should.be.false();
|
2018-10-12 20:44:06 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can read an invitation by id', async function () {
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`invites/${testUtils.DataGenerator.forKnex.invites[0].id}/`))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.invites);
|
|
|
|
jsonResponse.invites.should.have.length(1);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.invites[0], 'invite');
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
mailService.GhostMailer.prototype.send.called.should.be.false();
|
2018-10-12 20:44:06 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can add a new invite', async function () {
|
|
|
|
const res = await request
|
2019-02-04 17:16:24 +03:00
|
|
|
.post(localUtils.API.getApiQuery('invites/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
invites: [{email: 'test@example.com', role_id: testUtils.existingData.roles[1].id}]
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(201);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.invites);
|
|
|
|
jsonResponse.invites.should.have.length(1);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.invites[0], 'invite');
|
|
|
|
jsonResponse.invites[0].role_id.should.eql(testUtils.existingData.roles[1].id);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
mailService.GhostMailer.prototype.send.called.should.be.true();
|
2020-09-14 13:33:37 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
should.exist(res.headers.location);
|
|
|
|
res.headers.location.should.equal(`http://127.0.0.1:2369${localUtils.API.getApiQuery('invites/')}${res.body.invites[0].id}/`);
|
2018-10-12 20:44:06 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can destroy an existing invite', async function () {
|
|
|
|
await request.del(localUtils.API.getApiQuery(`invites/${testUtils.DataGenerator.forKnex.invites[0].id}/`))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(204);
|
|
|
|
|
|
|
|
mailService.GhostMailer.prototype.send.called.should.be.false();
|
2018-10-12 20:44:06 +03:00
|
|
|
});
|
|
|
|
});
|