SubscriptionGroupControllerTests.coffee 3.7 KB

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