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 () {
|
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 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({
|
2021-06-01 15:09:12 +03:00
|
|
|
invites: [{email: 'test@example.com', role_id: testUtils.getExistingData().roles[1].id}]
|
2019-02-04 17:16:24 +03:00
|
|
|
})
|
|
|
|
.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');
|
2021-06-01 15:09:12 +03:00
|
|
|
jsonResponse.invites[0].role_id.should.eql(testUtils.getExistingData().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
|
|
|
});
|
2022-08-24 10:28:20 +03:00
|
|
|
|
|
|
|
it('Cannot destroy an non-existent invite', async function () {
|
|
|
|
await request.del(localUtils.API.getApiQuery(`invites/abcd1234abcd1234abcd1234/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(404)
|
|
|
|
.expect((res) => {
|
|
|
|
res.body.errors[0].message.should.eql('Resource not found error, cannot delete invite.');
|
|
|
|
});
|
|
|
|
|
|
|
|
mailService.GhostMailer.prototype.send.called.should.be.false();
|
|
|
|
});
|
2018-10-12 20:44:06 +03:00
|
|
|
});
|