test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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("Errors", function() {
  138. before(function() {
  139. cache.get_redis().flushall();
  140. });
  141. it("should be rate limited (uuid)", function(done) {
  142. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  143. assert.strictEqual(JSON.parse(err).error, "TooManyRequestsException");
  144. done();
  145. });
  146. });
  147. it("should NOT be rate limited (username)", function(done) {
  148. helpers.get_avatar(username, false, 160, function(err, status, image) {
  149. assert.strictEqual(err, null);
  150. done();
  151. });
  152. });
  153. it("should time out on uuid info download", function(done) {
  154. var original_timeout = config.http_timeout;
  155. config.http_timeout = 1;
  156. networking.get_skin_url("069a79f444e94726a5befca90e38aaf5", function(err, skin_url) {
  157. assert.strictEqual(err.code, "ETIMEDOUT");
  158. config.http_timeout = original_timeout;
  159. done();
  160. });
  161. });
  162. it("should time out on username info download", function(done) {
  163. var original_timeout = config.http_timeout;
  164. config.http_timeout = 1;
  165. networking.get_skin_url("redstone_sheep", function(err, skin_url) {
  166. assert.strictEqual(err.code, "ETIMEDOUT");
  167. config.http_timeout = original_timeout;
  168. done();
  169. });
  170. });
  171. it("should time out on skin download", function(done) {
  172. var original_timeout = config.http_timeout;
  173. config.http_timeout = 1;
  174. networking.get_skin("http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", function(err, img) {
  175. assert.strictEqual(err.code, "ETIMEDOUT");
  176. config.http_timeout = original_timeout;
  177. done();
  178. });
  179. });
  180. it("should not find the skin", function(done) {
  181. assert.doesNotThrow(function() {
  182. networking.get_skin("http://textures.minecraft.net/texture/this-does-not-exist", function(err, img) {
  183. assert.strictEqual(err, null); // no error here, but it shouldn't throw exceptions
  184. done();
  185. });
  186. });
  187. });
  188. it("should handle file updates on invalid files", function(done) {
  189. assert.doesNotThrow(function() {
  190. cache.update_timestamp("0123456789abcdef0123456789abcdef", "invalid-file.png");
  191. });
  192. done();
  193. });
  194. });
  195. });