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

Commit

Permalink
- added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ingorichter committed Jan 15, 2014
1 parent 65c3dd6 commit 08b447c
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 22 deletions.
5 changes: 3 additions & 2 deletions downloadStats/downloadStatsUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ var tempFolder = config.tempFolder;
if (config.tempFolder) {
try {
fs.mkdirSync(tempFolder);
} catch(Exception) {
console.log('Tempfolder already exists');
} catch(e) {
// tell us what went wrong
console.error(e.toString());
}
} else {
tempFolder = new temporary.Dir().path;
Expand Down
7 changes: 6 additions & 1 deletion downloadStats/logfileProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,12 @@ LogfileProcessor.prototype = {
});

recentDownloads = _.sortBy(recentDownloads, "totalDownloads").reverse();
recentDownloadsPromise.resolve(recentDownloads);

var recentDownloadsWithMetadata = {};
recentDownloadsWithMetadata["startDate"] = new Date(sevenDaysAgo);
recentDownloadsWithMetadata["endDate"] = new Date();
recentDownloadsWithMetadata["mostDownloadedExtensions"] = recentDownloads;
recentDownloadsPromise.resolve(recentDownloadsWithMetadata);
});
});

Expand Down
9 changes: 8 additions & 1 deletion lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function addDownloadDataToPackage(name, version, newDownloads) {
if (registry[name]) {
var updated = false;

logger.debug("Extension package with name %s found", name);
logger.debug("Extension package with name " + name + " found");

var packageVersions = registry[name].versions;
packageVersions.forEach(function (versionInfo) {
Expand Down Expand Up @@ -135,6 +135,12 @@ function addDownloadDataToPackage(name, version, newDownloads) {
}
}
}

function updateRecentDownloads(recentDownloadData) {
registry["recentDownloads"] = recentDownloadData;
storage.saveRegistry(registry);
}

/**
* Adds or updates a package in the repository.
*
Expand Down Expand Up @@ -276,3 +282,4 @@ exports.addPackage = addPackage;
exports.configure = configure;
exports.getRegistry = getRegistry;
exports.addDownloadDataToPackage = addDownloadDataToPackage;
exports.updateRecentDownloads = updateRecentDownloads;
10 changes: 9 additions & 1 deletion lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,18 @@ function _stats(req, res) {
// read the uploaded JSON data
// NOTE: I don't have a good idea about the size limitation
var obj = JSON.parse(fs.readFileSync(req.files.file.path));

var recentDownloads = obj.recentDownloads;
if (obj.recentDownloads) {
delete obj.recentDownloads;
}

repository.updateRecentDownloads(recentDownloads);

_.each(obj, function(n, item) {
_.each(obj[item].downloads, function (versions) {
// iterate over all updatable versions
_.each(_.keys(versions), function (version) {
_.each(Object.keys(versions), function (version) {
repository.addDownloadDataToPackage(item, version, versions[version]);
});
});
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"preinstall": "npm install deps/*",
"start": "node app",
"test": "jasmine-node spec"
"test": "istanbul test jasmine-node spec"
},
"issues": {
"url": "http://github.com/adobe/brackets-registry/issues"
Expand Down Expand Up @@ -39,6 +39,7 @@
"devDependencies": {
"jasmine-node": "~1.11.0",
"rewire": "~1.1.3",
"fs-extra": "~0.6.4"
"fs-extra": "~0.6.4",
"istanbul": "~0.1.22"
}
}
13 changes: 11 additions & 2 deletions spec/logfileProcessor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ describe("LogfileProcessor", function () {
config["s3.bucket"] = "no_bucket_name";
});

it("Should return the information for 1 Extension", function (done) {
it("should throw an exception when configure without AWS credentials", function (done) {
try {
var logfileProcessor = new LogfileProcessor({});
} catch(e) {
expect(e.toString()).toBe("Error: Configuration error: aws.accesskey, aws.secretkey, or s3.bucket missing");
done();
}
});

it("should return the information for 1 Extension", function (done) {
var logfileProcessor = new LogfileProcessor(config);

logfileProcessor.extractDownloadStats(testLogfileDirectory + "/one-extension").then(function (downloadStats) {
Expand All @@ -50,7 +59,7 @@ describe("LogfileProcessor", function () {
});
});

it("Should return the information for 1 Extension and multiple versions", function (done) {
it("should return the information for 1 Extension and multiple versions", function (done) {
var logfileProcessor = new LogfileProcessor(config);
logfileProcessor.extractDownloadStats(testLogfileDirectory + "/one-extension-multiple-versions").then(function (downloadStats) {
expect(downloadStats["select-parent"].downloads.versions["1.0.0"]).toBe(1);
Expand Down
53 changes: 40 additions & 13 deletions spec/routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
var rewire = require("rewire"),
routes = rewire("../lib/routes"),
registry_utils = require("../lib/registry_utils"),
fs = require("fs");
fs = require("fs"),
path = require("path");

var repository = routes.__get__("repository");

Expand Down Expand Up @@ -408,19 +409,45 @@ describe("routes", function () {
expect(res.send.mostRecentCall.args[0]).toMatch(/<title><!\[CDATA\[my-extension v1\.0\.0\]\]><\/title>/);
expect(res.send.mostRecentCall.args[0]).toMatch(/<title><!\[CDATA\[another-extension v2\.0\.0\]\]><\/title>/);
});

it("should not accept post request to update the download stats other than localhost/127.0.0.1", function () {
req.ip = '10.32.1.2';
req.host = 'www.adobe.com';
_stats(req, res);
expect(res.send).toHaveBeenCalledWith(403);
});

xit("should accept post request to update the download stats from localhost/127.0.0.1", function () {
req.ip = '127.0.0.1';
req.host = 'localhost';
_stats(req, res);
expect(res.send).toHaveBeenCalledWith(201);
describe("Add download data", function () {
var repo = rewire("../lib/repository");

beforeEach(function () {
// configure repository with filestorage
var loaded = false;
repo.configure({"storage": "../lib/ramstorage.js"});
var registry = JSON.parse(fs.readFileSync(path.join(path.dirname(module.filename), "testRegistry", "registry.json")));
repo.__set__("registry", registry);
setTimeout(function () {
routes.__set__("repository", repo);
loaded = true;
}, 100);

waitsFor(function () {
return loaded;
}, "All loaded", 500);
});

it("should not accept post request to update the download stats other than localhost/127.0.0.1", function () {
req.ip = '10.32.1.2';
req.host = 'www.adobe.com';
_stats(req, res);
expect(res.send).toHaveBeenCalledWith(403);
});

it("should accept post request to update the download stats from localhost/127.0.0.1", function () {
req.ip = '127.0.0.1';
req.host = 'localhost';
req.files = {file: {path: path.join(path.dirname(module.filename), "stats/downloadStats.json")}};

_stats(req, res);

var registry = repo.getRegistry();
expect(res.send).toHaveBeenCalledWith(201);
expect(registry["snippets-extension"].versions[0].downloads).toBe(6);
expect(registry["snippets-extension"].totalDownloads).toBe(6);
});
});
});

Expand Down
Loading

0 comments on commit 08b447c

Please sign in to comment.