ClsiCookieManagerTests.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* eslint-disable
  2. n/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. ttlInSeconds: Math.random().toString(),
  48. ttlInSecondsRegular: Math.random().toString(),
  49. key: 'coooookie',
  50. },
  51. }
  52. this.requires = {
  53. '../../infrastructure/RedisWrapper': (this.RedisWrapper = {
  54. client: () => this.redis,
  55. }),
  56. '@overleaf/settings': this.settings,
  57. request: this.request,
  58. }
  59. return (this.ClsiCookieManager = SandboxedModule.require(modulePath, {
  60. requires: this.requires,
  61. })())
  62. })
  63. describe('getServerId', function () {
  64. it('should call get for the key', function (done) {
  65. this.redis.get.callsArgWith(1, null, 'clsi-7')
  66. return this.ClsiCookieManager._getServerId(
  67. this.project_id,
  68. this.user_id,
  69. '',
  70. (err, serverId) => {
  71. this.redis.get
  72. .calledWith(`clsiserver:${this.project_id}:${this.user_id}`)
  73. .should.equal(true)
  74. serverId.should.equal('clsi-7')
  75. return done()
  76. }
  77. )
  78. })
  79. it('should _populateServerIdViaRequest if no key is found', function (done) {
  80. this.ClsiCookieManager._populateServerIdViaRequest = sinon
  81. .stub()
  82. .yields(null)
  83. this.redis.get.callsArgWith(1, null)
  84. return this.ClsiCookieManager._getServerId(
  85. this.project_id,
  86. this.user_id,
  87. '',
  88. (err, serverId) => {
  89. this.ClsiCookieManager._populateServerIdViaRequest
  90. .calledWith(this.project_id, this.user_id)
  91. .should.equal(true)
  92. return done()
  93. }
  94. )
  95. })
  96. it('should _populateServerIdViaRequest if no key is blank', function (done) {
  97. this.ClsiCookieManager._populateServerIdViaRequest = sinon
  98. .stub()
  99. .yields(null)
  100. this.redis.get.callsArgWith(1, null, '')
  101. return this.ClsiCookieManager._getServerId(
  102. this.project_id,
  103. this.user_id,
  104. '',
  105. (err, serverId) => {
  106. this.ClsiCookieManager._populateServerIdViaRequest
  107. .calledWith(this.project_id, this.user_id)
  108. .should.equal(true)
  109. return done()
  110. }
  111. )
  112. })
  113. })
  114. describe('_populateServerIdViaRequest', function () {
  115. beforeEach(function () {
  116. this.response = 'some data'
  117. this.request.post.callsArgWith(1, null, this.response)
  118. return (this.ClsiCookieManager.setServerId = sinon
  119. .stub()
  120. .yields(null, 'clsi-9'))
  121. })
  122. it('should make a request to the clsi', function (done) {
  123. return this.ClsiCookieManager._populateServerIdViaRequest(
  124. this.project_id,
  125. this.user_id,
  126. 'standard',
  127. (err, serverId) => {
  128. const args = this.ClsiCookieManager.setServerId.args[0]
  129. args[0].should.equal(this.project_id)
  130. args[1].should.equal(this.user_id)
  131. args[2].should.equal('standard')
  132. args[3].should.deep.equal(this.response)
  133. return done()
  134. }
  135. )
  136. })
  137. it('should return the server id', function (done) {
  138. return this.ClsiCookieManager._populateServerIdViaRequest(
  139. this.project_id,
  140. this.user_id,
  141. '',
  142. (err, serverId) => {
  143. serverId.should.equal('clsi-9')
  144. return done()
  145. }
  146. )
  147. })
  148. })
  149. describe('setServerId', function () {
  150. beforeEach(function () {
  151. this.response = 'dsadsakj'
  152. this.ClsiCookieManager._parseServerIdFromResponse = sinon
  153. .stub()
  154. .returns('clsi-8')
  155. })
  156. it('should set the server id with a ttl', function (done) {
  157. return this.ClsiCookieManager.setServerId(
  158. this.project_id,
  159. this.user_id,
  160. 'standard',
  161. this.response,
  162. null,
  163. err => {
  164. this.redis.setex
  165. .calledWith(
  166. `clsiserver:${this.project_id}:${this.user_id}`,
  167. this.settings.clsiCookie.ttlInSeconds,
  168. 'clsi-8'
  169. )
  170. .should.equal(true)
  171. return done()
  172. }
  173. )
  174. })
  175. it('should set the server id with the regular ttl for reg instance', function (done) {
  176. this.ClsiCookieManager._parseServerIdFromResponse = sinon
  177. .stub()
  178. .returns('clsi-reg-8')
  179. this.ClsiCookieManager.setServerId(
  180. this.project_id,
  181. this.user_id,
  182. 'standard',
  183. this.response,
  184. null,
  185. err => {
  186. expect(this.redis.setex).to.have.been.calledWith(
  187. `clsiserver:${this.project_id}:${this.user_id}`,
  188. this.settings.clsiCookie.ttlInSecondsRegular,
  189. 'clsi-reg-8'
  190. )
  191. done()
  192. }
  193. )
  194. })
  195. it('should return the server id', function (done) {
  196. return this.ClsiCookieManager.setServerId(
  197. this.project_id,
  198. this.user_id,
  199. 'standard',
  200. this.response,
  201. null,
  202. (err, serverId) => {
  203. serverId.should.equal('clsi-8')
  204. return done()
  205. }
  206. )
  207. })
  208. it('should not set the server id if clsiCookies are not enabled', function (done) {
  209. delete this.settings.clsiCookie.key
  210. this.ClsiCookieManager = SandboxedModule.require(modulePath, {
  211. globals: {
  212. console,
  213. },
  214. requires: this.requires,
  215. })()
  216. return this.ClsiCookieManager.setServerId(
  217. this.project_id,
  218. this.user_id,
  219. 'standard',
  220. this.response,
  221. null,
  222. (err, serverId) => {
  223. this.redis.setex.called.should.equal(false)
  224. return done()
  225. }
  226. )
  227. })
  228. it('should not set the server id there is no server id in the response', function (done) {
  229. this.ClsiCookieManager._parseServerIdFromResponse = sinon
  230. .stub()
  231. .returns(null)
  232. return this.ClsiCookieManager.setServerId(
  233. this.project_id,
  234. this.user_id,
  235. 'standard',
  236. this.response,
  237. null,
  238. (err, serverId) => {
  239. this.redis.setex.called.should.equal(false)
  240. return done()
  241. }
  242. )
  243. })
  244. it('should also set in the secondary if secondary redis is enabled', function (done) {
  245. this.redis_secondary = { setex: sinon.stub().callsArg(3) }
  246. this.settings.redis.clsi_cookie_secondary = {}
  247. this.RedisWrapper.client = sinon.stub()
  248. this.RedisWrapper.client.withArgs('clsi_cookie').returns(this.redis)
  249. this.RedisWrapper.client
  250. .withArgs('clsi_cookie_secondary')
  251. .returns(this.redis_secondary)
  252. this.ClsiCookieManager = SandboxedModule.require(modulePath, {
  253. globals: {
  254. console,
  255. },
  256. requires: this.requires,
  257. })()
  258. this.ClsiCookieManager._parseServerIdFromResponse = sinon
  259. .stub()
  260. .returns('clsi-8')
  261. return this.ClsiCookieManager.setServerId(
  262. this.project_id,
  263. this.user_id,
  264. 'standard',
  265. this.response,
  266. null,
  267. (err, serverId) => {
  268. this.redis_secondary.setex
  269. .calledWith(
  270. `clsiserver:${this.project_id}:${this.user_id}`,
  271. this.settings.clsiCookie.ttlInSeconds,
  272. 'clsi-8'
  273. )
  274. .should.equal(true)
  275. return done()
  276. }
  277. )
  278. })
  279. })
  280. describe('getCookieJar', function () {
  281. beforeEach(function () {
  282. return (this.ClsiCookieManager._getServerId = sinon
  283. .stub()
  284. .yields(null, 'clsi-11'))
  285. })
  286. it('should return a jar with the cookie set populated from redis', function (done) {
  287. return this.ClsiCookieManager.getCookieJar(
  288. this.project_id,
  289. this.user_id,
  290. '',
  291. (err, jar) => {
  292. jar._jar.store.idx['clsi.example.com']['/'][
  293. this.settings.clsiCookie.key
  294. ].key.should.equal
  295. jar._jar.store.idx['clsi.example.com']['/'][
  296. this.settings.clsiCookie.key
  297. ].value.should.equal('clsi-11')
  298. return done()
  299. }
  300. )
  301. })
  302. it('should return empty cookie jar if clsiCookies are not enabled', function (done) {
  303. delete this.settings.clsiCookie.key
  304. this.ClsiCookieManager = SandboxedModule.require(modulePath, {
  305. globals: {
  306. console,
  307. },
  308. requires: this.requires,
  309. })()
  310. return this.ClsiCookieManager.getCookieJar(
  311. this.project_id,
  312. this.user_id,
  313. '',
  314. (err, jar) => {
  315. assert.deepEqual(jar, realRequst.jar())
  316. return done()
  317. }
  318. )
  319. })
  320. })
  321. })