ClsiCookieManagerTests.coffee 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. it "should _populateServerIdViaRequest if no key is blank", (done)->
  56. @ClsiCookieManager._populateServerIdViaRequest = sinon.stub().callsArgWith(1)
  57. @redis.get.callsArgWith(1, null, "")
  58. @ClsiCookieManager._getServerId @project_id, (err, serverId)=>
  59. @ClsiCookieManager._populateServerIdViaRequest.calledWith(@project_id).should.equal true
  60. done()
  61. describe "_populateServerIdViaRequest", ->
  62. beforeEach ->
  63. @response = "some data"
  64. @request.get.callsArgWith(1, null, @response)
  65. @ClsiCookieManager.setServerId = sinon.stub().callsArgWith(2, null, "clsi-9")
  66. it "should make a request to the clsi", (done)->
  67. @ClsiCookieManager._populateServerIdViaRequest @project_id, (err, serverId)=>
  68. args = @ClsiCookieManager.setServerId.args[0]
  69. args[0].should.equal @project_id
  70. args[1].should.deep.equal @response
  71. done()
  72. it "should return the server id", (done)->
  73. @ClsiCookieManager._populateServerIdViaRequest @project_id, (err, serverId)=>
  74. serverId.should.equal "clsi-9"
  75. done()
  76. describe "setServerId", ->
  77. beforeEach ->
  78. @response = "dsadsakj"
  79. @ClsiCookieManager._parseServerIdFromResponse = sinon.stub().returns("clsi-8")
  80. @redisMulti.exec.callsArgWith(0)
  81. it "should set the server id with a ttl", (done)->
  82. @ClsiCookieManager.setServerId @project_id, @response, (err)=>
  83. @redisMulti.set.calledWith("clsiserver:#{@project_id}", "clsi-8").should.equal true
  84. @redisMulti.expire.calledWith("clsiserver:#{@project_id}", @settings.clsiCookie.ttl).should.equal true
  85. done()
  86. it "should return the server id", (done)->
  87. @ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
  88. serverId.should.equal "clsi-8"
  89. done()
  90. it "should not set the server id if clsiCookies are not enabled", (done)->
  91. delete @settings.clsiCookie.key
  92. @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
  93. @ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
  94. @redisMulti.exec.called.should.equal false
  95. done()
  96. it "should not set the server id there is no server id in the response", (done)->
  97. @ClsiCookieManager._parseServerIdFromResponse = sinon.stub().returns(null)
  98. @ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
  99. @redisMulti.exec.called.should.equal false
  100. done()
  101. it "should also set in the secondary if secondary redis is enabled", (done) ->
  102. @redisSecondaryMulti =
  103. set:sinon.stub()
  104. expire:sinon.stub()
  105. exec:sinon.stub()
  106. @redis_secondary =
  107. multi: => @redisSecondaryMulti
  108. @settings.redis.clsi_cookie_secondary = {}
  109. @RedisWrapper.client = sinon.stub()
  110. @RedisWrapper.client.withArgs("clsi_cookie").returns(@redis)
  111. @RedisWrapper.client.withArgs("clsi_cookie_secondary").returns(@redis_secondary)
  112. @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
  113. @ClsiCookieManager._parseServerIdFromResponse = sinon.stub().returns("clsi-8")
  114. @ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
  115. @redisSecondaryMulti.set.calledWith("clsiserver:#{@project_id}", "clsi-8").should.equal true
  116. @redisSecondaryMulti.expire.calledWith("clsiserver:#{@project_id}", @settings.clsiCookie.ttl).should.equal true
  117. done()
  118. describe "getCookieJar", ->
  119. beforeEach ->
  120. @ClsiCookieManager._getServerId = sinon.stub().callsArgWith(1, null, "clsi-11")
  121. it "should return a jar with the cookie set populated from redis", (done)->
  122. @ClsiCookieManager.getCookieJar @project_id, (err, jar)=>
  123. jar._jar.store.idx["clsi.example.com"]["/"][@settings.clsiCookie.key].key.should.equal
  124. jar._jar.store.idx["clsi.example.com"]["/"][@settings.clsiCookie.key].value.should.equal "clsi-11"
  125. done()
  126. it "should return empty cookie jar if clsiCookies are not enabled", (done)->
  127. delete @settings.clsiCookie.key
  128. @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
  129. @ClsiCookieManager.getCookieJar @project_id, (err, jar)->
  130. assert.deepEqual jar, realRequst.jar()
  131. done()