test.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. var networking = require("../lib/networking");
  2. var helpers = require("../lib/helpers");
  3. var logging = require("../lib/logging");
  4. var cleaner = require("../lib/cleaner");
  5. var request = require("request");
  6. var config = require("../lib/config");
  7. var server = require("../lib/server");
  8. var assert = require("assert");
  9. var skins = require("../lib/skins");
  10. var cache = require("../lib/cache");
  11. var crc = require("crc").crc32;
  12. var fs = require("fs");
  13. // we don't want tests to fail because of slow internet
  14. config.http_timeout *= 3;
  15. // no spam
  16. if (process.env.VERBOSE_TEST !== "true") {
  17. logging.log = logging.debug = logging.warn = logging.error = function() {};
  18. }
  19. var uuids = fs.readFileSync("test/uuids.txt").toString().split(/\r?\n/);
  20. var names = fs.readFileSync("test/usernames.txt").toString().split(/\r?\n/);
  21. // Get a random UUID + name in order to prevent rate limiting
  22. var uuid = uuids[Math.round(Math.random() * (uuids.length - 1))];
  23. var name = names[Math.round(Math.random() * (names.length - 1))];
  24. // Let's hope these will never be assigned
  25. var steve_ids = [
  26. "fffffff0" + "fffffff0" + "fffffff0" + "fffffff0",
  27. "fffffff0" + "fffffff0" + "fffffff1" + "fffffff1",
  28. "fffffff0" + "fffffff1" + "fffffff0" + "fffffff1",
  29. "fffffff0" + "fffffff1" + "fffffff1" + "fffffff0",
  30. "fffffff1" + "fffffff0" + "fffffff0" + "fffffff1",
  31. "fffffff1" + "fffffff0" + "fffffff1" + "fffffff0",
  32. "fffffff1" + "fffffff1" + "fffffff0" + "fffffff0",
  33. "fffffff1" + "fffffff1" + "fffffff1" + "fffffff1",
  34. ];
  35. // Let's hope these will never be assigned
  36. var alex_ids = [
  37. "fffffff0" + "fffffff0" + "fffffff0" + "fffffff1",
  38. "fffffff0" + "fffffff0" + "fffffff1" + "fffffff0",
  39. "fffffff0" + "fffffff1" + "fffffff0" + "fffffff0",
  40. "fffffff0" + "fffffff1" + "fffffff1" + "fffffff1",
  41. "fffffff1" + "fffffff0" + "fffffff0" + "fffffff0",
  42. "fffffff1" + "fffffff0" + "fffffff1" + "fffffff1",
  43. "fffffff1" + "fffffff1" + "fffffff0" + "fffffff1",
  44. "fffffff1" + "fffffff1" + "fffffff1" + "fffffff0",
  45. ];
  46. var rid = "TestReqID: ";
  47. function getRandomInt(min, max) {
  48. return Math.floor(Math.random() * (max - min + 1)) + min;
  49. }
  50. var ids = [
  51. uuid.toLowerCase(),
  52. name.toLowerCase(),
  53. name.toUpperCase(),
  54. uuid.toUpperCase(),
  55. ];
  56. describe("Crafatar", function() {
  57. // we might have to make 2 HTTP requests
  58. this.timeout(config.http_timeout * 2 + 50);
  59. before(function() {
  60. cache.get_redis().flushall();
  61. // cause I don't know how big hard drives are these days
  62. config.cleaning_disk_limit = Infinity;
  63. config.cleaning_redis_limit = Infinity;
  64. cleaner.run();
  65. });
  66. describe("UUID/username", function() {
  67. it("non-hex uuid is invalid", function(done) {
  68. assert.strictEqual(helpers.id_valid("g098cb60fa8e427cb299793cbd302c9a"), false);
  69. done();
  70. });
  71. it("empty id is invalid", function(done) {
  72. assert.strictEqual(helpers.id_valid(""), false);
  73. done();
  74. });
  75. it("non-alphanumeric username is invalid", function(done) {
  76. assert.strictEqual(helpers.id_valid("usernäme"), false);
  77. done();
  78. });
  79. it("dashed username is invalid", function(done) {
  80. assert.strictEqual(helpers.id_valid("user-name"), false);
  81. done();
  82. });
  83. it(">16 length username is invalid", function(done) {
  84. assert.strictEqual(helpers.id_valid("ThisNameIsTooLong"), false);
  85. done();
  86. });
  87. it("lowercase uuid is valid", function(done) {
  88. assert.strictEqual(helpers.id_valid("0098cb60fa8e427cb299793cbd302c9a"), true);
  89. done();
  90. });
  91. it("uppercase uuid is valid", function(done) {
  92. assert.strictEqual(helpers.id_valid("1DCEF164FF0A47F2B9A691385C774EE7"), true);
  93. done();
  94. });
  95. it("dashed uuid is valid", function(done) {
  96. assert.strictEqual(helpers.id_valid("0098cb60-fa8e-427c-b299-793cbd302c9a"), true);
  97. done();
  98. });
  99. it("16 chars, underscored, capital, numbered username is valid", function(done) {
  100. assert.strictEqual(helpers.id_valid("__niceUs3rname__"), true);
  101. done();
  102. });
  103. it("1 char username is valid", function(done) {
  104. assert.strictEqual(helpers.id_valid("a"), true);
  105. done();
  106. });
  107. it("should not exist (uuid)", function(done) {
  108. var number = getRandomInt(0, 9).toString();
  109. networking.get_profile(rid, Array(33).join(number), function(err, profile) {
  110. assert.strictEqual(profile, null);
  111. done();
  112. });
  113. });
  114. it("should not exist (username)", function(done) {
  115. networking.get_username_url(rid, "Steve", 0, function(err, profile) {
  116. assert.strictEqual(err, null);
  117. done();
  118. });
  119. });
  120. });
  121. describe("Avatar", function() {
  122. it("uuid's account should exist, but skin should not", function(done) {
  123. // profile "Alex" - hoping it'll never have a skin
  124. networking.get_profile(rid, "ec561538f3fd461daff5086b22154bce", function(err, profile) {
  125. assert.notStrictEqual(profile, null);
  126. networking.get_uuid_url(profile, 1, function(url) {
  127. assert.strictEqual(url, null);
  128. done();
  129. });
  130. });
  131. });
  132. it("Username should default to Steve", function(done) {
  133. assert.strictEqual(skins.default_skin("TestUser"), "steve");
  134. done();
  135. });
  136. for (var a in alex_ids) {
  137. var alex_id = alex_ids[a];
  138. (function(alex_id) {
  139. it("UUID " + alex_id + " should default to Alex", function(done) {
  140. assert.strictEqual(skins.default_skin(alex_id), "alex");
  141. done();
  142. });
  143. })(alex_id);
  144. }
  145. for (var s in steve_ids) {
  146. var steve_id = steve_ids[s];
  147. (function(steve_id) {
  148. it("UUID " + steve_id + " should default to Steve", function(done) {
  149. assert.strictEqual(skins.default_skin(steve_id), "steve");
  150. done();
  151. });
  152. })(steve_id);
  153. }
  154. });
  155. describe("Errors", function() {
  156. it("should time out on uuid info download", function(done) {
  157. var original_timeout = config.http_timeout;
  158. config.http_timeout = 1;
  159. networking.get_profile(rid, "069a79f444e94726a5befca90e38aaf5", function(err, profile) {
  160. assert.strictEqual(err.code, "ETIMEDOUT");
  161. config.http_timeout = original_timeout;
  162. done();
  163. });
  164. });
  165. it("should time out on username info download", function(done) {
  166. var original_timeout = config.http_timeout;
  167. config.http_timeout = 1;
  168. networking.get_username_url(rid, "jomo", 0, function(err, url) {
  169. assert.strictEqual(err.code, "ETIMEDOUT");
  170. config.http_timeout = original_timeout;
  171. done();
  172. });
  173. });
  174. it("should time out on skin download", function(done) {
  175. var original_timeout = config.http_timeout;
  176. config.http_timeout = 1;
  177. networking.get_from(rid, "http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", function(body, res, error) {
  178. assert.strictEqual(error.code, "ETIMEDOUT");
  179. config.http_timeout = original_timeout;
  180. done();
  181. });
  182. });
  183. it("should not find the skin", function(done) {
  184. assert.doesNotThrow(function() {
  185. networking.get_from(rid, "http://textures.minecraft.net/texture/this-does-not-exist", function(img, response, err) {
  186. assert.strictEqual(err, null); // no error here, but it shouldn't throw exceptions
  187. done();
  188. });
  189. });
  190. });
  191. it("should ignore file updates on invalid files", function(done) {
  192. assert.doesNotThrow(function() {
  193. cache.update_timestamp(rid, "0123456789abcdef0123456789abcdef", "invalid-file.png", false, function(err) {
  194. done();
  195. });
  196. });
  197. });
  198. it("should not find the file", function(done) {
  199. skins.open_skin(rid, "non/existent/path", function(err, img) {
  200. assert.notStrictEqual(err, null);
  201. done();
  202. });
  203. });
  204. });
  205. describe("Server", function() {
  206. // throws Exception when default headers are not in res.headers
  207. function assert_headers(res) {
  208. assert(res.headers["content-type"]);
  209. assert("" + res.headers["response-time"]);
  210. assert(res.headers["x-request-id"]);
  211. assert.equal(res.headers["access-control-allow-origin"], "*");
  212. assert.equal(res.headers["cache-control"], "max-age=" + config.browser_cache_time + ", public");
  213. }
  214. // throws Exception when +url+ is requested with +etag+
  215. // and it does not return 304 without a body
  216. function assert_cache(url, etag, callback) {
  217. request.get(url, {
  218. headers: {
  219. "If-None-Match": etag
  220. }
  221. }, function(error, res, body) {
  222. assert.ifError(error);
  223. assert.ifError(body);
  224. assert.equal(res.statusCode, 304);
  225. assert(res.headers["etag"]);
  226. assert_headers(res);
  227. callback();
  228. });
  229. }
  230. before(function(done) {
  231. server.boot(function() {
  232. done();
  233. });
  234. });
  235. it("should return 405 Method Not Allowed for POST", function(done) {
  236. request.post("http://localhost:3000", function(error, res, body) {
  237. assert.ifError(error);
  238. assert.strictEqual(res.statusCode, 405);
  239. done();
  240. });
  241. });
  242. it("should return correct HTTP response for home page", function(done) {
  243. var url = "http://localhost:3000";
  244. request.get(url, function(error, res, body) {
  245. assert.ifError(error);
  246. assert.strictEqual(res.statusCode, 200);
  247. assert_headers(res);
  248. assert(res.headers["etag"]);
  249. assert.strictEqual(res.headers["content-type"], "text/html; charset=utf-8");
  250. assert.strictEqual(res.headers["etag"], "\"" + crc(body) + "\"");
  251. assert(body);
  252. assert_cache(url, res.headers["etag"], function() {
  253. done();
  254. });
  255. });
  256. });
  257. it("should return correct HTTP response for assets", function(done) {
  258. var url = "http://localhost:3000/stylesheets/style.css";
  259. request.get(url, function(error, res, body) {
  260. assert.ifError(error);
  261. assert.strictEqual(res.statusCode, 200);
  262. assert_headers(res);
  263. assert(res.headers["etag"]);
  264. assert.strictEqual(res.headers["content-type"], "text/css");
  265. assert.strictEqual(res.headers["etag"], "\"" + crc(body) + "\"");
  266. assert(body);
  267. assert_cache(url, res.headers["etag"], function() {
  268. done();
  269. });
  270. });
  271. });
  272. it("should return correct HTTP response for URL encoded URLs", function(done) {
  273. var url = "http://localhost:3000/%61%76%61%74%61%72%73/%6a%6f%6d%6f"; // avatars/jomo
  274. request.get(url, function(error, res, body) {
  275. assert.ifError(error);
  276. assert.strictEqual(res.statusCode, 200);
  277. assert_headers(res);
  278. assert(res.headers["etag"]);
  279. assert.strictEqual(res.headers["content-type"], "image/png");
  280. assert(body);
  281. done();
  282. });
  283. });
  284. it("should not fail on simultaneous requests", function(done) {
  285. var url = "http://localhost:3000/avatars/696a82ce41f44b51aa31b8709b8686f0";
  286. // 10 requests at once
  287. var requests = 10;
  288. var finished = 0;
  289. function partDone() {
  290. finished++;
  291. if (requests === finished) {
  292. done();
  293. }
  294. }
  295. function req() {
  296. request.get(url, function(error, res, body) {
  297. assert.ifError(error);
  298. assert.strictEqual(res.statusCode, 200);
  299. assert_headers(res);
  300. assert(res.headers["etag"]);
  301. assert.strictEqual(res.headers["content-type"], "image/png");
  302. assert(body);
  303. partDone();
  304. });
  305. }
  306. // make simultanous requests
  307. for (var j = 0; j < requests; j++) {
  308. req(j);
  309. }
  310. });
  311. var server_tests = {
  312. "avatar with existing username": {
  313. url: "http://localhost:3000/avatars/jeb_?size=16",
  314. etag: '"a846b82963"',
  315. crc32: 1623808067
  316. },
  317. "avatar with not existing username": {
  318. url: "http://localhost:3000/avatars/0?size=16",
  319. etag: '"steve"',
  320. crc32: 2416827277
  321. },
  322. "avatar with not existing username defaulting to alex": {
  323. url: "http://localhost:3000/avatars/0?size=16&default=alex",
  324. etag: '"alex"',
  325. crc32: 862751081
  326. },
  327. "avatar with not existing username defaulting to url": {
  328. url: "http://localhost:3000/avatars/0?size=16&default=http://example.com",
  329. crc32: 0,
  330. redirect: "http://example.com"
  331. },
  332. "helm avatar with existing username": {
  333. url: "http://localhost:3000/avatars/jeb_?size=16&helm",
  334. etag: '"a846b82963"',
  335. crc32: 646871998
  336. },
  337. "helm avatar with not existing username": {
  338. url: "http://localhost:3000/avatars/0?size=16&helm",
  339. etag: '"steve"',
  340. crc32: 2416827277
  341. },
  342. "helm avatar with not existing username defaulting to alex": {
  343. url: "http://localhost:3000/avatars/0?size=16&helm&default=alex",
  344. etag: '"alex"',
  345. crc32: 862751081
  346. },
  347. "helm avatar with not existing username defaulting to url": {
  348. url: "http://localhost:3000/avatars/0?size=16&helm&default=http://example.com",
  349. crc32: 0,
  350. redirect: "http://example.com"
  351. },
  352. "avatar with existing uuid": {
  353. url: "http://localhost:3000/avatars/853c80ef3c3749fdaa49938b674adae6?size=16",
  354. etag: '"a846b82963"',
  355. crc32: 1623808067
  356. },
  357. "avatar with not existing uuid": {
  358. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16",
  359. etag: '"steve"',
  360. crc32: 2416827277
  361. },
  362. "avatar with not existing uuid defaulting to alex": {
  363. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=alex",
  364. etag: '"alex"',
  365. crc32: 862751081
  366. },
  367. "avatar with not existing uuid defaulting to url": {
  368. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=http://example.com",
  369. crc32: 0,
  370. redirect: "http://example.com"
  371. },
  372. "helm avatar with existing uuid": {
  373. url: "http://localhost:3000/avatars/853c80ef3c3749fdaa49938b674adae6?size=16&helm",
  374. etag: '"a846b82963"',
  375. crc32: 646871998
  376. },
  377. "helm avatar with not existing uuid": {
  378. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm",
  379. etag: '"steve"',
  380. crc32: 2416827277
  381. },
  382. "helm avatar with not existing uuid defaulting to alex": {
  383. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm&default=alex",
  384. etag: '"alex"',
  385. crc32: 862751081
  386. },
  387. "helm avatar with not existing uuid defaulting to url": {
  388. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm&default=http://example.com",
  389. crc32: 0,
  390. redirect: "http://example.com"
  391. },
  392. "cape with existing username": {
  393. url: "http://localhost:3000/capes/jeb_",
  394. etag: '"3f688e0e69"',
  395. crc32: 989800403
  396. },
  397. "cape with not existing username": {
  398. url: "http://localhost:3000/capes/0",
  399. crc32: 0
  400. },
  401. "cape with not existing username defaulting to url": {
  402. url: "http://localhost:3000/capes/0?default=http://example.com",
  403. crc32: 0,
  404. redirect: "http://example.com"
  405. },
  406. "cape with existing uuid": {
  407. url: "http://localhost:3000/capes/853c80ef3c3749fdaa49938b674adae6",
  408. etag: '"3f688e0e69"',
  409. crc32: 989800403
  410. },
  411. "cape with not existing uuid": {
  412. url: "http://localhost:3000/capes/00000000000000000000000000000000",
  413. crc32: 0
  414. },
  415. "cape with not existing uuid defaulting to url": {
  416. url: "http://localhost:3000/capes/00000000000000000000000000000000?default=http://example.com",
  417. crc32: 0,
  418. redirect: "http://example.com"
  419. },
  420. "skin with existing username": {
  421. url: "http://localhost:3000/skins/jeb_",
  422. etag: '"a846b82963"',
  423. crc32: 110922424
  424. },
  425. "skin with not existing username": {
  426. url: "http://localhost:3000/skins/0",
  427. etag: '"steve"',
  428. crc32: 981937087
  429. },
  430. "skin with not existing username defaulting to alex": {
  431. url: "http://localhost:3000/skins/0?default=alex",
  432. etag: '"alex"',
  433. crc32: 2298915739
  434. },
  435. "skin with not existing username defaulting to url": {
  436. url: "http://localhost:3000/skins/0?default=http://example.com",
  437. crc32: 0,
  438. redirect: "http://example.com"
  439. },
  440. "skin with existing uuid": {
  441. url: "http://localhost:3000/skins/853c80ef3c3749fdaa49938b674adae6",
  442. etag: '"a846b82963"',
  443. crc32: 110922424
  444. },
  445. "skin with not existing uuid": {
  446. url: "http://localhost:3000/skins/00000000000000000000000000000000",
  447. etag: '"steve"',
  448. crc32: 981937087
  449. },
  450. "skin with not existing uuid defaulting to alex": {
  451. url: "http://localhost:3000/skins/00000000000000000000000000000000?default=alex",
  452. etag: '"alex"',
  453. crc32: 2298915739
  454. },
  455. "skin with not existing uuid defaulting to url": {
  456. url: "http://localhost:3000/skins/00000000000000000000000000000000?default=http://example.com",
  457. crc32: 0,
  458. redirect: "http://example.com"
  459. },
  460. "head render with existing username": {
  461. url: "http://localhost:3000/renders/head/jeb_?scale=2",
  462. etag: '"a846b82963"',
  463. crc32: [353633671, 370672768]
  464. },
  465. "head render with not existing username": {
  466. url: "http://localhost:3000/renders/head/0?scale=2",
  467. etag: '"steve"',
  468. crc32: [883439147, 433083528]
  469. },
  470. "head render with not existing username defaulting to alex": {
  471. url: "http://localhost:3000/renders/head/0?scale=2&default=alex",
  472. etag: '"alex"',
  473. crc32: [1240086237, 1108800327]
  474. },
  475. "head render with not existing username defaulting to url": {
  476. url: "http://localhost:3000/renders/head/0?scale=2&default=http://example.com",
  477. crc32: 0,
  478. redirect: "http://example.com"
  479. },
  480. "helm head render with existing username": {
  481. url: "http://localhost:3000/renders/head/jeb_?scale=2&helm",
  482. etag: '"a846b82963"',
  483. crc32: [3456497067, 3490318764]
  484. },
  485. "helm head render with not existing username": {
  486. url: "http://localhost:3000/renders/head/0?scale=2&helm",
  487. etag: '"steve"',
  488. crc32: [1858563554, 2647471936]
  489. },
  490. "helm head render with not existing username defaulting to alex": {
  491. url: "http://localhost:3000/renders/head/0?scale=2&helm&default=alex",
  492. etag: '"alex"',
  493. crc32: [2963161105, 1769904825]
  494. },
  495. "helm head render with not existing username defaulting to url": {
  496. url: "http://localhost:3000/renders/head/0?scale=2&helm&default=http://example.com",
  497. crc32: 0,
  498. redirect: "http://example.com"
  499. },
  500. "head render with existing uuid": {
  501. url: "http://localhost:3000/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2",
  502. etag: '"a846b82963"',
  503. crc32: [353633671, 370672768]
  504. },
  505. "head render with not existing uuid": {
  506. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2",
  507. etag: '"steve"',
  508. crc32: [883439147, 433083528]
  509. },
  510. "head render with not existing uuid defaulting to alex": {
  511. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=alex",
  512. etag: '"alex"',
  513. crc32: [1240086237, 1108800327]
  514. },
  515. "head render with not existing uuid defaulting to url": {
  516. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=http://example.com",
  517. crc32: 0,
  518. redirect: "http://example.com"
  519. },
  520. "helm head render with existing uuid": {
  521. url: "http://localhost:3000/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2&helm",
  522. etag: '"a846b82963"',
  523. crc32: [3456497067, 3490318764]
  524. },
  525. "helm head render with not existing uuid": {
  526. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm",
  527. etag: '"steve"',
  528. crc32: [1858563554, 2647471936]
  529. },
  530. "helm head render with not existing uuid defaulting to alex": {
  531. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm&default=alex",
  532. etag: '"alex"',
  533. crc32: [2963161105, 1769904825]
  534. },
  535. "helm head render with not existing uuid defaulting to url": {
  536. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm&default=http://example.com",
  537. crc32: 0,
  538. redirect: "http://example.com"
  539. },
  540. "body render with existing username": {
  541. url: "http://localhost:3000/renders/body/jeb_?scale=2",
  542. etag: '"a846b82963"',
  543. crc32: [1291941229, 2628108474]
  544. },
  545. "body render with not existing username": {
  546. url: "http://localhost:3000/renders/body/0?scale=2",
  547. etag: '"steve"',
  548. crc32: [2652947188, 2115706574]
  549. },
  550. "body render with not existing username defaulting to alex": {
  551. url: "http://localhost:3000/renders/body/0?scale=2&default=alex",
  552. etag: '"alex"',
  553. crc32: [407932087, 2516216042]
  554. },
  555. "body render with not existing username defaulting to url": {
  556. url: "http://localhost:3000/renders/body/0?scale=2&default=http://example.com",
  557. crc32: 0,
  558. redirect: "http://example.com"
  559. },
  560. "helm body render with existing username": {
  561. url: "http://localhost:3000/renders/body/jeb_?scale=2&helm",
  562. etag: '"a846b82963"',
  563. crc32: [3556188297, 4269754007]
  564. },
  565. "helm body render with not existing username": {
  566. url: "http://localhost:3000/renders/body/0?scale=2&helm",
  567. etag: '"steve"',
  568. crc32: [272191039, 542896675]
  569. },
  570. "helm body render with not existing username defaulting to alex": {
  571. url: "http://localhost:3000/renders/body/0?scale=2&helm&default=alex",
  572. etag: '"alex"',
  573. crc32: [737759773, 66512449]
  574. },
  575. "helm body render with not existing username defaulting to url": {
  576. url: "http://localhost:3000/renders/body/0?scale=2&helm&default=http://example.com",
  577. crc32: 0,
  578. redirect: "http://example.com"
  579. },
  580. "body render with existing uuid": {
  581. url: "http://localhost:3000/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2",
  582. etag: '"a846b82963"',
  583. crc32: [1291941229, 2628108474]
  584. },
  585. "body render with not existing uuid": {
  586. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2",
  587. etag: '"steve"',
  588. crc32: [2652947188, 2115706574]
  589. },
  590. "body render with not existing uuid defaulting to alex": {
  591. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&default=alex",
  592. etag: '"alex"',
  593. crc32: [407932087, 2516216042]
  594. },
  595. "body render with not existing uuid defaulting to url": {
  596. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&default=http://example.com",
  597. crc32: 0,
  598. redirect: "http://example.com"
  599. },
  600. "helm body render with existing uuid": {
  601. url: "http://localhost:3000/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2&helm",
  602. etag: '"a846b82963"',
  603. crc32: [3556188297, 4269754007]
  604. },
  605. "helm body render with not existing uuid": {
  606. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm",
  607. etag: '"steve"',
  608. crc32: [272191039, 542896675]
  609. },
  610. "helm body render with not existing uuid defaulting to alex": {
  611. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm&default=alex",
  612. etag: '"alex"',
  613. crc32: [737759773, 66512449]
  614. },
  615. "helm body render with not existing uuid defaulting to url": {
  616. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm&default=http://example.com",
  617. crc32: 0,
  618. redirect: "http://example.com"
  619. },
  620. };
  621. for (var description in server_tests) {
  622. var location = server_tests[description];
  623. (function(location) {
  624. it("should return correct HTTP response for " + description, function(done) {
  625. request.get(location.url, {followRedirect: false, encoding: null}, function(error, res, body) {
  626. assert.ifError(error);
  627. assert_headers(res);
  628. assert(res.headers["x-storage-type"]);
  629. assert.strictEqual(res.headers["etag"], location.etag);
  630. var matches = false;
  631. if (location.crc32 instanceof Array) {
  632. for (var i = 0; i < location.crc32.length; i++) {
  633. if (location.crc32[i] === crc(body)) {
  634. matches = true;
  635. break;
  636. }
  637. }
  638. } else {
  639. matches = (location.crc32 === crc(body));
  640. }
  641. assert.ok(matches);
  642. assert.strictEqual(res.headers["location"], location.redirect);
  643. if (location.etag === undefined) {
  644. assert.strictEqual(res.statusCode, location.redirect ? 307 : 404);
  645. assert.strictEqual(res.headers["content-type"], "text/plain");
  646. done();
  647. } else {
  648. assert(res.headers["etag"]);
  649. assert.strictEqual(res.headers["content-type"], "image/png");
  650. assert.strictEqual(res.statusCode, 200);
  651. assert_cache(location.url, res.headers["etag"], function() {
  652. done();
  653. });
  654. }
  655. });
  656. });
  657. }(location));
  658. }
  659. it("should return a 422 (invalid size)", function(done) {
  660. var size = config.max_size + 1;
  661. request.get("http://localhost:3000/avatars/Jake_0?size=" + size, function(error, res, body) {
  662. assert.strictEqual(res.statusCode, 422);
  663. done();
  664. });
  665. });
  666. it("should return a 422 (invalid scale)", function(done) {
  667. var scale = config.max_scale + 1;
  668. request.get("http://localhost:3000/renders/head/Jake_0?scale=" + scale, function(error, res, body) {
  669. assert.strictEqual(res.statusCode, 422);
  670. done();
  671. });
  672. });
  673. it("should return a 422 (invalid render type)", function(done) {
  674. request.get("http://localhost:3000/renders/invalid/Jake_0", function(error, res, body) {
  675. assert.strictEqual(res.statusCode, 422);
  676. done();
  677. });
  678. });
  679. // testing all paths for Invalid UserID
  680. var locations = ["avatars", "skins", "capes", "renders/body", "renders/head"];
  681. for (var l in locations) {
  682. var location = locations[l];
  683. (function(location) {
  684. it("should return a 422 (invalid id " + location + ")", function(done) {
  685. request.get("http://localhost:3000/" + location + "/thisisaninvaliduuid", function(error, res, body) {
  686. assert.strictEqual(res.statusCode, 422);
  687. done();
  688. });
  689. });
  690. })(location);
  691. }
  692. after(function(done) {
  693. server.close(function() {
  694. done();
  695. });
  696. });
  697. });
  698. // we have to make sure that we test both a 32x64 and 64x64 skin
  699. describe("Networking: Render", function() {
  700. it("should not fail (username, 32x64 skin)", function(done) {
  701. helpers.get_render(rid, "md_5", 6, true, true, function(err, hash, img) {
  702. assert.strictEqual(err, null);
  703. done();
  704. });
  705. });
  706. it("should not fail (username, 64x64 skin)", function(done) {
  707. helpers.get_render(rid, "Jake_0", 6, true, true, function(err, hash, img) {
  708. assert.strictEqual(err, null);
  709. done();
  710. });
  711. });
  712. });
  713. describe("Networking: Cape", function() {
  714. it("should not fail (guaranteed cape)", function(done) {
  715. helpers.get_cape(rid, "Dinnerbone", function(err, hash, status, img) {
  716. assert.strictEqual(err, null);
  717. done();
  718. });
  719. });
  720. it("should already exist", function(done) {
  721. before(function() {
  722. cache.get_redis().flushall();
  723. });
  724. helpers.get_cape(rid, "Dinnerbone", function(err, hash, status, img) {
  725. assert.strictEqual(err, null);
  726. done();
  727. });
  728. });
  729. it("should not be found", function(done) {
  730. helpers.get_cape(rid, "Jake_0", function(err, hash, status, img) {
  731. assert.strictEqual(img, null);
  732. done();
  733. });
  734. });
  735. });
  736. describe("Networking: Skin", function() {
  737. it("should not fail", function(done) {
  738. helpers.get_cape(rid, "Jake_0", function(err, hash, status, img) {
  739. assert.strictEqual(err, null);
  740. done();
  741. });
  742. });
  743. it("should already exist", function(done) {
  744. before(function() {
  745. cache.get_redis().flushall();
  746. });
  747. helpers.get_cape(rid, "Jake_0", function(err, hash, status, img) {
  748. assert.strictEqual(err, null);
  749. done();
  750. });
  751. });
  752. });
  753. // DRY with uuid and username tests
  754. for (var i in ids) {
  755. var id = ids[i];
  756. var id_type = id.length > 16 ? "uuid" : "name";
  757. // needs an anonymous function because id and id_type aren't constant
  758. (function(id, id_type) {
  759. describe("Networking: Avatar", function() {
  760. before(function() {
  761. cache.get_redis().flushall();
  762. console.log("\n\nRunning tests with " + id_type + " '" + id + "'\n\n");
  763. });
  764. it("should be downloaded", function(done) {
  765. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  766. assert.strictEqual(status, 2);
  767. done();
  768. });
  769. });
  770. it("should be cached", function(done) {
  771. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  772. assert.strictEqual(status === 0 || status === 1, true);
  773. done();
  774. });
  775. });
  776. if (id.length > 16) {
  777. console.log("can't run 'checked' test due to Mojang's rate limits :(");
  778. } else {
  779. it("should be checked", function(done) {
  780. var original_cache_time = config.local_cache_time;
  781. config.local_cache_time = 0;
  782. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  783. assert.strictEqual(status, 3);
  784. config.local_cache_time = original_cache_time;
  785. done();
  786. });
  787. });
  788. }
  789. });
  790. describe("Networking: Skin", function() {
  791. it("should not fail (uuid)", function(done) {
  792. helpers.get_skin(rid, id, function(err, hash, status, img) {
  793. assert.strictEqual(err, null);
  794. done();
  795. });
  796. });
  797. });
  798. describe("Networking: Render", function() {
  799. it("should not fail (full body)", function(done) {
  800. helpers.get_render(rid, id, 6, true, true, function(err, hash, img) {
  801. assert.strictEqual(err, null);
  802. done();
  803. });
  804. });
  805. it("should not fail (only head)", function(done) {
  806. helpers.get_render(rid, id, 6, true, false, function(err, hash, img) {
  807. assert.strictEqual(err, null);
  808. done();
  809. });
  810. });
  811. });
  812. describe("Networking: Cape", function() {
  813. it("should not fail (possible cape)", function(done) {
  814. helpers.get_cape(rid, id, function(err, hash, status, img) {
  815. assert.strictEqual(err, null);
  816. done();
  817. });
  818. });
  819. });
  820. describe("Errors", function() {
  821. before(function() {
  822. cache.get_redis().flushall();
  823. });
  824. if (id_type == "uuid") {
  825. it("uuid should be rate limited", function(done) {
  826. networking.get_profile(rid, id, function(err, profile) {
  827. assert.strictEqual(profile.error, "TooManyRequestsException");
  828. done();
  829. });
  830. });
  831. } else {
  832. it("username should NOT be rate limited (username)", function(done) {
  833. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  834. assert.strictEqual(err, null);
  835. done();
  836. });
  837. });
  838. }
  839. });
  840. })(id, id_type);
  841. }
  842. });