test.js 7.8 KB

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