Skip to content

Commit

Permalink
Fix ci for mongoose in node 14 (#3758)
Browse files Browse the repository at this point in the history
  • Loading branch information
uurien committed Nov 3, 2023
1 parent 7d844f6 commit dfeabd4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
26 changes: 16 additions & 10 deletions packages/datadog-instrumentations/test/mongoose.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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 () {
Expand Down Expand Up @@ -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')) {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/datadog-plugin-mongoose/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit dfeabd4

Please sign in to comment.