Add test for SubscriptionViewModelBuilder

This commit is contained in:
Alberto Fernández Capel 2018-01-09 16:48:04 +00:00
parent 6d007bfe74
commit a81b56556f
3 changed files with 75 additions and 11 deletions

View file

@ -11,7 +11,6 @@ _ = require("underscore")
buildBillingDetails = (recurlySubscription) ->
hostedLoginToken = recurlySubscription?.account?.hosted_login_token
recurlySubdomain = Settings?.apis?.recurly?.subdomain
if hostedLoginToken? && recurlySubdomain?
return [
"https://",
@ -21,20 +20,26 @@ buildBillingDetails = (recurlySubscription) ->
].join("")
module.exports =
buildUsersSubscriptionViewModel: (user, callback = (error, subscription, memberSubscriptions) ->) ->
buildUsersSubscriptionViewModel: (user, callback = (error, subscription, memberSubscriptions, billingDetailsLink) ->) ->
SubscriptionLocator.getUsersSubscription user, (err, subscription) ->
return callback(err) if err?
SubscriptionLocator.getMemberSubscriptions user, (err, memberSubscriptions = []) ->
return callback(err) if err?
if subscription?
return callback(error) if error?
plan = PlansLocator.findLocalPlanInSettings(subscription.planCode)
if !plan?
err = new Error("No plan found for planCode '#{subscription.planCode}'")
logger.error {user_id: user._id, err}, "error getting subscription plan for user"
return callback(err)
RecurlyWrapper.getSubscription subscription.recurlySubscription_id, includeAccount: true, (err, recurlySubscription)->
tax = recurlySubscription?.tax_in_cents || 0
callback null, {
admin_id:subscription.admin_id
name: plan.name
@ -47,6 +52,7 @@ module.exports =
groupPlan: subscription.groupPlan
trial_ends_at:recurlySubscription?.trial_ends_at
}, memberSubscriptions, buildBillingDetails(recurlySubscription)
else
callback null, null, memberSubscriptions, null

View file

@ -42,10 +42,6 @@ describe "SubscriptionController", ->
userHasSubscriptionOrIsGroupMember: sinon.stub()
userHasSubscription : sinon.stub()
@RecurlyWrapper =
sign: sinon.stub().callsArgWith(1, null, "somthing")
getSubscription: sinon.stub().callsArgWith(2, null, {})
@SubscriptionViewModelBuilder =
buildUsersSubscriptionViewModel:sinon.stub().callsArgWith(1, null, @activeRecurlySubscription)
buildViewModel: sinon.stub()
@ -72,7 +68,6 @@ describe "SubscriptionController", ->
'./SubscriptionViewModelBuilder': @SubscriptionViewModelBuilder
"./LimitationsManager": @LimitationsManager
"../../infrastructure/GeoIpLookup":@GeoIpLookup
'./RecurlyWrapper': @RecurlyWrapper
"logger-sharelatex":
log:->
warn:->
@ -182,7 +177,7 @@ describe "SubscriptionController", ->
opts.currency.should.equal @stubbedCurrencyCode
done()
@SubscriptionController.paymentPage @req, @res
describe "with a recurly subscription already", ->
it "should redirect to the subscription dashboard", (done)->
@LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
@ -191,7 +186,7 @@ describe "SubscriptionController", ->
url.should.equal "/user/subscription"
done()
@SubscriptionController.paymentPage(@req, @res)
describe "successful_subscription", ->
beforeEach (done) ->
@ -230,8 +225,6 @@ describe "SubscriptionController", ->
@res.callback = done
@settings.apis.recurly.subdomain = 'test'
@userSub = {account: {hosted_login_token: 'abcd'}}
@RecurlyWrapper.getSubscription = sinon.stub()
.callsArgWith(2, null, @userSub)
@LimitationsManager.userHasSubscriptionOrIsGroupMember
.callsArgWith(1, null, true, {})
@SubscriptionController.userSubscriptionPage @req, @res

View file

@ -0,0 +1,65 @@
SandboxedModule = require('sandboxed-module')
sinon = require 'sinon'
should = require("chai").should()
modulePath = '../../../../app/js/Features/Subscription/SubscriptionViewModelBuilder'
describe 'SubscriptionViewModelBuilder', ->
mockSubscription =
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"
beforeEach ->
@user =
email:"tom@yahoo.com",
_id: 'one',
signUpDate: new Date('2000-10-01')
@plan =
name: "test plan"
@SubscriptionFormatters =
formatDate: sinon.stub().returns("Formatted date")
formatPrice: sinon.stub().returns("Formatted price")
@RecurlyWrapper =
sign: sinon.stub().callsArgWith(1, null, "something")
getSubscription: sinon.stub().callsArgWith 2, null,
account:
hosted_login_token: "hosted_login_token"
@builder = SandboxedModule.require modulePath, requires:
"settings-sharelatex": { apis: { recurly: { subdomain: "example.com" }}}
"./RecurlyWrapper": @RecurlyWrapper
"./PlansLocator": @PlansLocator
"./SubscriptionLocator": @SubscriptionLocator
"./SubscriptionFormatters": @SubscriptionFormatters
"logger-sharelatex":
log:->
warn:->
"underscore": {}
@PlansLocator.findLocalPlanInSettings = sinon.stub().returns(@plan)
@SubscriptionLocator.getUsersSubscription = sinon.stub().callsArgWith(1, null, mockSubscription)
@SubscriptionLocator.getMemberSubscriptions = sinon.stub().callsArgWith(1, null, null)
it 'builds the user view model', ->
callback = (error, subscription, memberSubscriptions, billingDetailsLink) =>
@error = error
@subscription = subscription
@memberSubscriptions = memberSubscriptions
@billingDetailsLink = billingDetailsLink
@builder.buildUsersSubscriptionViewModel(@user, callback)
@subscription.name.should.eq 'test plan'
@subscription.nextPaymentDueAt.should.eq 'Formatted date'
@subscription.price.should.eq 'Formatted price'
@billingDetailsLink.should.eq "https://example.com.recurly.com/account/billing_info/edit?ht=hosted_login_token"