Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 522158 - Creating and deleting workspaces is returning the wrong …
…status codes
  • Loading branch information
mrennie committed Sep 11, 2017
1 parent 41c1e53 commit a8bb056
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
4 changes: 3 additions & 1 deletion modules/orionode/lib/metastore/fs/store.js
Expand Up @@ -259,7 +259,9 @@ Object.assign(FsMetastore.prototype, {
return reject(error);
}
if (!metadata) {
return reject(new Error(String("could not read workspace").concat(workspaceId)));
var err = new Error(String("could not read workspace: ").concat(workspaceId));
err.code = 404;
return reject(err);
}
var workspace = {
"id": metadata.UniqueId,
Expand Down
29 changes: 25 additions & 4 deletions modules/orionode/lib/metastore/util/metaUtil.js
Expand Up @@ -19,18 +19,39 @@ module.exports.encodeWorkspaceId = function (userId, workspaceName) {

module.exports.decodeUserIdFromWorkspaceId = function (workspaceId) {
var index = workspaceId.lastIndexOf(SEPARATOR);
if (index === -1) return null;
if (index === -1) {
return null;
}
return workspaceId.substring(0, index);
};

var decodeWorkspaceNameFromWorkspaceId = module.exports.decodeWorkspaceNameFromWorkspaceId = function (workspaceId) {
var index = workspaceId.lastIndexOf(SEPARATOR);
if (index === -1) return null;
if (index === -1) {
return null;
}
return workspaceId.substring(index + 1);
};

var getWorkspacePath = module.exports.getWorkspacePath = function(workspaceDir, workspaceId, userId){
return [workspaceDir, userId.substring(0,2), userId, decodeWorkspaceNameFromWorkspaceId(workspaceId)];
/**
* Tries to compose the workspace path from the workspace directory, the current workspaces ID, and the current
* user.
* @param {string} workspaceDir The path to the workspace directory
* @param {string} workspaceId The identifier of the workspace
* @param {string} userId The ID of the current user
* @returns {string[]} The array of segments to join to make the workspace path
*/
var getWorkspacePath = module.exports.getWorkspacePath = function(workspaceDir, workspaceId, userId) {
var segments = [workspaceDir];
if(typeof userId === 'string' && userId.length > 0) {
segments.push(userId.substring(0,2));
segments.push(userId);
}
var wid = decodeWorkspaceNameFromWorkspaceId(workspaceId);
if(wid) {
segments.push(wid);
}
return segments;
};

module.exports.createWorkspaceDir = function (workspaceDir, userId, workspaceId, callback) {
Expand Down
17 changes: 9 additions & 8 deletions modules/orionode/lib/workspace.js
Expand Up @@ -86,17 +86,18 @@ module.exports = function(options) {
Name: userId,
UserName: req.user.fullname || userId
};

return metaUtil.getWorkspaceMeta(req.user.workspaces, store, workspaceRoot)
.then(function(workspaceInfos){
workspaceJson.Workspaces = workspaceInfos || [];
return api.writeResponse(null, res, null, workspaceJson, true);
});
.then(function(workspaceInfos) {
workspaceJson.Workspaces = Array.isArray(workspaceInfos) ? workspaceInfos : [];
return api.writeResponse(null, res, null, workspaceJson, true);
}, function reject(err) {
return api.writeError(err.code || 500, res, err.message);
});
}
var workspaceId = rest;
store.getWorkspace(workspaceId, function(err, workspace) {
if (err) {
return writeError(400, res, err);
return writeError(err.code || 400, res, err);
}
if (!workspace) {
return writeError(404, res, "Workspace not found: " + rest);
Expand All @@ -121,7 +122,7 @@ module.exports = function(options) {
return writeError(singleUser ? 403 : 400, res, err);
}
getWorkspaceJson(req, workspace).then(function(workspaceJson) {
return api.writeResponse(null, res, null, workspaceJson, true);
return api.writeResponse(201, res, null, workspaceJson, true);
}).catch(function(err) {
api.writeResponse(400, res, null, err);
});
Expand Down Expand Up @@ -191,7 +192,7 @@ module.exports = function(options) {
if (err) {
return writeError(400, res, err);
}
return writeResponse(200, res);
return writeResponse(204, res);
});
});
}
Expand Down
9 changes: 6 additions & 3 deletions modules/orionode/test/metastore/test-simple.js
Expand Up @@ -210,6 +210,8 @@ describe("Orion metastore", function() {
it("testCreateSecondWorkspace", function(done) {
request()
.post(PREFIX)
.type('json')
.send({Name: 'Orion sandbox'})
.set('Slug', 'Orion sandbox')
.expect(201)
.end(function(err, res) {
Expand Down Expand Up @@ -259,7 +261,7 @@ describe("Orion metastore", function() {
var pLoc = res.body.Location;
request()
.delete(pLoc)
.expect(200)
.expect(204)
.end(function(err, res) {
testHelper.throwIfError(err);
request()
Expand All @@ -277,12 +279,13 @@ describe("Orion metastore", function() {
.expect(201)
.end(function(err, res) {
testHelper.throwIfError(err);
testHelper.withWorkspace(request, PREFIX, res.body.Name)
testHelper.withWorkspace(request, PREFIX, res.body.Id)
.end(function(err, res) {
testHelper.throwIfError(err);
var wLoc = res.body.Location;
request()
.delete(wLoc)
.expect(200)
.expect(204)
.end(function(err, res) {
testHelper.throwIfError(err);
request()
Expand Down
2 changes: 1 addition & 1 deletion modules/orionode/test/support/test_data.js
Expand Up @@ -58,7 +58,7 @@ exports.setUpWorkspace = function setUpWorkspace(wsDir, metastore, done) {
request()
.post(CONTEXT_PATH + '/workspace')
.set('Slug', 'Orion Content')
.expect(200)
.expect(201)
.end(done);
};

Expand Down

0 comments on commit a8bb056

Please sign in to comment.