Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
Merge pull request #32 from adobe/dangoor/duplicate-title
Browse files Browse the repository at this point in the history
Checks for duplicate titles.
  • Loading branch information
dangoor committed Apr 10, 2013
2 parents 1ea31a6 + 1019040 commit 5dce9e6
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
39 changes: 38 additions & 1 deletion lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var Errors = {
BAD_VERSION: "BAD_VERSION",
VALIDATION_FAILED: "VALIDATION_FAILED",
REGISTRY_NOT_LOADED: "REGISTRY_NOT_LOADED",
DUPLICATE_TITLE: "DUPLICATE_TITLE",

// These failures do not need to be localized. They are only displayed to people
// running the server.
Expand All @@ -72,6 +73,33 @@ function validConfiguration(callback) {
return true;
}

/**
* Checks the registry to see if any other extensions with the same
* title are present (other than the extension named).
* Returns false if the title is falsy.
*
* @param {String} name of the extension that is currently being validated
* @param {String} title of that extension
* @return true if the title is already present in another extension
*/
function titleAlreadyPresent(name, title) {
if (!title) {
return false;
}

title = title.toLowerCase();

var key;
for (key in registry) {
if (registry.hasOwnProperty(key) && key !== name &&
registry[key].metadata.title &&
registry[key].metadata.title.toLowerCase() === title) {
return true;
}
}
return false;
}

/**
* Adds or updates a package in the repository.
*
Expand All @@ -97,15 +125,24 @@ function addPackage(packagePath, userID, callback) {
return;
}

var error;

if (result.errors && result.errors.length) {
var error = new Error(Errors.VALIDATION_FAILED);
error = new Error(Errors.VALIDATION_FAILED);
error.errors = result.errors;
callback(error, null);
return;
}

var name = result.metadata.name;

if (titleAlreadyPresent(name, result.metadata.title)) {
error = new Error(Errors.VALIDATION_FAILED);
error.errors = [[Errors.DUPLICATE_TITLE, result.metadata.title]];
callback(error, null);
return;
}

// Look up the current repository entry to see if this is an add or update
var entry, updateRegistry;

Expand Down
1 change: 1 addition & 0 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var _stringMap = {
"INVALID_VERSION_NUMBER" : "The package version number ({0}) is invalid.",
"MISSING_MAIN" : "The package has no main.js file.",
"MISSING_PACKAGE_JSON" : "The package has no package.json file.",
"DUPLICATE_TITLE" : "Another extension with the title {{0}} already exists.",

// Unknown error
"UNKNOWN_ERROR" : "An error occurred, but unfortunately I don't know how to say this nicely:"
Expand Down
41 changes: 41 additions & 0 deletions spec/repository.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,45 @@ describe("Repository", function () {
});
});
});

it("should not allow two packages with the same title, even from the same owner", function (done) {
setValidationResult({
metadata: {
name: "anotherpkg",
version: "2.1.1"
}
});

repository.addPackage("nopackage.zip", username, function (err, entry) {
expect(err).toBeNull();

setValidationResult({
metadata: {
name: "superawesome",
title: "Super Awesome!",
description: "It's awesome.",
version: "1.0.0"
}
});
repository.addPackage("nopackage.zip", username, function (err, entry) {
expect(err).toBeNull();
setValidationResult({
metadata: {
name: "super-awesome",
title: "Super awesome!",
description: "It's awesomer.",
version: "1.0.0"
}
});

repository.addPackage("nopackage.zip", username, function (err, entry) {
expect(err).not.toBeNull();
expect(err.message).toEqual("VALIDATION_FAILED");
expect(err.errors.length).toEqual(1);
expect(err.errors[0][0]).toEqual("DUPLICATE_TITLE");
done();
});
});
});
});
});

0 comments on commit 5dce9e6

Please sign in to comment.