Turn audio into a shareable video. forked from nypublicradio/audiogram

server-test.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. var tape = require("tape"),
  2. path = require("path"),
  3. fs = require("fs"),
  4. queue = require("d3").queue,
  5. request = require("supertest");
  6. var serverSettings = require("./patch-settings")({
  7. workingDirectory: path.join(__dirname, "tmp", "working"),
  8. maxUploadSize: 100000,
  9. storagePath: path.join(__dirname, "tmp", "storage"),
  10. worker: true
  11. });
  12. var server = require("../server");
  13. var longSample = path.join(__dirname, "data", "glazed-donut.mp3"),
  14. shortSample = path.join(__dirname, "data", "short.mp3");
  15. tape("Server static", function(test) {
  16. request(server)
  17. .get("/")
  18. .expect(200)
  19. .expect("Content-Type", /html/)
  20. .end(function(err, res){
  21. test.error(err);
  22. test.assert(res.text.match(/audiogram/i));
  23. test.end();
  24. });
  25. });
  26. tape("Server static JS", function(test) {
  27. request(server)
  28. .get("/js/bundle.js")
  29. .expect(200)
  30. .end(function(err, res){
  31. test.error(err);
  32. test.end();
  33. });
  34. });
  35. tape("404 1", function(test) {
  36. request(server)
  37. .get("/settings/index.js")
  38. .expect(404)
  39. .end(function(err, res){
  40. test.error(err);
  41. test.end();
  42. });
  43. });
  44. tape("404 2", function(test) {
  45. request(server)
  46. .get("/something.html")
  47. .expect(404)
  48. .end(function(err, res){
  49. test.error(err);
  50. test.end();
  51. });
  52. });
  53. tape("Server static background", function(test) {
  54. request(server)
  55. .get("/settings/backgrounds/nyc.png")
  56. .expect(200)
  57. .expect("Content-Type", /image/)
  58. .end(function(err, res){
  59. test.error(err);
  60. test.end();
  61. });
  62. });
  63. tape("Max size", function(test) {
  64. request(server)
  65. .post("/submit/")
  66. .attach("audio", longSample)
  67. .field("settings", "{}")
  68. .expect(500)
  69. .end(function(err, res){
  70. test.assert(res.text.match(/uploads are limited/i));
  71. test.end();
  72. });
  73. });
  74. tape("Missing file", function(test) {
  75. request(server)
  76. .post("/submit/")
  77. .type("json")
  78. .field("settings", "{}")
  79. .expect(500)
  80. .end(function(err, res){
  81. test.assert(res.text.match(/audio/i));
  82. test.end();
  83. });
  84. });
  85. tape("Broken settings", function(test) {
  86. request(server)
  87. .post("/submit/")
  88. .type("multipart/form-data")
  89. .field("settings", "a")
  90. .expect(500)
  91. .end(function(err, res){
  92. test.assert(res.text.match(/settings/i));
  93. test.end();
  94. });
  95. });
  96. tape("Successful submission", function(test) {
  97. var jobsFile = path.join(__dirname, "..", ".jobs");
  98. request(server)
  99. .post("/submit/")
  100. .attach("audio", shortSample)
  101. .field("settings", JSON.stringify({ test: true }))
  102. .expect(200)
  103. .end(function(err, res){
  104. var body = JSON.parse(res.text);
  105. test.assert("id" in body);
  106. queue(1)
  107. .defer(fs.readFile, path.join(serverSettings.workingDirectory, body.id, "audio"))
  108. .defer(fs.readFile, path.join(serverSettings.storagePath, "audio", body.id))
  109. .defer(checkStatus, body.id)
  110. .defer(checkJobsFile, body.id)
  111. .await(function(err){
  112. test.error(err);
  113. test.end();
  114. });
  115. });
  116. function checkStatus(id, cb) {
  117. request(server)
  118. .get("/status/" + id + "/")
  119. .expect(200)
  120. .end(function(err, res){
  121. test.equal(JSON.parse(res.text).status, "queued");
  122. cb(err);
  123. });
  124. }
  125. function checkJobsFile(id, cb) {
  126. fs.readFile(jobsFile, "utf8", function(err, raw){
  127. var jobs = JSON.parse(raw);
  128. test.equal(jobs.jobs.pop().id, id);
  129. fs.writeFile(jobsFile, JSON.stringify(jobs), cb);
  130. });
  131. }
  132. });
  133. // Cleanup
  134. tape.onFinish(function(){
  135. require("rimraf")(path.join(__dirname, "tmp"), function(err){
  136. if (err) {
  137. throw err;
  138. }
  139. });
  140. });