Skip to content

Commit

Permalink
fix: revert mongoose v6.0.13 and related dependency update, update mo…
Browse files Browse the repository at this point in the history
…ngoose to v5.13.13 (#3219)

* Revert "feat(deps): update mongoose to v6.0.13, update mongoose dependencies likewise (#3093)"

This reverts commit d5ada10.

# Conflicts:
#	package-lock.json
#	package.json
#	src/app/models/form.server.model.ts

* fix: update package-lock

pinned mongoose to prior version before unlocking it due to type changes in minor patch versions of mongoose...

* fix: add missing ClientSession import

* feat: update mongoose v5.13.5 to v5.13.13

snyk doesn't allow vulnerabilities....... which 5.13.5 contains oops

* fix: mongoose v5.13.13 type updates in models

* fix(test): convert mockingoose mocks to normal jest spy mocks

* feat: remove unused mockingoose package
  • Loading branch information
karrui committed Dec 22, 2021
1 parent 6fd984e commit fd622ba
Show file tree
Hide file tree
Showing 59 changed files with 39,159 additions and 14,166 deletions.
51,368 changes: 37,947 additions & 13,421 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@
"body-parser": "^1.19.1",
"bootstrap": "3.4.1",
"boxicons": "1.8.0",
"bson-ext": "^4.0.2",
"bson-ext": "^2.0.6",
"busboy": "^0.3.1",
"celebrate": "^15.0.0",
"compression": "~1.7.2",
"connect-datadog": "0.0.9",
"connect-mongo": "^4.6.0",
"connect-mongo": "^4.4.1",
"convict": "^6.2.1",
"convict-format-with-validator": "^6.2.0",
"cookie-parser": "~1.4.6",
Expand Down Expand Up @@ -117,9 +117,8 @@
"libphonenumber-js": "^1.9.44",
"lodash": "^4.17.21",
"moment-timezone": "0.5.34",
"mongodb": "^4.2.2",
"mongodb-uri": "^0.9.7",
"mongoose": "^6.0.13",
"mongoose": "^5.13.13",
"multiparty": ">=4.2.2",
"neverthrow": "^4.3.1",
"ng-infinite-scroll": "^1.3.0",
Expand Down Expand Up @@ -177,6 +176,7 @@
"@types/jest": "^27.0.3",
"@types/json-stringify-safe": "^5.0.0",
"@types/lodash": "^4.14.178",
"@types/mongodb": "^3.6.20",
"@types/mongodb-uri": "^0.9.1",
"@types/node": "^17.0.1",
"@types/nodemailer": "^6.4.4",
Expand Down Expand Up @@ -223,7 +223,7 @@
"maildev": "^1.1.0",
"mini-css-extract-plugin": "^0.5.0",
"mockdate": "^3.0.5",
"mongodb-memory-server-core": "^8.0.4",
"mongodb-memory-server-core": "^6.9.6",
"ngrok": "^4.2.2",
"optimize-css-assets-webpack-plugin": "^5.0.8",
"prettier": "^2.5.1",
Expand Down
10 changes: 9 additions & 1 deletion src/app/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ const dbConfig: DbConfig = {
pass: '',
// Only create indexes in dev env to avoid adverse production impact.
autoIndex: isDev,
// Avoid using deprecated URL string parser in MongoDB driver
useNewUrlParser: true,
useUnifiedTopology: true,
// Avoid using deprecated collection.ensureIndex internally
useCreateIndex: true,
// upgrade to mongo driver's native findOneAndUpdate function instead of
// findAndModify.
useFindAndModify: false,
promiseLibrary: global.Promise,
},
}
Expand Down Expand Up @@ -172,7 +180,7 @@ const cookieSettings: SessionOptions['cookie'] = {
/**
* Fetches AWS credentials
*/
const configureAws = async (): Promise<void> => {
const configureAws = async () => {
if (!isDev) {
const getCredentials = () => {
return new Promise<void>((resolve, reject) => {
Expand Down
3 changes: 1 addition & 2 deletions src/app/loaders/express/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import MongoStore from 'connect-mongo'
import cookieParser from 'cookie-parser'
import { RequestHandler } from 'express'
import session from 'express-session'
import { MongoClient } from 'mongodb'
import { Connection } from 'mongoose'

import config from '../../config/config'
Expand All @@ -16,7 +15,7 @@ const sessionMiddlewares = (connection: Connection): RequestHandler[] => {
cookie: config.cookieSettings,
name: 'connect.sid',
store: MongoStore.create({
client: connection.getClient() as MongoClient,
client: connection.getClient(),
}),
})

Expand Down
4 changes: 2 additions & 2 deletions src/app/loaders/mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default async (): Promise<Connection> => {
process.exit(1)
}

const mongod = await MongoMemoryServer.create({
const mongod = new MongoMemoryServer({
binary: { version: String(process.env.MONGO_BINARY_VERSION) },
instance: {
port: 3000,
Expand All @@ -38,7 +38,7 @@ export default async (): Promise<Connection> => {
})

// Store the uri to connect to later on
config.db.uri = mongod.getUri()
config.db.uri = await mongod.getConnectionString()
}

// Actually connect to the database
Expand Down
39 changes: 22 additions & 17 deletions src/app/models/__tests__/admin_verification.server.model.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ObjectID } from 'bson'
import mongoose from 'mongoose'

import getAdminVerificationModel from 'src/app/models/admin_verification.server.model'
Expand All @@ -17,7 +18,7 @@ describe('AdminVerification Model', () => {

describe('Schema', () => {
const DEFAULT_PARAMS: IAdminVerification = {
admin: new mongoose.Types.ObjectId(),
admin: new ObjectID(),
expireAt: new Date(),
hashedContact: 'mockHashedContact',
hashedOtp: 'mockHashedOtp',
Expand Down Expand Up @@ -56,11 +57,13 @@ describe('AdminVerification Model', () => {
expect(actual._id).toBeDefined()
expect(actual.createdAt).toBeInstanceOf(Date)
expect(actual.updatedAt).toBeInstanceOf(Date)
expect(actual).toMatchObject({
...customParams,
// Add defaults that has not been overridden.
numOtpSent: 0,
})
expect(actual).toEqual(
expect.objectContaining({
...customParams,
// Add defaults that has not been overridden.
numOtpSent: 0,
}),
)
})

it('should throw validation error on missing admin', async () => {
Expand Down Expand Up @@ -130,7 +133,7 @@ describe('AdminVerification Model', () => {
it('should create successfully when document does not exist', async () => {
// Arrange
const params: UpsertOtpParams = {
admin: new mongoose.Types.ObjectId(),
admin: new ObjectID(),
expireAt: new Date(),
hashedContact: 'mockHashedContact',
hashedOtp: 'mockHashedOtp',
Expand All @@ -146,13 +149,13 @@ describe('AdminVerification Model', () => {
const expected = { ...params, numOtpAttempts: 0, numOtpSent: 1 }
// Should now have one document.
await expect(AdminVerification.countDocuments()).resolves.toEqual(1)
expect(actual).toMatchObject(expected)
expect(actual).toEqual(expect.objectContaining(expected))
})

it('should update successfully when a document already exists', async () => {
// Arrange
// Insert mock document into collection.
const adminId = new mongoose.Types.ObjectId()
const adminId = new ObjectID()
const oldExpireAt = new Date()
const newExpireAt = new Date(Date.now() + 9000000)
const oldNumOtpSent = 3
Expand Down Expand Up @@ -187,14 +190,14 @@ describe('AdminVerification Model', () => {
}
// Should still only have one document.
await expect(AdminVerification.countDocuments()).resolves.toEqual(1)
expect(actual).toMatchObject(expected)
expect(actual).toEqual(expect.objectContaining(expected))
})

it('should throw error if validation fails due to invalid upsert parameters', async () => {
// Arrange
const invalidParams: UpsertOtpParams = {
// Invalid admin parameter.
admin: null,
admin: undefined,
expireAt: new Date(),
hashedContact: 'mockHashedContact',
hashedOtp: 'mockHashedOtp',
Expand All @@ -216,7 +219,7 @@ describe('AdminVerification Model', () => {
it('should increment successfully', async () => {
// Arrange
// Insert mock document into collection.
const adminId = new mongoose.Types.ObjectId()
const adminId = new ObjectID()
const initialOtpAttempts = 5
const adminVerificationParams = {
admin: adminId,
Expand All @@ -237,17 +240,19 @@ describe('AdminVerification Model', () => {
// Assert
// Exactly the same as initial params, but with numOtpAttempts
// incremented by 1.
await expect(actualPromise).resolves.toMatchObject({
...adminVerificationParams,
numOtpAttempts: initialOtpAttempts + 1,
})
await expect(actualPromise).resolves.toEqual(
expect.objectContaining({
...adminVerificationParams,
numOtpAttempts: initialOtpAttempts + 1,
}),
)
})

it('should return null if document cannot be retrieved', async () => {
// Arrange
// Should have no documents yet.
await expect(AdminVerification.countDocuments()).resolves.toEqual(0)
const freshAdminId = new mongoose.Types.ObjectId()
const freshAdminId = new ObjectID()

// Act
const actualPromise =
Expand Down
19 changes: 9 additions & 10 deletions src/app/models/__tests__/encrypt-submission.server.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('Encrypt Submission Model', () => {
.tz('Asia/Singapore')
.format('Do MMM YYYY, h:mm:ss a'),
}
expect(result).toMatchObject(expected)
expect(result).toEqual(expected)
})

it('should return null when submission is of SubmissionType.Email', async () => {
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('Encrypt Submission Model', () => {
}))
.reverse(),
}
expect(actual).toMatchObject(expected)
expect(actual).toEqual(expected)
})

it('should return offset metadata with correct count when page number is provided', async () => {
Expand Down Expand Up @@ -179,7 +179,7 @@ describe('Encrypt Submission Model', () => {
},
],
}
expect(actual).toMatchObject(expected)
expect(actual).toEqual(expected)
})

it('should return offset metadata with correct count when page size is provided', async () => {
Expand Down Expand Up @@ -221,7 +221,7 @@ describe('Encrypt Submission Model', () => {
},
],
}
expect(actual).toMatchObject(expected)
expect(actual).toEqual(expected)
})

it('should return empty metadata array when given page has no metadata', async () => {
Expand Down Expand Up @@ -254,7 +254,7 @@ describe('Encrypt Submission Model', () => {
// show
metadata: [],
}
expect(actual).toMatchObject(expected)
expect(actual).toEqual(expected)
})

it('should return empty metadata array when formId has no metadata', async () => {
Expand All @@ -272,7 +272,7 @@ describe('Encrypt Submission Model', () => {
// show
metadata: [],
}
expect(actual).toMatchObject(expected)
expect(actual).toEqual(expected)
})
})

Expand All @@ -284,7 +284,6 @@ describe('Encrypt Submission Model', () => {
submissionType: SubmissionType.Encrypt,
form: validFormId,
encryptedContent: 'mock encrypted content abc',
verifiedContent: 'mock verified content',
version: 3,
})
const expectedSubmission = pick(
Expand All @@ -310,7 +309,7 @@ describe('Encrypt Submission Model', () => {
retrievedSubmissions.push(submission)
}
// Cursor stream should contain only that single submission.
expect(retrievedSubmissions).toMatchObject([expectedSubmission])
expect(retrievedSubmissions).toEqual([expectedSubmission])
})

it('should return cursor even if no submissions are found', async () => {
Expand All @@ -330,7 +329,7 @@ describe('Encrypt Submission Model', () => {
retrievedSubmissions.push(submission)
}
// Cursor stream should return nothing.
expect(retrievedSubmissions).toMatchObject([])
expect(retrievedSubmissions).toEqual([])
})
})

Expand Down Expand Up @@ -363,7 +362,7 @@ describe('Encrypt Submission Model', () => {
'version',
)
expect(actual).not.toBeNull()
expect(actual?.toJSON()).toMatchObject(expected)
expect(actual?.toJSON()).toEqual(expected)
})

it('should return null when submission id does not exist', async () => {
Expand Down
Loading

0 comments on commit fd622ba

Please sign in to comment.