Update existing RecurlyWrapper tests.

This commit is contained in:
Shane Kilkelly 2016-06-27 13:54:54 +01:00
parent 401565ba23
commit d853eb5916

View file

@ -2,8 +2,8 @@ should = require('chai').should()
sinon = require 'sinon' sinon = require 'sinon'
crypto = require 'crypto' crypto = require 'crypto'
querystring = require 'querystring' querystring = require 'querystring'
RecurlyWrapper = require "../../../../app/js/Features/Subscription/RecurlyWrapper" modulePath = "../../../../app/js/Features/Subscription/RecurlyWrapper"
Settings = require "settings-sharelatex" SandboxedModule = require('sandboxed-module')
tk = require("timekeeper") tk = require("timekeeper")
fixtures = fixtures =
@ -97,22 +97,37 @@ mockApiRequest = (options, callback) ->
describe "RecurlyWrapper", -> describe "RecurlyWrapper", ->
beforeEach ->
Settings.plans = [{ before ->
planCode: "collaborator" @settings =
name: "Collaborator" plans: [{
features: planCode: "collaborator"
collaborators: -1 name: "Collaborator"
versioning: true features:
}] collaborators: -1
Settings.defaultPlanCode = versioning: true
collaborators: 0 }]
versioning: false defaultPlanCode:
collaborators: 0
versioning: false
apis:
recurly:
apiKey: 'nonsense'
privateKey: 'private_nonsense'
@RecurlyWrapper = RecurlyWrapper = SandboxedModule.require modulePath, requires:
"settings-sharelatex": @settings
"logger-sharelatex":
err: sinon.stub()
error: sinon.stub()
log: sinon.stub()
"request": sinon.stub()
describe "sign", -> describe "sign", ->
before (done) -> before (done) ->
tk.freeze Date.now() # freeze the time for these tests tk.freeze Date.now() # freeze the time for these tests
RecurlyWrapper.sign({ @RecurlyWrapper.sign({
subscription : subscription :
plan_code : "gold" plan_code : "gold"
name : "$$$" name : "$$$"
@ -127,7 +142,7 @@ describe "RecurlyWrapper", ->
it "should be signed correctly", -> it "should be signed correctly", ->
signed = @signature.split("|")[0] signed = @signature.split("|")[0]
query = @signature.split("|")[1] query = @signature.split("|")[1]
crypto.createHmac("sha1", Settings.apis.recurly.privateKey).update(query).digest("hex").should.equal signed crypto.createHmac("sha1", @settings.apis.recurly.privateKey).update(query).digest("hex").should.equal signed
it "should be url escaped", -> it "should be url escaped", ->
query = @signature.split("|")[1] query = @signature.split("|")[1]
@ -180,7 +195,7 @@ describe "RecurlyWrapper", ->
" <a name=\"terminate\" href=\"https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/terminate\" method=\"put\"/>" + " <a name=\"terminate\" href=\"https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/terminate\" method=\"put\"/>" +
" <a name=\"postpone\" href=\"https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/postpone\" method=\"put\"/>" + " <a name=\"postpone\" href=\"https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/postpone\" method=\"put\"/>" +
"</subscription>" "</subscription>"
RecurlyWrapper._parseXml xml, (error, data) -> @RecurlyWrapper._parseXml xml, (error, data) ->
data.subscription.plan.plan_code.should.equal "gold" data.subscription.plan.plan_code.should.equal "gold"
data.subscription.plan.name.should.equal "Gold plan" data.subscription.plan.name.should.equal "Gold plan"
data.subscription.uuid.should.equal "44f83d7cba354d5b84812419f923ea96" data.subscription.uuid.should.equal "44f83d7cba354d5b84812419f923ea96"
@ -188,18 +203,22 @@ describe "RecurlyWrapper", ->
data.subscription.unit_amount_in_cents.should.equal 800 data.subscription.unit_amount_in_cents.should.equal 800
data.subscription.currency.should.equal "EUR" data.subscription.currency.should.equal "EUR"
data.subscription.quantity.should.equal 1 data.subscription.quantity.should.equal 1
data.subscription.activated_at.should.deep.equal new Date("2011-05-27T07:00:00Z") data.subscription.activated_at.should.deep.equal new Date("2011-05-27T07:00:00Z")
should.equal data.subscription.canceled_at, null should.equal data.subscription.canceled_at, null
should.equal data.subscription.expires_at, null should.equal data.subscription.expires_at, null
data.subscription.current_period_started_at.should.deep.equal new Date("2011-06-27T07:00:00Z") data.subscription.current_period_started_at.should.deep.equal new Date("2011-06-27T07:00:00Z")
data.subscription.current_period_ends_at.should.deep.equal new Date("2011-07-27T07:00:00Z") data.subscription.current_period_ends_at.should.deep.equal new Date("2011-07-27T07:00:00Z")
should.equal data.subscription.trial_started_at, null should.equal data.subscription.trial_started_at, null
should.equal data.subscription.trial_ends_at, null should.equal data.subscription.trial_ends_at, null
data.subscription.subscription_add_ons.should.deep.equal [{
data.subscription.subscription_add_ons[0].should.deep.equal {
add_on_code: "ipaddresses" add_on_code: "ipaddresses"
quantity: "10" quantity: "10"
unit_amount_in_cents: "150" unit_amount_in_cents: "150"
}] }
data.subscription.account.url.should.equal "https://api.recurly.com/v2/accounts/1" data.subscription.account.url.should.equal "https://api.recurly.com/v2/accounts/1"
data.subscription.url.should.equal "https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96" data.subscription.url.should.equal "https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96"
data.subscription.plan.url.should.equal "https://api.recurly.com/v2/plans/gold" data.subscription.plan.url.should.equal "https://api.recurly.com/v2/plans/gold"
@ -208,11 +227,11 @@ describe "RecurlyWrapper", ->
describe "getSubscription", -> describe "getSubscription", ->
describe "with proper subscription id", -> describe "with proper subscription id", ->
before -> before ->
@apiRequest = sinon.stub(RecurlyWrapper, "apiRequest", mockApiRequest) @apiRequest = sinon.stub(@RecurlyWrapper, "apiRequest", mockApiRequest)
RecurlyWrapper.getSubscription "44f83d7cba354d5b84812419f923ea96", (error, recurlySubscription) => @RecurlyWrapper.getSubscription "44f83d7cba354d5b84812419f923ea96", (error, recurlySubscription) =>
@recurlySubscription = recurlySubscription @recurlySubscription = recurlySubscription
after -> after ->
RecurlyWrapper.apiRequest.restore() @RecurlyWrapper.apiRequest.restore()
it "should look up the subscription at the normal API end point", -> it "should look up the subscription at the normal API end point", ->
@apiRequest.args[0][0].url.should.equal "subscriptions/44f83d7cba354d5b84812419f923ea96" @apiRequest.args[0][0].url.should.equal "subscriptions/44f83d7cba354d5b84812419f923ea96"
@ -222,11 +241,11 @@ describe "RecurlyWrapper", ->
describe "with ReculyJS token", -> describe "with ReculyJS token", ->
before -> before ->
@apiRequest = sinon.stub(RecurlyWrapper, "apiRequest", mockApiRequest) @apiRequest = sinon.stub(@RecurlyWrapper, "apiRequest", mockApiRequest)
RecurlyWrapper.getSubscription "70db44b10f5f4b238669480c9903f6f5", {recurlyJsResult: true}, (error, recurlySubscription) => @RecurlyWrapper.getSubscription "70db44b10f5f4b238669480c9903f6f5", {recurlyJsResult: true}, (error, recurlySubscription) =>
@recurlySubscription = recurlySubscription @recurlySubscription = recurlySubscription
after -> after ->
RecurlyWrapper.apiRequest.restore() @RecurlyWrapper.apiRequest.restore()
it "should return the subscription", -> it "should return the subscription", ->
@recurlySubscription.uuid.should.equal "44f83d7cba354d5b84812419f923ea96" @recurlySubscription.uuid.should.equal "44f83d7cba354d5b84812419f923ea96"
@ -236,11 +255,11 @@ describe "RecurlyWrapper", ->
describe "with includeAccount", -> describe "with includeAccount", ->
beforeEach -> beforeEach ->
@apiRequest = sinon.stub(RecurlyWrapper, "apiRequest", mockApiRequest) @apiRequest = sinon.stub(@RecurlyWrapper, "apiRequest", mockApiRequest)
RecurlyWrapper.getSubscription "44f83d7cba354d5b84812419f923ea96", {includeAccount: true}, (error, recurlySubscription) => @RecurlyWrapper.getSubscription "44f83d7cba354d5b84812419f923ea96", {includeAccount: true}, (error, recurlySubscription) =>
@recurlySubscription = recurlySubscription @recurlySubscription = recurlySubscription
afterEach -> afterEach ->
RecurlyWrapper.apiRequest.restore() @RecurlyWrapper.apiRequest.restore()
it "should request the account from the API", -> it "should request the account from the API", ->
@apiRequest.args[1][0].url.should.equal "accounts/104" @apiRequest.args[1][0].url.should.equal "accounts/104"
@ -252,14 +271,14 @@ describe "RecurlyWrapper", ->
describe "updateSubscription", -> describe "updateSubscription", ->
beforeEach (done) -> beforeEach (done) ->
@recurlySubscriptionId = "subscription-id-123" @recurlySubscriptionId = "subscription-id-123"
@apiRequest = sinon.stub RecurlyWrapper, "apiRequest", (options, callback) => @apiRequest = sinon.stub @RecurlyWrapper, "apiRequest", (options, callback) =>
@requestOptions = options @requestOptions = options
callback null, {}, fixtures["subscriptions/44f83d7cba354d5b84812419f923ea96"] callback null, {}, fixtures["subscriptions/44f83d7cba354d5b84812419f923ea96"]
RecurlyWrapper.updateSubscription @recurlySubscriptionId, { plan_code : "silver", timeframe: "now" }, (error, recurlySubscription) => @RecurlyWrapper.updateSubscription @recurlySubscriptionId, { plan_code : "silver", timeframe: "now" }, (error, recurlySubscription) =>
@recurlySubscription = recurlySubscription @recurlySubscription = recurlySubscription
done() done()
afterEach -> afterEach ->
RecurlyWrapper.apiRequest.restore() @RecurlyWrapper.apiRequest.restore()
it "should send an update request to the API", -> it "should send an update request to the API", ->
@apiRequest.called.should.equal true @apiRequest.called.should.equal true
@ -280,14 +299,14 @@ describe "RecurlyWrapper", ->
describe "cancelSubscription", -> describe "cancelSubscription", ->
beforeEach (done) -> beforeEach (done) ->
@recurlySubscriptionId = "subscription-id-123" @recurlySubscriptionId = "subscription-id-123"
@apiRequest = sinon.stub RecurlyWrapper, "apiRequest", (options, callback) => @apiRequest = sinon.stub @RecurlyWrapper, "apiRequest", (options, callback) =>
options.url.should.equal "subscriptions/#{@recurlySubscriptionId}/cancel" options.url.should.equal "subscriptions/#{@recurlySubscriptionId}/cancel"
options.method.should.equal "put" options.method.should.equal "put"
callback() callback()
RecurlyWrapper.cancelSubscription(@recurlySubscriptionId, done) @RecurlyWrapper.cancelSubscription(@recurlySubscriptionId, done)
afterEach -> afterEach ->
RecurlyWrapper.apiRequest.restore() @RecurlyWrapper.apiRequest.restore()
it "should send a cancel request to the API", -> it "should send a cancel request to the API", ->
@apiRequest.called.should.equal true @apiRequest.called.should.equal true
@ -295,14 +314,14 @@ describe "RecurlyWrapper", ->
describe "reactivateSubscription", -> describe "reactivateSubscription", ->
beforeEach (done) -> beforeEach (done) ->
@recurlySubscriptionId = "subscription-id-123" @recurlySubscriptionId = "subscription-id-123"
@apiRequest = sinon.stub RecurlyWrapper, "apiRequest", (options, callback) => @apiRequest = sinon.stub @RecurlyWrapper, "apiRequest", (options, callback) =>
options.url.should.equal "subscriptions/#{@recurlySubscriptionId}/reactivate" options.url.should.equal "subscriptions/#{@recurlySubscriptionId}/reactivate"
options.method.should.equal "put" options.method.should.equal "put"
callback() callback()
RecurlyWrapper.reactivateSubscription(@recurlySubscriptionId, done) @RecurlyWrapper.reactivateSubscription(@recurlySubscriptionId, done)
afterEach -> afterEach ->
RecurlyWrapper.apiRequest.restore() @RecurlyWrapper.apiRequest.restore()
it "should send a cancel request to the API", -> it "should send a cancel request to the API", ->
@apiRequest.called.should.equal true @apiRequest.called.should.equal true
@ -314,21 +333,20 @@ describe "RecurlyWrapper", ->
beforeEach (done) -> beforeEach (done) ->
@recurlyAccountId = "account-id-123" @recurlyAccountId = "account-id-123"
@coupon_code = "312321312" @coupon_code = "312321312"
@apiRequest = sinon.stub RecurlyWrapper, "apiRequest", (options, callback) => @apiRequest = sinon.stub @RecurlyWrapper, "apiRequest", (options, callback) =>
options.url.should.equal "coupons/#{@coupon_code}/redeem" options.url.should.equal "coupons/#{@coupon_code}/redeem"
options.body.indexOf("<account_code>#{@recurlyAccountId}</account_code>").should.not.equal -1 options.body.indexOf("<account_code>#{@recurlyAccountId}</account_code>").should.not.equal -1
options.body.indexOf("<currency>USD</currency>").should.not.equal -1 options.body.indexOf("<currency>USD</currency>").should.not.equal -1
options.method.should.equal "post" options.method.should.equal "post"
callback() callback()
RecurlyWrapper.redeemCoupon(@recurlyAccountId, @coupon_code, done) @RecurlyWrapper.redeemCoupon(@recurlyAccountId, @coupon_code, done)
afterEach -> afterEach ->
RecurlyWrapper.apiRequest.restore() @RecurlyWrapper.apiRequest.restore()
it "should send the request to redem the coupon", -> it "should send the request to redem the coupon", ->
@apiRequest.called.should.equal true @apiRequest.called.should.equal true
describe "_addressToXml", -> describe "_addressToXml", ->
beforeEach -> beforeEach ->
@ -341,7 +359,7 @@ describe "RecurlyWrapper", ->
nonsenseKey: "rubbish" nonsenseKey: "rubbish"
it 'should generate the correct xml', () -> it 'should generate the correct xml', () ->
result = RecurlyWrapper._addressToXml @address result = @RecurlyWrapper._addressToXml @address
should.equal( should.equal(
result, result,
""" """
@ -355,5 +373,52 @@ describe "RecurlyWrapper", ->
""" """
) )
describe 'createSubscription', ->
beforeEach ->
@user =
_id: 'some_id'
email: 'user@example.com'
@subscriptionDetails =
currencyCode: "EUR"
plan_code: "some_plan_code"
coupon_code: ""
isPaypal: true
address:
address1: "addr_one"
address2: "addr_two"
country: "some_country"
state: "some_state"
zip: "some_zip"
@recurly_token_id = "a-token-id"
describe 'when all goes well', ->
beforeEach ->
describe 'when paypal', ->
beforeEach ->
describe 'when not paypal', ->
beforeEach ->
describe '_createCreditCardSubscription', ->
beforeEach ->
describe 'when all goes well', ->
beforeEach ->
describe 'when api request produces an error', ->
beforeEach ->
describe '_createPaypalSubscription', ->
beforeEach ->
describe 'when all goes well', ->
beforeEach ->