SubscriptionGroupControllerTests.coffee 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. SandboxedModule = require('sandboxed-module')
  2. should = require('chai').should()
  3. sinon = require 'sinon'
  4. assert = require("chai").assert
  5. modulePath = "../../../../app/js/Features/Subscription/SubscriptionGroupController"
  6. MockResponse = require "../helpers/MockResponse"
  7. describe "SubscriptionGroupController", ->
  8. beforeEach ->
  9. @user = {_id:"!@312431",email:"user@email.com"}
  10. @adminUserId = "123jlkj"
  11. @subscription_id = "123434325412"
  12. @user_email = "bob@gmail.com"
  13. @req =
  14. session:
  15. user:
  16. _id: @adminUserId
  17. email:@user_email
  18. params:
  19. subscription_id:@subscription_id
  20. query:{}
  21. @subscription = {}
  22. @GroupHandler =
  23. addUserToGroup: sinon.stub().callsArgWith(2, null, @user)
  24. removeUserFromGroup: sinon.stub().callsArgWith(2)
  25. removeEmailInviteFromGroup: sinon.stub().callsArgWith(2)
  26. isUserPartOfGroup: sinon.stub()
  27. sendVerificationEmail:sinon.stub()
  28. processGroupVerification:sinon.stub()
  29. getPopulatedListOfMembers: sinon.stub().callsArgWith(1, null, [@user])
  30. @SubscriptionLocator = getUsersSubscription: sinon.stub().callsArgWith(1, null, @subscription)
  31. @AuthenticationController =
  32. getLoggedInUserId: (req) -> req.session.user._id
  33. getSessionUser: (req) -> req.session.user
  34. @SubscriptionDomainHandler =
  35. findDomainLicenceBySubscriptionId:sinon.stub()
  36. @OneTimeTokenHandler =
  37. getValueFromTokenAndExpire:sinon.stub()
  38. @ErrorsController =
  39. notFound:sinon.stub()
  40. @Controller = SandboxedModule.require modulePath, requires:
  41. "./SubscriptionGroupHandler":@GroupHandler
  42. "logger-sharelatex": log:->
  43. "./SubscriptionLocator": @SubscriptionLocator
  44. "./SubscriptionDomainHandler":@SubscriptionDomainHandler
  45. "../Errors/ErrorController":@ErrorsController
  46. '../Authentication/AuthenticationController': @AuthenticationController
  47. @token = "super-secret-token"
  48. describe "addUserToGroup", ->
  49. it "should use the admin id for the logged in user and take the email address from the body", (done)->
  50. newEmail = " boB@gmaiL.com "
  51. @req.body = email: newEmail
  52. res =
  53. json : (data)=>
  54. @GroupHandler.addUserToGroup.calledWith(@adminUserId, "bob@gmail.com").should.equal true
  55. data.user.should.deep.equal @user
  56. done()
  57. @Controller.addUserToGroup @req, res
  58. describe "removeUserFromGroup", ->
  59. it "should use the admin id for the logged in user and take the user id from the params", (done)->
  60. userIdToRemove = "31231"
  61. @req.params = user_id: userIdToRemove
  62. res =
  63. send : =>
  64. @GroupHandler.removeUserFromGroup.calledWith(@adminUserId, userIdToRemove).should.equal true
  65. done()
  66. @Controller.removeUserFromGroup @req, res
  67. describe "removeEmailInviteFromGroup", ->
  68. it "should use the admin id for the logged in user and take the email from the params", (done)->
  69. email = "jo@example.com"
  70. @req.params = email: email
  71. res =
  72. send : =>
  73. @GroupHandler.removeEmailInviteFromGroup.calledWith(@adminUserId, email).should.equal true
  74. done()
  75. @Controller.removeEmailInviteFromGroup @req, res
  76. describe "renderSubscriptionGroupAdminPage", ->
  77. it "should redirect you if you don't have a group account", (done)->
  78. @subscription.groupPlan = false
  79. res =
  80. redirect : (path)=>
  81. path.should.equal("/user/subscription")
  82. done()
  83. @Controller.renderSubscriptionGroupAdminPage @req, res
  84. it "should redirect you don't have a subscription", (done)->
  85. @SubscriptionLocator.getUsersSubscription = sinon.stub().callsArgWith(1)
  86. res =
  87. redirect : (path)=>
  88. path.should.equal("/user/subscription")
  89. done()
  90. @Controller.renderSubscriptionGroupAdminPage @req, res
  91. describe "renderGroupInvitePage", ->
  92. describe "with a valid licence", ->
  93. beforeEach ->
  94. @SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns({subscription_id:@subscription_id, adminUser_id:@adminUserId})
  95. it "should render subscriptions/group/invite if not part of group", (done)->
  96. @GroupHandler.isUserPartOfGroup.callsArgWith(2, null, false)
  97. res =
  98. render : (pageName)=>
  99. pageName.should.equal "subscriptions/group/invite"
  100. done()
  101. @Controller.renderGroupInvitePage @req, res
  102. it "should redirect to custom page if is already part of group", (done)->
  103. @GroupHandler.isUserPartOfGroup.callsArgWith(2, null, true)
  104. res =
  105. redirect : (location)=>
  106. location.should.equal "/user/subscription/custom_account"
  107. done()
  108. @Controller.renderGroupInvitePage @req, res
  109. describe "without a valid licence", ->
  110. beforeEach ->
  111. @SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns(undefined)
  112. it "should send a 500", (done)->
  113. @Controller.renderGroupInvitePage @req, {}
  114. @ErrorsController.notFound.called.should.equal true
  115. done()
  116. describe "beginJoinGroup", ->
  117. describe "with a valid licence", ->
  118. beforeEach ->
  119. @licenceName = "get amazing licence"
  120. @SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns({name:@licenceName})
  121. @GroupHandler.sendVerificationEmail.callsArgWith(3)
  122. it "should ask the SubscriptionGroupHandler to send the verification email", (done)->
  123. res =
  124. sendStatus : (statusCode)=>
  125. statusCode.should.equal 200
  126. @GroupHandler.sendVerificationEmail.calledWith(@subscription_id, @licenceName, @user_email).should.equal true
  127. done()
  128. @Controller.beginJoinGroup @req, res
  129. describe "without a valid licence", ->
  130. beforeEach ->
  131. @SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns(undefined)
  132. it "should send a 500", (done)->
  133. @Controller.beginJoinGroup @req, {}
  134. @ErrorsController.notFound.called.should.equal true
  135. done()
  136. describe "completeJoin", ->
  137. describe "with a valid licence", ->
  138. beforeEach ->
  139. @GroupHandler.processGroupVerification.callsArgWith(3)
  140. @SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns({name:@licenceName})
  141. it "should redirect to the success page upon processGroupVerification", (done)->
  142. @req.query.token = @token
  143. res =
  144. redirect : (location)=>
  145. @GroupHandler.processGroupVerification.calledWith(@user_email, @subscription_id, @token).should.equal true
  146. location.should.equal "/user/subscription/#{@subscription_id}/group/successful-join"
  147. done()
  148. @Controller.completeJoin @req, res
  149. describe "without a valid licence", ->
  150. it "should send a 500", (done)->
  151. @SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns(undefined)
  152. @Controller.completeJoin @req, {}
  153. @ErrorsController.notFound.called.should.equal true
  154. done()
  155. it "should redirect to the invited page with querystring if token was not found", (done)->
  156. @SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns({name:@licenceName})
  157. @req.query.token = @token
  158. @GroupHandler.processGroupVerification.callsArgWith(3, "token_not_found")
  159. res =
  160. redirect : (location)=>
  161. location.should.equal "/user/subscription/#{@subscription_id}/group/invited?expired=true"
  162. done()
  163. @Controller.completeJoin @req, res
  164. describe "exportGroupCsv", ->
  165. beforeEach ->
  166. @subscription.groupPlan = true
  167. @res = new MockResponse()
  168. @res.contentType = sinon.stub()
  169. @res.header = sinon.stub()
  170. @res.send = sinon.stub()
  171. @Controller.exportGroupCsv @req, @res
  172. it "should set the correct content type on the request", ->
  173. @res.contentType
  174. .calledWith("text/csv")
  175. .should.equal true
  176. it "should name the exported csv file", ->
  177. @res.header
  178. .calledWith(
  179. "Content-Disposition",
  180. "attachment; filename=Group.csv")
  181. .should.equal true
  182. it "should export the correct csv", ->
  183. @res.send
  184. .calledWith("user@email.com\n")
  185. .should.equal true