SubscriptionDomainHandlerTests.coffee 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. sinon = require('sinon')
  6. modulePath = path.join __dirname, "../../../../app/js/Features/Subscription/SubscriptionDomainHandler"
  7. expect = require("chai").expect
  8. describe "SubscriptionDomainHandler", ->
  9. beforeEach ->
  10. @adminUser_id = 12345
  11. @otherAdminUser_id = 32131231234
  12. @ThirdOtherAdminUser_id = 33424324
  13. @settings =
  14. domainLicences: [
  15. {domains:["uni.edu", "student.uni.edu"], adminUser_id:@adminUser_id}
  16. {domains:["student.myuni.com", "teacher.myuni.com"], adminUser_id:@otherAdminUser_id}
  17. {domains:["highcools.site"], adminUser_id:@ThirdOtherAdminUser_id}
  18. ]
  19. @SubscriptionGroupHandler =
  20. addUserToGroup: sinon.stub().callsArg(2)
  21. @SubscriptionDomainHandler = SandboxedModule.require modulePath, requires:
  22. "settings-sharelatex":@settings
  23. "logger-sharelatex": log:->
  24. "./SubscriptionGroupHandler": @SubscriptionGroupHandler
  25. describe "_findDomainLicence", ->
  26. it "should find the domain", (done)->
  27. licence = @SubscriptionDomainHandler._findDomainLicence "bob@uni.edu"
  28. licence.adminUser_id.should.equal @adminUser_id
  29. done()
  30. it "should find the if email is subdomain", (done)->
  31. licence = @SubscriptionDomainHandler._findDomainLicence "bob@somewherelse.highcools.site"
  32. licence.adminUser_id.should.equal @ThirdOtherAdminUser_id
  33. done()
  34. it "should find one of the other emails in the domain list", (done)->
  35. licence = @SubscriptionDomainHandler._findDomainLicence "sally@teacher.myuni.com"
  36. licence.adminUser_id.should.equal @otherAdminUser_id
  37. done()
  38. it "should return undefined if no licence matches even if end of email is same", (done)->
  39. licence = @SubscriptionDomainHandler._findDomainLicence "bob@someotherhighcools.site"
  40. expect(licence).to.not.exist
  41. done(licence)
  42. it "should return undefined if no licence matches", (done)->
  43. licence = @SubscriptionDomainHandler._findDomainLicence "bob@other.edu"
  44. expect(licence).to.not.exist
  45. done(licence)
  46. describe "autoAllocate", ->
  47. beforeEach ->
  48. @email = "bob@somewhere.com"
  49. @SubscriptionDomainHandler._findDomainLicence = sinon.stub()
  50. it "should call the SubscriptionGroupHandler if there is licence", (done)->
  51. @SubscriptionDomainHandler._findDomainLicence.returns(@settings.domainLicences[0])
  52. @SubscriptionDomainHandler.autoAllocate {email:@email}, (err)=>
  53. @SubscriptionGroupHandler.addUserToGroup.calledWith(@adminUser_id, @email).should.equal true
  54. done()
  55. it "should not call the SubscriptionGroupHandler if there is no licence", (done)->
  56. @SubscriptionDomainHandler._findDomainLicence.returns()
  57. @SubscriptionDomainHandler.autoAllocate {email:@email}, (err)=>
  58. @SubscriptionGroupHandler.addUserToGroup.called.should.equal false
  59. done()