ClsiCookieManagerTests.coffee 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "redis-sharelatex" :
  37. createClient: =>
  38. @redis
  39. "settings-sharelatex": @settings
  40. "request": @request
  41. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
  42. @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
  43. describe "getServerId", ->
  44. it "should call get for the key", (done)->
  45. @redis.get.callsArgWith(1, null, "clsi-7")
  46. @ClsiCookieManager._getServerId @project_id, (err, serverId)=>
  47. @redis.get.calledWith("clsiserver:#{@project_id}").should.equal true
  48. serverId.should.equal "clsi-7"
  49. done()
  50. it "should _populateServerIdViaRequest if no key is found", (done)->
  51. @ClsiCookieManager._populateServerIdViaRequest = sinon.stub().callsArgWith(1)
  52. @redis.get.callsArgWith(1, null)
  53. @ClsiCookieManager._getServerId @project_id, (err, serverId)=>
  54. @ClsiCookieManager._populateServerIdViaRequest.calledWith(@project_id).should.equal true
  55. done()
  56. describe "_populateServerIdViaRequest", ->
  57. beforeEach ->
  58. @response = "some data"
  59. @request.get.callsArgWith(1, null, @response)
  60. @ClsiCookieManager.setServerId = sinon.stub().callsArgWith(2, null, "clsi-9")
  61. it "should make a request to the clsi", (done)->
  62. @ClsiCookieManager._populateServerIdViaRequest @project_id, (err, serverId)=>
  63. args = @ClsiCookieManager.setServerId.args[0]
  64. args[0].should.equal @project_id
  65. args[1].should.deep.equal @response
  66. done()
  67. it "should return the server id", (done)->
  68. @ClsiCookieManager._populateServerIdViaRequest @project_id, (err, serverId)=>
  69. serverId.should.equal "clsi-9"
  70. done()
  71. describe "setServerId", ->
  72. beforeEach ->
  73. @response = "dsadsakj"
  74. @ClsiCookieManager._parseServerIdFromResponse = sinon.stub().returns("clsi-8")
  75. @redisMulti.exec.callsArgWith(0)
  76. it "should set the server id with a ttl", (done)->
  77. @ClsiCookieManager.setServerId @project_id, @response, (err)=>
  78. @redisMulti.set.calledWith("clsiserver:#{@project_id}", "clsi-8").should.equal true
  79. @redisMulti.expire.calledWith("clsiserver:#{@project_id}", @settings.clsiCookie.ttl).should.equal true
  80. done()
  81. it "should return the server id", (done)->
  82. @ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
  83. serverId.should.equal "clsi-8"
  84. done()
  85. it "should not set the server id if clsiCookies are not enabled", (done)->
  86. delete @settings.clsiCookie.key
  87. @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
  88. @ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
  89. @redisMulti.exec.called.should.equal false
  90. done()
  91. describe "getCookieJar", ->
  92. beforeEach ->
  93. @ClsiCookieManager._getServerId = sinon.stub().callsArgWith(1, null, "clsi-11")
  94. it "should return a jar with the cookie set populated from redis", (done)->
  95. @ClsiCookieManager.getCookieJar @project_id, (err, jar)=>
  96. jar._jar.store.idx["clsi.example.com"]["/"][@settings.clsiCookie.key].key.should.equal
  97. jar._jar.store.idx["clsi.example.com"]["/"][@settings.clsiCookie.key].value.should.equal "clsi-11"
  98. done()
  99. it "should return empty cookie jar if clsiCookies are not enabled", (done)->
  100. delete @settings.clsiCookie.key
  101. @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
  102. @ClsiCookieManager.getCookieJar @project_id, (err, jar)->
  103. assert.deepEqual jar, realRequst.jar()
  104. done()