SendingFileTest.coffee 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. assert = require("chai").assert
  2. sinon = require('sinon')
  3. chai = require('chai')
  4. should = chai.should()
  5. expect = chai.expect
  6. modulePath = "../../../app/js/LocalFileWriter.js"
  7. SandboxedModule = require('sandboxed-module')
  8. fs = require("fs")
  9. request = require("request")
  10. settings = require("settings-sharelatex")
  11. describe "Filestore", ->
  12. before (done)->
  13. @localFileReadPath = "/tmp/filestore_acceptence_tests_file_read.txt"
  14. @localFileWritePath = "/tmp/filestore_acceptence_tests_file_write.txt"
  15. @constantFileContent = [
  16. "hello world"
  17. "line 2 goes here #{Math.random()}"
  18. "there are 3 lines in all"
  19. ].join("\n")
  20. fs.writeFile(@localFileReadPath, @constantFileContent, done)
  21. @filestoreUrl = "http://localhost:#{settings.internal.filestore.port}"
  22. beforeEach (done)->
  23. fs.unlink @localFileWritePath, =>
  24. done()
  25. it "should send a 200 for status endpoing", (done)->
  26. request "#{@filestoreUrl}/status", (err, response, body)->
  27. response.statusCode.should.equal 200
  28. body.indexOf("filestore").should.not.equal -1
  29. body.indexOf("up").should.not.equal -1
  30. done()
  31. describe "with a file on the server", ->
  32. beforeEach (done)->
  33. @timeout(1000 * 10)
  34. @file_id = Math.random()
  35. @fileUrl = "#{@filestoreUrl}/project/acceptence_tests/file/#{@file_id}"
  36. writeStream = request.post(@fileUrl)
  37. writeStream.on "end", done
  38. fs.createReadStream(@localFileReadPath).pipe writeStream
  39. it "should be able get the file back", (done)->
  40. @timeout(1000 * 10)
  41. request.get @fileUrl, (err, response, body)=>
  42. body.should.equal @constantFileContent
  43. done()
  44. it "should be able to delete the file", (done)->
  45. @timeout(1000 * 20)
  46. request.del @fileUrl, (err, response, body)=>
  47. response.statusCode.should.equal 204
  48. request.get @fileUrl, (err, response, body)=>
  49. body.indexOf("NoSuchKey").should.not.equal -1
  50. done()
  51. it "should be able to copy files", (done)->
  52. @timeout(1000 * 20)
  53. newProjectID = "acceptence_tests_copyied_project"
  54. newFileId = Math.random()
  55. newFileUrl = "#{@filestoreUrl}/project/#{newProjectID}/file/#{newFileId}"
  56. opts =
  57. method: 'put'
  58. uri: newFileUrl
  59. json:
  60. source:
  61. project_id:"acceptence_tests"
  62. file_id: @file_id
  63. request opts, (err, response, body)=>
  64. response.statusCode.should.equal 200
  65. request.del @fileUrl, (err, response, body)=>
  66. response.statusCode.should.equal 204
  67. request.get newFileUrl, (err, response, body)=>
  68. body.should.equal @constantFileContent
  69. done()