GettingProjectDocsTests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * decaffeinate suggestions:
  3. * DS101: Remove unnecessary use of Array.from
  4. * DS102: Remove unnecessary code created because of implicit returns
  5. * DS207: Consider shorter variations of null checks
  6. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  7. */
  8. const sinon = require("sinon");
  9. const chai = require("chai");
  10. chai.should();
  11. const {
  12. expect
  13. } = chai;
  14. const MockWebApi = require("./helpers/MockWebApi");
  15. const DocUpdaterClient = require("./helpers/DocUpdaterClient");
  16. const DocUpdaterApp = require("./helpers/DocUpdaterApp");
  17. describe("Getting documents for project", function() {
  18. before(function(done) {
  19. this.lines = ["one", "two", "three"];
  20. this.version = 42;
  21. return DocUpdaterApp.ensureRunning(done);
  22. });
  23. describe("when project state hash does not match", function() {
  24. before(function(done) {
  25. this.projectStateHash = DocUpdaterClient.randomId();
  26. [this.project_id, this.doc_id] = Array.from([DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]);
  27. MockWebApi.insertDoc(this.project_id, this.doc_id, {lines: this.lines, version: this.version});
  28. return DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
  29. if (error != null) { throw error; }
  30. return DocUpdaterClient.getProjectDocs(this.project_id, this.projectStateHash, (error, res, returnedDocs) => {
  31. this.res = res;
  32. this.returnedDocs = returnedDocs;
  33. return done();
  34. });
  35. });
  36. });
  37. return it("should return a 409 Conflict response", function() {
  38. return this.res.statusCode.should.equal(409);
  39. });
  40. });
  41. describe("when project state hash matches", function() {
  42. before(function(done) {
  43. this.projectStateHash = DocUpdaterClient.randomId();
  44. [this.project_id, this.doc_id] = Array.from([DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]);
  45. MockWebApi.insertDoc(this.project_id, this.doc_id, {lines: this.lines, version: this.version});
  46. return DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
  47. if (error != null) { throw error; }
  48. return DocUpdaterClient.getProjectDocs(this.project_id, this.projectStateHash, (error, res0, returnedDocs0) => {
  49. // set the hash
  50. this.res0 = res0;
  51. this.returnedDocs0 = returnedDocs0;
  52. return DocUpdaterClient.getProjectDocs(this.project_id, this.projectStateHash, (error, res, returnedDocs) => {
  53. // the hash should now match
  54. this.res = res;
  55. this.returnedDocs = returnedDocs;
  56. return done();
  57. });
  58. });
  59. });
  60. });
  61. it("should return a 200 response", function() {
  62. return this.res.statusCode.should.equal(200);
  63. });
  64. return it("should return the documents", function() {
  65. return this.returnedDocs.should.deep.equal([ {_id: this.doc_id, lines: this.lines, v: this.version} ]);
  66. });
  67. });
  68. return describe("when the doc has been removed", function() {
  69. before(function(done) {
  70. this.projectStateHash = DocUpdaterClient.randomId();
  71. [this.project_id, this.doc_id] = Array.from([DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]);
  72. MockWebApi.insertDoc(this.project_id, this.doc_id, {lines: this.lines, version: this.version});
  73. return DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
  74. if (error != null) { throw error; }
  75. return DocUpdaterClient.getProjectDocs(this.project_id, this.projectStateHash, (error, res0, returnedDocs0) => {
  76. // set the hash
  77. this.res0 = res0;
  78. this.returnedDocs0 = returnedDocs0;
  79. return DocUpdaterClient.deleteDoc(this.project_id, this.doc_id, (error, res, body) => {
  80. // delete the doc
  81. return DocUpdaterClient.getProjectDocs(this.project_id, this.projectStateHash, (error, res1, returnedDocs) => {
  82. // the hash would match, but the doc has been deleted
  83. this.res = res1;
  84. this.returnedDocs = returnedDocs;
  85. return done();
  86. });
  87. });
  88. });
  89. });
  90. });
  91. return it("should return a 409 Conflict response", function() {
  92. return this.res.statusCode.should.equal(409);
  93. });
  94. });
  95. });