test.js 7.7 KB

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