From cdc5650115c6ee07978f8a52f614604363f4d3c3 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Thu, 14 Mar 2024 10:50:03 +0400 Subject: [PATCH] add middleware tests --- __tests__/unit/models/community.mode.test.ts | 41 +++++++------- __tests__/unit/models/platform.model.test.ts | 59 ++++++++++++++------ __tests__/unit/models/user.model.test.ts | 27 +++++---- __tests__/utils/setupTestDB.ts | 34 +++++------ package.json | 2 +- src/config/index.ts | 6 +- src/models/schemas/Platform.schema.ts | 34 ++++++++++- 7 files changed, 128 insertions(+), 75 deletions(-) diff --git a/__tests__/unit/models/community.mode.test.ts b/__tests__/unit/models/community.mode.test.ts index d5dde25..0de8e30 100644 --- a/__tests__/unit/models/community.mode.test.ts +++ b/__tests__/unit/models/community.mode.test.ts @@ -1,9 +1,9 @@ import { Community, User, Platform } from '../../../src/models'; import { ICommunity } from '../../../src/interfaces'; import { Types } from 'mongoose'; -// import setupTestDB from '../../utils/setupTestDB'; +import setupTestDB from '../../utils/setupTestDB'; -// setupTestDB(); +setupTestDB(); describe('Community model', () => { describe('Community validation', () => { @@ -20,31 +20,30 @@ describe('Community model', () => { test('should correctly validate a valid community', async () => { await expect(new Community(community).validate()).resolves.toBeUndefined(); }); + describe('Middlewares', () => { - // describe('Cascade deletes', () => { + test('Pre Remove: should clean up when community is deleted', async () => { + const user = new User({ discordId: 'discordId' }); + await user.save(); - // test('should clean up when community is deleted', async () => { - // const user = new User({ discordId: 'discordId' }); - // await user.save(); + const community = new Community({ users: [user._id], name: 'community' }); + await community.save(); + user.communities?.push(community._id) - // const community = new Community({ users: [user._id], name: 'community' }); - // await community.save(); - // user.communities?.push(community._id) + const platform = new Platform({ name: 'platform', community: community._id }); + await platform.save(); - // const platform = new Platform({ name: 'platform', community: community._id }); - // await platform.save(); + community.platforms?.push(platform._id); + await community.save(); - // community.platforms?.push(platform._id); - // await community.save(); + await community.remove(); - // await community.remove(); + const userDoc = await User.findById(user._id); + expect(userDoc?.communities).not.toContain(community._id); - // const userDoc = await User.findById(user._id); - // expect(userDoc?.communities).not.toContain(community._id); - - // const platformDoc = await Platform.findById(platform._id); - // expect(platformDoc).toBe(null); - // }); - // }); + const platformDoc = await Platform.findById(platform._id); + expect(platformDoc).toBe(null); + }); + }); }); }); diff --git a/__tests__/unit/models/platform.model.test.ts b/__tests__/unit/models/platform.model.test.ts index 1a8a160..beb543d 100644 --- a/__tests__/unit/models/platform.model.test.ts +++ b/__tests__/unit/models/platform.model.test.ts @@ -1,9 +1,9 @@ -import { Platform, Community } from '../../../src/models'; +import { Platform, Community ,User} from '../../../src/models'; import { IPlatform } from '../../../src/interfaces'; import { Types } from 'mongoose'; -// import setupTestDB from '../../utils/setupTestDB'; +import setupTestDB from '../../utils/setupTestDB'; -// setupTestDB(); +setupTestDB(); describe('Platform model', () => { describe('Platform validation', () => { @@ -24,27 +24,50 @@ describe('Platform model', () => { await expect(new Platform(platform).validate()).resolves.toBeUndefined(); }); - // describe('Cascade deletes', () => { + describe('Middlewares', () => { - // test('should clean up when community is deleted', async () => { - // const community = new Community({ users: [new Types.ObjectId()], name: 'community' }); - // await community.save(); + test('Pre Remove: should clean up when community is deleted', async () => { + const community = new Community({ users: [new Types.ObjectId()], name: 'community' }); + await community.save(); - // const platform = new Platform({ name: 'platform', community: community._id }); - // await platform.save(); + const platform = new Platform({ name: 'platform', community: community._id }); + await platform.save(); - // community.platforms?.push(platform._id); - // await community.save(); + community.platforms?.push(platform._id); + await community.save(); - // await platform.remove(); + await platform.remove(); - // const communityDoc = await Community.findById(community._id); + const communityDoc = await Community.findById(community._id); - // expect(communityDoc?.platforms).not.toContain(platform._id); + expect(communityDoc?.platforms).not.toContain(platform._id); - // const platformDoc = await Platform.findById(platform._id); - // expect(platformDoc).toBe(null); - // }); - // }); + const platformDoc = await Platform.findById(platform._id); + expect(platformDoc).toBe(null); + }); + + test('Post Save: should add platformId to the community and admin role for the creator of community', async () => { + const user = new User({ discordId: 'discordId' }); + await user.save(); + + const community = new Community({ users: [user._id], name: 'community' }); + await community.save(); + user.communities?.push(community._id) + + const platform = new Platform({ name: 'platform', community: community._id }); + await platform.save(); + + community.platforms?.push(platform._id); + await community.save(); + + await community.remove(); + + const userDoc = await User.findById(user._id); + expect(userDoc?.communities).not.toContain(community._id); + + const platformDoc = await Platform.findById(platform._id); + expect(platformDoc).toBe(null); + }); + }); }); }); diff --git a/__tests__/unit/models/user.model.test.ts b/__tests__/unit/models/user.model.test.ts index d450f46..3df43ac 100644 --- a/__tests__/unit/models/user.model.test.ts +++ b/__tests__/unit/models/user.model.test.ts @@ -1,9 +1,9 @@ import { User, Community } from '../../../src/models'; import { IUser } from '../../../src/interfaces'; import { Types } from 'mongoose'; -// import setupTestDB from '../../utils/setupTestDB'; +import setupTestDB from '../../utils/setupTestDB'; -// setupTestDB(); +setupTestDB(); describe('User model', () => { describe('User validation', () => { @@ -22,20 +22,19 @@ describe('User model', () => { }); }); - // describe('Cascade deletes', () => { + describe('Middlewares', () => { + test('Pre Remove: should remove user reference from community when user is deleted', async () => { + const user = new User({ discordId: 'discordId' }); + await user.save(); - // test('should remove user reference from community when user is deleted', async () => { - // const user = new User({ discordId: 'discordId' }); - // await user.save(); + const community = new Community({ users: [user._id], name: 'community' }); + await community.save(); - // const community = new Community({ users: [user._id], name: 'community' }); - // await community.save(); + await user.remove(); - // await user.remove(); + const communityDoc = await Community.findById(community._id); + expect(communityDoc?.users).not.toContain(user._id); - // const communityDoc = await Community.findById(community._id); - // expect(communityDoc?.users).not.toContain(user._id); - - // }); - // }); + }); + }); }); diff --git a/__tests__/utils/setupTestDB.ts b/__tests__/utils/setupTestDB.ts index 19f8197..cd498c5 100644 --- a/__tests__/utils/setupTestDB.ts +++ b/__tests__/utils/setupTestDB.ts @@ -1,21 +1,21 @@ -// import mongoose from 'mongoose'; -// import config from '../../src/config'; +import mongoose from 'mongoose'; +import config from '../../src/config'; -// const setupTestDB = () => { -// beforeAll(async () => { -// mongoose.set('strictQuery', false); -// await mongoose.connect(config.mongoose.serverURL); -// }); +const setupTestDB = () => { + beforeAll(async () => { + mongoose.set('strictQuery', false); + await mongoose.connect(config.mongoose.serverURL); + }); -// beforeEach(async () => { -// await Promise.all( -// Object.values(mongoose.connection.collections).map(async (collection) => collection.deleteMany({})), -// ); -// }); + beforeEach(async () => { + await Promise.all( + Object.values(mongoose.connection.collections).map(async (collection) => collection.deleteMany({})), + ); + }); -// afterAll(async () => { -// await mongoose.disconnect(); -// }); -// }; + afterAll(async () => { + await mongoose.disconnect(); + }); +}; -// export default setupTestDB; +export default setupTestDB; diff --git a/package.json b/package.json index 5cfc002..9c04c8f 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "build": "tsc", "start": "node ./dist/index.js", "dev": "nodemon ./src/index.ts", - "test": "jest --detectOpenHandles", + "test": "env-cmd -f ./src/config/test.env jest --runInBand --detectOpenHandles", "format": "prettier --write \"src/**/*.ts\" \"__tests__/**/*.ts\" \"*.ts\" ", "prepublishOnly": "npm test", "version": "npm run format && git add -A src", diff --git a/src/config/index.ts b/src/config/index.ts index c87cb55..340ff8b 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -19,8 +19,8 @@ if (error != null) { export default { mongoose: { - serverURL: `mongodb://${envVars.DB_USER}:${envVars.DB_PASSWORD}@${envVars.DB_HOST}:${envVars.DB_PORT}/${envVars.DB_NAME}`, - botURL: `mongodb://${envVars.DB_USER}:${envVars.DB_PASSWORD}@${envVars.DB_HOST}:${envVars.DB_PORT}`, - dbURL: `mongodb://${envVars.DB_USER}:${envVars.DB_PASSWORD}@${envVars.DB_HOST}:${envVars.DB_PORT}`, + serverURL: `mongodb://${envVars.DB_USER}:${envVars.DB_PASSWORD}@${envVars.DB_HOST}:${envVars.DB_PORT}/${envVars.DB_NAME}?authSource=admin`, + botURL: `mongodb://${envVars.DB_USER}:${envVars.DB_PASSWORD}@${envVars.DB_HOST}:${envVars.DB_PORT}?authSource=admin`, + dbURL: `mongodb://${envVars.DB_USER}:${envVars.DB_PASSWORD}@${envVars.DB_HOST}:${envVars.DB_PORT}?authSource=admin`, }, }; diff --git a/src/models/schemas/Platform.schema.ts b/src/models/schemas/Platform.schema.ts index 60d32fa..9f12142 100644 --- a/src/models/schemas/Platform.schema.ts +++ b/src/models/schemas/Platform.schema.ts @@ -1,7 +1,7 @@ import { Schema, type Document, Types } from 'mongoose'; import { toJSON, paginate } from './plugins'; import { type IPlatform, type PlatformModel } from '../../interfaces'; -import { Announcement, Community } from '../index'; +import { Announcement, Community, Platform, User } from '../index'; const platformSchema = new Schema( { @@ -75,4 +75,36 @@ platformSchema.pre('remove', async function (this: Document) { await announcementDeletion(platformId); }); +platformSchema.post('save', async function () { + const platformId = this._id; + const platform = await Platform.findById(platformId); + if (platform !== null) { + const community = await Community.findById(platform.community); + if (community !== null) { + const user = await User.findById(community?.users[0]); + if (user !== null) { + await Community.updateOne( + { _id: community._id }, + { + $addToSet: { + platforms: platform._id, + roles: { + $each: [{ + roleType: 'admin', + source: { + platform: 'discord', + identifierType: 'member', + identifierValues: [user.discordId], + platformId: platform._id, + } + }] + } + } + } + ); + } + } + } +}); + export default platformSchema;