diff --git a/services/web/app/coffee/Features/FileStore/FileStoreHandler.coffee b/services/web/app/coffee/Features/FileStore/FileStoreHandler.coffee index a4b6453b66..5e092af75f 100644 --- a/services/web/app/coffee/Features/FileStore/FileStoreHandler.coffee +++ b/services/web/app/coffee/Features/FileStore/FileStoreHandler.coffee @@ -99,10 +99,17 @@ module.exports = FileStoreHandler = file_id:oldFile_id uri: @_buildUrl(newProject_id, newFile_id) timeout:fiveMinsInMs - request opts, (err)-> + request opts, (err, response)-> if err? logger.err err:err, oldProject_id:oldProject_id, oldFile_id:oldFile_id, newProject_id:newProject_id, newFile_id:newFile_id, "something went wrong telling filestore api to copy file" - callback(err, opts.uri) + callback(err) + else if 200 <= response.statusCode < 300 + # successful response + callback(null, opts.uri) + else + err = new Error("non-ok response from filestore for copyFile: #{response.statusCode}") + logger.err {uri: opts.uri, statusCode: response.statusCode}, "error uploading to filestore" + callback(err) _buildUrl: (project_id, file_id)-> return "#{settings.apis.filestore.url}/project/#{project_id}/file/#{file_id}" diff --git a/services/web/test/acceptance/coffee/helpers/MockFileStoreApi.coffee b/services/web/test/acceptance/coffee/helpers/MockFileStoreApi.coffee index c096e58b40..5a7d70efa9 100644 --- a/services/web/test/acceptance/coffee/helpers/MockFileStoreApi.coffee +++ b/services/web/test/acceptance/coffee/helpers/MockFileStoreApi.coffee @@ -1,4 +1,5 @@ express = require("express") +bodyParser = require "body-parser" app = express() module.exports = MockFileStoreApi = @@ -22,6 +23,18 @@ module.exports = MockFileStoreApi = { content } = @files[project_id][file_id] res.send content + # handle file copying + app.put "/project/:project_id/file/:file_id", bodyParser.json(), (req, res, next) => + {project_id, file_id} = req.params + source = req.body.source + {content} = @files[source.project_id]?[source.file_id] + if !content? + res.sendStatus 500 + else + @files[project_id] ?= {} + @files[project_id][file_id] = { content } + res.sendStatus 200 + app.listen 3009, (error) -> throw error if error? .on "error", (error) -> diff --git a/services/web/test/unit/coffee/FileStore/FileStoreHandlerTests.coffee b/services/web/test/unit/coffee/FileStore/FileStoreHandlerTests.coffee index b45b464a47..5cde818a27 100644 --- a/services/web/test/unit/coffee/FileStore/FileStoreHandlerTests.coffee +++ b/services/web/test/unit/coffee/FileStore/FileStoreHandlerTests.coffee @@ -210,7 +210,7 @@ describe "FileStoreHandler", -> @newFile_id = "new file id" it "should post json", (done)-> - @request.callsArgWith(1, null) + @request.callsArgWith(1, null, {statusCode: 200}) @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, => @request.args[0][0].method.should.equal "put" @@ -220,13 +220,13 @@ describe "FileStoreHandler", -> done() it "builds the correct url", (done)-> - @request.callsArgWith(1, null) + @request.callsArgWith(1, null, {statusCode: 200}) @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, => @handler._buildUrl.calledWith(@newProject_id, @newFile_id).should.equal true done() it "returns the url", (done)-> - @request.callsArgWith(1, null) + @request.callsArgWith(1, null, {statusCode: 200}) @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, (err, url) => url.should.equal "http://filestore.stubbedBuilder.com" done() @@ -237,3 +237,10 @@ describe "FileStoreHandler", -> @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, (err)=> err.should.equal error done() + + it "should return an error for a non-success statusCode", (done)-> + @request.callsArgWith(1, null, {statusCode: 500}) + @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, (err)=> + err.should.be.an('error') + err.message.should.equal 'non-ok response from filestore for copyFile: 500' + done() \ No newline at end of file