SubscriptionGroupControllerTests.coffee 6.8 KB

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