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

View file

@ -42,10 +42,6 @@ describe "SubscriptionController", ->
userHasSubscriptionOrIsGroupMember: sinon.stub() userHasSubscriptionOrIsGroupMember: sinon.stub()
userHasSubscription : sinon.stub() userHasSubscription : sinon.stub()
@RecurlyWrapper =
sign: sinon.stub().callsArgWith(1, null, "somthing")
getSubscription: sinon.stub().callsArgWith(2, null, {})
@SubscriptionViewModelBuilder = @SubscriptionViewModelBuilder =
buildUsersSubscriptionViewModel:sinon.stub().callsArgWith(1, null, @activeRecurlySubscription) buildUsersSubscriptionViewModel:sinon.stub().callsArgWith(1, null, @activeRecurlySubscription)
buildViewModel: sinon.stub() buildViewModel: sinon.stub()
@ -72,7 +68,6 @@ describe "SubscriptionController", ->
'./SubscriptionViewModelBuilder': @SubscriptionViewModelBuilder './SubscriptionViewModelBuilder': @SubscriptionViewModelBuilder
"./LimitationsManager": @LimitationsManager "./LimitationsManager": @LimitationsManager
"../../infrastructure/GeoIpLookup":@GeoIpLookup "../../infrastructure/GeoIpLookup":@GeoIpLookup
'./RecurlyWrapper': @RecurlyWrapper
"logger-sharelatex": "logger-sharelatex":
log:-> log:->
warn:-> warn:->
@ -230,8 +225,6 @@ describe "SubscriptionController", ->
@res.callback = done @res.callback = done
@settings.apis.recurly.subdomain = 'test' @settings.apis.recurly.subdomain = 'test'
@userSub = {account: {hosted_login_token: 'abcd'}} @userSub = {account: {hosted_login_token: 'abcd'}}
@RecurlyWrapper.getSubscription = sinon.stub()
.callsArgWith(2, null, @userSub)
@LimitationsManager.userHasSubscriptionOrIsGroupMember @LimitationsManager.userHasSubscriptionOrIsGroupMember
.callsArgWith(1, null, true, {}) .callsArgWith(1, null, true, {})
@SubscriptionController.userSubscriptionPage @req, @res @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"