2020-05-08 13:03:44 +01:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
|
2021-10-06 11:12:21 +01:00
|
|
|
const urlUtils = require('../../../../../core/shared/url-utils');
|
|
|
|
const membersService = require('../../../../../core/server/services/members');
|
|
|
|
const membersMiddleware = require('../../../../../core/server/services/members/middleware');
|
|
|
|
const settingsCache = require('../../../../../core/shared/settings-cache');
|
2022-03-09 17:49:25 +05:30
|
|
|
const models = require('../../../../../core/server/models');
|
2020-05-08 13:03:44 +01:00
|
|
|
|
|
|
|
describe('Members Service Middleware', function () {
|
|
|
|
describe('createSessionFromMagicLink', function () {
|
2022-02-20 16:02:42 +02:00
|
|
|
let oldSSR;
|
2022-03-09 17:49:25 +05:30
|
|
|
let oldProductModel;
|
2020-05-08 13:03:44 +01:00
|
|
|
let req;
|
|
|
|
let res;
|
|
|
|
let next;
|
|
|
|
|
2022-03-09 17:49:25 +05:30
|
|
|
before(function () {
|
|
|
|
models.init();
|
|
|
|
});
|
|
|
|
|
2020-05-08 13:03:44 +01:00
|
|
|
beforeEach(function () {
|
|
|
|
req = {};
|
|
|
|
res = {};
|
|
|
|
next = sinon.stub();
|
|
|
|
|
|
|
|
res.redirect = sinon.stub().returns('');
|
|
|
|
|
|
|
|
// Stub the members Service, handle this in separate tests
|
2022-02-20 16:02:42 +02:00
|
|
|
oldSSR = membersService.ssr;
|
|
|
|
membersService.ssr = {
|
|
|
|
exchangeTokenForSession: sinon.stub()
|
|
|
|
};
|
2020-05-08 13:03:44 +01:00
|
|
|
|
2022-03-09 17:49:25 +05:30
|
|
|
// Stub the members Service, handle this in separate tests
|
|
|
|
oldProductModel = models.Product;
|
|
|
|
models.Product = {
|
|
|
|
findOne: sinon.stub()
|
|
|
|
};
|
|
|
|
|
2020-05-08 13:03:44 +01:00
|
|
|
sinon.stub(urlUtils, 'getSubdir').returns('/blah');
|
2020-11-23 09:36:45 +00:00
|
|
|
sinon.stub(urlUtils, 'getSiteUrl').returns('https://site.com/blah');
|
2020-05-08 13:03:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2022-02-20 16:02:42 +02:00
|
|
|
membersService.ssr = oldSSR;
|
2022-03-09 17:49:25 +05:30
|
|
|
models.Product = oldProductModel;
|
2020-05-08 13:03:44 +01:00
|
|
|
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
|
2022-03-09 17:49:25 +05:30
|
|
|
membersService.ssr.exchangeTokenForSession.resolves({
|
|
|
|
subscriptions: [{
|
|
|
|
status: 'active',
|
|
|
|
tier: {
|
|
|
|
welcome_page_url: ''
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
});
|
2020-05-08 13:03:44 +01: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('/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 09:36:45 +00:00
|
|
|
|
2022-03-09 17:49:25 +05:30
|
|
|
it('redirects free member to custom redirect on signup', async function () {
|
2020-11-23 09:36:45 +00:00
|
|
|
req.url = '/members?token=test&action=signup';
|
|
|
|
req.query = {token: 'test', action: 'signup'};
|
|
|
|
|
|
|
|
// Fake token handling failure
|
|
|
|
membersService.ssr.exchangeTokenForSession.resolves();
|
|
|
|
|
2022-03-09 17:49:25 +05:30
|
|
|
// Fake welcome page for free tier
|
|
|
|
models.Product.findOne.resolves({
|
|
|
|
get: () => {
|
|
|
|
return 'https://custom.com/redirect/';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-23 09:36:45 +00: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/redirect/');
|
|
|
|
});
|
|
|
|
|
2022-03-09 17:49:25 +05:30
|
|
|
it('redirects paid member to custom redirect on signup', async function () {
|
2020-11-23 09:36:45 +00:00
|
|
|
req.url = '/members?token=test&action=signup';
|
|
|
|
req.query = {token: 'test', action: 'signup'};
|
|
|
|
|
|
|
|
// Fake token handling failure
|
|
|
|
membersService.ssr.exchangeTokenForSession.resolves({
|
2021-01-28 22:55:38 +05:30
|
|
|
subscriptions: [{
|
2022-03-09 17:49:25 +05:30
|
|
|
status: 'active',
|
|
|
|
tier: {
|
|
|
|
welcome_page_url: 'https://custom.com/paid'
|
|
|
|
}
|
2021-01-28 22:55:38 +05:30
|
|
|
}]
|
2020-11-23 09:36:45 +00: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 13:03:44 +01:00
|
|
|
});
|
|
|
|
});
|