not-node 6.3.49 → 6.3.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-node",
3
- "version": "6.3.49",
3
+ "version": "6.3.51",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -56,8 +56,6 @@
56
56
  "jsonwebtoken": "^9.0.0",
57
57
  "lower-case": "*",
58
58
  "method-override": "^3.0.0",
59
- "mock-require": "^3.0.3",
60
- "mongoose": "*",
61
59
  "mongoose-validator": "*",
62
60
  "nconf": "*",
63
61
  "not-config": "*",
@@ -91,7 +89,9 @@
91
89
  "jsdoc": "^4.0.0",
92
90
  "mocha": "*",
93
91
  "mocha-suppress-logs": "^0.3.1",
94
- "mongodb-memory-server": "^8.11.0",
92
+ "mock-require": "^3.0.3",
93
+ "mongodb-memory-server": "^9.1.6",
94
+ "mongoose": "^8.1.1",
95
95
  "not-error": "^0.2.9",
96
96
  "not-validation": "^0.0.9",
97
97
  "npm-run-all": "^4.1.5",
package/src/common.js CHANGED
@@ -381,23 +381,26 @@ module.exports.getValueFromEnv = getValueFromEnv;
381
381
  * @param {object} sign signature object {fieldName: someValueOfTargetType}
382
382
  * @param {boolean} [strict=true] if you need exact properties as in signature, when false - properties not described in signature are ok
383
383
  * @param {boolean} [typeStrict=true] compares types of properties in obj and signature
384
+ * @param {boolean} [valueStrict=false] compares values of properties in obj and signature
384
385
  * @return {boolean} true if object structured as signature
385
386
  */
386
387
  const compareObjectSignatures = (
387
388
  obj,
388
389
  sign,
389
390
  strict = true,
390
- typeStrict = true
391
+ typeStrict = true,
392
+ valueStrict = false
391
393
  ) => {
392
394
  const objKeys = Object.keys(obj);
393
395
  const signKeys = Object.keys(sign);
394
396
  const checkKey = (key) => {
395
397
  if (objKeys.includes(key)) {
396
- if (typeStrict) {
397
- return typeof obj[key] === typeof sign[key];
398
- } else {
399
- return true;
398
+ const isTypeStrict = typeof obj[key] === typeof sign[key];
399
+ const isValueStrict = obj[key] === sign[key];
400
+ if (typeStrict && !isTypeStrict) {
401
+ return false;
400
402
  }
403
+ return valueStrict ? isValueStrict : true;
401
404
  } else {
402
405
  return false;
403
406
  }
package/src/form/form.js CHANGED
@@ -1,3 +1,4 @@
1
+ const Schema = require("mongoose").Schema;
1
2
  const validator = require("validator");
2
3
  const notPath = require("not-path");
3
4
  const FormFabric = require("./fabric");
@@ -588,6 +589,23 @@ class Form {
588
589
  };
589
590
  }
590
591
 
592
+ /**
593
+ *
594
+ * @param {Object} schemaField
595
+ */
596
+ extractDefaultTransformers(schemaField) {
597
+ if (typeof schemaField === "undefined" || schemaField === null) {
598
+ return [];
599
+ }
600
+ switch (schemaField.type) {
601
+ case String:
602
+ case Schema.Types.String:
603
+ return ["xss"];
604
+ default:
605
+ return [];
606
+ }
607
+ }
608
+
591
609
  /**
592
610
  *
593
611
  * @param {import('../types.js').notNodeExpressRequest} req
@@ -597,7 +615,7 @@ class Form {
597
615
  */
598
616
  createInstructionFromRouteActionFields(
599
617
  req,
600
- mainInstruction = ["fromBody", "xss"],
618
+ mainInstruction = ["fromBody"],
601
619
  exceptions = {}
602
620
  ) {
603
621
  const result = {};
@@ -614,7 +632,17 @@ class Form {
614
632
  if (objHas(exceptions, fieldName)) {
615
633
  result[fieldName] = exceptions[fieldName];
616
634
  } else {
617
- result[fieldName] = mainInstruction;
635
+ const fieldTransformers = this.extractDefaultTransformers(
636
+ schema[fieldName]
637
+ );
638
+ if (Array.isArray(fieldTransformers)) {
639
+ result[fieldName] = [
640
+ ...mainInstruction,
641
+ ...fieldTransformers,
642
+ ];
643
+ } else {
644
+ result[fieldName] = [...mainInstruction];
645
+ }
618
646
  }
619
647
  });
620
648
  // @ts-ignore
@@ -631,7 +659,7 @@ class Form {
631
659
  */
632
660
  extractByInstructionsFromRouteActionFields(
633
661
  req,
634
- mainInstruction = ["fromBody", "xss"],
662
+ mainInstruction = ["fromBody"],
635
663
  exceptions = {},
636
664
  additional = {}
637
665
  ) {
@@ -1,6 +1,6 @@
1
1
  const { objHas, isFunc, executeFunctionAsAsync } = require("../common");
2
2
  const ModelRoutine = require("../model/routine");
3
- const { deleteResponseSuccess } = require("../model/utils.js");
3
+
4
4
  const {
5
5
  DBExceptionDocumentIsNotFound,
6
6
  DBExceptionDeleteWasNotSuccessful,
@@ -360,7 +360,7 @@ module.exports = ({
360
360
  let query = { _id: targetId };
361
361
  checkShouldOwn(query, shouldOwn, identity);
362
362
  const result = await model.findOneAndDelete(query).exec();
363
- if (!deleteResponseSuccess(result)) {
363
+ if (!result) {
364
364
  throw new DBExceptionDeleteWasNotSuccessful({
365
365
  params: {
366
366
  result,
@@ -1,6 +1,6 @@
1
1
  /** @module Model/Increment */
2
2
 
3
- const { updateResponseSuccess, insertResponseSuccess } = require("./utils.js");
3
+ const { updateResponseSuccess } = require("./utils.js");
4
4
  const {
5
5
  IncrementExceptionIDGeneratorRebaseFailed,
6
6
  IncrementExceptionIDGenerationFailed,
@@ -105,7 +105,8 @@ function newGetNext() {
105
105
  upsert: true,
106
106
  };
107
107
  const res = await secureUpdate(thisModel, which, cmd, opts);
108
- if (updateResponseSuccess(res) || insertResponseSuccess(res, 1)) {
108
+
109
+ if (updateResponseSuccess(res, 1)) {
109
110
  const doc = await thisModel.findOne({ id });
110
111
  return doc.seq;
111
112
  } else {
@@ -1,25 +1,50 @@
1
- const { findSignature } = require("../common");
2
-
3
- const INSERT_SIGNATURE = {
4
- acknowledged: true,
5
- modifiedCount: 0,
6
- upsertedId: {},
7
- upsertedCount: 1,
8
- matchedCount: 0,
9
- };
10
-
11
- const SIGNATURES = {
12
- INSERT: [INSERT_SIGNATURE],
13
- UPDATE: [],
14
- DELETE: [],
15
- };
1
+ const { compareObjectSignatures } = require("../common");
2
+
3
+ function getBaseResult() {
4
+ return {
5
+ acknowledged: true,
6
+ };
7
+ }
8
+
9
+ function createInsertManySuccessSignature(count) {
10
+ return Object.assign(
11
+ {
12
+ insertedCount: count,
13
+ },
14
+ getBaseResult()
15
+ );
16
+ }
17
+
18
+ function createInsertOneSuccessSignature() {
19
+ return Object.assign({}, getBaseResult());
20
+ }
21
+
22
+ function createUpdateManySuccessSignature(count) {
23
+ return Object.assign(
24
+ {
25
+ matchedCount: count,
26
+ upsertedCount: 0,
27
+ modifiedCount: 0,
28
+ },
29
+ getBaseResult()
30
+ );
31
+ }
32
+
33
+ function createDeleteManySuccessSignature(count) {
34
+ return Object.assign(
35
+ {
36
+ deletedCount: count,
37
+ },
38
+ getBaseResult()
39
+ );
40
+ }
16
41
 
17
42
  function insertResponseSuccess(res, count = 1) {
18
- const ind = findSignature(res, SIGNATURES.INSERT);
19
- if (ind === -1) {
20
- return false;
21
- }
22
- return SIGNATURES.INSERT[ind].upsertedCount === count;
43
+ const targetSignature =
44
+ count === 1
45
+ ? createInsertOneSuccessSignature()
46
+ : createInsertManySuccessSignature(count);
47
+ return compareObjectSignatures(res, targetSignature, false, true, true);
23
48
  }
24
49
 
25
50
  module.exports.insertResponseSuccess = insertResponseSuccess;
@@ -28,32 +53,25 @@ module.exports.insertResponseSuccess = insertResponseSuccess;
28
53
  * checking result of modification queries to ensure that changes were made
29
54
  */
30
55
  function updateResponseSuccess(res, count = 1) {
31
- if (res) {
32
- const responseList = Object.keys(res);
33
- if (responseList.includes("ok")) {
34
- return res.ok === 1 && res.n === count;
35
- } else {
36
- return res.matchedCount === count && res.acknowledged;
37
- }
56
+ const targetSignature = createUpdateManySuccessSignature(count);
57
+ if (compareObjectSignatures(res, targetSignature, false, true)) {
58
+ return res.upsertedCount + res.modifiedCount === count;
38
59
  } else {
39
60
  return false;
40
61
  }
41
62
  }
42
63
  module.exports.updateResponseSuccess = updateResponseSuccess;
43
64
 
65
+ function deleteManyResponseSuccess(res, count) {
66
+ const targetSignature = createDeleteManySuccessSignature(count);
67
+ return compareObjectSignatures(res, targetSignature, true, true, true);
68
+ }
69
+
70
+ module.exports.deleteManyResponseSuccess = deleteManyResponseSuccess;
44
71
  /**
45
72
  * checking result of modification queries to ensure that changes were made
46
73
  */
47
74
  function deleteResponseSuccess(res, count = 1) {
48
- if (res) {
49
- const responseList = Object.keys(res);
50
- if (responseList.includes("ok")) {
51
- return res.ok === 1 && res.n === count;
52
- } else {
53
- return res.deletedCount === count;
54
- }
55
- } else {
56
- return false;
57
- }
75
+ return deleteManyResponseSuccess(res, count);
58
76
  }
59
77
  module.exports.deleteResponseSuccess = deleteResponseSuccess;
@@ -4,7 +4,7 @@ module.exports = ({ Auth, expect }) => {
4
4
  describe("Fields", () => {
5
5
  describe("getOwnerId", () => {
6
6
  it("data has ownerId as String", () => {
7
- const val = mongoose.Types.ObjectId().toString();
7
+ const val = new mongoose.Types.ObjectId().toString();
8
8
  const data = {
9
9
  owner: val,
10
10
  };
@@ -13,7 +13,7 @@ module.exports = ({ Auth, expect }) => {
13
13
  });
14
14
 
15
15
  it("data has ownerId as ObjectId", () => {
16
- const val = mongoose.Types.ObjectId();
16
+ const val = new mongoose.Types.ObjectId();
17
17
  const data = {
18
18
  owner: val,
19
19
  };
@@ -35,7 +35,7 @@ module.exports = ({ Auth, expect }) => {
35
35
 
36
36
  describe("isOwner", () => {
37
37
  it("data.ownerId:ObjectId, user_id not empty:string, equal", () => {
38
- const owner = mongoose.Types.ObjectId();
38
+ const owner = new mongoose.Types.ObjectId();
39
39
  const data = {
40
40
  owner: owner,
41
41
  };
@@ -45,15 +45,15 @@ module.exports = ({ Auth, expect }) => {
45
45
 
46
46
  it("data.ownerId:ObjectId, user_id:ObjectId, not equal", () => {
47
47
  const data = {
48
- owner: mongoose.Types.ObjectId(),
48
+ owner: new mongoose.Types.ObjectId(),
49
49
  };
50
- let result = Auth.isOwner(data, mongoose.Types.ObjectId());
50
+ let result = Auth.isOwner(data, new mongoose.Types.ObjectId());
51
51
  expect(result).to.deep.equal(false);
52
52
  });
53
53
 
54
54
  it("data.ownerId not defined, user_id:ObjectId, not equal", () => {
55
55
  const data = {};
56
- let result = Auth.isOwner(data, mongoose.Types.ObjectId());
56
+ let result = Auth.isOwner(data, new mongoose.Types.ObjectId());
57
57
  expect(result).to.deep.equal(false);
58
58
  });
59
59
 
package/test/common.js CHANGED
@@ -22,9 +22,9 @@ describe("Common", function () {
22
22
  });
23
23
 
24
24
  let testie = "Иероним Босх";
25
- describe(`validateObjectId, build in validator failed on ${testie}`, function () {
25
+ describe(`validateObjectId, build in validator not failed on ${testie}`, function () {
26
26
  it(`Mongoose.Types.ObjectId.isValid('${testie}') -> true`, function () {
27
- expect(ObjectId.isValid(testie)).to.be.ok;
27
+ expect(ObjectId.isValid(testie)).to.be.not.ok;
28
28
  });
29
29
 
30
30
  it(`validateObjectId(${testie}) -> false`, function () {
@@ -37,7 +37,7 @@ module.exports = ({ expect }) => {
37
37
  it("check if user root - true", function () {
38
38
  var t = {
39
39
  session: {
40
- user: mongoose.Types.ObjectId(),
40
+ user: new mongoose.Types.ObjectId(),
41
41
  role: ["root"],
42
42
  },
43
43
  };
@@ -47,7 +47,7 @@ module.exports = ({ expect }) => {
47
47
  it("check if user root - false", function () {
48
48
  var t = {
49
49
  session: {
50
- user: mongoose.Types.ObjectId(),
50
+ user: new mongoose.Types.ObjectId(),
51
51
  save() {},
52
52
  },
53
53
  };
@@ -60,7 +60,7 @@ module.exports = ({ expect }) => {
60
60
  it("get role - root", function () {
61
61
  var t = {
62
62
  session: {
63
- user: mongoose.Types.ObjectId(),
63
+ user: new mongoose.Types.ObjectId(),
64
64
  role: ["root"],
65
65
  save() {},
66
66
  },
@@ -71,7 +71,7 @@ module.exports = ({ expect }) => {
71
71
  it("get role - undefined", function () {
72
72
  var t = {
73
73
  session: {
74
- user: mongoose.Types.ObjectId(),
74
+ user: new mongoose.Types.ObjectId(),
75
75
  save() {},
76
76
  },
77
77
  };
@@ -84,7 +84,7 @@ module.exports = ({ expect }) => {
84
84
  it("session exist, set role - root", function () {
85
85
  var t = {
86
86
  session: {
87
- user: mongoose.Types.ObjectId(),
87
+ user: new mongoose.Types.ObjectId(),
88
88
  role: ["user"],
89
89
  save() {},
90
90
  },
@@ -108,14 +108,14 @@ module.exports = ({ expect }) => {
108
108
  save() {},
109
109
  },
110
110
  };
111
- const id = mongoose.Types.ObjectId();
111
+ const id = new mongoose.Types.ObjectId();
112
112
  new Provider(t).setUserId(id);
113
113
  expect(t.session.user).to.eql(id);
114
114
  });
115
115
 
116
116
  it("session not exist, set _id", function () {
117
117
  const t = {};
118
- const id = mongoose.Types.ObjectId();
118
+ const id = new mongoose.Types.ObjectId();
119
119
  new Provider(t).setUserId(id);
120
120
  expect(t).to.be.deep.eql({});
121
121
  });
@@ -125,7 +125,7 @@ module.exports = ({ expect }) => {
125
125
  it("session exist, user id exist", function () {
126
126
  const t = {
127
127
  session: {
128
- user: mongoose.Types.ObjectId(),
128
+ user: new mongoose.Types.ObjectId(),
129
129
  role: ["user"],
130
130
  save() {},
131
131
  },
@@ -145,7 +145,7 @@ module.exports = ({ expect }) => {
145
145
  it("session exist, session id exist", function () {
146
146
  const t = {
147
147
  session: {
148
- id: mongoose.Types.ObjectId(),
148
+ id: new mongoose.Types.ObjectId(),
149
149
  role: ["user"],
150
150
  save() {},
151
151
  },
@@ -166,7 +166,7 @@ module.exports = ({ expect }) => {
166
166
  const t = {
167
167
  session: { save() {} },
168
168
  };
169
- const id = mongoose.Types.ObjectId();
169
+ const id = new mongoose.Types.ObjectId();
170
170
  new Provider(t).setAuth(id, "root");
171
171
  expect(t.session.user.toString()).to.eql(id.toString());
172
172
  expect(t.session.role).to.eql("root");
@@ -174,7 +174,7 @@ module.exports = ({ expect }) => {
174
174
 
175
175
  it(SESSION_NOT_EXISTS, function () {
176
176
  const t = {};
177
- const id = mongoose.Types.ObjectId();
177
+ const id = new mongoose.Types.ObjectId();
178
178
  new Provider(t).setAuth(id, "user");
179
179
  expect(t).to.be.deep.eql({});
180
180
  });
@@ -182,7 +182,7 @@ module.exports = ({ expect }) => {
182
182
 
183
183
  describe("setGuest", function () {
184
184
  it("session exist", function () {
185
- const id = mongoose.Types.ObjectId();
185
+ const id = new mongoose.Types.ObjectId();
186
186
  const t = {
187
187
  session: { user: id, role: "admin", save() {} },
188
188
  user: { _id: id },
@@ -202,7 +202,7 @@ module.exports = ({ expect }) => {
202
202
 
203
203
  describe("cleanse", function () {
204
204
  it("session exist, destroy method exists", function () {
205
- const id = mongoose.Types.ObjectId();
205
+ const id = new mongoose.Types.ObjectId();
206
206
  let destroyed = false;
207
207
  const t = {
208
208
  session: {
@@ -221,7 +221,7 @@ module.exports = ({ expect }) => {
221
221
  });
222
222
 
223
223
  it("session exist, destroy method exists", function () {
224
- const id = mongoose.Types.ObjectId();
224
+ const id = new mongoose.Types.ObjectId();
225
225
  const t = {
226
226
  session: {
227
227
  user: id,
@@ -317,7 +317,7 @@ module.exports = ({
317
317
 
318
318
  describe("getOne", () => {
319
319
  it("versioning, populate not specified", async () => {
320
- const id = mongoose.Types.ObjectId();
320
+ const id = new mongoose.Types.ObjectId();
321
321
  const ctx = {
322
322
  schema: {
323
323
  statics: {
@@ -350,7 +350,7 @@ module.exports = ({
350
350
  });
351
351
 
352
352
  it("!versioning, populate not specified", async () => {
353
- const id = mongoose.Types.ObjectId();
353
+ const id = new mongoose.Types.ObjectId();
354
354
  const ctx = {
355
355
  schema: {
356
356
  statics: {},
@@ -379,7 +379,7 @@ module.exports = ({
379
379
  });
380
380
 
381
381
  it("versioning, populate === null", async () => {
382
- const id = mongoose.Types.ObjectId();
382
+ const id = new mongoose.Types.ObjectId();
383
383
  const ctx = {
384
384
  schema: {
385
385
  statics: {
@@ -517,7 +517,7 @@ module.exports = ({
517
517
 
518
518
  describe("getOneRaw", () => {
519
519
  it("simple run", async () => {
520
- const id = mongoose.Types.ObjectId();
520
+ const id = new mongoose.Types.ObjectId();
521
521
  const ctx = {
522
522
  schema: {
523
523
  statics: {
@@ -0,0 +1,50 @@
1
+ const expect = require("chai").expect,
2
+ utils = require("../../src/model/utils");
3
+
4
+ module.exports = ({ mongod, mongoose }) => {
5
+ let TestModel;
6
+ before(() => {
7
+ TestModel = mongoose.model(
8
+ "TestModel",
9
+ new mongoose.Schema({ name: { type: String, required: true } })
10
+ );
11
+ });
12
+
13
+ describe("Utils", function () {
14
+ describe("insertManyResponseSuccess", () => {
15
+ it("ok", async () => {
16
+ const res = await TestModel.insertMany(
17
+ [
18
+ {
19
+ name: "FirstInsertOne",
20
+ },
21
+ {
22
+ name: "FirstInsertTwo",
23
+ },
24
+ ],
25
+ { rawResult: true }
26
+ );
27
+
28
+ expect(utils.insertResponseSuccess(res, 2)).to.be.true;
29
+ });
30
+
31
+ it("errors", async () => {
32
+ try {
33
+ const res = await TestModel.insertMany(
34
+ [
35
+ {
36
+ name: Date.now(),
37
+ },
38
+ { name: "SecondInsertTwo" },
39
+ ],
40
+ { rawResult: true }
41
+ );
42
+
43
+ expect(utils.insertResponseSuccess(res, 1)).to.be.false;
44
+ } catch (e) {
45
+ console.error(e);
46
+ }
47
+ });
48
+ });
49
+ });
50
+ };
@@ -255,7 +255,7 @@ module.exports = ({ mongoose }) => {
255
255
  ...secondHistoryVersion,
256
256
  username: "username3",
257
257
  __versions: [
258
- mongoose.Types.ObjectId(),
258
+ new mongoose.Types.ObjectId(),
259
259
  second._id,
260
260
  first._id,
261
261
  ],
package/test/notModel.js CHANGED
@@ -41,6 +41,12 @@ describe("notModel", function () {
41
41
  mongoose,
42
42
  });
43
43
 
44
+ require("./model/utils")({
45
+ expect,
46
+ mongod,
47
+ mongoose,
48
+ });
49
+
44
50
  after(async () => {
45
51
  await mongoose.disconnect();
46
52
  await mongod.stop();