2019-09-03 06:07:03 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const MagicLink = require('../');
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
|
|
|
const sandbox = sinon.createSandbox();
|
2019-10-10 12:21:03 +03:00
|
|
|
const secret = crypto.randomBytes(64);
|
2019-09-03 06:07:03 +03:00
|
|
|
|
|
|
|
describe('MagicLink', function () {
|
|
|
|
it('Exports a function', function () {
|
|
|
|
should.equal(typeof MagicLink, 'function');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#sendMagicLink', function () {
|
2019-10-01 09:51:43 +03:00
|
|
|
it('Sends an email to the user with a link generated from getSigninURL(token, type)', async function () {
|
2019-09-03 06:07:03 +03:00
|
|
|
const options = {
|
2020-09-18 15:21:46 +03:00
|
|
|
tokenProvider: new MagicLink.JWTTokenProvider(secret),
|
2019-09-03 06:07:03 +03:00
|
|
|
getSigninURL: sandbox.stub().returns('FAKEURL'),
|
|
|
|
getText: sandbox.stub().returns('SOMETEXT'),
|
|
|
|
getHTML: sandbox.stub().returns('SOMEHTML'),
|
2019-10-10 16:19:56 +03:00
|
|
|
getSubject: sandbox.stub().returns('SOMESUBJECT'),
|
2019-09-03 06:07:03 +03:00
|
|
|
transporter: {
|
|
|
|
sendMail: sandbox.stub().resolves()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const service = new MagicLink(options);
|
|
|
|
|
|
|
|
const args = {
|
|
|
|
email: 'test@example.com',
|
2020-09-17 17:42:01 +03:00
|
|
|
tokenData: {
|
|
|
|
id: '420'
|
|
|
|
},
|
2019-10-01 09:51:43 +03:00
|
|
|
type: 'blazeit'
|
2019-09-03 06:07:03 +03:00
|
|
|
};
|
|
|
|
const {token} = await service.sendMagicLink(args);
|
|
|
|
|
|
|
|
should.ok(options.getSigninURL.calledOnce);
|
2020-10-29 09:40:28 +03:00
|
|
|
should.ok(options.getSigninURL.firstCall.calledWithExactly(token, 'blazeit', ''));
|
2019-09-03 06:07:03 +03:00
|
|
|
|
2019-10-11 07:29:11 +03:00
|
|
|
should.ok(options.getText.calledOnce);
|
|
|
|
should.ok(options.getText.firstCall.calledWithExactly('FAKEURL', 'blazeit', 'test@example.com'));
|
|
|
|
|
|
|
|
should.ok(options.getHTML.calledOnce);
|
|
|
|
should.ok(options.getHTML.firstCall.calledWithExactly('FAKEURL', 'blazeit', 'test@example.com'));
|
|
|
|
|
|
|
|
should.ok(options.getSubject.calledOnce);
|
|
|
|
should.ok(options.getSubject.firstCall.calledWithExactly('blazeit'));
|
|
|
|
|
2019-09-03 06:07:03 +03:00
|
|
|
should.ok(options.transporter.sendMail.calledOnce);
|
|
|
|
should.equal(options.transporter.sendMail.firstCall.args[0].to, args.email);
|
2019-10-10 16:19:56 +03:00
|
|
|
should.equal(options.transporter.sendMail.firstCall.args[0].subject, options.getSubject.firstCall.returnValue);
|
2019-09-03 06:07:03 +03:00
|
|
|
should.equal(options.transporter.sendMail.firstCall.args[0].text, options.getText.firstCall.returnValue);
|
|
|
|
should.equal(options.transporter.sendMail.firstCall.args[0].html, options.getHTML.firstCall.returnValue);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-09-17 17:42:01 +03:00
|
|
|
describe('#getDataFromToken', function () {
|
2019-09-03 06:07:03 +03:00
|
|
|
it('Returns the user data which from the token that was encoded by #sendMagicLink', async function () {
|
|
|
|
const options = {
|
2020-09-18 15:23:17 +03:00
|
|
|
tokenProvider: new MagicLink.JWTTokenProvider(secret),
|
2019-09-03 06:07:03 +03:00
|
|
|
getSigninURL: sandbox.stub().returns('FAKEURL'),
|
|
|
|
getText: sandbox.stub().returns('SOMETEXT'),
|
|
|
|
getHTML: sandbox.stub().returns('SOMEHTML'),
|
|
|
|
transporter: {
|
|
|
|
sendMail: sandbox.stub().resolves()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const service = new MagicLink(options);
|
|
|
|
|
|
|
|
const args = {
|
|
|
|
email: 'test@example.com',
|
2020-09-17 17:42:01 +03:00
|
|
|
tokenData: {
|
|
|
|
id: '420'
|
|
|
|
}
|
2019-09-03 06:07:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const {token} = await service.sendMagicLink(args);
|
2020-09-17 17:42:01 +03:00
|
|
|
const data = await service.getDataFromToken(token);
|
2019-09-03 06:07:03 +03:00
|
|
|
|
2020-09-17 17:42:01 +03:00
|
|
|
should.deepEqual(data.id, args.tokenData.id);
|
2019-09-03 06:07:03 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|