Migrated authentication.acceptInvitation method to v2

This commit is contained in:
Nazar Gargol 2019-07-24 12:40:18 +02:00
parent 4da03a38b6
commit f4b97d3bc8
5 changed files with 65 additions and 3 deletions

View File

@ -1,6 +1,7 @@
const auth = require('../../services/auth');
const api = require('./index');
const web = require('../../web');
const auth = require('../../services/auth');
const invitations = require('../../services/invitations');
module.exports = {
docName: 'authentication',
@ -56,5 +57,21 @@ module.exports = {
});
});
}
},
acceptInvitation: {
validation: {
docName: 'invitations'
},
permissions: false,
query(frame) {
return Promise.resolve()
.then(() => {
return auth.setup.assertSetupCompleted(true);
})
.then(() => {
return invitations.accept(frame.data);
});
}
}
};

View File

@ -0,0 +1,14 @@
const common = require('../../../../../lib/common');
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:output:authentication');
module.exports = {
acceptInvitation(data, apiConfig, frame) {
debug('acceptInvitation');
frame.response = {
invitation: [
{message: common.i18n.t('common.api.authentication.mail.invitationAccepted')}
]
};
}
};

View File

@ -0,0 +1,28 @@
const Promise = require('bluebird');
const debug = require('ghost-ignition').debug('api:v2:utils:validators:input:invitation');
const common = require('../../../../../lib/common');
module.exports = {
acceptInvitation(apiConfig, frame) {
debug('acceptInvitation');
const data = frame.data.invitation[0];
if (!data.token) {
return Promise.reject(new common.errors.ValidationError({message: common.i18n.t('errors.api.authentication.noTokenProvided')}));
}
if (!data.email) {
return Promise.reject(new common.errors.ValidationError({message: common.i18n.t('errors.api.authentication.noEmailProvided')}));
}
if (!data.password) {
return Promise.reject(new common.errors.ValidationError({message: common.i18n.t('errors.api.authentication.noPasswordProvided')}));
}
if (!data.name) {
return Promise.reject(new common.errors.ValidationError({message: common.i18n.t('errors.api.authentication.noNameProvided')}));
}
}
};

View File

@ -187,7 +187,7 @@ module.exports = function apiRoutes() {
api.http(apiv2.authentication.generateResetToken)
);
router.put('/authentication/passwordreset', shared.middlewares.brute.globalBlock, api.http(apiv2.authentication.resetPassword));
router.post('/authentication/invitation', api.http(api.authentication.acceptInvitation));
router.post('/authentication/invitation', api.http(apiv2.authentication.acceptInvitation));
router.get('/authentication/invitation', api.http(api.authentication.isInvitation));
router.post('/authentication/setup', api.http(api.authentication.setup));
router.put('/authentication/setup', mw.authAdminApi, api.http(api.authentication.updateSetup));

View File

@ -426,7 +426,10 @@ describe.only('Authentication API v2', function () {
}]
})
.expect('Content-Type', /json/)
.expect(200);
.expect(200)
.then((res) => {
res.body.invitation[0].message.should.equal('Invitation accepted.');
});
});
});
});