From 9f8bba12d8749bbe4d5c4482f76d79c407ff0cdf Mon Sep 17 00:00:00 2001 From: Ugaitz Urien Date: Thu, 2 Nov 2023 13:33:37 +0100 Subject: [PATCH] Fix ci for mongoose in node 14 (#3758) --- .../test/mongoose.spec.js | 26 ++++++++++++------- .../test/index.spec.js | 4 +++ .../test/integration-test/client.spec.js | 4 +++ ...n-mongodb-analyzer.mongoose.plugin.spec.js | 6 ++++- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/packages/datadog-instrumentations/test/mongoose.spec.js b/packages/datadog-instrumentations/test/mongoose.spec.js index 28fdf4764f1..adaae709c82 100644 --- a/packages/datadog-instrumentations/test/mongoose.spec.js +++ b/packages/datadog-instrumentations/test/mongoose.spec.js @@ -3,6 +3,7 @@ const agent = require('../../dd-trace/test/plugins/agent') const { channel } = require('../src/helpers/instrument') const semver = require('semver') +const { NODE_MAJOR } = require('../../../version') const startCh = channel('datadog:mongoose:model:filter:start') const finishCh = channel('datadog:mongoose:model:filter:finish') @@ -14,6 +15,9 @@ describe('mongoose instrumentations', () => { iterationRanges.forEach(range => { describe(range, () => { withVersions('mongoose', ['mongoose'], range, (version) => { + const specificVersion = require(`../../../versions/mongoose@${version}`).version() + if (NODE_MAJOR === 14 && semver.satisfies(specificVersion, '>=8')) return + let Test, dbName, id, mongoose function connect () { @@ -129,15 +133,17 @@ describe('mongoose instrumentations', () => { }) } - it('continue working as expected with promise', (done) => { - Test.count({ type: 'test' }).then((res) => { - expect(res).to.be.equal(3) + if (!semver.satisfies(specificVersion, '>=8')) { + // Model.count method removed from mongoose 8.0.0 + it('continue working as expected with promise', (done) => { + Test.count({ type: 'test' }).then((res) => { + expect(res).to.be.equal(3) - done() + done() + }) }) - }) - - testCallbacksCalled('count', [{ type: 'test' }]) + testCallbacksCalled('count', [{ type: 'test' }]) + } }) if (semver.intersects(version, '>=6')) { @@ -164,8 +170,8 @@ describe('mongoose instrumentations', () => { testCallbacksCalled('countDocuments', [{ type: 'test' }]) }) } - - if (semver.intersects(version, '>=5')) { + if (semver.intersects(version, '>=5') && semver.satisfies(specificVersion, '<8')) { + // Model.count method removed from mongoose 8.0.0 describe('deleteOne', () => { if (range !== '>=7') { it('continue working as expected with cb', (done) => { @@ -243,7 +249,7 @@ describe('mongoose instrumentations', () => { testCallbacksCalled('findOne', [{ type: 'test' }]) }) - if (semver.intersects(version, '>=6')) { + if (semver.intersects(version, '>=6') && semver.satisfies(specificVersion, '<8')) { describe('findOneAndDelete', () => { if (range !== '>=7') { it('continue working as expected with cb', (done) => { diff --git a/packages/datadog-plugin-mongoose/test/index.spec.js b/packages/datadog-plugin-mongoose/test/index.spec.js index ed0da6a0206..305ddeca31c 100644 --- a/packages/datadog-plugin-mongoose/test/index.spec.js +++ b/packages/datadog-plugin-mongoose/test/index.spec.js @@ -2,6 +2,7 @@ const semver = require('semver') const agent = require('../../dd-trace/test/plugins/agent') +const { NODE_MAJOR } = require('../../../version') describe('Plugin', () => { let id @@ -10,6 +11,9 @@ describe('Plugin', () => { describe('mongoose', () => { withVersions('mongoose', ['mongoose'], (version) => { + const specificVersion = require(`../../../versions/mongoose@${version}`).version() + if (NODE_MAJOR === 14 && semver.satisfies(specificVersion, '>=8')) return + let mongoose // This needs to be called synchronously right before each test to make diff --git a/packages/datadog-plugin-mongoose/test/integration-test/client.spec.js b/packages/datadog-plugin-mongoose/test/integration-test/client.spec.js index e91d4885edf..d393fdd774b 100644 --- a/packages/datadog-plugin-mongoose/test/integration-test/client.spec.js +++ b/packages/datadog-plugin-mongoose/test/integration-test/client.spec.js @@ -8,6 +8,7 @@ const { } = require('../../../../integration-tests/helpers') const { assert } = require('chai') const { NODE_MAJOR } = require('../../../../version') +const semver = require('semver') // newer packages are not supported on older node versions const range = NODE_MAJOR < 16 ? '<5' : '>=4' @@ -18,6 +19,9 @@ describe('esm', () => { let sandbox withVersions('mongoose', ['mongoose'], range, version => { + const specificVersion = require(`../../../../versions/mongoose@${version}`).version() + if (NODE_MAJOR === 14 && semver.satisfies(specificVersion, '>=8')) return + before(async function () { this.timeout(20000) sandbox = await createSandbox([`'mongoose@${version}'`], false, [ diff --git a/packages/dd-trace/test/appsec/iast/analyzers/nosql-injection-mongodb-analyzer.mongoose.plugin.spec.js b/packages/dd-trace/test/appsec/iast/analyzers/nosql-injection-mongodb-analyzer.mongoose.plugin.spec.js index 624bb26617f..787f737c156 100644 --- a/packages/dd-trace/test/appsec/iast/analyzers/nosql-injection-mongodb-analyzer.mongoose.plugin.spec.js +++ b/packages/dd-trace/test/appsec/iast/analyzers/nosql-injection-mongodb-analyzer.mongoose.plugin.spec.js @@ -7,10 +7,14 @@ const semver = require('semver') const os = require('os') const path = require('path') const fs = require('fs') +const { NODE_MAJOR } = require('../../../../../../version') describe('nosql injection detection in mongodb - whole feature', () => { withVersions('express', 'express', '>4.18.0', expressVersion => { withVersions('mongoose', 'mongoose', '>4.0.0', mongooseVersion => { + const specificMongooseVersion = require(`../../../../../../versions/mongoose@${mongooseVersion}`).version() + if (NODE_MAJOR === 14 && semver.satisfies(specificMongooseVersion, '>=8')) return + const vulnerableMethodFilename = 'mongoose-vulnerable-method.js' let mongoose, Test, tmpFilePath @@ -106,7 +110,7 @@ describe('nosql injection detection in mongodb - whole feature', () => { } }) - if (semver.satisfies(mongooseVersion, '>=6')) { + if (semver.satisfies(specificMongooseVersion, '>=6')) { testThatRequestHasNoVulnerability({ testDescription: 'should not have NOSQL_MONGODB_INJECTION vulnerability with mongoose.sanitizeFilter', fn: async (req, res) => {