ClsiCookieManagerTests.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* eslint-disable
  2. node/handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const { assert, expect } = require('chai')
  16. const modulePath = '../../../../app/src/Features/Compile/ClsiCookieManager.js'
  17. const SandboxedModule = require('sandboxed-module')
  18. const realRequst = require('request')
  19. describe('ClsiCookieManager', function () {
  20. beforeEach(function () {
  21. const self = this
  22. this.redis = {
  23. auth() {},
  24. get: sinon.stub(),
  25. setex: sinon.stub().callsArg(3),
  26. }
  27. this.project_id = '123423431321-proj-id'
  28. this.user_id = 'abc-user-id'
  29. this.request = {
  30. post: sinon.stub(),
  31. cookie: realRequst.cookie,
  32. jar: realRequst.jar,
  33. defaults: () => {
  34. return this.request
  35. },
  36. }
  37. this.settings = {
  38. redis: {
  39. web: 'redis.something',
  40. },
  41. apis: {
  42. clsi: {
  43. url: 'http://clsi.example.com',
  44. },
  45. },
  46. clsiCookie: {
  47. ttl: Math.random(),
  48. key: 'coooookie',
  49. },
  50. }
  51. this.requires = {
  52. '../../infrastructure/RedisWrapper': (this.RedisWrapper = {
  53. client: () => this.redis,
  54. }),
  55. '@overleaf/settings': this.settings,
  56. request: this.request,
  57. }
  58. return (this.ClsiCookieManager = SandboxedModule.require(modulePath, {
  59. requires: this.requires,
  60. })())
  61. })
  62. describe('getServerId', function () {
  63. it('should call get for the key', function (done) {
  64. this.redis.get.callsArgWith(1, null, 'clsi-7')
  65. return this.ClsiCookieManager._getServerId(
  66. this.project_id,
  67. this.user_id,
  68. (err, serverId) => {
  69. this.redis.get
  70. .calledWith(`clsiserver:${this.project_id}:${this.user_id}`)
  71. .should.equal(true)
  72. serverId.should.equal('clsi-7')
  73. return done()
  74. }
  75. )
  76. })
  77. it('should _populateServerIdViaRequest if no key is found', function (done) {
  78. this.ClsiCookieManager._populateServerIdViaRequest = sinon
  79. .stub()
  80. .callsArgWith(2)
  81. this.redis.get.callsArgWith(1, null)
  82. return this.ClsiCookieManager._getServerId(
  83. this.project_id,
  84. this.user_id,
  85. (err, serverId) => {
  86. this.ClsiCookieManager._populateServerIdViaRequest
  87. .calledWith(this.project_id, this.user_id)
  88. .should.equal(true)
  89. return done()
  90. }
  91. )
  92. })
  93. it('should _populateServerIdViaRequest if no key is blank', function (done) {
  94. this.ClsiCookieManager._populateServerIdViaRequest = sinon
  95. .stub()
  96. .callsArgWith(2)
  97. this.redis.get.callsArgWith(1, null, '')
  98. return this.ClsiCookieManager._getServerId(
  99. this.project_id,
  100. this.user_id,
  101. (err, serverId) => {
  102. this.ClsiCookieManager._populateServerIdViaRequest
  103. .calledWith(this.project_id, this.user_id)
  104. .should.equal(true)
  105. return done()
  106. }
  107. )
  108. })
  109. })
  110. describe('_populateServerIdViaRequest', function () {
  111. beforeEach(function () {
  112. this.response = 'some data'
  113. this.request.post.callsArgWith(1, null, this.response)
  114. return (this.ClsiCookieManager.setServerId = sinon
  115. .stub()
  116. .callsArgWith(3, null, 'clsi-9'))
  117. })
  118. it('should make a request to the clsi', function (done) {
  119. return this.ClsiCookieManager._populateServerIdViaRequest(
  120. this.project_id,
  121. this.user_id,
  122. (err, serverId) => {
  123. const args = this.ClsiCookieManager.setServerId.args[0]
  124. args[0].should.equal(this.project_id)
  125. args[1].should.equal(this.user_id)
  126. args[2].should.deep.equal(this.response)
  127. return done()
  128. }
  129. )
  130. })
  131. it('should return the server id', function (done) {
  132. return this.ClsiCookieManager._populateServerIdViaRequest(
  133. this.project_id,
  134. this.user_id,
  135. (err, serverId) => {
  136. serverId.should.equal('clsi-9')
  137. return done()
  138. }
  139. )
  140. })
  141. })
  142. describe('setServerId', function () {
  143. beforeEach(function () {
  144. this.response = 'dsadsakj'
  145. this.ClsiCookieManager._parseServerIdFromResponse = sinon
  146. .stub()
  147. .returns('clsi-8')
  148. })
  149. it('should set the server id with a ttl', function (done) {
  150. return this.ClsiCookieManager.setServerId(
  151. this.project_id,
  152. this.user_id,
  153. this.response,
  154. err => {
  155. this.redis.setex
  156. .calledWith(
  157. `clsiserver:${this.project_id}:${this.user_id}`,
  158. this.settings.clsiCookie.ttl,
  159. 'clsi-8'
  160. )
  161. .should.equal(true)
  162. return done()
  163. }
  164. )
  165. })
  166. it('should return the server id', function (done) {
  167. return this.ClsiCookieManager.setServerId(
  168. this.project_id,
  169. this.user_id,
  170. this.response,
  171. (err, serverId) => {
  172. serverId.should.equal('clsi-8')
  173. return done()
  174. }
  175. )
  176. })
  177. it('should not set the server id if clsiCookies are not enabled', function (done) {
  178. delete this.settings.clsiCookie.key
  179. this.ClsiCookieManager = SandboxedModule.require(modulePath, {
  180. globals: {
  181. console: console,
  182. },
  183. requires: this.requires,
  184. })()
  185. return this.ClsiCookieManager.setServerId(
  186. this.project_id,
  187. this.user_id,
  188. this.response,
  189. (err, serverId) => {
  190. this.redis.setex.called.should.equal(false)
  191. return done()
  192. }
  193. )
  194. })
  195. it('should not set the server id there is no server id in the response', function (done) {
  196. this.ClsiCookieManager._parseServerIdFromResponse = sinon
  197. .stub()
  198. .returns(null)
  199. return this.ClsiCookieManager.setServerId(
  200. this.project_id,
  201. this.user_id,
  202. this.response,
  203. (err, serverId) => {
  204. this.redis.setex.called.should.equal(false)
  205. return done()
  206. }
  207. )
  208. })
  209. it('should also set in the secondary if secondary redis is enabled', function (done) {
  210. this.redis_secondary = { setex: sinon.stub().callsArg(3) }
  211. this.settings.redis.clsi_cookie_secondary = {}
  212. this.RedisWrapper.client = sinon.stub()
  213. this.RedisWrapper.client.withArgs('clsi_cookie').returns(this.redis)
  214. this.RedisWrapper.client
  215. .withArgs('clsi_cookie_secondary')
  216. .returns(this.redis_secondary)
  217. this.ClsiCookieManager = SandboxedModule.require(modulePath, {
  218. globals: {
  219. console: console,
  220. },
  221. requires: this.requires,
  222. })()
  223. this.ClsiCookieManager._parseServerIdFromResponse = sinon
  224. .stub()
  225. .returns('clsi-8')
  226. return this.ClsiCookieManager.setServerId(
  227. this.project_id,
  228. this.user_id,
  229. this.response,
  230. (err, serverId) => {
  231. this.redis_secondary.setex
  232. .calledWith(
  233. `clsiserver:${this.project_id}:${this.user_id}`,
  234. this.settings.clsiCookie.ttl,
  235. 'clsi-8'
  236. )
  237. .should.equal(true)
  238. return done()
  239. }
  240. )
  241. })
  242. })
  243. describe('getCookieJar', function () {
  244. beforeEach(function () {
  245. return (this.ClsiCookieManager._getServerId = sinon
  246. .stub()
  247. .callsArgWith(2, null, 'clsi-11'))
  248. })
  249. it('should return a jar with the cookie set populated from redis', function (done) {
  250. return this.ClsiCookieManager.getCookieJar(
  251. this.project_id,
  252. this.user_id,
  253. (err, jar) => {
  254. jar._jar.store.idx['clsi.example.com']['/'][
  255. this.settings.clsiCookie.key
  256. ].key.should.equal
  257. jar._jar.store.idx['clsi.example.com']['/'][
  258. this.settings.clsiCookie.key
  259. ].value.should.equal('clsi-11')
  260. return done()
  261. }
  262. )
  263. })
  264. it('should return empty cookie jar if clsiCookies are not enabled', function (done) {
  265. delete this.settings.clsiCookie.key
  266. this.ClsiCookieManager = SandboxedModule.require(modulePath, {
  267. globals: {
  268. console: console,
  269. },
  270. requires: this.requires,
  271. })()
  272. return this.ClsiCookieManager.getCookieJar(
  273. this.project_id,
  274. this.user_id,
  275. (err, jar) => {
  276. assert.deepEqual(jar, realRequst.jar())
  277. return done()
  278. }
  279. )
  280. })
  281. })
  282. })