2020-05-08 15:03:44 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('../../../../core/shared/url-utils');
|
2020-05-08 15:03:44 +03:00
|
|
|
const membersService = require('../../../../core/server/services/members');
|
|
|
|
const membersMiddleware = require('../../../../core/server/services/members/middleware');
|
2021-06-30 16:56:57 +03:00
|
|
|
const settingsCache = require('../../../../core/shared/settings-cache');
|
2020-05-08 15:03:44 +03:00
|
|
|
|
|
|
|
describe('Members Service Middleware', function () {
|
|
|
|
describe('createSessionFromMagicLink', function () {
|
|
|
|
let req;
|
|
|
|
let res;
|
|
|
|
let next;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
req = {};
|
|
|
|
res = {};
|
|
|
|
next = sinon.stub();
|
|
|
|
|
|
|
|
res.redirect = sinon.stub().returns('');
|
|
|
|
|
|
|
|
// Stub the members Service, handle this in separate tests
|
|
|
|
membersService.ssr.exchangeTokenForSession = sinon.stub();
|
|
|
|
|
|
|
|
sinon.stub(urlUtils, 'getSubdir').returns('/blah');
|
2020-11-23 12:36:45 +03:00
|
|
|
sinon.stub(urlUtils, 'getSiteUrl').returns('https://site.com/blah');
|
2020-05-08 15:03:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls next if url does not include a token', async function () {
|
|
|
|
req.url = '/members';
|
|
|
|
req.query = {};
|
|
|
|
|
|
|
|
// Call the middleware
|
|
|
|
await membersMiddleware.createSessionFromMagicLink(req, res, next);
|
|
|
|
|
|
|
|
// Check behaviour
|
|
|
|
next.calledOnce.should.be.true();
|
|
|
|
next.firstCall.args.should.be.an.Array().with.lengthOf(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('redirects correctly on success', async function () {
|
|
|
|
req.url = '/members?token=test&action=signup';
|
|
|
|
req.query = {token: 'test', action: 'signup'};
|
|
|
|
|
|
|
|
// Fake token handling success
|
|
|
|
membersService.ssr.exchangeTokenForSession.resolves();
|
|
|
|
|
|
|
|
// Call the middleware
|
|
|
|
await membersMiddleware.createSessionFromMagicLink(req, res, next);
|
|
|
|
|
|
|
|
// Check behaviour
|
|
|
|
next.calledOnce.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
|
|
|
res.redirect.firstCall.args[0].should.eql('/blah/?action=signup&success=true');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('redirects correctly on failure', async function () {
|
|
|
|
req.url = '/members?token=test&action=signup';
|
|
|
|
req.query = {token: 'test', action: 'signup'};
|
|
|
|
|
|
|
|
// Fake token handling failure
|
|
|
|
membersService.ssr.exchangeTokenForSession.rejects();
|
|
|
|
|
|
|
|
// Call the middleware
|
|
|
|
await membersMiddleware.createSessionFromMagicLink(req, res, next);
|
|
|
|
|
|
|
|
// Check behaviour
|
|
|
|
next.calledOnce.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
|
|
|
res.redirect.firstCall.args[0].should.eql('/blah/?action=signup&success=false');
|
|
|
|
});
|
2020-11-23 12:36:45 +03:00
|
|
|
|
|
|
|
it('redirects to custom redirect on signup', async function () {
|
|
|
|
req.url = '/members?token=test&action=signup';
|
|
|
|
req.query = {token: 'test', action: 'signup'};
|
|
|
|
|
|
|
|
sinon.stub(settingsCache, 'get')
|
|
|
|
.withArgs('members_free_signup_redirect')
|
|
|
|
.returns('https://custom.com/redirect');
|
|
|
|
|
|
|
|
// Fake token handling failure
|
|
|
|
membersService.ssr.exchangeTokenForSession.resolves();
|
|
|
|
|
|
|
|
// Call the middleware
|
|
|
|
await membersMiddleware.createSessionFromMagicLink(req, res, next);
|
|
|
|
|
|
|
|
// Check behaviour
|
|
|
|
next.calledOnce.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
|
|
|
res.redirect.firstCall.args[0].should.eql('https://custom.com/redirect/');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('redirects to custom redirect on signup', async function () {
|
|
|
|
req.url = '/members?token=test&action=signup';
|
|
|
|
req.query = {token: 'test', action: 'signup'};
|
|
|
|
|
|
|
|
sinon.stub(settingsCache, 'get')
|
|
|
|
.withArgs('members_paid_signup_redirect')
|
|
|
|
.returns('https://custom.com/paid');
|
|
|
|
|
|
|
|
// Fake token handling failure
|
|
|
|
membersService.ssr.exchangeTokenForSession.resolves({
|
2021-01-28 20:25:38 +03:00
|
|
|
subscriptions: [{
|
|
|
|
status: 'active'
|
|
|
|
}]
|
2020-11-23 12:36:45 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// Call the middleware
|
|
|
|
await membersMiddleware.createSessionFromMagicLink(req, res, next);
|
|
|
|
|
|
|
|
// Check behaviour
|
|
|
|
next.calledOnce.should.be.false();
|
|
|
|
res.redirect.calledOnce.should.be.true();
|
|
|
|
res.redirect.firstCall.args[0].should.eql('https://custom.com/paid/');
|
|
|
|
});
|
2020-05-08 15:03:44 +03:00
|
|
|
});
|
|
|
|
});
|