ClsiRequestManagerTests.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Compile/ClsiRequestManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. realRequst = require("request")
  8. describe "ClsiRequestManager", ->
  9. beforeEach ->
  10. @redisMulti =
  11. set:sinon.stub()
  12. get:sinon.stub()
  13. expire:sinon.stub()
  14. exec:sinon.stub()
  15. self = @
  16. @project_id = "123423431321"
  17. @request =
  18. get: sinon.stub()
  19. cookie:realRequst.cookie
  20. jar: realRequst.jar
  21. @ClsiRequestManager = SandboxedModule.require modulePath, requires:
  22. "redis-sharelatex" :
  23. createClient: =>
  24. auth:->
  25. multi: -> return self.redisMulti
  26. "settings-sharelatex": @settings =
  27. redis:
  28. web:"redis.something"
  29. apis:
  30. clsi:
  31. url: "http://clsi.example.com"
  32. "request": @request
  33. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
  34. describe "getServerId", ->
  35. it "should call get for the key", (done)->
  36. @redisMulti.exec.callsArgWith(0, null, ["clsi-7"])
  37. @ClsiRequestManager._getServerId @project_id, (err, serverId)=>
  38. @redisMulti.get.calledWith("clsiserver:#{@project_id}").should.equal true
  39. serverId.should.equal "clsi-7"
  40. done()
  41. it "should expire the key", (done)->
  42. @redisMulti.exec.callsArgWith(0, null, ["clsi-7"])
  43. @ClsiRequestManager._getServerId @project_id, (err, serverId)=>
  44. @redisMulti.expire.calledWith("clsiserver:#{@project_id}", 60 * 60 * 24 * 7).should.equal true
  45. done()
  46. it "should _getServerIdViaRequest if no key is found", (done)->
  47. @ClsiRequestManager._getServerIdViaRequest = sinon.stub().callsArgWith(1)
  48. @redisMulti.exec.callsArgWith(0, null, [])
  49. @ClsiRequestManager._getServerId @project_id, (err, serverId)=>
  50. @ClsiRequestManager._getServerIdViaRequest.calledWith(@project_id).should.equal true
  51. done()
  52. describe "_getServerIdViaRequest", ->
  53. it "should make a request to the clsi", (done)->
  54. response = "some data"
  55. @request.get.callsArgWith(1, null, response)
  56. @ClsiRequestManager.setServerId = sinon.stub().callsArgWith(2)
  57. @ClsiRequestManager._getServerIdViaRequest @project_id, (err, serverId)=>
  58. args = @ClsiRequestManager.setServerId.args[0]
  59. args[0].should.equal @project_id
  60. args[1].should.deep.equal response
  61. done()
  62. describe "setServerId", ->
  63. it "should set the server id with a ttl", (done)->
  64. @ClsiRequestManager._parseServerIdFromResponse = sinon.stub().returns("clsi-8")
  65. response = "dsadsakj"
  66. @redisMulti.exec.callsArgWith(0)
  67. @ClsiRequestManager.setServerId @project_id, response, (err)=>
  68. @redisMulti.set.calledWith("clsiserver:#{@project_id}", "clsi-8").should.equal true
  69. @redisMulti.expire.calledWith("clsiserver:#{@project_id}", 60 * 60 * 24 * 7).should.equal true
  70. done()
  71. describe "getCookieJar", ->
  72. it "should return a jar with the cookie set populated from redis", (done)->
  73. @ClsiRequestManager._getServerId = sinon.stub().callsArgWith(1, null, "clsi-11")
  74. opts = {}
  75. @ClsiRequestManager.getCookieJar @project_id, opts, (err, jar)->
  76. jar._jar.store.idx["clsi.example.com"]["/"].clsiserver.key.should.equal "clsiserver"
  77. jar._jar.store.idx["clsi.example.com"]["/"].clsiserver.value.should.equal "clsi-11"
  78. done()
  79. # describe "_parseServerIdFromResponse", ->
  80. # it "take the cookie from the response", (done)->
  81. # a.should.equal