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');
|
|
|
|
const ghost = testUtils.startGhost;
|
2019-01-21 19:53:44 +03:00
|
|
|
|
2018-10-12 20:44:06 +03:00
|
|
|
let request;
|
|
|
|
|
2019-02-04 17:16:24 +03:00
|
|
|
describe('Invites API', function () {
|
|
|
|
let ghostServer;
|
2018-10-12 20:44:06 +03:00
|
|
|
|
|
|
|
before(function () {
|
|
|
|
return ghost()
|
|
|
|
.then(function (_ghostServer) {
|
|
|
|
ghostServer = _ghostServer;
|
|
|
|
request = supertest.agent(config.get('url'));
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return localUtils.doAuth(request, 'invites');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-02-04 17:16:24 +03:00
|
|
|
it('Can fetch all invites', function (done) {
|
|
|
|
request.get(localUtils.API.getApiQuery('invites/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.end(function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
localUtils.API.checkResponse(jsonResponse, 'invites');
|
|
|
|
localUtils.API.checkResponse(jsonResponse.invites[0], 'invite');
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
mailService.GhostMailer.prototype.send.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2018-10-12 20:44:06 +03:00
|
|
|
});
|
|
|
|
|
2019-02-04 17:16:24 +03:00
|
|
|
it('Can read an invitation by id', function (done) {
|
|
|
|
request.get(localUtils.API.getApiQuery(`invites/${testUtils.DataGenerator.forKnex.invites[0].id}/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.end(function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
localUtils.API.checkResponse(jsonResponse.invites[0], 'invite');
|
|
|
|
|
|
|
|
mailService.GhostMailer.prototype.send.called.should.be.false();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2018-10-12 20:44:06 +03:00
|
|
|
});
|
|
|
|
|
2019-02-04 17:16:24 +03:00
|
|
|
it('Can add a new invite', function (done) {
|
|
|
|
request
|
|
|
|
.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)
|
|
|
|
.expect(201)
|
|
|
|
.end(function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
localUtils.API.checkResponse(jsonResponse.invites[0], 'invite');
|
|
|
|
jsonResponse.invites[0].role_id.should.eql(testUtils.existingData.roles[1].id);
|
|
|
|
|
|
|
|
mailService.GhostMailer.prototype.send.called.should.be.true();
|
|
|
|
|
2020-09-14 13:33:37 +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}/`);
|
|
|
|
|
2019-02-04 17:16:24 +03:00
|
|
|
done();
|
|
|
|
});
|
2018-10-12 20:44:06 +03:00
|
|
|
});
|
|
|
|
|
2019-02-04 17:16:24 +03:00
|
|
|
it('Can destroy an existing invite', function (done) {
|
|
|
|
request.del(localUtils.API.getApiQuery(`invites/${testUtils.DataGenerator.forKnex.invites[0].id}/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(204)
|
|
|
|
.end(function (err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
mailService.GhostMailer.prototype.send.called.should.be.false();
|
|
|
|
done();
|
|
|
|
});
|
2018-10-12 20:44:06 +03:00
|
|
|
});
|
|
|
|
});
|