test.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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 (finished === requests) {
  292. // all requests have finished
  293. done();
  294. }
  295. }
  296. function req() {
  297. request.get(url, function(error, res, body) {
  298. assert.ifError(error);
  299. assert.strictEqual(res.statusCode, 200);
  300. assert_headers(res);
  301. assert(res.headers["etag"]);
  302. assert.strictEqual(res.headers["content-type"], "image/png");
  303. assert(body);
  304. partDone();
  305. });
  306. }
  307. // make simultanous requests
  308. for (var j = 0; j < requests; j++) {
  309. req(j);
  310. }
  311. });
  312. var server_tests = {
  313. "avatar with existing username": {
  314. url: "http://localhost:3000/avatars/jeb_?size=16",
  315. etag: '"a846b82963"',
  316. crc32: 1623808067
  317. },
  318. "avatar with not existing username": {
  319. url: "http://localhost:3000/avatars/0?size=16",
  320. etag: '"steve"',
  321. crc32: 2416827277
  322. },
  323. "avatar with not existing username defaulting to alex": {
  324. url: "http://localhost:3000/avatars/0?size=16&default=alex",
  325. etag: '"alex"',
  326. crc32: 862751081
  327. },
  328. "avatar with not existing username defaulting to url": {
  329. url: "http://localhost:3000/avatars/0?size=16&default=http://example.com",
  330. crc32: 0,
  331. redirect: "http://example.com"
  332. },
  333. "helm avatar with existing username": {
  334. url: "http://localhost:3000/avatars/jeb_?size=16&helm",
  335. etag: '"a846b82963"',
  336. crc32: 646871998
  337. },
  338. "helm avatar with not existing username": {
  339. url: "http://localhost:3000/avatars/0?size=16&helm",
  340. etag: '"steve"',
  341. crc32: 2416827277
  342. },
  343. "helm avatar with not existing username defaulting to alex": {
  344. url: "http://localhost:3000/avatars/0?size=16&helm&default=alex",
  345. etag: '"alex"',
  346. crc32: 862751081
  347. },
  348. "helm avatar with not existing username defaulting to url": {
  349. url: "http://localhost:3000/avatars/0?size=16&helm&default=http://example.com",
  350. crc32: 0,
  351. redirect: "http://example.com"
  352. },
  353. "avatar with existing uuid": {
  354. url: "http://localhost:3000/avatars/853c80ef3c3749fdaa49938b674adae6?size=16",
  355. etag: '"a846b82963"',
  356. crc32: 1623808067
  357. },
  358. "avatar with not existing uuid": {
  359. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16",
  360. etag: '"steve"',
  361. crc32: 2416827277
  362. },
  363. "avatar with not existing uuid defaulting to alex": {
  364. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=alex",
  365. etag: '"alex"',
  366. crc32: 862751081
  367. },
  368. "avatar with not existing uuid defaulting to url": {
  369. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=http://example.com",
  370. crc32: 0,
  371. redirect: "http://example.com"
  372. },
  373. "helm avatar with existing uuid": {
  374. url: "http://localhost:3000/avatars/853c80ef3c3749fdaa49938b674adae6?size=16&helm",
  375. etag: '"a846b82963"',
  376. crc32: 646871998
  377. },
  378. "helm avatar with not existing uuid": {
  379. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm",
  380. etag: '"steve"',
  381. crc32: 2416827277
  382. },
  383. "helm avatar with not existing uuid defaulting to alex": {
  384. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm&default=alex",
  385. etag: '"alex"',
  386. crc32: 862751081
  387. },
  388. "helm avatar with not existing uuid defaulting to url": {
  389. url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm&default=http://example.com",
  390. crc32: 0,
  391. redirect: "http://example.com"
  392. },
  393. "cape with existing username": {
  394. url: "http://localhost:3000/capes/jeb_",
  395. etag: '"3f688e0e69"',
  396. crc32: 989800403
  397. },
  398. "cape with not existing username": {
  399. url: "http://localhost:3000/capes/0",
  400. crc32: 0
  401. },
  402. "cape with not existing username defaulting to url": {
  403. url: "http://localhost:3000/capes/0?default=http://example.com",
  404. crc32: 0,
  405. redirect: "http://example.com"
  406. },
  407. "cape with existing uuid": {
  408. url: "http://localhost:3000/capes/853c80ef3c3749fdaa49938b674adae6",
  409. etag: '"3f688e0e69"',
  410. crc32: 989800403
  411. },
  412. "cape with not existing uuid": {
  413. url: "http://localhost:3000/capes/00000000000000000000000000000000",
  414. crc32: 0
  415. },
  416. "cape with not existing uuid defaulting to url": {
  417. url: "http://localhost:3000/capes/00000000000000000000000000000000?default=http://example.com",
  418. crc32: 0,
  419. redirect: "http://example.com"
  420. },
  421. "skin with existing username": {
  422. url: "http://localhost:3000/skins/jeb_",
  423. etag: '"a846b82963"',
  424. crc32: 110922424
  425. },
  426. "skin with not existing username": {
  427. url: "http://localhost:3000/skins/0",
  428. etag: '"steve"',
  429. crc32: 981937087
  430. },
  431. "skin with not existing username defaulting to alex": {
  432. url: "http://localhost:3000/skins/0?default=alex",
  433. etag: '"alex"',
  434. crc32: 2298915739
  435. },
  436. "skin with not existing username defaulting to url": {
  437. url: "http://localhost:3000/skins/0?default=http://example.com",
  438. crc32: 0,
  439. redirect: "http://example.com"
  440. },
  441. "skin with existing uuid": {
  442. url: "http://localhost:3000/skins/853c80ef3c3749fdaa49938b674adae6",
  443. etag: '"a846b82963"',
  444. crc32: 110922424
  445. },
  446. "skin with not existing uuid": {
  447. url: "http://localhost:3000/skins/00000000000000000000000000000000",
  448. etag: '"steve"',
  449. crc32: 981937087
  450. },
  451. "skin with not existing uuid defaulting to alex": {
  452. url: "http://localhost:3000/skins/00000000000000000000000000000000?default=alex",
  453. etag: '"alex"',
  454. crc32: 2298915739
  455. },
  456. "skin with not existing uuid defaulting to url": {
  457. url: "http://localhost:3000/skins/00000000000000000000000000000000?default=http://example.com",
  458. crc32: 0,
  459. redirect: "http://example.com"
  460. },
  461. "head render with existing username": {
  462. url: "http://localhost:3000/renders/head/jeb_?scale=2",
  463. etag: '"a846b82963"',
  464. crc32: [353633671, 370672768]
  465. },
  466. "head render with not existing username": {
  467. url: "http://localhost:3000/renders/head/0?scale=2",
  468. etag: '"steve"',
  469. crc32: [883439147, 433083528]
  470. },
  471. "head render with not existing username defaulting to alex": {
  472. url: "http://localhost:3000/renders/head/0?scale=2&default=alex",
  473. etag: '"alex"',
  474. crc32: [1240086237, 1108800327]
  475. },
  476. "head render with not existing username defaulting to url": {
  477. url: "http://localhost:3000/renders/head/0?scale=2&default=http://example.com",
  478. crc32: 0,
  479. redirect: "http://example.com"
  480. },
  481. "helm head render with existing username": {
  482. url: "http://localhost:3000/renders/head/jeb_?scale=2&helm",
  483. etag: '"a846b82963"',
  484. crc32: [3456497067, 3490318764]
  485. },
  486. "helm head render with not existing username": {
  487. url: "http://localhost:3000/renders/head/0?scale=2&helm",
  488. etag: '"steve"',
  489. crc32: [1858563554, 2647471936]
  490. },
  491. "helm head render with not existing username defaulting to alex": {
  492. url: "http://localhost:3000/renders/head/0?scale=2&helm&default=alex",
  493. etag: '"alex"',
  494. crc32: [2963161105, 1769904825]
  495. },
  496. "helm head render with not existing username defaulting to url": {
  497. url: "http://localhost:3000/renders/head/0?scale=2&helm&default=http://example.com",
  498. crc32: 0,
  499. redirect: "http://example.com"
  500. },
  501. "head render with existing uuid": {
  502. url: "http://localhost:3000/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2",
  503. etag: '"a846b82963"',
  504. crc32: [353633671, 370672768]
  505. },
  506. "head render with not existing uuid": {
  507. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2",
  508. etag: '"steve"',
  509. crc32: [883439147, 433083528]
  510. },
  511. "head render with not existing uuid defaulting to alex": {
  512. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=alex",
  513. etag: '"alex"',
  514. crc32: [1240086237, 1108800327]
  515. },
  516. "head render with not existing uuid defaulting to url": {
  517. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=http://example.com",
  518. crc32: 0,
  519. redirect: "http://example.com"
  520. },
  521. "helm head render with existing uuid": {
  522. url: "http://localhost:3000/renders/head/853c80ef3c3749fdaa49938b674adae6?scale=2&helm",
  523. etag: '"a846b82963"',
  524. crc32: [3456497067, 3490318764]
  525. },
  526. "helm head render with not existing uuid": {
  527. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm",
  528. etag: '"steve"',
  529. crc32: [1858563554, 2647471936]
  530. },
  531. "helm head render with not existing uuid defaulting to alex": {
  532. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm&default=alex",
  533. etag: '"alex"',
  534. crc32: [2963161105, 1769904825]
  535. },
  536. "helm head render with not existing uuid defaulting to url": {
  537. url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm&default=http://example.com",
  538. crc32: 0,
  539. redirect: "http://example.com"
  540. },
  541. "body render with existing username": {
  542. url: "http://localhost:3000/renders/body/jeb_?scale=2",
  543. etag: '"a846b82963"',
  544. crc32: [1291941229, 2628108474]
  545. },
  546. "body render with not existing username": {
  547. url: "http://localhost:3000/renders/body/0?scale=2",
  548. etag: '"steve"',
  549. crc32: [2652947188, 2115706574]
  550. },
  551. "body render with not existing username defaulting to alex": {
  552. url: "http://localhost:3000/renders/body/0?scale=2&default=alex",
  553. etag: '"alex"',
  554. crc32: [407932087, 2516216042]
  555. },
  556. "body render with not existing username defaulting to url": {
  557. url: "http://localhost:3000/renders/body/0?scale=2&default=http://example.com",
  558. crc32: 0,
  559. redirect: "http://example.com"
  560. },
  561. "helm body render with existing username": {
  562. url: "http://localhost:3000/renders/body/jeb_?scale=2&helm",
  563. etag: '"a846b82963"',
  564. crc32: [3556188297, 4269754007]
  565. },
  566. "helm body render with not existing username": {
  567. url: "http://localhost:3000/renders/body/0?scale=2&helm",
  568. etag: '"steve"',
  569. crc32: [272191039, 542896675]
  570. },
  571. "helm body render with not existing username defaulting to alex": {
  572. url: "http://localhost:3000/renders/body/0?scale=2&helm&default=alex",
  573. etag: '"alex"',
  574. crc32: [737759773, 66512449]
  575. },
  576. "helm body render with not existing username defaulting to url": {
  577. url: "http://localhost:3000/renders/body/0?scale=2&helm&default=http://example.com",
  578. crc32: 0,
  579. redirect: "http://example.com"
  580. },
  581. "body render with existing uuid": {
  582. url: "http://localhost:3000/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2",
  583. etag: '"a846b82963"',
  584. crc32: [1291941229, 2628108474]
  585. },
  586. "body render with not existing uuid": {
  587. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2",
  588. etag: '"steve"',
  589. crc32: [2652947188, 2115706574]
  590. },
  591. "body render with not existing uuid defaulting to alex": {
  592. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&default=alex",
  593. etag: '"alex"',
  594. crc32: [407932087, 2516216042]
  595. },
  596. "body render with not existing uuid defaulting to url": {
  597. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&default=http://example.com",
  598. crc32: 0,
  599. redirect: "http://example.com"
  600. },
  601. "helm body render with existing uuid": {
  602. url: "http://localhost:3000/renders/body/853c80ef3c3749fdaa49938b674adae6?scale=2&helm",
  603. etag: '"a846b82963"',
  604. crc32: [3556188297, 4269754007]
  605. },
  606. "helm body render with not existing uuid": {
  607. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm",
  608. etag: '"steve"',
  609. crc32: [272191039, 542896675]
  610. },
  611. "helm body render with not existing uuid defaulting to alex": {
  612. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm&default=alex",
  613. etag: '"alex"',
  614. crc32: [737759773, 66512449]
  615. },
  616. "helm body render with not existing uuid defaulting to url": {
  617. url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm&default=http://example.com",
  618. crc32: 0,
  619. redirect: "http://example.com"
  620. },
  621. };
  622. for (var description in server_tests) {
  623. var location = server_tests[description];
  624. (function(location) {
  625. it("should return correct HTTP response for " + description, function(done) {
  626. request.get(location.url, {followRedirect: false, encoding: null}, function(error, res, body) {
  627. assert.ifError(error);
  628. assert_headers(res);
  629. assert(res.headers["x-storage-type"]);
  630. assert.strictEqual(res.headers["etag"], location.etag);
  631. var matches = false;
  632. if (location.crc32 instanceof Array) {
  633. for (var i = 0; i < location.crc32.length; i++) {
  634. if (location.crc32[i] === crc(body)) {
  635. matches = true;
  636. break;
  637. }
  638. }
  639. } else {
  640. matches = (location.crc32 === crc(body));
  641. }
  642. assert.ok(matches);
  643. assert.strictEqual(res.headers["location"], location.redirect);
  644. if (location.etag === undefined) {
  645. assert.strictEqual(res.statusCode, location.redirect ? 307 : 404);
  646. assert.strictEqual(res.headers["content-type"], "text/plain");
  647. done();
  648. } else {
  649. assert(res.headers["etag"]);
  650. assert.strictEqual(res.headers["content-type"], "image/png");
  651. assert.strictEqual(res.statusCode, 200);
  652. assert_cache(location.url, res.headers["etag"], function() {
  653. done();
  654. });
  655. }
  656. });
  657. });
  658. }(location));
  659. }
  660. it("should return a 422 (invalid size)", function(done) {
  661. var size = config.max_size + 1;
  662. request.get("http://localhost:3000/avatars/Jake_0?size=" + size, function(error, res, body) {
  663. assert.strictEqual(res.statusCode, 422);
  664. done();
  665. });
  666. });
  667. it("should return a 422 (invalid scale)", function(done) {
  668. var scale = config.max_scale + 1;
  669. request.get("http://localhost:3000/renders/head/Jake_0?scale=" + scale, function(error, res, body) {
  670. assert.strictEqual(res.statusCode, 422);
  671. done();
  672. });
  673. });
  674. it("should return a 422 (invalid render type)", function(done) {
  675. request.get("http://localhost:3000/renders/invalid/Jake_0", function(error, res, body) {
  676. assert.strictEqual(res.statusCode, 422);
  677. done();
  678. });
  679. });
  680. // testing all paths for Invalid UserID
  681. var locations = ["avatars", "skins", "capes", "renders/body", "renders/head"];
  682. for (var l in locations) {
  683. var location = locations[l];
  684. (function(location) {
  685. it("should return a 422 (invalid id " + location + ")", function(done) {
  686. request.get("http://localhost:3000/" + location + "/thisisaninvaliduuid", function(error, res, body) {
  687. assert.strictEqual(res.statusCode, 422);
  688. done();
  689. });
  690. });
  691. })(location);
  692. }
  693. after(function(done) {
  694. server.close(function() {
  695. done();
  696. });
  697. });
  698. });
  699. // we have to make sure that we test both a 32x64 and 64x64 skin
  700. describe("Networking: Render", function() {
  701. it("should not fail (username, 32x64 skin)", function(done) {
  702. helpers.get_render(rid, "md_5", 6, true, true, function(err, hash, img) {
  703. assert.strictEqual(err, null);
  704. done();
  705. });
  706. });
  707. it("should not fail (username, 64x64 skin)", function(done) {
  708. helpers.get_render(rid, "Jake_0", 6, true, true, function(err, hash, img) {
  709. assert.strictEqual(err, null);
  710. done();
  711. });
  712. });
  713. });
  714. describe("Networking: Cape", function() {
  715. it("should not fail (guaranteed cape)", function(done) {
  716. helpers.get_cape(rid, "Dinnerbone", function(err, hash, status, img) {
  717. assert.strictEqual(err, null);
  718. done();
  719. });
  720. });
  721. it("should already exist", function(done) {
  722. before(function() {
  723. cache.get_redis().flushall();
  724. });
  725. helpers.get_cape(rid, "Dinnerbone", function(err, hash, status, img) {
  726. assert.strictEqual(err, null);
  727. done();
  728. });
  729. });
  730. it("should not be found", function(done) {
  731. helpers.get_cape(rid, "Jake_0", function(err, hash, status, img) {
  732. assert.strictEqual(img, null);
  733. done();
  734. });
  735. });
  736. });
  737. describe("Networking: Skin", function() {
  738. it("should not fail", function(done) {
  739. helpers.get_cape(rid, "Jake_0", function(err, hash, status, img) {
  740. assert.strictEqual(err, null);
  741. done();
  742. });
  743. });
  744. it("should already exist", function(done) {
  745. before(function() {
  746. cache.get_redis().flushall();
  747. });
  748. helpers.get_cape(rid, "Jake_0", function(err, hash, status, img) {
  749. assert.strictEqual(err, null);
  750. done();
  751. });
  752. });
  753. });
  754. // DRY with uuid and username tests
  755. for (var i in ids) {
  756. var id = ids[i];
  757. var id_type = id.length > 16 ? "uuid" : "name";
  758. // needs an anonymous function because id and id_type aren't constant
  759. (function(id, id_type) {
  760. describe("Networking: Avatar", function() {
  761. before(function() {
  762. cache.get_redis().flushall();
  763. console.log("\n\nRunning tests with " + id_type + " '" + id + "'\n\n");
  764. });
  765. it("should be downloaded", function(done) {
  766. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  767. assert.strictEqual(status, 2);
  768. done();
  769. });
  770. });
  771. it("should be cached", function(done) {
  772. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  773. assert.strictEqual(status === 0 || status === 1, true);
  774. done();
  775. });
  776. });
  777. if (id.length > 16) {
  778. console.log("can't run 'checked' test due to Mojang's rate limits :(");
  779. } else {
  780. it("should be checked", function(done) {
  781. var original_cache_time = config.local_cache_time;
  782. config.local_cache_time = 0;
  783. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  784. assert.strictEqual(status, 3);
  785. config.local_cache_time = original_cache_time;
  786. done();
  787. });
  788. });
  789. }
  790. });
  791. describe("Networking: Skin", function() {
  792. it("should not fail (uuid)", function(done) {
  793. helpers.get_skin(rid, id, function(err, hash, status, img) {
  794. assert.strictEqual(err, null);
  795. done();
  796. });
  797. });
  798. });
  799. describe("Networking: Render", function() {
  800. it("should not fail (full body)", function(done) {
  801. helpers.get_render(rid, id, 6, true, true, function(err, hash, img) {
  802. assert.strictEqual(err, null);
  803. done();
  804. });
  805. });
  806. it("should not fail (only head)", function(done) {
  807. helpers.get_render(rid, id, 6, true, false, function(err, hash, img) {
  808. assert.strictEqual(err, null);
  809. done();
  810. });
  811. });
  812. });
  813. describe("Networking: Cape", function() {
  814. it("should not fail (possible cape)", function(done) {
  815. helpers.get_cape(rid, id, function(err, hash, status, img) {
  816. assert.strictEqual(err, null);
  817. done();
  818. });
  819. });
  820. });
  821. describe("Errors", function() {
  822. before(function() {
  823. cache.get_redis().flushall();
  824. });
  825. if (id_type == "uuid") {
  826. it("uuid should be rate limited", function(done) {
  827. networking.get_profile(rid, id, function(err, profile) {
  828. assert.strictEqual(profile.error, "TooManyRequestsException");
  829. done();
  830. });
  831. });
  832. } else {
  833. it("username should NOT be rate limited (username)", function(done) {
  834. helpers.get_avatar(rid, id, false, 160, function(err, status, image) {
  835. assert.strictEqual(err, null);
  836. done();
  837. });
  838. });
  839. }
  840. });
  841. })(id, id_type);
  842. }
  843. });