ClsiCookieManagerTests.coffee 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. assert = chai.assert
  4. should = chai.should()
  5. expect = chai.expect
  6. modulePath = "../../../../app/js/Features/Compile/ClsiCookieManager.js"
  7. SandboxedModule = require('sandboxed-module')
  8. realRequst = require("request")
  9. describe "ClsiCookieManager", ->
  10. beforeEach ->
  11. self = @
  12. @redisMulti =
  13. set:sinon.stub()
  14. get:sinon.stub()
  15. expire:sinon.stub()
  16. exec:sinon.stub()
  17. @redis =
  18. auth:->
  19. get:sinon.stub()
  20. multi: -> return self.redisMulti
  21. @project_id = "123423431321"
  22. @request =
  23. get: sinon.stub()
  24. cookie:realRequst.cookie
  25. jar: realRequst.jar
  26. @settings =
  27. redis:
  28. web:"redis.something"
  29. apis:
  30. clsi:
  31. url: "http://clsi.example.com"
  32. clsiCookie:
  33. ttl:Math.random()
  34. key: "coooookie"
  35. @requires =
  36. "../../infrastructure/RedisWrapper": @RedisWrapper =
  37. client: => @redis
  38. "settings-sharelatex": @settings
  39. "request": @request
  40. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
  41. @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
  42. describe "getServerId", ->
  43. it "should call get for the key", (done)->
  44. @redis.get.callsArgWith(1, null, "clsi-7")
  45. @ClsiCookieManager._getServerId @project_id, (err, serverId)=>
  46. @redis.get.calledWith("clsiserver:#{@project_id}").should.equal true
  47. serverId.should.equal "clsi-7"
  48. done()
  49. it "should _populateServerIdViaRequest if no key is found", (done)->
  50. @ClsiCookieManager._populateServerIdViaRequest = sinon.stub().callsArgWith(1)
  51. @redis.get.callsArgWith(1, null)
  52. @ClsiCookieManager._getServerId @project_id, (err, serverId)=>
  53. @ClsiCookieManager._populateServerIdViaRequest.calledWith(@project_id).should.equal true
  54. done()
  55. describe "_populateServerIdViaRequest", ->
  56. beforeEach ->
  57. @response = "some data"
  58. @request.get.callsArgWith(1, null, @response)
  59. @ClsiCookieManager.setServerId = sinon.stub().callsArgWith(2, null, "clsi-9")
  60. it "should make a request to the clsi", (done)->
  61. @ClsiCookieManager._populateServerIdViaRequest @project_id, (err, serverId)=>
  62. args = @ClsiCookieManager.setServerId.args[0]
  63. args[0].should.equal @project_id
  64. args[1].should.deep.equal @response
  65. done()
  66. it "should return the server id", (done)->
  67. @ClsiCookieManager._populateServerIdViaRequest @project_id, (err, serverId)=>
  68. serverId.should.equal "clsi-9"
  69. done()
  70. describe "setServerId", ->
  71. beforeEach ->
  72. @response = "dsadsakj"
  73. @ClsiCookieManager._parseServerIdFromResponse = sinon.stub().returns("clsi-8")
  74. @redisMulti.exec.callsArgWith(0)
  75. it "should set the server id with a ttl", (done)->
  76. @ClsiCookieManager.setServerId @project_id, @response, (err)=>
  77. @redisMulti.set.calledWith("clsiserver:#{@project_id}", "clsi-8").should.equal true
  78. @redisMulti.expire.calledWith("clsiserver:#{@project_id}", @settings.clsiCookie.ttl).should.equal true
  79. done()
  80. it "should return the server id", (done)->
  81. @ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
  82. serverId.should.equal "clsi-8"
  83. done()
  84. it "should not set the server id if clsiCookies are not enabled", (done)->
  85. delete @settings.clsiCookie.key
  86. @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
  87. @ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
  88. @redisMulti.exec.called.should.equal false
  89. done()
  90. it "should also set in the secondary if secondary redis is enabled", (done) ->
  91. @redisSecondaryMulti =
  92. set:sinon.stub()
  93. expire:sinon.stub()
  94. exec:sinon.stub()
  95. @redis_secondary =
  96. multi: => @redisSecondaryMulti
  97. @settings.redis.clsi_cookie_secondary = {}
  98. @RedisWrapper.client = sinon.stub()
  99. @RedisWrapper.client.withArgs("clsi_cookie").returns(@redis)
  100. @RedisWrapper.client.withArgs("clsi_cookie_secondary").returns(@redis_secondary)
  101. @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
  102. @ClsiCookieManager._parseServerIdFromResponse = sinon.stub().returns("clsi-8")
  103. @ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
  104. @redisSecondaryMulti.set.calledWith("clsiserver:#{@project_id}", "clsi-8").should.equal true
  105. @redisSecondaryMulti.expire.calledWith("clsiserver:#{@project_id}", @settings.clsiCookie.ttl).should.equal true
  106. done()
  107. describe "getCookieJar", ->
  108. beforeEach ->
  109. @ClsiCookieManager._getServerId = sinon.stub().callsArgWith(1, null, "clsi-11")
  110. it "should return a jar with the cookie set populated from redis", (done)->
  111. @ClsiCookieManager.getCookieJar @project_id, (err, jar)=>
  112. jar._jar.store.idx["clsi.example.com"]["/"][@settings.clsiCookie.key].key.should.equal
  113. jar._jar.store.idx["clsi.example.com"]["/"][@settings.clsiCookie.key].value.should.equal "clsi-11"
  114. done()
  115. it "should return empty cookie jar if clsiCookies are not enabled", (done)->
  116. delete @settings.clsiCookie.key
  117. @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
  118. @ClsiCookieManager.getCookieJar @project_id, (err, jar)->
  119. assert.deepEqual jar, realRequst.jar()
  120. done()