| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361 |
- const SandboxedModule = require('sandboxed-module')
- const sinon = require('sinon')
- const { assert, expect } = require('chai')
- const MockRequest = require('../helpers/MockRequest')
- const MockResponse = require('../helpers/MockResponse')
- const modulePath =
- '../../../../app/src/Features/Subscription/SubscriptionController'
- const SubscriptionErrors = require('../../../../app/src/Features/Subscription/Errors')
- const mockSubscriptions = {
- 'subscription-123-active': {
- uuid: 'subscription-123-active',
- plan: {
- name: 'Gold',
- plan_code: 'gold',
- },
- current_period_ends_at: new Date(),
- state: 'active',
- unit_amount_in_cents: 999,
- account: {
- account_code: 'user-123',
- },
- },
- }
- describe('SubscriptionController', function () {
- beforeEach(function () {
- this.user = {
- email: 'tom@yahoo.com',
- _id: 'one',
- signUpDate: new Date('2000-10-01'),
- emails: [{ email: 'tom@yahoo.com', confirmedAt: new Date('2000-10-02') }],
- }
- this.activeRecurlySubscription =
- mockSubscriptions['subscription-123-active']
- this.SessionManager = {
- getLoggedInUser: sinon.stub().callsArgWith(1, null, this.user),
- getLoggedInUserId: sinon.stub().returns(this.user._id),
- getSessionUser: sinon.stub().returns(this.user),
- isUserLoggedIn: sinon.stub().returns(true),
- }
- this.SubscriptionHandler = {
- createSubscription: sinon.stub().callsArgWith(3),
- updateSubscription: sinon.stub().callsArgWith(3),
- reactivateSubscription: sinon.stub().callsArgWith(1),
- cancelSubscription: sinon.stub().callsArgWith(1),
- syncSubscription: sinon.stub().yields(),
- attemptPaypalInvoiceCollection: sinon.stub().yields(),
- startFreeTrial: sinon.stub(),
- promises: {
- createSubscription: sinon.stub().resolves(),
- updateSubscription: sinon.stub().resolves(),
- reactivateSubscription: sinon.stub().resolves(),
- cancelSubscription: sinon.stub().resolves(),
- syncSubscription: sinon.stub().resolves(),
- attemptPaypalInvoiceCollection: sinon.stub().resolves(),
- startFreeTrial: sinon.stub().resolves(),
- },
- }
- this.LimitationsManager = {
- hasPaidSubscription: sinon.stub(),
- userHasV1OrV2Subscription: sinon.stub(),
- userHasV2Subscription: sinon.stub(),
- promises: {
- hasPaidSubscription: sinon.stub().resolves(),
- userHasV1OrV2Subscription: sinon.stub().resolves(),
- userHasV2Subscription: sinon.stub().resolves(),
- },
- }
- this.SubscriptionViewModelBuilder = {
- buildUsersSubscriptionViewModel: sinon.stub().callsArgWith(1, null, {}),
- buildPlansList: sinon.stub(),
- promises: {
- buildUsersSubscriptionViewModel: sinon.stub().resolves({}),
- getBestSubscription: sinon.stub().resolves({}),
- },
- buildPlansListForSubscriptionDash: sinon
- .stub()
- .returns({ plans: [], planCodesChangingAtTermEnd: [] }),
- }
- this.settings = {
- coupon_codes: {
- upgradeToAnnualPromo: {
- student: 'STUDENTCODEHERE',
- collaborator: 'COLLABORATORCODEHERE',
- },
- },
- groupPlanModalOptions: {
- plan_codes: [],
- currencies: [
- {
- display: 'GBP (£)',
- code: 'GBP',
- },
- ],
- sizes: ['42'],
- usages: [{ code: 'foo', display: 'Foo' }],
- },
- apis: {
- recurly: {
- subdomain: 'sl',
- },
- },
- siteUrl: 'http://de.overleaf.dev:3000',
- }
- this.AuthorizationManager = {
- promises: {
- isUserSiteAdmin: sinon.stub().resolves(false),
- },
- }
- this.GeoIpLookup = {
- isValidCurrencyParam: sinon.stub().returns(true),
- getCurrencyCode: sinon.stub().yields('USD', 'US'),
- promises: {
- getCurrencyCode: sinon.stub().resolves({
- countryCode: 'US',
- currencyCode: 'USD',
- }),
- },
- }
- this.UserGetter = {
- getUser: sinon.stub().callsArgWith(2, null, this.user),
- promises: {
- getUser: sinon.stub().resolves(this.user),
- },
- }
- this.SplitTestV2Hander = {
- promises: {
- getAssignment: sinon.stub().resolves({ variant: 'default' }),
- isSplitTestActive: sinon.stub().resolves(true),
- },
- }
- this.SubscriptionHelper = {
- generateInitialLocalizedGroupPrice: sinon.stub(),
- }
- this.Features = {
- hasFeature: sinon.stub().returns(false),
- }
- this.SubscriptionController = SandboxedModule.require(modulePath, {
- requires: {
- '../Authorization/AuthorizationManager': this.AuthorizationManager,
- '../SplitTests/SplitTestHandler': this.SplitTestV2Hander,
- '../Authentication/SessionManager': this.SessionManager,
- './SubscriptionHandler': this.SubscriptionHandler,
- './SubscriptionHelper': this.SubscriptionHelper,
- './SubscriptionViewModelBuilder': this.SubscriptionViewModelBuilder,
- './LimitationsManager': this.LimitationsManager,
- '../../infrastructure/GeoIpLookup': this.GeoIpLookup,
- '@overleaf/settings': this.settings,
- '../User/UserGetter': this.UserGetter,
- './RecurlyWrapper': (this.RecurlyWrapper = {
- updateAccountEmailAddress: sinon.stub().yields(),
- }),
- './RecurlyEventHandler': {
- sendRecurlyAnalyticsEvent: sinon.stub().resolves(),
- },
- './FeaturesUpdater': (this.FeaturesUpdater = {}),
- './GroupPlansData': (this.GroupPlansData = {}),
- './V1SubscriptionManager': (this.V1SubscriptionManager = {}),
- '../Errors/HttpErrorHandler': (this.HttpErrorHandler = {
- unprocessableEntity: sinon.stub().callsFake((req, res, message) => {
- res.status(422)
- res.json({ message })
- }),
- }),
- './Errors': SubscriptionErrors,
- '../Analytics/AnalyticsManager': (this.AnalyticsManager = {
- recordEventForUser: sinon.stub(),
- recordEventForSession: sinon.stub(),
- setUserPropertyForUser: sinon.stub(),
- }),
- '../../infrastructure/Modules': {
- promises: { hooks: { fire: sinon.stub().resolves() } },
- },
- '../../infrastructure/Features': this.Features,
- '../../util/currency': (this.currency = {
- formatCurrencyLocalized: sinon.stub(),
- }),
- },
- })
- this.res = new MockResponse()
- this.req = new MockRequest()
- this.req.body = {}
- this.req.query = { planCode: '123123' }
- this.stubbedCurrencyCode = 'GBP'
- })
- describe('plansPage', function () {
- beforeEach(function () {
- this.req.ip = '1234.3123.3131.333 313.133.445.666 653.5345.5345.534'
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- currencyCode: this.stubbedCurrencyCode,
- })
- })
- describe('ip override', function () {
- beforeEach(function () {
- this.req.ip = '1.2.3.4'
- this.req.query = { ip: '5.6.7.8' }
- this.GeoIpLookup.promises.getCurrencyCode.withArgs('1.2.3.4').resolves({
- currencyCode: 'GBP',
- })
- this.GeoIpLookup.promises.getCurrencyCode.withArgs('5.6.7.8').resolves({
- currencyCode: 'USD',
- })
- })
- it('should ignore override for non admin', function (done) {
- this.res.render = (page, opts) => {
- opts.recommendedCurrency.should.equal('GBP')
- done()
- }
- this.AuthorizationManager.promises.isUserSiteAdmin.resolves(false)
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- it('should accept override for admin', function (done) {
- this.res.render = (page, opts) => {
- opts.recommendedCurrency.should.equal('USD')
- done()
- }
- this.AuthorizationManager.promises.isUserSiteAdmin.resolves(true)
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- })
- describe('groupPlanModal data', function () {
- it('should pass local currency if valid', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans')
- opts.groupPlanModalDefaults.currency.should.equal('GBP')
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- currencyCode: 'GBP',
- })
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- it('should fallback to USD when valid', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans')
- opts.groupPlanModalDefaults.currency.should.equal('USD')
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- currencyCode: 'FOO',
- })
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- it('should pass valid options for group plan modal and discard invalid', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans')
- opts.groupPlanModalDefaults.size.should.equal('42')
- opts.groupPlanModalDefaults.plan_code.should.equal('collaborator')
- opts.groupPlanModalDefaults.currency.should.equal('GBP')
- opts.groupPlanModalDefaults.usage.should.equal('foo')
- done()
- }
- this.GeoIpLookup.isValidCurrencyParam.returns(false)
- this.req.query = {
- number: '42',
- currency: 'ABC',
- plan: 'does-not-exist',
- usage: 'foo',
- }
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- })
- describe('showInrGeoBanner data', function () {
- it('should return true for Indian Users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans')
- opts.showInrGeoBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'IN',
- })
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- it('should return false for US Users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans')
- opts.showInrGeoBanner.should.equal(false)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'US',
- })
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- })
- describe('showBrlGeoBanner data', function () {
- it('should return true for Brazilian users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans')
- opts.showBrlGeoBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'BR',
- })
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- it('should return false for US users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans')
- opts.showBrlGeoBanner.should.equal(false)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'US',
- })
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- })
- describe('showLATAMBanner', function () {
- describe('latam variant', function () {
- beforeEach(function () {
- this.SplitTestV2Hander.promises.getAssignment
- .withArgs(this.req, this.res, 'geo-pricing-latam-v2')
- .resolves({
- variant: 'latam',
- })
- })
- it('should return true for Mexican users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans')
- opts.showLATAMBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'MX',
- })
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- it('should return true for Colombian users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans')
- opts.showLATAMBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'CO',
- })
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- it('should return true for Chilean users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans')
- opts.showLATAMBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'CL',
- })
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- it('should return true for Peruvian users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans')
- opts.showLATAMBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'PE',
- })
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- it('should return true for US users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans')
- opts.showLATAMBanner.should.equal(false)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'US',
- })
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- })
- describe('default variant', function () {
- beforeEach(function () {
- this.SplitTestV2Hander.promises.getAssignment
- .withArgs(this.req, this.res, 'geo-pricing-latam-v2')
- .resolves({
- variant: 'default',
- })
- })
- it('should return false', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans')
- opts.showLATAMBanner.should.equal(false)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'MX',
- })
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- })
- })
- describe('localCcyAssignment', function () {
- it('uses formatCurrencyLocalized when variant is enabled', function (done) {
- this.SplitTestV2Hander.promises.getAssignment
- .withArgs(this.req, this.res, 'local-ccy-format-v2')
- .resolves({
- variant: 'enabled',
- })
- this.res.render = (page, opts) => {
- expect(opts.formatCurrency).to.equal(
- this.currency.formatCurrencyLocalized
- )
- done()
- }
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- it('uses formatCurrencyDefault when variant is default', function (done) {
- this.res.render = (page, opts) => {
- expect(opts.formatCurrency).to.equal(
- this.SubscriptionHelper.formatCurrencyDefault
- )
- done()
- }
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- })
- describe('website-redesign-plans test', function () {
- beforeEach(function () {
- this.req.query = {}
- })
- describe('"default" variant', function () {
- // note: if test is not active, default variant is assigned
- beforeEach(function () {
- this.SplitTestV2Hander.promises.getAssignment
- .withArgs(this.req, this.res, 'website-redesign-plans')
- .resolves({
- variant: 'default',
- })
- })
- it('renders "default" variant', function (done) {
- this.res.render = page => {
- page.should.equal('subscriptions/plans')
- expect(this.res.redirect).to.not.have.been.called
- done()
- }
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- })
- describe('"new-design" variant', function () {
- beforeEach(function () {
- this.SplitTestV2Hander.promises.getAssignment
- .withArgs(this.req, this.res, 'website-redesign-plans')
- .resolves({
- variant: 'new-design',
- })
- })
- it('redirects to "new-design" variant', function (done) {
- this.res.callback = () => {
- expect(this.res.redirect).to.have.been.calledWith(
- 302,
- '/user/subscription/plans-2'
- )
- done()
- }
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- it('passes query params when redirecting to new design variant', function (done) {
- this.req.query = { currency: 'USD' }
- this.res.callback = () => {
- expect(this.res.redirect).to.have.been.calledWith(
- 302,
- '/user/subscription/plans-2?currency=USD'
- )
- done()
- }
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- })
- describe('"light-design" variant', function () {
- beforeEach(function () {
- this.SplitTestV2Hander.promises.getAssignment
- .withArgs(this.req, this.res, 'website-redesign-plans')
- .resolves({
- variant: 'light-design',
- })
- })
- it('renders "light-design" variant', function (done) {
- this.res.callback = () => {
- expect(this.res.redirect).to.have.been.calledWith(
- 302,
- '/user/subscription/plans-3'
- )
- done()
- }
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- it('passes query params when redirecting to new design variant', function (done) {
- this.req.query = { currency: 'USD' }
- this.res.callback = () => {
- expect(this.res.redirect).to.have.been.calledWith(
- 302,
- '/user/subscription/plans-3?currency=USD'
- )
- done()
- }
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- })
- })
- it('should return correct countryCode', function (done) {
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'MX',
- })
- this.res.render = (page, opts) => {
- expect(opts.countryCode).to.equal('MX')
- done()
- }
- this.SubscriptionController.plansPage(this.req, this.res)
- })
- })
- describe('plansPage light touch redesign', function () {
- beforeEach(function () {
- this.req.ip = '1234.3123.3131.333 313.133.445.666 653.5345.5345.534'
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- currencyCode: this.stubbedCurrencyCode,
- })
- })
- describe('ip override', function () {
- beforeEach(function () {
- this.req.ip = '1.2.3.4'
- this.req.query = { ip: '5.6.7.8' }
- this.GeoIpLookup.promises.getCurrencyCode.withArgs('1.2.3.4').resolves({
- currencyCode: 'GBP',
- })
- this.GeoIpLookup.promises.getCurrencyCode.withArgs('5.6.7.8').resolves({
- currencyCode: 'USD',
- })
- })
- it('should ignore override for non admin', function (done) {
- this.res.render = (page, opts) => {
- opts.recommendedCurrency.should.equal('GBP')
- done()
- }
- this.AuthorizationManager.promises.isUserSiteAdmin.resolves(false)
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- it('should accept override for admin', function (done) {
- this.res.render = (page, opts) => {
- opts.recommendedCurrency.should.equal('USD')
- done()
- }
- this.AuthorizationManager.promises.isUserSiteAdmin.resolves(true)
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- })
- describe('groupPlanModal data', function () {
- it('should pass local currency if valid', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans-light-design')
- opts.groupPlanModalDefaults.currency.should.equal('GBP')
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- currencyCode: 'GBP',
- })
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- it('should fallback to USD when valid', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans-light-design')
- opts.groupPlanModalDefaults.currency.should.equal('USD')
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- currencyCode: 'FOO',
- })
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- it('should pass valid options for group plan modal and discard invalid', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans-light-design')
- opts.groupPlanModalDefaults.size.should.equal('42')
- opts.groupPlanModalDefaults.plan_code.should.equal('collaborator')
- opts.groupPlanModalDefaults.currency.should.equal('GBP')
- opts.groupPlanModalDefaults.usage.should.equal('foo')
- done()
- }
- this.GeoIpLookup.isValidCurrencyParam.returns(false)
- this.req.query = {
- number: '42',
- currency: 'ABC',
- plan: 'does-not-exist',
- usage: 'foo',
- }
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- })
- describe('showInrGeoBanner data', function () {
- it('should return true for Indian Users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans-light-design')
- opts.showInrGeoBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'IN',
- })
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- it('should return false for US Users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans-light-design')
- opts.showInrGeoBanner.should.equal(false)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'US',
- })
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- })
- describe('showBrlGeoBanner data', function () {
- it('should return true for Brazilian users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans-light-design')
- opts.showBrlGeoBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'BR',
- })
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- it('should return false for US users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans-light-design')
- opts.showBrlGeoBanner.should.equal(false)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'US',
- })
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- })
- describe('showLATAMBanner', function () {
- describe('latam variant', function () {
- beforeEach(function () {
- this.SplitTestV2Hander.promises.getAssignment
- .withArgs(this.req, this.res, 'geo-pricing-latam-v2')
- .resolves({
- variant: 'latam',
- })
- })
- it('should return true for Mexican users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans-light-design')
- opts.showLATAMBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'MX',
- })
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- it('should return true for Colombian users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans-light-design')
- opts.showLATAMBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'CO',
- })
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- it('should return true for Chilean users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans-light-design')
- opts.showLATAMBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'CL',
- })
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- it('should return true for Peruvian users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans-light-design')
- opts.showLATAMBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'PE',
- })
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- it('should return true for US users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans-light-design')
- opts.showLATAMBanner.should.equal(false)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'US',
- })
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- })
- describe('default variant', function () {
- beforeEach(function () {
- this.SplitTestV2Hander.promises.getAssignment
- .withArgs(this.req, this.res, 'geo-pricing-latam-v2')
- .resolves({
- variant: 'default',
- })
- })
- it('should return false', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/plans-light-design')
- opts.showLATAMBanner.should.equal(false)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'MX',
- })
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- })
- })
- describe('localCcyAssignment', function () {
- it('uses formatCurrencyLocalized when variant is enabled', function (done) {
- this.SplitTestV2Hander.promises.getAssignment
- .withArgs(this.req, this.res, 'local-ccy-format-v2')
- .resolves({
- variant: 'enabled',
- })
- this.res.render = (page, opts) => {
- expect(opts.formatCurrency).to.equal(
- this.currency.formatCurrencyLocalized
- )
- done()
- }
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- it('uses formatCurrencyDefault when variant is default', function (done) {
- this.res.render = (page, opts) => {
- expect(opts.formatCurrency).to.equal(
- this.SubscriptionHelper.formatCurrencyDefault
- )
- done()
- }
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- })
- it('should return correct countryCode', function (done) {
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'MX',
- })
- this.res.render = (page, opts) => {
- expect(opts.countryCode).to.equal('MX')
- done()
- }
- this.SubscriptionController.plansPageLightDesign(this.req, this.res)
- })
- })
- describe('interstitialPaymentPage', function () {
- beforeEach(function () {
- this.req.ip = '1234.3123.3131.333 313.133.445.666 653.5345.5345.534'
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- currencyCode: this.stubbedCurrencyCode,
- })
- })
- describe('with a user without subscription', function () {
- it('should render the interstitial payment page', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/interstitial-payment')
- done()
- }
- this.SubscriptionController.interstitialPaymentPage(this.req, this.res)
- })
- })
- describe('with a user with subscription', function () {
- it('should redirect to the subscription dashboard', function (done) {
- this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(
- true
- )
- this.res.redirect = url => {
- url.should.equal('/user/subscription?hasSubscription=true')
- done()
- }
- this.SubscriptionController.interstitialPaymentPage(this.req, this.res)
- })
- })
- describe('showInrGeoBanner data', function () {
- it('should return true for Indian users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/interstitial-payment')
- opts.showInrGeoBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'IN',
- })
- this.SubscriptionController.interstitialPaymentPage(this.req, this.res)
- })
- it('should be false for US users', function (done) {
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'US',
- })
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/interstitial-payment')
- opts.showInrGeoBanner.should.equal(false)
- done()
- }
- this.SubscriptionController.interstitialPaymentPage(this.req, this.res)
- })
- })
- describe('showBrlGeoBanner data', function () {
- it('should return true for Brazilian users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/interstitial-payment')
- opts.showBrlGeoBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'BR',
- })
- this.SubscriptionController.interstitialPaymentPage(this.req, this.res)
- })
- it('should return false for US users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/interstitial-payment')
- opts.showBrlGeoBanner.should.equal(false)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'US',
- })
- this.SubscriptionController.interstitialPaymentPage(this.req, this.res)
- })
- })
- describe('showLATAMBanner', function () {
- describe('latam variant', function () {
- beforeEach(function () {
- this.SplitTestV2Hander.promises.getAssignment
- .withArgs(this.req, this.res, 'geo-pricing-latam-v2')
- .resolves({
- variant: 'latam',
- })
- })
- it('should return true for Mexican users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/interstitial-payment')
- opts.showLATAMBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'MX',
- })
- this.SubscriptionController.interstitialPaymentPage(
- this.req,
- this.res
- )
- })
- it('should return true for Colombian users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/interstitial-payment')
- opts.showLATAMBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'CO',
- })
- this.SubscriptionController.interstitialPaymentPage(
- this.req,
- this.res
- )
- })
- it('should return true for Chilean users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/interstitial-payment')
- opts.showLATAMBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'CL',
- })
- this.SubscriptionController.interstitialPaymentPage(
- this.req,
- this.res
- )
- })
- it('should return true for Peruvian users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/interstitial-payment')
- opts.showLATAMBanner.should.equal(true)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'PE',
- })
- this.SubscriptionController.interstitialPaymentPage(
- this.req,
- this.res
- )
- })
- it('should return true for US users', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/interstitial-payment')
- opts.showLATAMBanner.should.equal(false)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'US',
- })
- this.SubscriptionController.interstitialPaymentPage(
- this.req,
- this.res
- )
- })
- })
- describe('default variant', function () {
- beforeEach(function () {
- this.SplitTestV2Hander.promises.getAssignment
- .withArgs(this.req, this.res, 'website-redesign-plan')
- .resolves({
- variant: 'default',
- })
- })
- it('should return false', function (done) {
- this.res.render = (page, opts) => {
- page.should.equal('subscriptions/interstitial-payment')
- opts.showLATAMBanner.should.equal(false)
- done()
- }
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'MX',
- })
- this.SubscriptionController.interstitialPaymentPage(
- this.req,
- this.res
- )
- })
- })
- })
- it('should return correct countryCode', function (done) {
- this.GeoIpLookup.promises.getCurrencyCode.resolves({
- countryCode: 'MX',
- })
- this.res.render = (page, opts) => {
- expect(opts.countryCode).to.equal('MX')
- done()
- }
- this.SubscriptionController.interstitialPaymentPage(this.req, this.res)
- })
- })
- describe('successfulSubscription', function () {
- it('without a personal subscription', function (done) {
- this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
- {}
- )
- this.res.redirect = url => {
- url.should.equal('/user/subscription/plans')
- done()
- }
- this.SubscriptionController.successfulSubscription(this.req, this.res)
- })
- it('with a personal subscription', function (done) {
- this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
- {
- personalSubscription: 'foo',
- }
- )
- this.res.render = (url, variables) => {
- url.should.equal('subscriptions/successful-subscription-react')
- assert.deepEqual(variables, {
- title: 'thank_you',
- personalSubscription: 'foo',
- postCheckoutRedirect: undefined,
- user: this.user,
- })
- done()
- }
- this.SubscriptionController.successfulSubscription(this.req, this.res)
- })
- it('with an error', function (done) {
- this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
- undefined
- )
- this.SubscriptionController.successfulSubscription(
- this.req,
- this.res,
- error => {
- assert.isNotNull(error)
- done()
- }
- )
- })
- })
- describe('userSubscriptionPage', function () {
- beforeEach(function (done) {
- this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
- {
- personalSubscription: (this.personalSubscription = {
- 'personal-subscription': 'mock',
- }),
- memberGroupSubscriptions: (this.memberGroupSubscriptions = {
- 'group-subscriptions': 'mock',
- }),
- }
- )
- this.SubscriptionViewModelBuilder.buildPlansList.returns(
- (this.plans = { plans: 'mock' })
- )
- this.SubscriptionViewModelBuilder.buildPlansListForSubscriptionDash.returns(
- {
- plans: this.plans,
- planCodesChangingAtTermEnd: [],
- }
- )
- this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(false)
- this.res.render = (view, data) => {
- this.data = data
- expect(view).to.equal('subscriptions/dashboard-react')
- done()
- }
- this.SubscriptionController.userSubscriptionPage(this.req, this.res, done)
- })
- it('should load the personal, groups and v1 subscriptions', function () {
- expect(this.data.personalSubscription).to.deep.equal(
- this.personalSubscription
- )
- expect(this.data.memberGroupSubscriptions).to.deep.equal(
- this.memberGroupSubscriptions
- )
- })
- it('should load the user', function () {
- expect(this.data.user).to.deep.equal(this.user)
- })
- it('should load the plans', function () {
- expect(this.data.plans).to.deep.equal(this.plans)
- })
- it('should load an empty list of groups with settings available', function () {
- expect(this.data.groupSettingsEnabledFor).to.deep.equal([])
- })
- })
- describe('updateSubscription via post', function () {
- beforeEach(function (done) {
- this.res = {
- redirect() {
- done()
- },
- }
- sinon.spy(this.res, 'redirect')
- this.plan_code = '1234'
- this.req.body.plan_code = this.plan_code
- this.SubscriptionController.updateSubscription(this.req, this.res)
- })
- it('should send the user and subscriptionId to the handler', function (done) {
- this.SubscriptionHandler.updateSubscription
- .calledWith(this.user, this.plan_code)
- .should.equal(true)
- done()
- })
- it('should redurect to the subscription page', function (done) {
- this.res.redirect.calledWith('/user/subscription').should.equal(true)
- done()
- })
- })
- describe('updateAccountEmailAddress via put', function () {
- it('should send the user and subscriptionId to RecurlyWrapper', function () {
- this.res.sendStatus = sinon.spy()
- this.SubscriptionController.updateAccountEmailAddress(this.req, this.res)
- this.RecurlyWrapper.updateAccountEmailAddress
- .calledWith(this.user._id, this.user.email)
- .should.equal(true)
- })
- it('should respond with 200', function () {
- this.res.sendStatus = sinon.spy()
- this.SubscriptionController.updateAccountEmailAddress(this.req, this.res)
- this.res.sendStatus.calledWith(200).should.equal(true)
- })
- it('should send the error to the next handler when updating recurly account email fails', function (done) {
- this.RecurlyWrapper.updateAccountEmailAddress.yields(new Error())
- this.next = sinon.spy(error => {
- expect(error).instanceOf(Error)
- done()
- })
- this.SubscriptionController.updateAccountEmailAddress(
- this.req,
- this.res,
- this.next
- )
- })
- })
- describe('reactivateSubscription', function () {
- describe('when the user has permission', function () {
- beforeEach(function (done) {
- this.res = {
- redirect() {
- done()
- },
- }
- this.req.assertPermission = sinon.stub()
- this.next = sinon.stub().callsFake(error => {
- done(error)
- })
- sinon.spy(this.res, 'redirect')
- this.SubscriptionController.reactivateSubscription(
- this.req,
- this.res,
- this.next
- )
- })
- it('should assert the user has permission to reactivate their subscription', function (done) {
- this.req.assertPermission
- .calledWith('reactivate-subscription')
- .should.equal(true)
- done()
- })
- it('should tell the handler to reactivate this user', function (done) {
- this.SubscriptionHandler.reactivateSubscription
- .calledWith(this.user)
- .should.equal(true)
- done()
- })
- it('should redurect to the subscription page', function (done) {
- this.res.redirect.calledWith('/user/subscription').should.equal(true)
- done()
- })
- })
- describe('when the user does not have permission', function () {
- beforeEach(function (done) {
- this.res = {
- redirect() {
- done()
- },
- }
- this.req.assertPermission = sinon.stub().throws()
- this.next = sinon.stub().callsFake(() => {
- done()
- })
- sinon.spy(this.res, 'redirect')
- this.SubscriptionController.reactivateSubscription(
- this.req,
- this.res,
- this.next
- )
- })
- it('should not reactivate the user', function (done) {
- this.req.assertPermission = sinon.stub().throws()
- this.SubscriptionHandler.reactivateSubscription.called.should.equal(
- false
- )
- done()
- })
- it('should call next with an error', function (done) {
- this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
- done()
- })
- })
- })
- describe('cancelSubscription', function () {
- beforeEach(function (done) {
- this.res = {
- redirect() {
- done()
- },
- }
- sinon.spy(this.res, 'redirect')
- this.SubscriptionController.cancelSubscription(this.req, this.res)
- })
- it('should tell the handler to cancel this user', function (done) {
- this.SubscriptionHandler.cancelSubscription
- .calledWith(this.user)
- .should.equal(true)
- done()
- })
- it('should redurect to the subscription page', function (done) {
- this.res.redirect
- .calledWith('/user/subscription/canceled')
- .should.equal(true)
- done()
- })
- })
- describe('recurly callback', function () {
- describe('with a sync subscription request', function () {
- beforeEach(function (done) {
- this.req = {
- body: {
- expired_subscription_notification: {
- account: {
- account_code: this.user._id,
- },
- subscription: {
- uuid: this.activeRecurlySubscription.uuid,
- plan: {
- plan_code: 'collaborator',
- state: 'active',
- },
- },
- },
- },
- }
- this.res = {
- sendStatus() {
- done()
- },
- }
- sinon.spy(this.res, 'sendStatus')
- this.SubscriptionController.recurlyCallback(this.req, this.res)
- })
- it('should tell the SubscriptionHandler to process the recurly callback', function (done) {
- this.SubscriptionHandler.syncSubscription.called.should.equal(true)
- done()
- })
- it('should send a 200', function (done) {
- this.res.sendStatus.calledWith(200)
- done()
- })
- })
- describe('with a billing info updated request', function () {
- beforeEach(function (done) {
- this.req = {
- body: {
- billing_info_updated_notification: {
- account: {
- account_code: 'mock-account-code',
- },
- },
- },
- }
- this.res = {
- sendStatus() {
- done()
- },
- }
- sinon.spy(this.res, 'sendStatus')
- this.SubscriptionController.recurlyCallback(this.req, this.res)
- })
- it('should call attemptPaypalInvoiceCollection', function (done) {
- this.SubscriptionHandler.attemptPaypalInvoiceCollection
- .calledWith('mock-account-code')
- .should.equal(true)
- done()
- })
- it('should send a 200', function (done) {
- this.res.sendStatus.calledWith(200)
- done()
- })
- })
- describe('with a non-actionable request', function () {
- beforeEach(function (done) {
- this.user.id = this.activeRecurlySubscription.account.account_code
- this.req = {
- body: {
- renewed_subscription_notification: {
- account: {
- account_code: this.user._id,
- },
- subscription: {
- uuid: this.activeRecurlySubscription.uuid,
- plan: {
- plan_code: 'collaborator',
- state: 'active',
- },
- },
- },
- },
- }
- this.res = {
- sendStatus() {
- done()
- },
- }
- sinon.spy(this.res, 'sendStatus')
- this.SubscriptionController.recurlyCallback(this.req, this.res)
- })
- it('should not call the subscriptionshandler', function () {
- this.SubscriptionHandler.syncSubscription.called.should.equal(false)
- this.SubscriptionHandler.attemptPaypalInvoiceCollection.called.should.equal(
- false
- )
- })
- it('should respond with a 200 status', function () {
- this.res.sendStatus.calledWith(200)
- })
- })
- })
- })
|