Jake 10 роки тому
батько
коміт
ed99d918c1
4 змінених файлів з 161 додано та 18 видалено
  1. 3 0
      .travis.yml
  2. 1 0
      routes/avatars.js
  3. 27 14
      server.js
  4. 130 4
      test/test.js

+ 3 - 0
.travis.yml

@@ -12,3 +12,6 @@ notifications:
     skip_join: true
 services:
   - redis-server
+cache:
+  directories:
+    - node_modules

+ 1 - 0
routes/avatars.js

@@ -45,6 +45,7 @@ module.exports = function(req, res) {
   userId = userId.replace(/-/g, "");
   logging.log(rid + "userid: " + userId);
 
+
   try {
     helpers.get_avatar(rid, userId, helm, size, function(err, status, image, hash) {
       logging.log(rid + "storage type: " + human_status[status]);

+ 27 - 14
server.js

@@ -7,6 +7,7 @@ var http = require("http");
 var mime = require("mime");
 var url = require("url");
 var fs = require("fs");
+var server = null;
 
 var routes = {
   index: require("./routes/index"),
@@ -53,22 +54,22 @@ function requestHandler(req, res) {
     try {
       switch (local_path) {
         case "":
-          routes.index(request, res);
-          break;
+        routes.index(request, res);
+        break;
         case "avatars":
-          routes.avatars(request, res);
-          break;
+        routes.avatars(request, res);
+        break;
         case "skins":
-          routes.skins(request, res);
-          break;
+        routes.skins(request, res);
+        break;
         case "renders":
-          routes.renders(request, res);
-          break;
+        routes.renders(request, res);
+        break;
         case "capes":
-          routes.capes(request, res);
-          break;
+        routes.capes(request, res);
+        break;
         default:
-          asset_request(request, res);
+        asset_request(request, res);
       }
     } catch(e) {
       var error = JSON.stringify(req.headers) + "\n" + e.stack;
@@ -86,15 +87,27 @@ function requestHandler(req, res) {
   }
 }
 
-var boot = module.exports = function () {
+var exp = {};
+
+exp.boot = function(callback) {
   var port = process.env.PORT || 3000;
   var bind_ip = process.env.BIND || "127.0.0.1";
   logging.log("Server running on http://" + bind_ip + ":" + port + "/");
-  http.createServer(requestHandler).listen(port, bind_ip);
+  server = http.createServer(requestHandler).listen(port, bind_ip, function() {
+    callback();
+  });
 };
 
+exp.close = function(callback) {
+  server.close(function() {
+    callback();
+  });
+}
+
+module.exports = exp;
+
 if (require.main === module) {
-  boot();
+  exp.boot(function(){});
 
   // cleaning worker
   setInterval(clean.run, config.cleaning_interval * 1000);

+ 130 - 4
test/test.js

@@ -1,6 +1,5 @@
 var assert = require("assert");
 var fs = require("fs");
-
 var networking = require("../modules/networking");
 var helpers = require("../modules/helpers");
 var logging = require("../modules/logging");
@@ -8,7 +7,9 @@ var config = require("../modules/config");
 var skins = require("../modules/skins");
 var cache = require("../modules/cache");
 var renders = require("../modules/renders");
+var server = require("../server");
 var cleaner = require("../modules/cleaner");
+var request = require("request");
 
 // we don't want tests to fail because of slow internet
 config.http_timeout *= 3;
@@ -32,8 +33,8 @@ function getRandomInt(min, max) {
 var ids = [
   uuid.toLowerCase(),
   name.toLowerCase(),
+  name.toUpperCase(),
   uuid.toUpperCase(),
-  name.toUpperCase()
 ];
 
 describe("Crafatar", function() {
@@ -42,6 +43,10 @@ describe("Crafatar", function() {
 
   before(function() {
     cache.get_redis().flushall();
+    // largest possible integers, cause I don't know
+    // how big hard drives are these days
+    config.cleaning_disk_limit = Math.pow(2, 32) - 1;;
+    config.cleaning_redis_limit = Math.pow(2, 32) - 1;;
     cleaner.run();
   });
 
@@ -100,7 +105,6 @@ describe("Crafatar", function() {
       });
     });
   });
-
   describe("Avatar", function() {
     // profile "Alex" - hoping it'll never have a skin
     var alex_uuid = "ec561538f3fd461daff5086b22154bce";
@@ -169,13 +173,135 @@ describe("Crafatar", function() {
       });
     });
     it("should not find the file", function(done) {
-      skins.open_skin(rid, 'non/existant/path', function(err, img) {
+      skins.open_skin(rid, "non/existant/path", function(err, img) {
         assert.notStrictEqual(err, null);
         done();
       });
     });
   });
 
+  describe("Server", function() {
+    before(function(done) {
+      server.boot(function() {
+        done();
+      });
+    });
+
+    // Test the home page
+    it("should return a 200 (home page)", function(done) {
+      request.get("http://localhost:3000", function(error, res, body) {
+        assert.equal(200, res.statusCode);
+        done();
+      });
+    });
+
+    it("should return a 200 (asset request)", function(done) {
+      request.get("http://localhost:3000/stylesheets/style.css", function(error, res, body) {
+        assert.equal(200, res.statusCode);
+        done();
+      });
+    });
+
+    // invalid method, we only allow GET and HEAD requests
+    it("should return a 405 (invalid method)", function(done) {
+      request.post("http://localhost:3000", 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();
+      });
+    });
+
+    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();
+      });
+    });
+
+    // no default images for capes, should 404
+    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();
+      });
+    });
+
+    it("should return a 422 (invalid render type)", function(done) {
+      request.get("http://localhost:3000/renders/side/Jake0oo0", function(error, res, body) {
+        assert.equal(422, res.statusCode);
+        done();
+      });
+    });
+
+    // testing all paths for valid inputs
+    var locations = ["avatars", "skins", "renders/head"]
+    for (var l in locations) {
+      var location = locations[l];
+      (function(location) {
+        it("should return a 200 (valid input " + location + ")", function(done) {
+          request.get("http://localhost:3000/" + location + "/Jake0oo0", function(error, res, body) {
+            assert.equal(200, res.statusCode);
+            done();
+          });
+        })
+        it("should return a 422 (invalid id " + location + ")", function(done) {
+          request.get("http://localhost:3000/" + location + "/thisisaninvaliduuid", function(error, res, body) {
+            assert.equal(422, res.statusCode);
+            done();
+          });
+        });
+      })(location);
+    }
+
+    // testing all paths for invalid id formats
+    var locations = ["avatars", "capes", "skins", "renders/head"]
+    for (var l in locations) {
+      var location = locations[l];
+      (function(location) {
+        it("should return a 422 (invalid id " + location + ")", function(done) {
+          request.get("http://localhost:3000/" + location + "/thisisaninvaliduuid", function(error, res, body) {
+            assert.equal(422, res.statusCode);
+            done();
+          });
+        });
+      })(location);
+    }
+
+    //testing all paths for default images
+    locations = ["avatars", "skins", "renders/head"]
+    for (var l in locations) {
+      var location = locations[l];
+      (function(location) {
+        it("should return a 404 (default steve image " + location + ")", function(done) {
+          request.get("http://localhost:3000/" + location + "/invalidjsvns?default=steve", function(error, res, body) {
+            assert.equal(404, res.statusCode);
+            done();
+          });
+        });
+        it("should return a 200 (default external image " + location + ")", function(done) {
+          request.get("http://localhost:3000/" + location + "/invalidjsvns?default=https%3A%2F%2Fi.imgur.com%2FocJVWAc.png", function(error, res, body) {
+            assert.equal(200, res.statusCode);
+            done();
+          });
+        });
+      })(location);
+    }
+
+    after(function(done) {
+      server.close(function() {
+        done();
+      })
+    });
+  });
+
   // we have to make sure that we test both a 32x64 and 64x64 skin
   describe("Networking: Render", function() {
     it("should not fail (username, 32x64 skin)", function(done) {