test.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. var assert = require('assert');
  2. var fs = require('fs');
  3. var networking = require('../modules/networking');
  4. var helpers = require('../modules/helpers');
  5. var config = require('../modules/config');
  6. var skins = require('../modules/skins');
  7. var cache = require("../modules/cache");
  8. // we don't want tests to fail because of slow internet
  9. config.http_timeout = 3000;
  10. var uuids = fs.readFileSync('test/uuids.txt').toString().split("\n");
  11. var usernames = fs.readFileSync('test/usernames.txt').toString().split("\n");
  12. // Get a random UUID + username in order to prevent rate limiting
  13. var uuid = uuids[Math.round(Math.random() * (uuids.length - 1))];
  14. var username = usernames[Math.round(Math.random() * (usernames.length - 1))];
  15. describe('Crafatar', function() {
  16. before(function() {
  17. cache.get_redis().flushall();
  18. });
  19. describe('UUID/username', function() {
  20. it("should be an invalid uuid", function(done) {
  21. assert.strictEqual(helpers.uuid_valid("g098cb60fa8e427cb299793cbd302c9a"), false);
  22. done();
  23. });
  24. it("should be an invalid uuid", function(done) {
  25. assert.strictEqual(helpers.uuid_valid(""), false);
  26. done();
  27. });
  28. it("should be an invalid username", function(done) {
  29. assert.strictEqual(helpers.uuid_valid("usernäme"), false);
  30. done();
  31. });
  32. it("should be an invalid username", function(done) {
  33. assert.strictEqual(helpers.uuid_valid("user-name"), false);
  34. done();
  35. });
  36. it("should be an invalid username", function(done) {
  37. assert.strictEqual(helpers.uuid_valid("ThisNameIsTooLong"), false);
  38. done();
  39. });
  40. it("should be a valid uuid", function(done) {
  41. assert.strictEqual(helpers.uuid_valid("0098cb60fa8e427cb299793cbd302c9a"), true);
  42. done();
  43. });
  44. it("should be a valid uuid", function(done) {
  45. assert.strictEqual(helpers.uuid_valid("0098cb60-fa8e-427c-b299-793cbd302c9a"), true);
  46. done();
  47. });
  48. it("should be a valid username", function(done) {
  49. assert.strictEqual(helpers.uuid_valid("__niceUs3rname__"), true);
  50. done();
  51. });
  52. it("should be a valid username", function(done) {
  53. assert.strictEqual(helpers.uuid_valid("a"), true);
  54. done();
  55. });
  56. it("should not exist (uuid)", function(done) {
  57. networking.get_skin_url("00000000000000000000000000000000", function(err, profile) {
  58. assert.strictEqual(err, 0);
  59. done();
  60. });
  61. });
  62. it("should not exist (username)", function(done) {
  63. networking.get_skin_url("Steve", function(err, profile) {
  64. assert.strictEqual(err, 0);
  65. done();
  66. });
  67. });
  68. });
  69. describe('Networking: Avatar', function() {
  70. it("should be downloaded (uuid)", function(done) {
  71. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  72. assert.strictEqual(status, 2);
  73. done();
  74. });
  75. });
  76. it("should be cached (uuid)", function(done) {
  77. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  78. assert.strictEqual(status, 1);
  79. done();
  80. });
  81. });
  82. /* We can't test this because of mojang's rate limits :(
  83. it("should be checked (uuid)", function(done) {
  84. var original_cache_time = config.local_cache_time;
  85. config.local_cache_time = 0;
  86. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  87. assert.strictEqual(status, 3);
  88. config.local_cache_time = original_cache_time;
  89. done();
  90. });
  91. });
  92. */
  93. it("should be downloaded (username)", function(done) {
  94. helpers.get_avatar(username, false, 160, function(err, status, image) {
  95. assert.strictEqual(status, 2);
  96. done();
  97. });
  98. });
  99. it("should be cached (username)", function(done) {
  100. helpers.get_avatar(username, false, 160, function(err, status, image) {
  101. assert.strictEqual(status, 1);
  102. done();
  103. });
  104. });
  105. it("should be checked (username)", function(done) {
  106. var original_cache_time = config.local_cache_time;
  107. config.local_cache_time = 0;
  108. helpers.get_avatar(username, false, 160, function(err, status, image) {
  109. assert.strictEqual(status, 3);
  110. config.local_cache_time = original_cache_time;
  111. done();
  112. });
  113. });
  114. it("should not exist (but account does)", function(done) {
  115. // profile 'Alex'
  116. helpers.get_avatar("ec561538f3fd461daff5086b22154bce", false, 160, function(err, status, image) {
  117. assert.strictEqual(status, 3);
  118. done();
  119. });
  120. });
  121. it("should default to Alex", function(done) {
  122. assert.strictEqual(skins.default_skin("ec561538f3fd461daff5086b22154bce"), "alex");
  123. done();
  124. });
  125. it("should default to Steve", function(done) {
  126. assert.strictEqual(skins.default_skin("b8ffc3d37dbf48278f69475f6690aabd"), "steve");
  127. done();
  128. });
  129. });
  130. describe('Mojang Errors', function() {
  131. before(function() {
  132. cache.get_redis().flushall();
  133. });
  134. it("should be rate limited", function(done) {
  135. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  136. assert.strictEqual(err, null);
  137. done();
  138. });
  139. });
  140. it("should time out on uuid info download", function(done) {
  141. var original_timeout = config.http_timeout;
  142. config.http_timeout = 1;
  143. networking.get_skin_url("069a79f444e94726a5befca90e38aaf5", function(err, skin_url) {
  144. assert.strictEqual(err.code, "ETIMEDOUT");
  145. config.http_timeout = original_timeout;
  146. done();
  147. });
  148. });
  149. it("should time out on username info download", function(done) {
  150. var original_timeout = config.http_timeout;
  151. config.http_timeout = 1;
  152. networking.get_skin_url("redstone_sheep", function(err, skin_url) {
  153. assert.strictEqual(err.code, "ETIMEDOUT");
  154. config.http_timeout = original_timeout;
  155. done();
  156. });
  157. });
  158. it("should time out on skin download", function(done) {
  159. var original_timeout = config.http_timeout;
  160. config.http_timeout = 1;
  161. networking.skin_file("http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", "face.png", "helm.png", function(err) {
  162. assert.strictEqual(err.code, "ETIMEDOUT");
  163. config.http_timeout = original_timeout;
  164. done();
  165. });
  166. });
  167. });
  168. });