ClsiCookieManagerTests.js 8.8 KB

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