RedactDocumentSource_test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. "use strict";
  2. if (!module.parent) return require.cache[__filename] = 0, (new(require("mocha"))()).addFile(__filename).ui("exports").run(process.exit);
  3. var assert = require("assert"),
  4. pipeline = require("../../../../lib/pipeline/"),
  5. RedactDocumentSource = pipeline.documentSources.RedactDocumentSource,
  6. CursorDocumentSource = pipeline.documentSources.CursorDocumentSource,
  7. ArrayRunner = require("../../../../lib/query/ArrayRunner");
  8. var exampleRedact = {$cond:{
  9. if:{$gt:[0,4]},
  10. then:"$$DESCEND",
  11. else:"$$PRUNE"
  12. }};
  13. function createCursorDocumentSource (input) {
  14. if (!input || input.constructor !== Array) throw new Error("invalid");
  15. return new CursorDocumentSource(null, new ArrayRunner(input), null);
  16. }
  17. function createRedactDocumentSource (src, expression) {
  18. var rds = RedactDocumentSource.createFromJson(expression);
  19. rds.setSource(src);
  20. return rds;
  21. }
  22. module.exports = {
  23. "RedactDocumentSource": {
  24. "constructor()": {
  25. "should not throw Error when constructing without args": function testConstructor() {
  26. assert.doesNotThrow(function() {
  27. new RedactDocumentSource();
  28. });
  29. }
  30. },
  31. "#getSourceName()": {
  32. "should return the correct source name; $redact": function testSourceName() {
  33. var rds = new RedactDocumentSource();
  34. assert.strictEqual(rds.getSourceName(), "$redact");
  35. }
  36. },
  37. "#getNext()": {
  38. "should return EOF": function testEOF(next) {
  39. var rds = RedactDocumentSource.createFromJson(exampleRedact);
  40. rds.setSource({
  41. getNext: function getNext(cb) {
  42. return cb(null, null);
  43. }
  44. });
  45. rds.getNext(function(err, doc) {
  46. assert.equal(null, doc);
  47. next();
  48. });
  49. },
  50. "should return Error in callback": function testError(next) {
  51. var rds = RedactDocumentSource.createFromJson({$cond:{
  52. if:{$gt:[0,{$add:["$a", 3]}]},
  53. then:"$$DESCEND",
  54. else:"$$PRUNE"
  55. }});
  56. rds.setSource(createCursorDocumentSource([{a:"foo"}]));
  57. rds.getNext(function(err, doc) {
  58. assert(err, "Expected Error");
  59. next();
  60. });
  61. },
  62. "iterator state accessors consistently report the source is exhausted": function assertExhausted() {
  63. var input = [{}];
  64. var cds = createCursorDocumentSource(input);
  65. var rds = RedactDocumentSource.createFromJson(exampleRedact);
  66. rds.setSource(cds);
  67. rds.getNext(function(err, actual) {
  68. rds.getNext(function(err, actual1) {
  69. assert.equal(null, actual1);
  70. rds.getNext(function(err, actual2) {
  71. assert.equal(null, actual2);
  72. rds.getNext(function(err, actual3) {
  73. assert.equal(null, actual3);
  74. });
  75. });
  76. });
  77. });
  78. },
  79. "callback is required": function requireCallback() {
  80. var rds = new RedactDocumentSource();
  81. assert.throws(rds.getNext.bind(rds));
  82. },
  83. },
  84. "#optimize()": {
  85. "Optimize the expression": function optimizeProject() {
  86. var rds = RedactDocumentSource.createFromJson(exampleRedact);
  87. assert.doesNotThrow(rds.optimize.bind(rds));
  88. }
  89. },
  90. "#createFromJson()": {
  91. "should error if called with non-object": function testNonObjectPassed() {
  92. //Empty args
  93. assert.throws(function() {
  94. RedactDocumentSource.createFromJson();
  95. });
  96. //Invalid spec
  97. assert.throws(function() {
  98. RedactDocumentSource.createFromJson({$invalidOperator: 1});
  99. });
  100. }
  101. },
  102. "#redact()": {
  103. "should redact subsection where tag does not match": function (done) {
  104. var cds = createCursorDocumentSource([{
  105. _id: 1,
  106. title: "123 Department Report",
  107. tags: ["G", "STLW"],
  108. year: 2014,
  109. subsections: [
  110. {
  111. subtitle: "Section 1: Overview",
  112. tags: ["SI", "G"],
  113. content: "Section 1: This is the content of section 1."
  114. },
  115. {
  116. subtitle: "Section 2: Analysis",
  117. tags: ["STLW"],
  118. content: "Section 2: This is the content of section 2."
  119. },
  120. {
  121. subtitle: "Section 3: Budgeting",
  122. tags: ["TK"],
  123. content: {
  124. text: "Section 3: This is the content of section3.",
  125. tags: ["HCS"]
  126. }
  127. }
  128. ]
  129. }]);
  130. var expression = {$cond:{
  131. if:{$gt: [{$size: {$setIntersection: ["$tags", [ "STLW", "G" ]]}},0]},
  132. then:"$$DESCEND",
  133. else:"$$PRUNE"
  134. }};
  135. var rds = createRedactDocumentSource(cds, expression);
  136. var result = {
  137. "_id": 1,
  138. "title": "123 Department Report",
  139. "tags": ["G", "STLW"],
  140. "year": 2014,
  141. "subsections": [{
  142. "subtitle": "Section 1: Overview",
  143. "tags": ["SI", "G"],
  144. "content": "Section 1: This is the content of section 1."
  145. }, {
  146. "subtitle": "Section 2: Analysis",
  147. "tags": ["STLW"],
  148. "content": "Section 2: This is the content of section 2."
  149. }]
  150. };
  151. rds.getNext(function (err, actual) {
  152. assert.deepEqual(actual, result);
  153. done();
  154. });
  155. },
  156. "should redact an entire subsection based on a defined access level": function (done) {
  157. var cds = createCursorDocumentSource([{
  158. _id: 1,
  159. level: 1,
  160. acctId: "xyz123",
  161. cc: {
  162. level: 5,
  163. type: "yy",
  164. expDate: new Date("2015-11-01"),
  165. billingAddr: {
  166. level: 5,
  167. addr1: "123 ABC Street",
  168. city: "Some City"
  169. },
  170. shippingAddr: [
  171. {
  172. level: 3,
  173. addr1: "987 XYZ Ave",
  174. city: "Some City"
  175. },
  176. {
  177. level: 3,
  178. addr1: "PO Box 0123",
  179. city: "Some City"
  180. }
  181. ]
  182. },
  183. status: "A"
  184. }]);
  185. var expression = {$cond:{
  186. if:{$eq:["$level",5]},
  187. then:"$$PRUNE",
  188. else:"$$DESCEND"
  189. }};
  190. var rds = createRedactDocumentSource(cds, expression);
  191. var result = {
  192. _id:1,
  193. level:1,
  194. acctId:"xyz123",
  195. status:"A"
  196. };
  197. rds.getNext(function (err, actual) {
  198. assert.deepEqual(actual, result);
  199. done();
  200. });
  201. }
  202. }
  203. }
  204. };