RecurlyWrapperTests.coffee 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. should = require('chai').should()
  2. sinon = require 'sinon'
  3. crypto = require 'crypto'
  4. querystring = require 'querystring'
  5. modulePath = "../../../../app/js/Features/Subscription/RecurlyWrapper"
  6. SandboxedModule = require('sandboxed-module')
  7. tk = require("timekeeper")
  8. fixtures =
  9. "subscriptions/44f83d7cba354d5b84812419f923ea96":
  10. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
  11. "<subscription href=\"https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96\">" +
  12. " <account href=\"https://api.recurly.com/v2/accounts/104\"/>" +
  13. " <plan href=\"https://api.recurly.com/v2/plans/gold\">" +
  14. " <plan_code>gold</plan_code>" +
  15. " <name>Gold plan</name>" +
  16. " </plan>" +
  17. " <uuid>44f83d7cba354d5b84812419f923ea96</uuid>" +
  18. " <state>active</state>" +
  19. " <unit_amount_in_cents type=\"integer\">800</unit_amount_in_cents>" +
  20. " <currency>EUR</currency>" +
  21. " <quantity type=\"integer\">1</quantity>" +
  22. " <activated_at type=\"datetime\">2011-05-27T07:00:00Z</activated_at>" +
  23. " <canceled_at nil=\"nil\"></canceled_at>" +
  24. " <expires_at nil=\"nil\"></expires_at>" +
  25. " <current_period_started_at type=\"datetime\">2011-06-27T07:00:00Z</current_period_started_at>" +
  26. " <current_period_ends_at type=\"datetime\">2011-07-27T07:00:00Z</current_period_ends_at>" +
  27. " <trial_started_at nil=\"nil\"></trial_started_at>" +
  28. " <trial_ends_at nil=\"nil\"></trial_ends_at>" +
  29. " <subscription_add_ons type=\"array\">" +
  30. " <subscription_add_on>" +
  31. " <add_on_code>ipaddresses</add_on_code>" +
  32. " <quantity>10</quantity>" +
  33. " <unit_amount_in_cents>150</unit_amount_in_cents>" +
  34. " </subscription_add_on>" +
  35. " </subscription_add_ons>" +
  36. " <a name=\"cancel\" href=\"https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/cancel\" method=\"put\"/>" +
  37. " <a name=\"terminate\" href=\"https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/terminate\" method=\"put\"/>" +
  38. " <a name=\"postpone\" href=\"https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/postpone\" method=\"put\"/>" +
  39. "</subscription>"
  40. "recurly_js/result/70db44b10f5f4b238669480c9903f6f5":
  41. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
  42. "<subscription href=\"https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96\">" +
  43. " <account href=\"https://api.recurly.com/v2/accounts/104\"/>" +
  44. " <plan href=\"https://api.recurly.com/v2/plans/gold\">" +
  45. " <plan_code>gold</plan_code>" +
  46. " <name>Gold plan</name>" +
  47. " </plan>" +
  48. " <uuid>44f83d7cba354d5b84812419f923ea96</uuid>" +
  49. " <state>active</state>" +
  50. " <unit_amount_in_cents type=\"integer\">800</unit_amount_in_cents>" +
  51. " <currency>EUR</currency>" +
  52. " <quantity type=\"integer\">1</quantity>" +
  53. " <activated_at type=\"datetime\">2011-05-27T07:00:00Z</activated_at>" +
  54. " <canceled_at nil=\"nil\"></canceled_at>" +
  55. " <expires_at nil=\"nil\"></expires_at>" +
  56. " <current_period_started_at type=\"datetime\">2011-06-27T07:00:00Z</current_period_started_at>" +
  57. " <current_period_ends_at type=\"datetime\">2011-07-27T07:00:00Z</current_period_ends_at>" +
  58. " <trial_started_at nil=\"nil\"></trial_started_at>" +
  59. " <trial_ends_at nil=\"nil\"></trial_ends_at>" +
  60. " <subscription_add_ons type=\"array\">" +
  61. " <subscription_add_on>" +
  62. " <add_on_code>ipaddresses</add_on_code>" +
  63. " <quantity>10</quantity>" +
  64. " <unit_amount_in_cents>150</unit_amount_in_cents>" +
  65. " </subscription_add_on>" +
  66. " </subscription_add_ons>" +
  67. " <a name=\"cancel\" href=\"https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/cancel\" method=\"put\"/>" +
  68. " <a name=\"terminate\" href=\"https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/terminate\" method=\"put\"/>" +
  69. " <a name=\"postpone\" href=\"https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/postpone\" method=\"put\"/>" +
  70. "</subscription>"
  71. "accounts/104":
  72. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
  73. "<account href=\"https://api.recurly.com/v2/accounts/104\">" +
  74. " <adjustments href=\"https://api.recurly.com/v2/accounts/1/adjustments\"/>" +
  75. " <billing_info href=\"https://api.recurly.com/v2/accounts/1/billing_info\"/>" +
  76. " <invoices href=\"https://api.recurly.com/v2/accounts/1/invoices\"/>" +
  77. " <redemption href=\"https://api.recurly.com/v2/accounts/1/redemption\"/>" +
  78. " <subscriptions href=\"https://api.recurly.com/v2/accounts/1/subscriptions\"/>" +
  79. " <transactions href=\"https://api.recurly.com/v2/accounts/1/transactions\"/>" +
  80. " <account_code>104</account_code>" +
  81. " <state>active</state>" +
  82. " <username nil=\"nil\"></username>" +
  83. " <email>verena@example.com</email>" +
  84. " <first_name>Verena</first_name>" +
  85. " <last_name>Example</last_name>" +
  86. " <accept_language nil=\"nil\"></accept_language>" +
  87. " <hosted_login_token>a92468579e9c4231a6c0031c4716c01d</hosted_login_token>" +
  88. " <created_at type=\"datetime\">2011-10-25T12:00:00</created_at>" +
  89. "</account>"
  90. mockApiRequest = (options, callback) ->
  91. if fixtures[options.url]
  92. callback(null, {statusCode : 200}, fixtures[options.url])
  93. else
  94. callback("Not found", {statusCode : 404})
  95. describe "RecurlyWrapper", ->
  96. before ->
  97. @settings =
  98. plans: [{
  99. planCode: "collaborator"
  100. name: "Collaborator"
  101. features:
  102. collaborators: -1
  103. versioning: true
  104. }]
  105. defaultPlanCode:
  106. collaborators: 0
  107. versioning: false
  108. apis:
  109. recurly:
  110. apiKey: 'nonsense'
  111. privateKey: 'private_nonsense'
  112. @RecurlyWrapper = RecurlyWrapper = SandboxedModule.require modulePath, requires:
  113. "settings-sharelatex": @settings
  114. "logger-sharelatex":
  115. err: sinon.stub()
  116. error: sinon.stub()
  117. log: sinon.stub()
  118. "request": sinon.stub()
  119. describe "sign", ->
  120. before (done) ->
  121. tk.freeze Date.now() # freeze the time for these tests
  122. @RecurlyWrapper.sign({
  123. subscription :
  124. plan_code : "gold"
  125. name : "$$$"
  126. }, (error, signature) =>
  127. @signature = signature
  128. done()
  129. )
  130. after ->
  131. tk.reset()
  132. it "should be signed correctly", ->
  133. signed = @signature.split("|")[0]
  134. query = @signature.split("|")[1]
  135. crypto.createHmac("sha1", @settings.apis.recurly.privateKey).update(query).digest("hex").should.equal signed
  136. it "should be url escaped", ->
  137. query = @signature.split("|")[1]
  138. should.equal query.match(/\[/), null
  139. query.match(/\%5B/).should.not.equal null
  140. it "should contain the passed data", ->
  141. query = querystring.parse @signature.split("|")[1]
  142. query["subscription[plan_code]"].should.equal "gold"
  143. query["subscription[name]"].should.equal "$$$"
  144. it "should contain a nonce", ->
  145. query = querystring.parse @signature.split("|")[1]
  146. should.exist query["nonce"]
  147. it "should contain a timestamp", ->
  148. query = querystring.parse @signature.split("|")[1]
  149. query["timestamp"].should.equal Math.round(Date.now() / 1000) + ""
  150. describe "_parseXml", ->
  151. it "should convert different data types into correct representations", (done) ->
  152. xml = """
  153. <?xml version="1.0" encoding="UTF-8"?>
  154. <subscription href="https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96">
  155. <account href="https://api.recurly.com/v2/accounts/1"/>
  156. <plan href="https://api.recurly.com/v2/plans/gold">
  157. <plan_code>gold</plan_code>
  158. <name>Gold plan</name>
  159. </plan>
  160. <uuid>44f83d7cba354d5b84812419f923ea96</uuid>
  161. <state>active</state>
  162. <unit_amount_in_cents type="integer">800</unit_amount_in_cents>
  163. <currency>EUR</currency>
  164. <quantity type="integer">1</quantity>
  165. <activated_at type="datetime">2011-05-27T07:00:00Z</activated_at>
  166. <canceled_at nil="nil"></canceled_at>
  167. <expires_at nil="nil"></expires_at>
  168. <current_period_started_at type="datetime">2011-06-27T07:00:00Z</current_period_started_at>
  169. <current_period_ends_at type="datetime">2011-07-27T07:00:00Z</current_period_ends_at>
  170. <trial_started_at nil="nil"></trial_started_at>
  171. <trial_ends_at nil="nil"></trial_ends_at>
  172. <subscription_add_ons type="array">
  173. <subscription_add_on>
  174. <add_on_code>ipaddresses</add_on_code>
  175. <quantity>10</quantity>
  176. <unit_amount_in_cents>150</unit_amount_in_cents>
  177. </subscription_add_on>
  178. </subscription_add_ons>
  179. <a name="cancel" href="https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/cancel" method="put"/>
  180. <a name="terminate" href="https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/terminate" method="put"/>
  181. <a name="postpone" href="https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96/postpone" method="put"/>
  182. </subscription>
  183. """
  184. @RecurlyWrapper._parseXml xml, (error, data) ->
  185. data.subscription.plan.plan_code.should.equal "gold"
  186. data.subscription.plan.name.should.equal "Gold plan"
  187. data.subscription.uuid.should.equal "44f83d7cba354d5b84812419f923ea96"
  188. data.subscription.state.should.equal "active"
  189. data.subscription.unit_amount_in_cents.should.equal 800
  190. data.subscription.currency.should.equal "EUR"
  191. data.subscription.quantity.should.equal 1
  192. data.subscription.activated_at.should.deep.equal new Date("2011-05-27T07:00:00Z")
  193. should.equal data.subscription.canceled_at, null
  194. should.equal data.subscription.expires_at, null
  195. data.subscription.current_period_started_at.should.deep.equal new Date("2011-06-27T07:00:00Z")
  196. data.subscription.current_period_ends_at.should.deep.equal new Date("2011-07-27T07:00:00Z")
  197. should.equal data.subscription.trial_started_at, null
  198. should.equal data.subscription.trial_ends_at, null
  199. data.subscription.subscription_add_ons[0].should.deep.equal {
  200. add_on_code: "ipaddresses"
  201. quantity: "10"
  202. unit_amount_in_cents: "150"
  203. }
  204. data.subscription.account.url.should.equal "https://api.recurly.com/v2/accounts/1"
  205. data.subscription.url.should.equal "https://api.recurly.com/v2/subscriptions/44f83d7cba354d5b84812419f923ea96"
  206. data.subscription.plan.url.should.equal "https://api.recurly.com/v2/plans/gold"
  207. done()
  208. describe "getSubscription", ->
  209. describe "with proper subscription id", ->
  210. before ->
  211. @apiRequest = sinon.stub(@RecurlyWrapper, "apiRequest", mockApiRequest)
  212. @RecurlyWrapper.getSubscription "44f83d7cba354d5b84812419f923ea96", (error, recurlySubscription) =>
  213. @recurlySubscription = recurlySubscription
  214. after ->
  215. @RecurlyWrapper.apiRequest.restore()
  216. it "should look up the subscription at the normal API end point", ->
  217. @apiRequest.args[0][0].url.should.equal "subscriptions/44f83d7cba354d5b84812419f923ea96"
  218. it "should return the subscription", ->
  219. @recurlySubscription.uuid.should.equal "44f83d7cba354d5b84812419f923ea96"
  220. describe "with ReculyJS token", ->
  221. before ->
  222. @apiRequest = sinon.stub(@RecurlyWrapper, "apiRequest", mockApiRequest)
  223. @RecurlyWrapper.getSubscription "70db44b10f5f4b238669480c9903f6f5", {recurlyJsResult: true}, (error, recurlySubscription) =>
  224. @recurlySubscription = recurlySubscription
  225. after ->
  226. @RecurlyWrapper.apiRequest.restore()
  227. it "should return the subscription", ->
  228. @recurlySubscription.uuid.should.equal "44f83d7cba354d5b84812419f923ea96"
  229. it "should look up the subscription at the RecurlyJS API end point", ->
  230. @apiRequest.args[0][0].url.should.equal "recurly_js/result/70db44b10f5f4b238669480c9903f6f5"
  231. describe "with includeAccount", ->
  232. beforeEach ->
  233. @apiRequest = sinon.stub(@RecurlyWrapper, "apiRequest", mockApiRequest)
  234. @RecurlyWrapper.getSubscription "44f83d7cba354d5b84812419f923ea96", {includeAccount: true}, (error, recurlySubscription) =>
  235. @recurlySubscription = recurlySubscription
  236. afterEach ->
  237. @RecurlyWrapper.apiRequest.restore()
  238. it "should request the account from the API", ->
  239. @apiRequest.args[1][0].url.should.equal "accounts/104"
  240. it "should populate the account attribute", ->
  241. @recurlySubscription.account.account_code.should.equal "104"
  242. describe "updateSubscription", ->
  243. beforeEach (done) ->
  244. @recurlySubscriptionId = "subscription-id-123"
  245. @apiRequest = sinon.stub @RecurlyWrapper, "apiRequest", (options, callback) =>
  246. @requestOptions = options
  247. callback null, {}, fixtures["subscriptions/44f83d7cba354d5b84812419f923ea96"]
  248. @RecurlyWrapper.updateSubscription @recurlySubscriptionId, { plan_code : "silver", timeframe: "now" }, (error, recurlySubscription) =>
  249. @recurlySubscription = recurlySubscription
  250. done()
  251. afterEach ->
  252. @RecurlyWrapper.apiRequest.restore()
  253. it "should send an update request to the API", ->
  254. @apiRequest.called.should.equal true
  255. @requestOptions.body.should.equal """
  256. <subscription>
  257. <plan_code>silver</plan_code>
  258. <timeframe>now</timeframe>
  259. </subscription>
  260. """
  261. @requestOptions.url.should.equal "subscriptions/#{@recurlySubscriptionId}"
  262. @requestOptions.method.should.equal "put"
  263. it "should return the updated subscription", ->
  264. should.exist @recurlySubscription
  265. @recurlySubscription.plan.plan_code.should.equal "gold"
  266. describe "cancelSubscription", ->
  267. beforeEach (done) ->
  268. @recurlySubscriptionId = "subscription-id-123"
  269. @apiRequest = sinon.stub @RecurlyWrapper, "apiRequest", (options, callback) =>
  270. options.url.should.equal "subscriptions/#{@recurlySubscriptionId}/cancel"
  271. options.method.should.equal "put"
  272. callback()
  273. @RecurlyWrapper.cancelSubscription(@recurlySubscriptionId, done)
  274. afterEach ->
  275. @RecurlyWrapper.apiRequest.restore()
  276. it "should send a cancel request to the API", ->
  277. @apiRequest.called.should.equal true
  278. describe "reactivateSubscription", ->
  279. beforeEach (done) ->
  280. @recurlySubscriptionId = "subscription-id-123"
  281. @apiRequest = sinon.stub @RecurlyWrapper, "apiRequest", (options, callback) =>
  282. options.url.should.equal "subscriptions/#{@recurlySubscriptionId}/reactivate"
  283. options.method.should.equal "put"
  284. callback()
  285. @RecurlyWrapper.reactivateSubscription(@recurlySubscriptionId, done)
  286. afterEach ->
  287. @RecurlyWrapper.apiRequest.restore()
  288. it "should send a cancel request to the API", ->
  289. @apiRequest.called.should.equal true
  290. describe "redeemCoupon", ->
  291. beforeEach (done) ->
  292. @recurlyAccountId = "account-id-123"
  293. @coupon_code = "312321312"
  294. @apiRequest = sinon.stub @RecurlyWrapper, "apiRequest", (options, callback) =>
  295. options.url.should.equal "coupons/#{@coupon_code}/redeem"
  296. options.body.indexOf("<account_code>#{@recurlyAccountId}</account_code>").should.not.equal -1
  297. options.body.indexOf("<currency>USD</currency>").should.not.equal -1
  298. options.method.should.equal "post"
  299. callback()
  300. @RecurlyWrapper.redeemCoupon(@recurlyAccountId, @coupon_code, done)
  301. afterEach ->
  302. @RecurlyWrapper.apiRequest.restore()
  303. it "should send the request to redem the coupon", ->
  304. @apiRequest.called.should.equal true
  305. describe "_addressToXml", ->
  306. beforeEach ->
  307. @address =
  308. address1: "addr_one"
  309. address2: "addr_two"
  310. country: "some_country"
  311. state: "some_state"
  312. zip: "some_zip"
  313. nonsenseKey: "rubbish"
  314. it 'should generate the correct xml', () ->
  315. result = @RecurlyWrapper._addressToXml @address
  316. should.equal(
  317. result,
  318. """
  319. <billing_info>
  320. <address1>addr_one</address1>
  321. <address2 nil="nil">addr_two</address2>
  322. <country>some_country</country>
  323. <state>some_state</state>
  324. <zip>some_zip</zip>
  325. </billing_info>\n
  326. """
  327. )
  328. describe 'createSubscription', ->
  329. beforeEach ->
  330. @user =
  331. _id: 'some_id'
  332. email: 'user@example.com'
  333. @subscriptionDetails =
  334. currencyCode: "EUR"
  335. plan_code: "some_plan_code"
  336. coupon_code: ""
  337. isPaypal: true
  338. address:
  339. address1: "addr_one"
  340. address2: "addr_two"
  341. country: "some_country"
  342. state: "some_state"
  343. zip: "some_zip"
  344. @recurly_token_id = "a-token-id"
  345. describe 'when all goes well', ->
  346. beforeEach ->
  347. describe 'when paypal', ->
  348. beforeEach ->
  349. describe 'when not paypal', ->
  350. beforeEach ->
  351. describe '_createCreditCardSubscription', ->
  352. beforeEach ->
  353. describe 'when all goes well', ->
  354. beforeEach ->
  355. describe 'when api request produces an error', ->
  356. beforeEach ->
  357. describe '_createPaypalSubscription', ->
  358. beforeEach ->
  359. describe 'when all goes well', ->
  360. beforeEach ->