test.js 31 KB

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