SubscriptionGroupControllerTests.coffee 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "Subscription Group Controller", ->
  8. beforeEach ->
  9. @user = {_id:"!@312431",email:"user@email.com"}
  10. @subscription = {}
  11. @GroupHandler =
  12. addUserToGroup: sinon.stub().callsArgWith(2, null, @user)
  13. removeUserFromGroup: sinon.stub().callsArgWith(2)
  14. getPopulatedListOfMembers: sinon.stub().callsArgWith(1, null, [@user])
  15. @SubscriptionLocator = getUsersSubscription: sinon.stub().callsArgWith(1, null, @subscription)
  16. @Controller = SandboxedModule.require modulePath, requires:
  17. "./SubscriptionGroupHandler":@GroupHandler
  18. "logger-sharelatex": log:->
  19. "./SubscriptionLocator": @SubscriptionLocator
  20. @adminUserId = "123jlkj"
  21. @req =
  22. session:
  23. user: _id: @adminUserId
  24. describe "addUserToGroup", ->
  25. it "should use the admin id for the logged in user and take the email address from the body", (done)->
  26. newEmail = "31231"
  27. @req.body = email: newEmail
  28. res =
  29. json : (data)=>
  30. @GroupHandler.addUserToGroup.calledWith(@adminUserId, newEmail).should.equal true
  31. data.user.should.deep.equal @user
  32. done()
  33. @Controller.addUserToGroup @req, res
  34. describe "removeUserFromGroup", ->
  35. it "should use the admin id for the logged in user and take the email address from the body", (done)->
  36. userIdToRemove = "31231"
  37. @req.params = user_id: userIdToRemove
  38. res =
  39. send : =>
  40. @GroupHandler.removeUserFromGroup.calledWith(@adminUserId, userIdToRemove).should.equal true
  41. done()
  42. @Controller.removeUserFromGroup @req, res
  43. describe "renderSubscriptionGroupAdminPage", ->
  44. it "should redirect you if you don't have a group account", (done)->
  45. @subscription.groupPlan = false
  46. res =
  47. redirect : (path)=>
  48. path.should.equal("/")
  49. done()
  50. @Controller.renderSubscriptionGroupAdminPage @req, res
  51. describe "exportGroupCsv", ->
  52. beforeEach ->
  53. @subscription.groupPlan = true
  54. @res = new MockResponse()
  55. @res.contentType = sinon.stub()
  56. @res.header = sinon.stub()
  57. @res.send = sinon.stub()
  58. @Controller.exportGroupCsv @req, @res
  59. it "should set the correct content type on the request", ->
  60. @res.contentType
  61. .calledWith("text/csv")
  62. .should.equal true
  63. it "should name the exported csv file", ->
  64. @res.header
  65. .calledWith(
  66. "Content-Disposition",
  67. "attachment; filename=Group.csv")
  68. .should.equal true
  69. it "should export the correct csv", ->
  70. @res.send
  71. .calledWith("user@email.com\n")
  72. .should.equal true