test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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('UUID/username', function() {
  16. before(function() {
  17. cache.get_redis().flushall();
  18. });
  19. describe('UUID', 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", function(done) {
  57. networking.get_profile("00000000000000000000000000000000", function(err, profile) {
  58. assert.strictEqual(err, 0);
  59. done();
  60. });
  61. });
  62. });
  63. describe('Avatar', function() {
  64. it("should be downloaded (uuid)", function(done) {
  65. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  66. assert.strictEqual(status, 2);
  67. done();
  68. });
  69. });
  70. it("should be local (uuid)", function(done) {
  71. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  72. assert.strictEqual(status, 1);
  73. done();
  74. });
  75. });
  76. it("should be checked (uuid)", function(done) {
  77. var original_cache_time = config.local_cache_time;
  78. config.local_cache_time = 0;
  79. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  80. assert.strictEqual(status, 2);
  81. config.local_cache_time = original_cache_time;
  82. done();
  83. });
  84. });
  85. it("should be downloaded (username)", function(done) {
  86. helpers.get_avatar(username, false, 160, function(err, status, image) {
  87. assert.strictEqual(status, 2);
  88. done();
  89. });
  90. });
  91. it("should be local (username)", function(done) {
  92. helpers.get_avatar(username, false, 160, function(err, status, image) {
  93. assert.strictEqual(status, 1);
  94. done();
  95. });
  96. });
  97. it("should be checked (username)", function(done) {
  98. var original_cache_time = config.local_cache_time;
  99. config.local_cache_time = 0;
  100. helpers.get_avatar(username, false, 160, function(err, status, image) {
  101. assert.strictEqual(status, 2);
  102. config.local_cache_time = original_cache_time;
  103. done();
  104. });
  105. });
  106. it("should not exist (but account does)", function(done) {
  107. // profile 'Alex'
  108. helpers.get_avatar("ec561538f3fd461daff5086b22154bce", false, 160, function(err, status, image) {
  109. assert.strictEqual(status, 3);
  110. done();
  111. });
  112. });
  113. });
  114. describe('Mojang Errors', function() {
  115. before(function() {
  116. cache.get_redis().flushall();
  117. });
  118. it("should be rate limited", function(done) {
  119. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  120. assert.strictEqual(err, null);
  121. done();
  122. });
  123. });
  124. it("should time out on profile download", function(done) {
  125. config.http_timeout = 1;
  126. networking.get_profile("069a79f444e94726a5befca90e38aaf5", function(err, profile) {
  127. assert.strictEqual(err.code, "ETIMEDOUT");
  128. config.http_timeout = 3000;
  129. done();
  130. });
  131. });
  132. it("should time out on skin download", function(done) {
  133. config.http_timeout = 1;
  134. networking.skin_file("http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", "face.png", "helm.png", function(err) {
  135. assert.strictEqual(err.code, "ETIMEDOUT");
  136. config.http_timeout = 3000;
  137. done();
  138. });
  139. });
  140. });
  141. });