2022-10-04 19:17:26 +03:00
|
|
|
const assert = require('assert');
|
2019-09-03 06:07:03 +03:00
|
|
|
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 () {
|
2022-10-04 19:17:26 +03:00
|
|
|
assert.equal(typeof MagicLink, 'function');
|
2019-09-03 06:07:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#sendMagicLink', function () {
|
2022-10-04 19:17:26 +03:00
|
|
|
it('Throws when passed comma separated emails', async function () {
|
|
|
|
const options = {
|
|
|
|
tokenProvider: new MagicLink.JWTTokenProvider(secret),
|
|
|
|
getSigninURL: sandbox.stub().returns('FAKEURL'),
|
|
|
|
getText: sandbox.stub().returns('SOMETEXT'),
|
|
|
|
getHTML: sandbox.stub().returns('SOMEHTML'),
|
|
|
|
getSubject: sandbox.stub().returns('SOMESUBJECT'),
|
|
|
|
transporter: {
|
|
|
|
sendMail: sandbox.stub().resolves()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const service = new MagicLink(options);
|
|
|
|
|
|
|
|
const args = {
|
|
|
|
email: 'one@email.com,two@email.com',
|
|
|
|
tokenData: {
|
|
|
|
id: '420'
|
|
|
|
},
|
|
|
|
type: 'blazeit',
|
|
|
|
referrer: 'https://whatever.com'
|
|
|
|
};
|
|
|
|
|
|
|
|
let errored = false;
|
|
|
|
try {
|
|
|
|
await service.sendMagicLink(args);
|
|
|
|
} catch (err) {
|
|
|
|
errored = true;
|
|
|
|
} finally {
|
|
|
|
assert(errored, 'sendMagicLink should error when given comma separated emails');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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'
|
|
|
|
},
|
2022-07-15 13:02:58 +03:00
|
|
|
type: 'blazeit',
|
|
|
|
referrer: 'https://whatever.com'
|
2019-09-03 06:07:03 +03:00
|
|
|
};
|
|
|
|
const {token} = await service.sendMagicLink(args);
|
|
|
|
|
2022-10-04 19:17:26 +03:00
|
|
|
assert(options.getSigninURL.calledOnce);
|
|
|
|
assert(options.getSigninURL.firstCall.calledWithExactly(token, 'blazeit', 'https://whatever.com'));
|
2019-09-03 06:07:03 +03:00
|
|
|
|
2022-10-04 19:17:26 +03:00
|
|
|
assert(options.getText.calledOnce);
|
|
|
|
assert(options.getText.firstCall.calledWithExactly('FAKEURL', 'blazeit', 'test@example.com'));
|
2019-10-11 07:29:11 +03:00
|
|
|
|
2022-10-04 19:17:26 +03:00
|
|
|
assert(options.getHTML.calledOnce);
|
|
|
|
assert(options.getHTML.firstCall.calledWithExactly('FAKEURL', 'blazeit', 'test@example.com'));
|
2019-10-11 07:29:11 +03:00
|
|
|
|
2022-10-04 19:17:26 +03:00
|
|
|
assert(options.getSubject.calledOnce);
|
|
|
|
assert(options.getSubject.firstCall.calledWithExactly('blazeit'));
|
2019-10-11 07:29:11 +03:00
|
|
|
|
2022-10-04 19:17:26 +03:00
|
|
|
assert(options.transporter.sendMail.calledOnce);
|
|
|
|
assert.equal(options.transporter.sendMail.firstCall.args[0].to, args.email);
|
|
|
|
assert.equal(options.transporter.sendMail.firstCall.args[0].subject, options.getSubject.firstCall.returnValue);
|
|
|
|
assert.equal(options.transporter.sendMail.firstCall.args[0].text, options.getText.firstCall.returnValue);
|
|
|
|
assert.equal(options.transporter.sendMail.firstCall.args[0].html, options.getHTML.firstCall.returnValue);
|
2019-09-03 06:07:03 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
2022-10-04 19:17:26 +03:00
|
|
|
assert.deepEqual(data.id, args.tokenData.id);
|
2019-09-03 06:07:03 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|