test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. var request = require('supertest');
  2. var asset = require('assert');
  3. var should = require('should');
  4. var fs = require('fs')
  5. var uuids = fs.readFileSync('uuids.txt').toString().split("\n");
  6. // Get a random UUID in order to prevent rate limiting
  7. var uuid = uuids[Math.floor((Math.random() * 200) + 1)];
  8. // Only deletes files, doesn't delete directory.
  9. var deleteFolderRecursive = function(path) {
  10. if( fs.existsSync(path) ) {
  11. fs.readdirSync(path).forEach(function(file,index){
  12. var curPath = path + "/" + file;
  13. if(fs.lstatSync(curPath).isDirectory()) {
  14. deleteFolderRecursive(curPath);
  15. } else {
  16. fs.unlinkSync(curPath);
  17. }
  18. });
  19. }
  20. };
  21. describe('Avatar Serving', function(){
  22. before(function() {
  23. deleteFolderRecursive('../skins/');
  24. })
  25. describe('UUID', function(){
  26. it("should respond with a 422", function(done){
  27. request('http://localhost:3000')
  28. .get('/avatars/invaliduuid')
  29. .expect(422)
  30. .end(function(err,res) {
  31. if (err) throw err;
  32. res.statusCode.should.eql(422);
  33. done();
  34. });
  35. });
  36. it("should respond with a 404", function(done){
  37. request('http://localhost:3000')
  38. .get('/avatars/2d5aa9cdaeb049189930461fc9b91dd5')
  39. .expect(404)
  40. .end(function(err,res) {
  41. if (err) throw err;
  42. res.statusCode.should.eql(404);
  43. done();
  44. });
  45. });
  46. it("should be downloaded", function(done){
  47. request('http://localhost:3000')
  48. .get('/avatars/' + uuid)
  49. .expect(200)
  50. .expect('X-Storage-Type', "downloaded")
  51. .end(function(err,res) {
  52. if (err) throw err;
  53. res.statusCode.should.eql(200);
  54. done();
  55. });
  56. });
  57. it("should respond with a valid avatar", function(done){
  58. request('http://localhost:3000')
  59. .get('/avatars/' + uuid)
  60. .expect(200)
  61. .expect('Content-Type', "image/png")
  62. .end(function(err,res) {
  63. if (err) throw err;
  64. res.statusCode.should.eql(200);
  65. done();
  66. });
  67. });
  68. it("should should be locally saved", function(done){
  69. request('http://localhost:3000')
  70. .get('/avatars/' + uuid)
  71. .expect(200)
  72. .expect('X-Storage-Type', "local")
  73. .end(function(err,res) {
  74. if (err) throw err;
  75. res.statusCode.should.eql(200);
  76. done();
  77. });
  78. });
  79. it("should should be rate limited", function(done){
  80. deleteFolderRecursive('../skins/');
  81. request('http://localhost:3000')
  82. .get('/avatars/' + uuid)
  83. .expect(404)
  84. .end(function(err,res) {
  85. if (err) throw err;
  86. res.statusCode.should.eql(404);
  87. done();
  88. });
  89. });
  90. });
  91. });