SubscriptionGroupControllerTests.coffee 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. getPopulatedListOfMembers: sinon.stub().callsArgWith(1, null, [@user])
  28. @SubscriptionLocator = getUsersSubscription: sinon.stub().callsArgWith(1, null, @subscription)
  29. @AuthenticationController =
  30. getLoggedInUserId: (req) -> req.session.user._id
  31. getSessionUser: (req) -> req.session.user
  32. @SubscriptionDomainHandler =
  33. findDomainLicenceBySubscriptionId:sinon.stub()
  34. @OneTimeTokenHandler =
  35. getValueFromTokenAndExpire:sinon.stub()
  36. @ErrorsController =
  37. notFound:sinon.stub()
  38. @Controller = SandboxedModule.require modulePath, requires:
  39. "./SubscriptionGroupHandler":@GroupHandler
  40. "logger-sharelatex": log:->
  41. "./SubscriptionLocator": @SubscriptionLocator
  42. "./SubscriptionDomainHandler":@SubscriptionDomainHandler
  43. "../Errors/ErrorController":@ErrorsController
  44. '../Authentication/AuthenticationController': @AuthenticationController
  45. @token = "super-secret-token"
  46. describe "addUserToGroup", ->
  47. it "should use the admin id for the logged in user and take the email address from the body", (done)->
  48. newEmail = " boB@gmaiL.com "
  49. @req.body = email: newEmail
  50. res =
  51. json : (data)=>
  52. @GroupHandler.addUserToGroup.calledWith(@adminUserId, "bob@gmail.com").should.equal true
  53. data.user.should.deep.equal @user
  54. done()
  55. @Controller.addUserToGroup @req, res
  56. describe "removeUserFromGroup", ->
  57. it "should use the admin id for the logged in user and take the user id from the params", (done)->
  58. userIdToRemove = "31231"
  59. @req.params = user_id: userIdToRemove
  60. res =
  61. send : =>
  62. @GroupHandler.removeUserFromGroup.calledWith(@adminUserId, userIdToRemove).should.equal true
  63. done()
  64. @Controller.removeUserFromGroup @req, res
  65. describe "renderSubscriptionGroupAdminPage", ->
  66. it "should redirect you if you don't have a group account", (done)->
  67. @subscription.groupPlan = false
  68. res =
  69. redirect : (path)=>
  70. path.should.equal("/user/subscription")
  71. done()
  72. @Controller.renderSubscriptionGroupAdminPage @req, res
  73. it "should redirect you don't have a subscription", (done)->
  74. @SubscriptionLocator.getUsersSubscription = sinon.stub().callsArgWith(1)
  75. res =
  76. redirect : (path)=>
  77. path.should.equal("/user/subscription")
  78. done()
  79. @Controller.renderSubscriptionGroupAdminPage @req, res
  80. describe "exportGroupCsv", ->
  81. beforeEach ->
  82. @subscription.groupPlan = true
  83. @res = new MockResponse()
  84. @res.contentType = sinon.stub()
  85. @res.header = sinon.stub()
  86. @res.send = sinon.stub()
  87. @Controller.exportGroupCsv @req, @res
  88. it "should set the correct content type on the request", ->
  89. @res.contentType
  90. .calledWith("text/csv")
  91. .should.equal true
  92. it "should name the exported csv file", ->
  93. @res.header
  94. .calledWith(
  95. "Content-Disposition",
  96. "attachment; filename=Group.csv")
  97. .should.equal true
  98. it "should export the correct csv", ->
  99. @res.send
  100. .calledWith("user@email.com\n")
  101. .should.equal true