SubscriptionGroupControllerTests.coffee 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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("/")
  82. done()
  83. @Controller.renderSubscriptionGroupAdminPage @req, res
  84. describe "renderGroupInvitePage", ->
  85. describe "with a valid licence", ->
  86. beforeEach ->
  87. @SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns({subscription_id:@subscription_id, adminUser_id:@adminUserId})
  88. it "should render subscriptions/group/invite if not part of group", (done)->
  89. @GroupHandler.isUserPartOfGroup.callsArgWith(2, null, false)
  90. res =
  91. render : (pageName)=>
  92. pageName.should.equal "subscriptions/group/invite"
  93. done()
  94. @Controller.renderGroupInvitePage @req, res
  95. it "should redirect to custom page if is already part of group", (done)->
  96. @GroupHandler.isUserPartOfGroup.callsArgWith(2, null, true)
  97. res =
  98. redirect : (location)=>
  99. location.should.equal "/user/subscription/custom_account"
  100. done()
  101. @Controller.renderGroupInvitePage @req, res
  102. describe "without a valid licence", ->
  103. beforeEach ->
  104. @SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns(undefined)
  105. it "should send a 500", (done)->
  106. @Controller.renderGroupInvitePage @req, {}
  107. @ErrorsController.notFound.called.should.equal true
  108. done()
  109. describe "beginJoinGroup", ->
  110. describe "with a valid licence", ->
  111. beforeEach ->
  112. @licenceName = "get amazing licence"
  113. @SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns({name:@licenceName})
  114. @GroupHandler.sendVerificationEmail.callsArgWith(3)
  115. it "should ask the SubscriptionGroupHandler to send the verification email", (done)->
  116. res =
  117. sendStatus : (statusCode)=>
  118. statusCode.should.equal 200
  119. @GroupHandler.sendVerificationEmail.calledWith(@subscription_id, @licenceName, @user_email).should.equal true
  120. done()
  121. @Controller.beginJoinGroup @req, res
  122. describe "without a valid licence", ->
  123. beforeEach ->
  124. @SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns(undefined)
  125. it "should send a 500", (done)->
  126. @Controller.beginJoinGroup @req, {}
  127. @ErrorsController.notFound.called.should.equal true
  128. done()
  129. describe "completeJoin", ->
  130. describe "with a valid licence", ->
  131. beforeEach ->
  132. @GroupHandler.processGroupVerification.callsArgWith(3)
  133. @SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns({name:@licenceName})
  134. it "should redirect to the success page upon processGroupVerification", (done)->
  135. @req.query.token = @token
  136. res =
  137. redirect : (location)=>
  138. @GroupHandler.processGroupVerification.calledWith(@user_email, @subscription_id, @token).should.equal true
  139. location.should.equal "/user/subscription/#{@subscription_id}/group/successful-join"
  140. done()
  141. @Controller.completeJoin @req, res
  142. describe "without a valid licence", ->
  143. it "should send a 500", (done)->
  144. @SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns(undefined)
  145. @Controller.completeJoin @req, {}
  146. @ErrorsController.notFound.called.should.equal true
  147. done()
  148. it "should redirect to the invited page with querystring if token was not found", (done)->
  149. @SubscriptionDomainHandler.findDomainLicenceBySubscriptionId.returns({name:@licenceName})
  150. @req.query.token = @token
  151. @GroupHandler.processGroupVerification.callsArgWith(3, "token_not_found")
  152. res =
  153. redirect : (location)=>
  154. location.should.equal "/user/subscription/#{@subscription_id}/group/invited?expired=true"
  155. done()
  156. @Controller.completeJoin @req, res
  157. describe "exportGroupCsv", ->
  158. beforeEach ->
  159. @subscription.groupPlan = true
  160. @res = new MockResponse()
  161. @res.contentType = sinon.stub()
  162. @res.header = sinon.stub()
  163. @res.send = sinon.stub()
  164. @Controller.exportGroupCsv @req, @res
  165. it "should set the correct content type on the request", ->
  166. @res.contentType
  167. .calledWith("text/csv")
  168. .should.equal true
  169. it "should name the exported csv file", ->
  170. @res.header
  171. .calledWith(
  172. "Content-Disposition",
  173. "attachment; filename=Group.csv")
  174. .should.equal true
  175. it "should export the correct csv", ->
  176. @res.send
  177. .calledWith("user@email.com\n")
  178. .should.equal true