2
0

test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 uuid", function(done) {
  29. assert.strictEqual(helpers.uuid_valid("0098cb60-fa8e-427c-b299-793cbd302c9a"), false);
  30. done();
  31. });
  32. it("should be an invalid username", function(done) {
  33. assert.strictEqual(helpers.uuid_valid("usernäme"), false);
  34. done();
  35. });
  36. it("should be an invalid username", function(done) {
  37. assert.strictEqual(helpers.uuid_valid("user-name"), false);
  38. done();
  39. });
  40. it("should be an invalid username", function(done) {
  41. assert.strictEqual(helpers.uuid_valid("ThisNameIsTooLong"), false);
  42. done();
  43. });
  44. it("should be a valid uuid", function(done) {
  45. assert.strictEqual(helpers.uuid_valid("0098cb60fa8e427cb299793cbd302c9a"), 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 not exist", function(done) {
  53. networking.get_profile("00000000000000000000000000000000", function(err, profile) {
  54. assert.strictEqual(err, 0);
  55. done();
  56. });
  57. });
  58. });
  59. describe('Avatar', function() {
  60. it("should be downloaded (uuid)", function(done) {
  61. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  62. assert.strictEqual(status, 2);
  63. done();
  64. });
  65. });
  66. it("should be local (uuid)", function(done) {
  67. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  68. assert.strictEqual(status, 1);
  69. done();
  70. });
  71. });
  72. it("should be downloaded (username)", function(done) {
  73. helpers.get_avatar(username, false, 160, function(err, status, image) {
  74. assert.strictEqual(status, 2);
  75. done();
  76. });
  77. });
  78. it("should be local (username)", function(done) {
  79. helpers.get_avatar(username, false, 160, function(err, status, image) {
  80. assert.strictEqual(status, 1);
  81. done();
  82. });
  83. });
  84. it("should not exist (but account does)", function(done) {
  85. // profile 'Alex'
  86. helpers.get_avatar("ec561538f3fd461daff5086b22154bce", false, 160, function(err, status, image) {
  87. assert.strictEqual(status, 3);
  88. done();
  89. });
  90. });
  91. });
  92. describe('Mojang Errors', function() {
  93. before(function() {
  94. cache.get_redis().flushall();
  95. });
  96. it("should be rate limited", function(done) {
  97. helpers.get_avatar(uuid, false, 160, function(err, status, image) {
  98. assert.strictEqual(err, null);
  99. done();
  100. });
  101. });
  102. it("should time out on profile download", function(done) {
  103. config.http_timeout = 1;
  104. networking.get_profile("069a79f444e94726a5befca90e38aaf5", function(err, profile) {
  105. assert.strictEqual(err.code, "ETIMEDOUT");
  106. config.http_timeout = 3000;
  107. done();
  108. });
  109. });
  110. it("should time out on skin download", function(done) {
  111. config.http_timeout = 1;
  112. networking.skin_file("http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", "face.png", "helm.png", function(err) {
  113. assert.strictEqual(err.code, "ETIMEDOUT");
  114. config.http_timeout = 3000;
  115. done();
  116. });
  117. });
  118. });
  119. });