SubscriptionGroupControllerTests.coffee 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. @subscriptionId = "123434325412"
  12. @user_email = "bob@gmail.com"
  13. @req =
  14. session:
  15. user:
  16. _id: @adminUserId
  17. email:@user_email
  18. params:
  19. subscriptionId:@subscriptionId
  20. query:{}
  21. @subscription = {
  22. _id: @subscriptionId
  23. }
  24. @GroupHandler =
  25. addUserToGroup: sinon.stub().callsArgWith(2, null, @user)
  26. removeUserFromGroup: sinon.stub().callsArgWith(2)
  27. isUserPartOfGroup: sinon.stub()
  28. getPopulatedListOfMembers: sinon.stub().callsArgWith(1, null, [@user])
  29. @SubscriptionLocator =
  30. findManagedSubscription: 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 subscription 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(@subscriptionId, "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 subscription 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(@subscriptionId, userIdToRemove).should.equal true
  65. done()
  66. @Controller.removeUserFromGroup @req, res
  67. describe "renderSubscriptionGroupAdminPage", ->
  68. it "should redirect you if you don't have a group account", (done)->
  69. @subscription.groupPlan = false
  70. res =
  71. redirect : (path)=>
  72. path.should.equal("/user/subscription")
  73. done()
  74. @Controller.renderSubscriptionGroupAdminPage @req, res
  75. it "should redirect you don't have a subscription", (done)->
  76. @SubscriptionLocator.getUsersSubscription = sinon.stub().callsArgWith(1)
  77. res =
  78. redirect : (path)=>
  79. path.should.equal("/user/subscription")
  80. done()
  81. @Controller.renderSubscriptionGroupAdminPage @req, res
  82. describe "exportGroupCsv", ->
  83. beforeEach ->
  84. @subscription.groupPlan = true
  85. @res = new MockResponse()
  86. @res.contentType = sinon.stub()
  87. @res.header = sinon.stub()
  88. @res.send = sinon.stub()
  89. @Controller.exportGroupCsv @req, @res
  90. it "should set the correct content type on the request", ->
  91. @res.contentType
  92. .calledWith("text/csv")
  93. .should.equal true
  94. it "should name the exported csv file", ->
  95. @res.header
  96. .calledWith(
  97. "Content-Disposition",
  98. "attachment; filename=Group.csv")
  99. .should.equal true
  100. it "should export the correct csv", ->
  101. @res.send
  102. .calledWith("user@email.com\n")
  103. .should.equal true