AWSSDKPersistorManagerTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-dupe-keys,
  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 should = chai.should()
  17. const { expect } = chai
  18. const modulePath = '../../../app/js/AWSSDKPersistorManager.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. describe('AWSSDKPersistorManager', function() {
  21. beforeEach(function() {
  22. this.settings = {
  23. filestore: {
  24. backend: 'aws-sdk'
  25. }
  26. }
  27. this.s3 = {
  28. upload: sinon.stub(),
  29. getObject: sinon.stub(),
  30. copyObject: sinon.stub(),
  31. deleteObject: sinon.stub(),
  32. listObjects: sinon.stub(),
  33. deleteObjects: sinon.stub(),
  34. headObject: sinon.stub()
  35. }
  36. this.awssdk = { S3: sinon.stub().returns(this.s3) }
  37. this.requires = {
  38. 'aws-sdk': this.awssdk,
  39. 'settings-sharelatex': this.settings,
  40. 'logger-sharelatex': {
  41. log() {},
  42. err() {}
  43. },
  44. fs: (this.fs = { createReadStream: sinon.stub() }),
  45. './Errors': (this.Errors = { NotFoundError: sinon.stub() })
  46. }
  47. this.key = 'my/key'
  48. this.bucketName = 'my-bucket'
  49. this.error = 'my error'
  50. return (this.AWSSDKPersistorManager = SandboxedModule.require(modulePath, {
  51. requires: this.requires
  52. }))
  53. })
  54. describe('sendFile', function() {
  55. beforeEach(function() {
  56. this.stream = {}
  57. this.fsPath = '/usr/local/some/file'
  58. return this.fs.createReadStream.returns(this.stream)
  59. })
  60. it('should put the file with s3.upload', function(done) {
  61. this.s3.upload.callsArgWith(1)
  62. return this.AWSSDKPersistorManager.sendFile(
  63. this.bucketName,
  64. this.key,
  65. this.fsPath,
  66. err => {
  67. expect(err).to.not.be.ok
  68. expect(this.s3.upload.calledOnce, 'called only once').to.be.true
  69. expect(
  70. this.s3.upload.calledWith({
  71. Bucket: this.bucketName,
  72. Key: this.key,
  73. Body: this.stream
  74. }),
  75. 'called with correct arguments'
  76. ).to.be.true
  77. return done()
  78. }
  79. )
  80. })
  81. return it('should dispatch the error from s3.upload', function(done) {
  82. this.s3.upload.callsArgWith(1, this.error)
  83. return this.AWSSDKPersistorManager.sendFile(
  84. this.bucketName,
  85. this.key,
  86. this.fsPath,
  87. err => {
  88. expect(err).to.equal(this.error)
  89. return done()
  90. }
  91. )
  92. })
  93. })
  94. describe('sendStream', function() {
  95. beforeEach(function() {
  96. return (this.stream = {})
  97. })
  98. it('should put the file with s3.upload', function(done) {
  99. this.s3.upload.callsArgWith(1)
  100. return this.AWSSDKPersistorManager.sendStream(
  101. this.bucketName,
  102. this.key,
  103. this.stream,
  104. err => {
  105. expect(err).to.not.be.ok
  106. expect(this.s3.upload.calledOnce, 'called only once').to.be.true
  107. expect(
  108. this.s3.upload.calledWith({
  109. Bucket: this.bucketName,
  110. Key: this.key,
  111. Body: this.stream
  112. }),
  113. 'called with correct arguments'
  114. ).to.be.true
  115. return done()
  116. }
  117. )
  118. })
  119. return it('should dispatch the error from s3.upload', function(done) {
  120. this.s3.upload.callsArgWith(1, this.error)
  121. return this.AWSSDKPersistorManager.sendStream(
  122. this.bucketName,
  123. this.key,
  124. this.stream,
  125. err => {
  126. expect(err).to.equal(this.error)
  127. return done()
  128. }
  129. )
  130. })
  131. })
  132. describe('getFileStream', function() {
  133. beforeEach(function() {
  134. this.opts = {}
  135. this.stream = {}
  136. this.read_stream = { on: (this.read_stream_on = sinon.stub()) }
  137. this.object = { createReadStream: sinon.stub().returns(this.read_stream) }
  138. return this.s3.getObject.returns(this.object)
  139. })
  140. it('should return a stream from s3.getObject', function(done) {
  141. this.read_stream_on.withArgs('readable').callsArgWith(1)
  142. return this.AWSSDKPersistorManager.getFileStream(
  143. this.bucketName,
  144. this.key,
  145. this.opts,
  146. (err, stream) => {
  147. expect(this.read_stream_on.calledTwice)
  148. expect(err).to.not.be.ok
  149. expect(stream, 'returned the stream').to.equal(this.read_stream)
  150. expect(
  151. this.s3.getObject.calledWith({
  152. Bucket: this.bucketName,
  153. Key: this.key
  154. }),
  155. 'called with correct arguments'
  156. ).to.be.true
  157. return done()
  158. }
  159. )
  160. })
  161. describe('with start and end options', function() {
  162. beforeEach(function() {
  163. return (this.opts = {
  164. start: 0,
  165. end: 8
  166. })
  167. })
  168. return it('should pass headers to the s3.GetObject', function(done) {
  169. this.read_stream_on.withArgs('readable').callsArgWith(1)
  170. this.AWSSDKPersistorManager.getFileStream(
  171. this.bucketName,
  172. this.key,
  173. this.opts,
  174. (err, stream) => {
  175. return expect(
  176. this.s3.getObject.calledWith({
  177. Bucket: this.bucketName,
  178. Key: this.key,
  179. Range: 'bytes=0-8'
  180. }),
  181. 'called with correct arguments'
  182. ).to.be.true
  183. }
  184. )
  185. return done()
  186. })
  187. })
  188. return describe('error conditions', function() {
  189. describe("when the file doesn't exist", function() {
  190. beforeEach(function() {
  191. this.error = new Error()
  192. return (this.error.code = 'NoSuchKey')
  193. })
  194. return it('should produce a NotFoundError', function(done) {
  195. this.read_stream_on.withArgs('error').callsArgWith(1, this.error)
  196. return this.AWSSDKPersistorManager.getFileStream(
  197. this.bucketName,
  198. this.key,
  199. this.opts,
  200. (err, stream) => {
  201. expect(stream).to.not.be.ok
  202. expect(err).to.be.ok
  203. expect(
  204. err instanceof this.Errors.NotFoundError,
  205. 'error is a correct instance'
  206. ).to.equal(true)
  207. return done()
  208. }
  209. )
  210. })
  211. })
  212. return describe('when there is some other error', function() {
  213. beforeEach(function() {
  214. return (this.error = new Error())
  215. })
  216. return it('should dispatch the error from s3 object stream', function(done) {
  217. this.read_stream_on.withArgs('error').callsArgWith(1, this.error)
  218. return this.AWSSDKPersistorManager.getFileStream(
  219. this.bucketName,
  220. this.key,
  221. this.opts,
  222. (err, stream) => {
  223. expect(stream).to.not.be.ok
  224. expect(err).to.be.ok
  225. expect(err).to.equal(this.error)
  226. return done()
  227. }
  228. )
  229. })
  230. })
  231. })
  232. })
  233. describe('copyFile', function() {
  234. beforeEach(function() {
  235. this.destKey = 'some/key'
  236. return (this.stream = {})
  237. })
  238. it('should copy the file with s3.copyObject', function(done) {
  239. this.s3.copyObject.callsArgWith(1)
  240. return this.AWSSDKPersistorManager.copyFile(
  241. this.bucketName,
  242. this.key,
  243. this.destKey,
  244. err => {
  245. expect(err).to.not.be.ok
  246. expect(this.s3.copyObject.calledOnce, 'called only once').to.be.true
  247. expect(
  248. this.s3.copyObject.calledWith({
  249. Bucket: this.bucketName,
  250. Key: this.destKey,
  251. CopySource: this.bucketName + '/' + this.key
  252. }),
  253. 'called with correct arguments'
  254. ).to.be.true
  255. return done()
  256. }
  257. )
  258. })
  259. return it('should dispatch the error from s3.copyObject', function(done) {
  260. this.s3.copyObject.callsArgWith(1, this.error)
  261. return this.AWSSDKPersistorManager.copyFile(
  262. this.bucketName,
  263. this.key,
  264. this.destKey,
  265. err => {
  266. expect(err).to.equal(this.error)
  267. return done()
  268. }
  269. )
  270. })
  271. })
  272. describe('deleteFile', function() {
  273. it('should delete the file with s3.deleteObject', function(done) {
  274. this.s3.deleteObject.callsArgWith(1)
  275. return this.AWSSDKPersistorManager.deleteFile(
  276. this.bucketName,
  277. this.key,
  278. err => {
  279. expect(err).to.not.be.ok
  280. expect(this.s3.deleteObject.calledOnce, 'called only once').to.be.true
  281. expect(
  282. this.s3.deleteObject.calledWith({
  283. Bucket: this.bucketName,
  284. Key: this.key
  285. }),
  286. 'called with correct arguments'
  287. ).to.be.true
  288. return done()
  289. }
  290. )
  291. })
  292. return it('should dispatch the error from s3.deleteObject', function(done) {
  293. this.s3.deleteObject.callsArgWith(1, this.error)
  294. return this.AWSSDKPersistorManager.deleteFile(
  295. this.bucketName,
  296. this.key,
  297. err => {
  298. expect(err).to.equal(this.error)
  299. return done()
  300. }
  301. )
  302. })
  303. })
  304. describe('deleteDirectory', function() {
  305. it('should list the directory content using s3.listObjects', function(done) {
  306. this.s3.listObjects.callsArgWith(1, null, { Contents: [] })
  307. return this.AWSSDKPersistorManager.deleteDirectory(
  308. this.bucketName,
  309. this.key,
  310. err => {
  311. expect(err).to.not.be.ok
  312. expect(this.s3.listObjects.calledOnce, 'called only once').to.be.true
  313. expect(
  314. this.s3.listObjects.calledWith({
  315. Bucket: this.bucketName,
  316. Prefix: this.key
  317. }),
  318. 'called with correct arguments'
  319. ).to.be.true
  320. return done()
  321. }
  322. )
  323. })
  324. it('should dispatch the error from s3.listObjects', function(done) {
  325. this.s3.listObjects.callsArgWith(1, this.error)
  326. return this.AWSSDKPersistorManager.deleteDirectory(
  327. this.bucketName,
  328. this.key,
  329. err => {
  330. expect(err).to.equal(this.error)
  331. return done()
  332. }
  333. )
  334. })
  335. return describe('with directory content', function() {
  336. beforeEach(function() {
  337. return (this.fileList = [{ Key: 'foo' }, { Key: 'bar', Key: 'baz' }])
  338. })
  339. it('should forward the file keys to s3.deleteObjects', function(done) {
  340. this.s3.listObjects.callsArgWith(1, null, { Contents: this.fileList })
  341. this.s3.deleteObjects.callsArgWith(1)
  342. return this.AWSSDKPersistorManager.deleteDirectory(
  343. this.bucketName,
  344. this.key,
  345. err => {
  346. expect(err).to.not.be.ok
  347. expect(this.s3.deleteObjects.calledOnce, 'called only once').to.be
  348. .true
  349. expect(
  350. this.s3.deleteObjects.calledWith({
  351. Bucket: this.bucketName,
  352. Delete: {
  353. Quiet: true,
  354. Objects: this.fileList
  355. }
  356. }),
  357. 'called with correct arguments'
  358. ).to.be.true
  359. return done()
  360. }
  361. )
  362. })
  363. return it('should dispatch the error from s3.deleteObjects', function(done) {
  364. this.s3.listObjects.callsArgWith(1, null, { Contents: this.fileList })
  365. this.s3.deleteObjects.callsArgWith(1, this.error)
  366. return this.AWSSDKPersistorManager.deleteDirectory(
  367. this.bucketName,
  368. this.key,
  369. err => {
  370. expect(err).to.equal(this.error)
  371. return done()
  372. }
  373. )
  374. })
  375. })
  376. })
  377. describe('checkIfFileExists', function() {
  378. it('should check for the file with s3.headObject', function(done) {
  379. this.s3.headObject.callsArgWith(1, null, {})
  380. return this.AWSSDKPersistorManager.checkIfFileExists(
  381. this.bucketName,
  382. this.key,
  383. (err, exists) => {
  384. expect(err).to.not.be.ok
  385. expect(this.s3.headObject.calledOnce, 'called only once').to.be.true
  386. expect(
  387. this.s3.headObject.calledWith({
  388. Bucket: this.bucketName,
  389. Key: this.key
  390. }),
  391. 'called with correct arguments'
  392. ).to.be.true
  393. return done()
  394. }
  395. )
  396. })
  397. it('should return false on an inexistant file', function(done) {
  398. this.s3.headObject.callsArgWith(1, null, {})
  399. return this.AWSSDKPersistorManager.checkIfFileExists(
  400. this.bucketName,
  401. this.key,
  402. (err, exists) => {
  403. expect(exists).to.be.false
  404. return done()
  405. }
  406. )
  407. })
  408. it('should return true on an existing file', function(done) {
  409. this.s3.headObject.callsArgWith(1, null, { ETag: 'etag' })
  410. return this.AWSSDKPersistorManager.checkIfFileExists(
  411. this.bucketName,
  412. this.key,
  413. (err, exists) => {
  414. expect(exists).to.be.true
  415. return done()
  416. }
  417. )
  418. })
  419. return it('should dispatch the error from s3.headObject', function(done) {
  420. this.s3.headObject.callsArgWith(1, this.error)
  421. return this.AWSSDKPersistorManager.checkIfFileExists(
  422. this.bucketName,
  423. this.key,
  424. (err, exists) => {
  425. expect(err).to.equal(this.error)
  426. return done()
  427. }
  428. )
  429. })
  430. })
  431. return describe('directorySize', function() {
  432. it('should list the directory content using s3.listObjects', function(done) {
  433. this.s3.listObjects.callsArgWith(1, null, { Contents: [] })
  434. return this.AWSSDKPersistorManager.directorySize(
  435. this.bucketName,
  436. this.key,
  437. err => {
  438. expect(err).to.not.be.ok
  439. expect(this.s3.listObjects.calledOnce, 'called only once').to.be.true
  440. expect(
  441. this.s3.listObjects.calledWith({
  442. Bucket: this.bucketName,
  443. Prefix: this.key
  444. }),
  445. 'called with correct arguments'
  446. ).to.be.true
  447. return done()
  448. }
  449. )
  450. })
  451. it('should dispatch the error from s3.listObjects', function(done) {
  452. this.s3.listObjects.callsArgWith(1, this.error)
  453. return this.AWSSDKPersistorManager.directorySize(
  454. this.bucketName,
  455. this.key,
  456. err => {
  457. expect(err).to.equal(this.error)
  458. return done()
  459. }
  460. )
  461. })
  462. return it('should sum directory files sizes', function(done) {
  463. this.s3.listObjects.callsArgWith(1, null, {
  464. Contents: [{ Size: 1024 }, { Size: 2048 }]
  465. })
  466. return this.AWSSDKPersistorManager.directorySize(
  467. this.bucketName,
  468. this.key,
  469. (err, size) => {
  470. expect(size).to.equal(3072)
  471. return done()
  472. }
  473. )
  474. })
  475. })
  476. })