test.js 7.4 KB

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