From 04cd39ae9f3b4589d2710c81924d3780f820a315 Mon Sep 17 00:00:00 2001 From: Jake Date: Mon, 9 Feb 2015 22:09:34 -0600 Subject: [PATCH] Test invalid UUIDs on all routes --- test/test.js | 329 +++++++++++++++++++++++++++------------------------ 1 file changed, 174 insertions(+), 155 deletions(-) diff --git a/test/test.js b/test/test.js index 08950f0..a451bcd 100644 --- a/test/test.js +++ b/test/test.js @@ -31,10 +31,10 @@ function getRandomInt(min, max) { } var ids = [ - uuid.toLowerCase(), - name.toLowerCase(), - uuid.toUpperCase(), - name.toUpperCase(), +uuid.toLowerCase(), +name.toLowerCase(), +uuid.toUpperCase(), +name.toUpperCase(), ]; describe("Crafatar", function() { @@ -101,7 +101,7 @@ describe("Crafatar", function() { }); }); - describe("Avatar", function() { +describe("Avatar", function() { // profile "Alex" - hoping it'll never have a skin var alex_uuid = "ec561538f3fd461daff5086b22154bce"; // profile "Steven" (Steve doesn't exist) - hoping it'll never have a skin @@ -125,54 +125,132 @@ describe("Crafatar", function() { done(); }); }); - describe("Errors", function() { - it("should time out on uuid info download", function(done) { - var original_timeout = config.http_timeout; - config.http_timeout = 1; - networking.get_profile(rid, "069a79f444e94726a5befca90e38aaf5", function(err, profile) { - assert.strictEqual(err.code, "ETIMEDOUT"); - config.http_timeout = original_timeout; - done(); - }); +describe("Errors", function() { + it("should time out on uuid info download", function(done) { + var original_timeout = config.http_timeout; + config.http_timeout = 1; + networking.get_profile(rid, "069a79f444e94726a5befca90e38aaf5", function(err, profile) { + assert.strictEqual(err.code, "ETIMEDOUT"); + config.http_timeout = original_timeout; + done(); }); - it("should time out on username info download", function(done) { - var original_timeout = config.http_timeout; - config.http_timeout = 1; - networking.get_username_url(rid, "jomo", 0, function(err, url) { - assert.strictEqual(err.code, "ETIMEDOUT"); - config.http_timeout = original_timeout; - done(); - }); + }); + it("should time out on username info download", function(done) { + var original_timeout = config.http_timeout; + config.http_timeout = 1; + networking.get_username_url(rid, "jomo", 0, function(err, url) { + assert.strictEqual(err.code, "ETIMEDOUT"); + config.http_timeout = original_timeout; + done(); }); - it("should time out on skin download", function(done) { - var original_timeout = config.http_timeout; - config.http_timeout = 1; - networking.get_from(rid, "http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", function(body, res, error) { - assert.strictEqual(error.code, "ETIMEDOUT"); - config.http_timeout = original_timeout; - done(); - }); + }); + it("should time out on skin download", function(done) { + var original_timeout = config.http_timeout; + config.http_timeout = 1; + networking.get_from(rid, "http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", function(body, res, error) { + assert.strictEqual(error.code, "ETIMEDOUT"); + config.http_timeout = original_timeout; + done(); }); - it("should not find the skin", function(done) { - assert.doesNotThrow(function() { - networking.get_from(rid, "http://textures.minecraft.net/texture/this-does-not-exist", function(img, response, err) { + }); + it("should not find the skin", function(done) { + assert.doesNotThrow(function() { + networking.get_from(rid, "http://textures.minecraft.net/texture/this-does-not-exist", function(img, response, err) { assert.strictEqual(err, null); // no error here, but it shouldn't throw exceptions done(); }); - }); }); - it("should ignore file updates on invalid files", function(done) { - assert.doesNotThrow(function() { - cache.update_timestamp(rid, "0123456789abcdef0123456789abcdef", "invalid-file.png"); - }); + }); + it("should ignore file updates on invalid files", function(done) { + assert.doesNotThrow(function() { + cache.update_timestamp(rid, "0123456789abcdef0123456789abcdef", "invalid-file.png"); + }); + done(); + }); + it("should not find the file", function(done) { + skins.open_skin(rid, 'non/existant/path', function(err, img) { + assert.notStrictEqual(err, null); done(); }); - it("should not find the file", function(done) { - skins.open_skin(rid, 'non/existant/path', function(err, img) { - assert.notStrictEqual(err, null); + }); +}); + +describe("Server", function() { + before(function(done) { + server.boot(function() { + done(); + }); + }); + + // Test the home page + it("should return a 200", function(done) { + request.get('http://localhost:3000', function(error, res, body) { + assert.equal(200, res.statusCode); done(); }); }); + + // invalid method, we only allow GET and HEAD requests + it("should return a 405", function(done) { + request.post('http://localhost:3000/avatars/Jake0oo0', function(error, res, body) { + assert.equal(405, res.statusCode); + done(); + }); + }); + + it("should return a 422 (invalid size)", function(done) { + var size = config.max_size + 1; + request.get('http://localhost:3000/avatars/Jake0oo0?size=' + size, function(error, res, body) { + assert.equal(422, res.statusCode); + done(); + }); + }); + + var locations = ["avatars", "capes", "skins", "renders/head"] + for (var l in locations) { + var location = locations[l]; + it("should return a 422 (invalid uuid " + location + ")", function(done) { + request.get('http://localhost:3000/' + location + '/thisisaninvaliduuid', function(error, res, body) { + assert.equal(422, res.statusCode); + done(); + }); + }); + } + + it("should return a 422 (invalid scale)", function(done) { + var scale = config.max_scale + 1; + request.get('http://localhost:3000/renders/head/Jake0oo0?scale=' + scale, function(error, res, body) { + assert.equal(422, res.statusCode); + done(); + }); + }); + + it("should return a 404 (default steve image)", function(done) { + request.get('http://localhost:3000/avatars/invalidjsvns?default=steve', function(error, res, body) { + assert.equal(404, res.statusCode); + done(); + }); + }); + + it("should return a 200 (default external image)", function(done) { + request.get('http://localhost:3000/avatars/invalidjsvns?default=https%3A%2F%2Fi.imgur.com%2FocJVWAc.png', function(error, res, body) { + assert.equal(200, res.statusCode); + done(); + }); + }); + + it("should return a 404 (no cape)", function(done) { + request.get('http://localhost:3000/capes/Jake0oo0', function(error, res, body) { + assert.equal(404, res.statusCode); + done(); + }); + }); + + after(function(done) { + server.close(function() { + done(); + }) + }); }); // we have to make sure that we test both a 32x64 and 64x64 skin @@ -273,120 +351,61 @@ describe("Crafatar", function() { } }); - describe("Networking: Skin", function() { - it("should not fail (uuid)", function(done) { - helpers.get_skin(rid, id, function(err, hash, img) { - assert.strictEqual(err, null); - done(); - }); - }); - }); - - describe("Networking: Render", function() { - it("should not fail (full body)", function(done) { - helpers.get_render(rid, id, 6, true, true, function(err, hash, img) { - assert.strictEqual(err, null); - done(); - }); - }); - it("should not fail (only head)", function(done) { - helpers.get_render(rid, id, 6, true, false, function(err, hash, img) { - assert.strictEqual(err, null); - done(); - }); - }); - }); - - describe("Networking: Cape", function() { - it("should not fail (possible cape)", function(done) { - helpers.get_cape(rid, id, function(err, hash, img) { - assert.strictEqual(err, null); - done(); - }); - }); - }); - - - describe("Errors", function() { - before(function() { - cache.get_redis().flushall(); - }); - - if (id_type == "uuid") { - it("uuid should be rate limited", function(done) { - networking.get_profile(rid, id, function(err, profile) { - assert.strictEqual(profile.error, "TooManyRequestsException"); - done(); - }); - }); - } else { - it("username should NOT be rate limited (username)", function(done) { - helpers.get_avatar(rid, id, false, 160, function(err, status, image) { - assert.strictEqual(err, null); - done(); - }); - }); - } - }); - })(id, id_type); - } - describe("Server", function() { - before(function(done) { - server.boot(function() { - done(); - }); - }); - - // Test the home page - it("should return a 200", function(done) { - request.get('http://localhost:3000', function(error, res, body) { - assert.equal(200, res.statusCode); - done(); - }); - }); - - // invalid method, we only allow GET and HEAD requests - it("should return a 405", function(done) { - request.post('http://localhost:3000/avatars/Jake0oo0', function(error, res, body) { - assert.equal(405, res.statusCode); - done(); - }); - }); - - it("should return a 422 (invalid scale)", function(done) { - var scale = config.max_scale + 1; - request.get('http://localhost:3000/avatars/Jake0oo0?scale=' + scale, function(error, res, body) { - assert.equal(422, res.statusCode); - done(); - }); - }); - - it("should return a 422 (invalid uuid)", function(done) { - request.get('http://localhost:3000/avatars/thisisaninvaliduuid', function(error, res, body) { - assert.equal(422, res.statusCode); - done(); - }); - }); - - it("should return a 422 (invalid size)", function(done) { - var size = config.max_size + 1; - request.get('http://localhost:3000/renders/Jake0oo0?size=' + size, function(error, res, body) { - assert.equal(422, res.statusCode); - done(); - }); - }); - - it("should return a 301 (default image)", function(done) { - request.get('http://localhost:3000/renders/invalidjsvns?def=steve', function(error, res, body) { - assert.equal(301, res.statusCode); - done(); - }); - }); - - after(function(done) { - server.close(function() { - done(); - }) +describe("Networking: Skin", function() { + it("should not fail (uuid)", function(done) { + helpers.get_skin(rid, id, function(err, hash, img) { + assert.strictEqual(err, null); + done(); }); }); +}); + +describe("Networking: Render", function() { + it("should not fail (full body)", function(done) { + helpers.get_render(rid, id, 6, true, true, function(err, hash, img) { + assert.strictEqual(err, null); + done(); + }); + }); + it("should not fail (only head)", function(done) { + helpers.get_render(rid, id, 6, true, false, function(err, hash, img) { + assert.strictEqual(err, null); + done(); + }); + }); +}); + +describe("Networking: Cape", function() { + it("should not fail (possible cape)", function(done) { + helpers.get_cape(rid, id, function(err, hash, img) { + assert.strictEqual(err, null); + done(); + }); + }); +}); + + +describe("Errors", function() { + before(function() { + cache.get_redis().flushall(); + }); + + if (id_type == "uuid") { + it("uuid should be rate limited", function(done) { + networking.get_profile(rid, id, function(err, profile) { + assert.strictEqual(profile.error, "TooManyRequestsException"); + done(); + }); + }); + } else { + it("username should NOT be rate limited (username)", function(done) { + helpers.get_avatar(rid, id, false, 160, function(err, status, image) { + assert.strictEqual(err, null); + done(); + }); + }); + } +}); +})(id, id_type); +} }); \ No newline at end of file