test.js 28 KB

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