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

server-test.js 4.3KB

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