SubscriptionGroupControllerTests.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "exportGroupCsv", ->
  68. beforeEach ->
  69. @subscription.groupPlan = true
  70. @res = new MockResponse()
  71. @res.contentType = sinon.stub()
  72. @res.header = sinon.stub()
  73. @res.send = sinon.stub()
  74. @Controller.exportGroupCsv @req, @res
  75. it "should set the correct content type on the request", ->
  76. @res.contentType
  77. .calledWith("text/csv")
  78. .should.equal true
  79. it "should name the exported csv file", ->
  80. @res.header
  81. .calledWith(
  82. "Content-Disposition",
  83. "attachment; filename=Group.csv")
  84. .should.equal true
  85. it "should export the correct csv", ->
  86. @res.send
  87. .calledWith("user@email.com\n")
  88. .should.equal true