test.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. var assert = require("assert");
  2. var fs = require("fs");
  3. var networking = require("../modules/networking");
  4. var helpers = require("../modules/helpers");
  5. var logging = require("../modules/logging");
  6. var config = require("../modules/config");
  7. var skins = require("../modules/skins");
  8. var cache = require("../modules/cache");
  9. var renders = require("../modules/renders");
  10. // we don't want tests to fail because of slow internet
  11. config.http_timeout *= 3;
  12. // no spam
  13. logging.log = function(){};
  14. var uuids = fs.readFileSync("test/uuids.txt").toString().split(/\r?\n/);
  15. var usernames = fs.readFileSync("test/usernames.txt").toString().split(/\r?\n/);
  16. // Get a random UUID + username in order to prevent rate limiting
  17. var uuid = uuids[Math.round(Math.random() * (uuids.length - 1))];
  18. console.log("using uuid '" + uuid + "'");
  19. var username = usernames[Math.round(Math.random() * (usernames.length - 1))];
  20. console.log("using username '" + username + "'");
  21. describe("Crafatar", function() {
  22. // we might have to make 2 HTTP requests
  23. this.timeout(config.http_timeout * 2 + 50);
  24. before(function() {
  25. cache.get_redis().flushall();
  26. });
  27. describe("UUID/username", function() {
  28. it("should be an invalid uuid", function(done) {
  29. assert.strictEqual(helpers.uuid_valid("g098cb60fa8e427cb299793cbd302c9a"), false);
  30. done();
  31. });
  32. it("should be an invalid uuid", function(done) {
  33. assert.strictEqual(helpers.uuid_valid(""), false);
  34. done();
  35. });
  36. it("should be an invalid username", function(done) {
  37. assert.strictEqual(helpers.uuid_valid("usernäme"), false);
  38. done();
  39. });
  40. it("should be an invalid username", function(done) {
  41. assert.strictEqual(helpers.uuid_valid("user-name"), false);
  42. done();
  43. });
  44. it("should be an invalid username", function(done) {
  45. assert.strictEqual(helpers.uuid_valid("ThisNameIsTooLong"), false);
  46. done();
  47. });
  48. it("should be a valid uuid", function(done) {
  49. assert.strictEqual(helpers.uuid_valid("0098cb60fa8e427cb299793cbd302c9a"), true);
  50. done();
  51. });
  52. it("should be a valid uuid", function(done) {
  53. assert.strictEqual(helpers.uuid_valid("1DCEF164FF0A47F2B9A691385C774EE7"), true);
  54. done();
  55. });
  56. it("should be a valid uuid", function(done) {
  57. assert.strictEqual(helpers.uuid_valid("0098cb60-fa8e-427c-b299-793cbd302c9a"), true);
  58. done();
  59. });
  60. it("should be a valid username", function(done) {
  61. assert.strictEqual(helpers.uuid_valid("__niceUs3rname__"), true);
  62. done();
  63. });
  64. it("should be a valid username", function(done) {
  65. assert.strictEqual(helpers.uuid_valid("a"), true);
  66. done();
  67. });
  68. it("should not exist (uuid)", function(done) {
  69. networking.get_skin_url("00000000000000000000000000000000", function(err, profile) {
  70. assert.strictEqual(err, null);
  71. done();
  72. });
  73. });
  74. it("should not exist (username)", function(done) {
  75. networking.get_skin_url("Steve", function(err, profile) {
  76. assert.strictEqual(err, null);
  77. done();
  78. });
  79. });
  80. });
  81. describe("Networking: Avatar", function() {
  82. it("should be downloaded (uuid)", function(done) {
  83. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  84. assert.strictEqual(status, 2);
  85. done();
  86. });
  87. });
  88. it("should be cached (uuid)", function(done) {
  89. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  90. assert.strictEqual(status === 0 || status === 1, true);
  91. done();
  92. });
  93. });
  94. /* We can't test this because of mojang's rate limits :(
  95. it("should be checked (uuid)", function(done) {
  96. var original_cache_time = config.local_cache_time;
  97. config.local_cache_time = 0;
  98. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  99. assert.strictEqual(status, 3);
  100. config.local_cache_time = original_cache_time;
  101. done();
  102. });
  103. });
  104. */
  105. it("should be downloaded (username)", function(done) {
  106. helpers.get_avatar(username, false, 160, function(err, status, image) {
  107. assert.strictEqual(status, 2);
  108. done();
  109. });
  110. });
  111. it("should be cached (username)", function(done) {
  112. helpers.get_avatar(username, false, 160, function(err, status, image) {
  113. assert.strictEqual(status === 0 || status === 1, true);
  114. done();
  115. });
  116. });
  117. it("should be checked (username)", function(done) {
  118. var original_cache_time = config.local_cache_time;
  119. config.local_cache_time = 0;
  120. helpers.get_avatar(username, false, 160, function(err, status, image) {
  121. assert.strictEqual(status, 3);
  122. config.local_cache_time = original_cache_time;
  123. done();
  124. });
  125. });
  126. it("should not exist (but account does)", function(done) {
  127. // profile "Alex"
  128. helpers.get_avatar("ec561538f3fd461daff5086b22154bce", false, 160, function(err, status, image) {
  129. assert.strictEqual(status, 2);
  130. done();
  131. });
  132. });
  133. it("should default to Alex", function(done) {
  134. assert.strictEqual(skins.default_skin("ec561538f3fd461daff5086b22154bce"), "alex");
  135. done();
  136. });
  137. it("should default to Steve", function(done) {
  138. assert.strictEqual(skins.default_skin("b8ffc3d37dbf48278f69475f6690aabd"), "steve");
  139. done();
  140. });
  141. });
  142. describe("Networking: Skin", function() {
  143. it("should not fail (uuid)", function(done) {
  144. helpers.get_skin(uuid, function(err, hash, img) {
  145. assert.strictEqual(err, null);
  146. done();
  147. });
  148. });
  149. it("should not fail (username)", function(done) {
  150. helpers.get_skin(username, function(err, hash, img) {
  151. assert.strictEqual(err, null);
  152. done();
  153. });
  154. });
  155. });
  156. describe("Networking: Renders", function() {
  157. describe("Head", function() {
  158. it("should not fail (username)", function(done) {
  159. helpers.get_render(username, 6, true, false, function(err, status, hash, img) {
  160. assert.strictEqual(err, null);
  161. done();
  162. });
  163. });
  164. it("should not fail (uuid)", function(done) {
  165. helpers.get_render(username, 6, true, false, function(err, status, hash, img) {
  166. assert.strictEqual(err, null);
  167. done();
  168. });
  169. });
  170. });
  171. describe("Body", function() {
  172. it("should not fail (username)", function(done) {
  173. helpers.get_render(username, 6, true, true, function(err, status, hash, img) {
  174. assert.strictEqual(err, null);
  175. done();
  176. });
  177. });
  178. it("should not fail (uuid)", function(done) {
  179. helpers.get_render(username, 6, true, true, function(err, status, hash, img) {
  180. assert.strictEqual(err, null);
  181. done();
  182. });
  183. });
  184. });
  185. });
  186. describe("Errors", function() {
  187. before(function() {
  188. cache.get_redis().flushall();
  189. });
  190. it("should be rate limited (uuid)", function(done) {
  191. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  192. assert.strictEqual(JSON.parse(err).error, "TooManyRequestsException");
  193. done();
  194. });
  195. });
  196. it("should NOT be rate limited (username)", function(done) {
  197. helpers.get_avatar(username, false, 160, function(err, status, image) {
  198. assert.strictEqual(err, null);
  199. done();
  200. });
  201. });
  202. it("should time out on uuid info download", function(done) {
  203. var original_timeout = config.http_timeout;
  204. config.http_timeout = 1;
  205. networking.get_skin_url("069a79f444e94726a5befca90e38aaf5", function(err, skin_url) {
  206. assert.strictEqual(err.code, "ETIMEDOUT");
  207. config.http_timeout = original_timeout;
  208. done();
  209. });
  210. });
  211. it("should time out on username info download", function(done) {
  212. var original_timeout = config.http_timeout;
  213. config.http_timeout = 1;
  214. networking.get_skin_url("redstone_sheep", function(err, skin_url) {
  215. assert.strictEqual(err.code, "ETIMEDOUT");
  216. config.http_timeout = original_timeout;
  217. done();
  218. });
  219. });
  220. it("should time out on skin download", function(done) {
  221. var original_timeout = config.http_timeout;
  222. config.http_timeout = 1;
  223. networking.get_skin("http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", function(err, img) {
  224. assert.strictEqual(err.code, "ETIMEDOUT");
  225. config.http_timeout = original_timeout;
  226. done();
  227. });
  228. });
  229. it("should not find the skin", function(done) {
  230. assert.doesNotThrow(function() {
  231. networking.get_skin("http://textures.minecraft.net/texture/this-does-not-exist", function(err, img) {
  232. assert.strictEqual(err, null); // no error here, but it shouldn't throw exceptions
  233. done();
  234. });
  235. });
  236. });
  237. it("should handle file updates on invalid files", function(done) {
  238. assert.doesNotThrow(function() {
  239. cache.update_timestamp("0123456789abcdef0123456789abcdef", "invalid-file.png");
  240. });
  241. done();
  242. });
  243. });
  244. });