Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 521135 - Doing an over-writing move is returning the wrong status…
… code
  • Loading branch information
mrennie committed Sep 12, 2017
1 parent d1a3fec commit e14ba77
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 77 deletions.
13 changes: 6 additions & 7 deletions modules/orionode/lib/fileUtil.js
Expand Up @@ -465,11 +465,11 @@ exports.handleFilePOST = function(workspaceRoot, fileRoot, req, res, destFile, m
.then(function(stats) {
if (isCopy) {
return copy(sourceFile.path, destFile.path)
.then(function(result) {
var eventData = { type: "rename", isDir: stats.isDirectory(), file: destFile, sourceFile: sourceFile };
exports.fireFileModificationEvent(eventData);
return result;
});
.then(function(result) {
var eventData = { type: "rename", isDir: stats.isDirectory(), file: destFile, sourceFile: sourceFile };
exports.fireFileModificationEvent(eventData);
return result;
});
} else {
return fs.renameAsync(sourceFile.path, destFile.path)
.then(function(result) {
Expand All @@ -487,7 +487,7 @@ exports.handleFilePOST = function(workspaceRoot, fileRoot, req, res, destFile, m
.then(writeResponse.bind(null, destExists))
.catch(function(err) {
if (err.code === 'ENOENT') {
return api.writeError(err.code || 404, res, 'File not found:' + sourceUrl);
return api.writeError(typeof err.code === 'number' || 404, res, 'File not found:' + sourceUrl);
}
return api.writeError(err.code || 500, res, err);
});
Expand All @@ -504,7 +504,6 @@ exports.handleFilePOST = function(workspaceRoot, fileRoot, req, res, destFile, m
} else {
var eventData = { type: "write", file: destFile };
exports.fireFileModificationEvent(eventData);

return fs.writeFileAsync(destFile.path, '');
}

Expand Down
96 changes: 48 additions & 48 deletions modules/orionode/lib/metastore/fs/store.js
Expand Up @@ -602,56 +602,56 @@ Object.assign(FsMetastore.prototype, {
if (error) {
reject(new Error("createRenameDeleteProject failed to read workspace metadata for: " + workspaceId, error));
}
var projectJson = {
"OrionVersion": metadata.OrionVersion,
"UniqueId": projectInfo.projectName,
"WorkspaceId": workspaceId,
"FullName": projectInfo.projectName,
"Properties": {}
};
if (projectInfo.projectName){
if (projectInfo.contentLocation.startsWith(this._options.workspaceDir)) {
projectJson["ContentLocation"] = SERVERWORKSPACE + projectInfo.contentLocation.substr(this._options.workspaceDir.length);
var projectJson = {
"OrionVersion": metadata.OrionVersion,
"UniqueId": projectInfo.projectName,
"WorkspaceId": workspaceId,
"FullName": projectInfo.projectName,
"Properties": {}
};
if (projectInfo.projectName){
if (projectInfo.contentLocation.startsWith(this._options.workspaceDir)) {
projectJson["ContentLocation"] = SERVERWORKSPACE + projectInfo.contentLocation.substr(this._options.workspaceDir.length);
} else {
projectJson["ContentLocation"] = projectInfo.contentLocation;
}
this._createProjectMetadata(workspaceId, projectInfo.projectName, projectJson, function(error) {
if (error) {
return reject(error);
}
if (projectInfo.originalPath) { // originalPath is in the format of "[ContextPath] (optional) + /file + [workspaceId] + /[originalName]/"
deleteProjectMetafile.call(this);
}
if(metadata) {
metadata.ProjectNames.indexOf(projectInfo.projectName) === -1 && metadata.ProjectNames.push(projectInfo.projectName);
this._updateWorkspaceMetadata(workspaceId, metadata, function(error) {
if (error) {
reject(new Error("createRenameDeleteProject failed to write workspace metadata for: " + workspaceId, error));
}
resolve();
});
}
}.bind(this));
} else if (projectInfo.originalPath) {
deleteProjectMetafile.call(this);
this._updateWorkspaceMetadata(workspaceId, metadata, function(error) {
if (error) {
reject(new Error("createRenameDeleteProject failed to write workspace metadata for: " + workspaceId, error));
}
resolve();
});
} else {
projectJson["ContentLocation"] = projectInfo.contentLocation;
resolve(); // This shouldn't happen
}
function deleteProjectMetafile(){
// Delete the old project metadata json file, and update workspace metadata Projectnames
var segs = projectInfo.originalPath.split("/");
var oldProjectName = projectInfo.originalPath.endsWith("/") ? segs[segs.length - 2] : segs[segs.length - 1];
var index = metadata && metadata.ProjectNames.indexOf(oldProjectName) || -1;
index !== -1 && metadata.ProjectNames.splice(index, 1);
var metaFile = getProjectMetadataFileName(this._options, workspaceId, oldProjectName);
fs.unlinkAsync(metaFile).catchReturn({ code: 'ENOENT' }, null);
}
this._createProjectMetadata(workspaceId, projectInfo.projectName, projectJson, function(error) {
if (error) {
return reject(error);
}
if (projectInfo.originalPath) { // originalPath is in the format of "[ContextPath] (optional) + /file + [workspaceId] + /[originalName]/"
deleteProjectMetafile.call(this);
}
if(metadata) {
metadata.ProjectNames.indexOf(projectInfo.projectName) === -1 && metadata.ProjectNames.push(projectInfo.projectName);
this._updateWorkspaceMetadata(workspaceId, metadata, function(error) {
if (error) {
reject(new Error("createRenameDeleteProject failed to write workspace metadata for: " + workspaceId, error));
}
resolve();
});
}
}.bind(this));
} else if (projectInfo.originalPath) {
deleteProjectMetafile.call(this);
this._updateWorkspaceMetadata(workspaceId, metadata, function(error) {
if (error) {
reject(new Error("createRenameDeleteProject failed to write workspace metadata for: " + workspaceId, error));
}
resolve();
});
} else {
resolve(); // This shouldn't happen
}
function deleteProjectMetafile(){
// Delete the old project metadata json file, and update workspace metadata Projectnames
var segs = projectInfo.originalPath.split("/");
var oldProjectName = projectInfo.originalPath.endsWith("/") ? segs[segs.length - 2] : segs[segs.length - 1];
var index = metadata && metadata.ProjectNames.indexOf(oldProjectName) || -1;
index !== -1 && metadata.ProjectNames.splice(index, 1);
var metaFile = getProjectMetadataFileName(this._options, workspaceId, oldProjectName);
fs.unlinkAsync(metaFile).catchReturn({ code: 'ENOENT' }, null);
}
}.bind(this));
}.bind(this));
}.bind(this));
Expand Down
3 changes: 3 additions & 0 deletions modules/orionode/lib/workspace.js
Expand Up @@ -116,6 +116,9 @@ module.exports = function(options) {
var userId = req.user.username;
logAccess(userId);
var workspaceName = req.body && req.body.Name || fileUtil.decodeSlug(req.headers.slug);
if(typeof workspaceName !== 'string') {
return writeError(400, res, "No Name or Slug provided");
}
workspaceId = req.body && req.body.Id;
store.createWorkspace(userId, {name: workspaceName, id: workspaceId}, function(err, workspace) {
if (err) {
Expand Down
22 changes: 20 additions & 2 deletions modules/orionode/test/endpoints/test-file.js
Expand Up @@ -67,7 +67,7 @@ BufStream.prototype.data = function() {
* Unit test for the file REST API.
* see http://wiki.eclipse.org/Orion/Server_API/File_API
*/
describe('File API', function() {
describe('File endpoint', function() {
beforeEach(function(done) { // testData.setUp.bind(null, parentDir)
testData.setUp(WORKSPACE, function(){
testData.setUpWorkspace(WORKSPACE, MEATASTORE, done);
Expand Down Expand Up @@ -1165,7 +1165,25 @@ describe('File API', function() {
});
});
});
it("testCopyFileOverwrite");
it("testCopyFileOverwrite", function(done) {
request()
.post(PREFIX + '/project/my%20folder') //create a file will will overwrite
.set('Slug', 'fizz2.txt')
.expect(201)
.end(function(err, res) {
testHelper.throwIfError(err);
request()
.post(PREFIX + '/project/my%20folder')
.set('Slug', 'fizz2.txt')
.set('X-Create-Options', 'copy,overwrite')
.send({ Location: PREFIX + '/project/fizz.txt' })
.expect(200) //spec'd to return 200 of over-writing move
.end(function(err, res) {
throwIfError(err);
done();
});
});
});
it('copy a file', function(done) {
request()
.post(PREFIX + '/project/my%20folder')
Expand Down
74 changes: 55 additions & 19 deletions modules/orionode/test/endpoints/test-workspace.js
Expand Up @@ -60,7 +60,6 @@ function withDefaultWorkspace(callback) {
}

describe("Workspace endpoint", function() {

beforeEach(function(done) { // testData.setUp.bind(null, parentDir)
testData.setUp(WORKSPACE, function(){
testData.setUpWorkspace(WORKSPACE, MEATASTORE, done);
Expand All @@ -87,7 +86,15 @@ describe("Workspace endpoint", function() {
});
});
it.skip("testEncodedProjectContentLocation");
it.skip("testGetWorkspaceContentLocation");
it("testGetWorkspaceContentLocation", function(done){
testHelper.withWorkspace(request, PREFIX, WORKSPACE_ID)
.end(function(err, res) {
testHelper.throwIfError(err);
assert(res.body, "There must be a response body");
assert(typeof res.body.ContentLocation === 'string', "There must be a content location for the workspace");
done();
});
});
it.skip("testGetDefaultContentLocation");
it("testMoveBadRequest", function(done) {
withDefaultWorkspace(function(ws) {
Expand Down Expand Up @@ -194,7 +201,7 @@ describe("Workspace endpoint", function() {
});
});
});
it("testMoveProjectToFolder", function(done) {
it("testMoveProjectToProject", function(done) {
withDefaultWorkspace(function(ws) {
request()
.post(ws.Location)
Expand All @@ -205,32 +212,61 @@ describe("Workspace endpoint", function() {
var pLoc = res.body.Location;
request()
.post(ws.Location)
.set('Slug', 'testMoveProjectToFolderDest')
.set('Slug', 'someFolder')
.expect(201)
.end(function(err, res) {
testHelper.throwIfError(err);
request()
.post(ws.Location)
.type('json')
.set('X-Create-Options', "move")
.set('Slug', 'testMoveProjectToFolderSrc')
.send({Location: pLoc, Name: 'testMoveProjectToFolderSrc'})
.expect(201)
.end(function(err, res) {
testHelper.throwIfError(err);
//check the project is removed from the projects
withDefaultWorkspace(function(ws) {
assert(Array.isArray(ws.Projects));
assert(!ws.Projects.some(function(p) {
return p.Name === 'testMoveProjectToFolderSrc';
}));
done();
});
});
.set('Slug', 'someFolder')
.send({Location: pLoc})
.expect(500)
.end(done);
});
});
});
});
it("testMoveProjectToFolder", function(done) {
testHelper.withWorkspace(request, PREFIX, WORKSPACE_ID)
.end(function(err, res) {
testHelper.throwIfError(err);
var wLoc = res.body.Location;
request()
.post(wLoc)
.set('Slug', 'testMoveProjectToFolderSrc') // create the project to move
.expect(201)
.end(function(err, res) {
testHelper.throwIfError(err);
var pLoc = res.body.Location;
request()
.post(wLoc)
.set('Slug', 'someOtherProject') //create the other project to move to
.expect(201)
.end(function(err, res) {
testHelper.throwIfError(err);
var spLoc = res.body.Location;
request()
.post(spLoc)
.type('json')
.send({Name: 'someSubFolder', Directory: true})
.expect(201)
.end(function(err, res) {
testHelper.throwIfError(err);
request()
.post(res.body.Location)
.type('json')
.set('X-Create-Options', "move")
.set('Slug', 'someSubFolder')
.send({Location: pLoc, Name: 'testMoveProjectToFolderSrc'})
.expect(201)
.end(done);
});
});
});
});
});
it.skip("testCopyProjectNonDefaultLocation");
it.skip("testCopyFolderToProject", function(done) {
withDefaultWorkspace(function(ws) {
Expand Down Expand Up @@ -323,7 +359,7 @@ describe("Workspace endpoint", function() {
it("testCreateWorkspaceNullName", function(done) {
request()
.post(PREFIX)
.expect(403, done);
.expect(400, done);
});
it("testGetWorkspaceMetadata", function(done) {
withDefaultWorkspace(function(workspace) {
Expand Down
2 changes: 1 addition & 1 deletion modules/orionode/test/metastore/test-simple.js
Expand Up @@ -233,7 +233,7 @@ describe("Orion metastore", function() {
request()
.post(PREFIX)
.set('Slug', 'Orion ws')
.expect(400)
.expect(201)
.end(done);
});
});
Expand Down

0 comments on commit e14ba77

Please sign in to comment.