From 2ba6371c6910203986218a5c7fafc3d241eecbc2 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Mon, 15 Apr 2024 09:27:14 +0400 Subject: [PATCH 1/3] disable adding role for platform creator --- __tests__/unit/models/platform.model.test.ts | 136 ++++++++++--------- __tests__/utils/setupTestDB.ts | 34 ++--- package.json | 2 +- src/models/schemas/Platform.schema.ts | 26 ++-- 4 files changed, 102 insertions(+), 96 deletions(-) diff --git a/__tests__/unit/models/platform.model.test.ts b/__tests__/unit/models/platform.model.test.ts index a400908..2b59e1a 100644 --- a/__tests__/unit/models/platform.model.test.ts +++ b/__tests__/unit/models/platform.model.test.ts @@ -1,9 +1,9 @@ import { Platform, Community, User, Module } 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,78 +24,84 @@ describe('Platform model', () => { await expect(new Platform(platform).validate()).resolves.toBeUndefined(); }); - // describe('Middlewares', () => { - // test('Pre Remove: should clean up when platform is deleted', async () => { - // const user = new User({ discordId: 'discordId' }); - // await user.save(); + describe('Middlewares', () => { + test('Pre Remove: should clean up when platform 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(); - // const platform = new Platform({ name: 'platform', community: community._id }); - // await platform.save(); + const platform = new Platform({ name: 'platform', community: community._id }); + await platform.save(); - // const module1 = new Module({ - // name: 'hivemind', - // community: community._id, - // options: { platforms: [{ platform: platform._id }, { platform: new Types.ObjectId() }] }, - // }); - // await module1.save(); + const module1 = new Module({ + name: 'hivemind', + community: community._id, + options: { platforms: [{ platform: platform._id }, { platform: new Types.ObjectId() }] }, + }); + await module1.save(); - // const module2 = new Module({ - // name: 'hivemind', - // community: community._id, - // options: { platforms: [{ platform: new Types.ObjectId() }, { platform: platform._id }, { platform: new Types.ObjectId() }] }, - // }); - // await module2.save(); + const module2 = new Module({ + name: 'hivemind', + community: community._id, + options: { + platforms: [ + { platform: new Types.ObjectId() }, + { platform: platform._id }, + { platform: new Types.ObjectId() }, + ], + }, + }); + await module2.save(); - // let communityDoc = await Community.findById(community.id); - // if (communityDoc?.platforms) { - // const idAsString = platform.id.toHexString ? platform.id.toHexString() : platform.id; - // expect(communityDoc.platforms[0].toHexString()).toBe(idAsString); - // } - // await platform.remove(); - // communityDoc = await Community.findById(community.id); - // expect(communityDoc?.platforms).toEqual([]); - // expect(communityDoc?.roles).toEqual([]); + let communityDoc = await Community.findById(community.id); + if (communityDoc?.platforms) { + const idAsString = platform.id.toHexString ? platform.id.toHexString() : platform.id; + expect(communityDoc.platforms[0].toHexString()).toBe(idAsString); + } + await platform.remove(); + communityDoc = await Community.findById(community.id); + expect(communityDoc?.platforms).toEqual([]); + expect(communityDoc?.roles).toEqual([]); - // const platformDoc = await Platform.findById(platform._id); - // expect(platformDoc).toBe(null); + const platformDoc = await Platform.findById(platform._id); + expect(platformDoc).toBe(null); - // const module1Doc = await Module.findById(module1._id); - // const module2Doc = await Module.findById(module2._id); - // expect(module1Doc?.options?.platforms.length).toBe(1); - // expect(module2Doc?.options?.platforms.length).toBe(2); - // }); + const module1Doc = await Module.findById(module1._id); + const module2Doc = await Module.findById(module2._id); + expect(module1Doc?.options?.platforms.length).toBe(1); + expect(module2Doc?.options?.platforms.length).toBe(2); + }); - // 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(); + 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 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 communityDoc = await Community.findById(community.id); - // if (communityDoc?.platforms && communityDoc?.roles) { - // const idAsString = platform.id.toHexString ? platform.id.toHexString() : platform.id; - // expect(communityDoc.platforms[0].toHexString()).toBe(idAsString); - // expect(JSON.parse(JSON.stringify(communityDoc.roles))).toEqual([ - // { - // _id: expect.anything(), - // roleType: 'admin', - // source: { - // platform: 'discord', - // identifierType: 'member', - // identifierValues: [user.discordId], - // platformId: platform._id.toHexString(), - // }, - // }, - // ]); - // } - // }); - // }); + const platform = new Platform({ name: 'platform', community: community._id }); + await platform.save(); + const communityDoc = await Community.findById(community.id); + if (communityDoc?.platforms && communityDoc?.roles) { + const idAsString = platform.id.toHexString ? platform.id.toHexString() : platform.id; + expect(communityDoc.platforms[0].toHexString()).toBe(idAsString); + // expect(JSON.parse(JSON.stringify(communityDoc.roles))).toEqual([ + // { + // _id: expect.anything(), + // roleType: 'admin', + // source: { + // platform: 'discord', + // identifierType: 'member', + // identifierValues: [user.discordId], + // platformId: platform._id.toHexString(), + // }, + // }, + // ]); + } + }); + }); }); }); diff --git a/__tests__/utils/setupTestDB.ts b/__tests__/utils/setupTestDB.ts index 394512d..840c340 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 5fff4e2..1dd6c98 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@togethercrew.dev/db", - "version": "3.0.50", + "version": "3.0.51", "description": "All interactions with DB", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/src/models/schemas/Platform.schema.ts b/src/models/schemas/Platform.schema.ts index 5fa1a76..0d6e4d2 100644 --- a/src/models/schemas/Platform.schema.ts +++ b/src/models/schemas/Platform.schema.ts @@ -95,19 +95,19 @@ platformSchema.post('save', async function () { { $addToSet: { platforms: platform._id, - roles: { - $each: [ - { - roleType: 'admin', - source: { - platform: 'discord', - identifierType: 'member', - identifierValues: [user.discordId], - platformId: platform._id, - }, - }, - ], - }, + // roles: { + // $each: [ + // { + // roleType: 'admin', + // source: { + // platform: 'discord', + // identifierType: 'member', + // identifierValues: [user.discordId], + // platformId: platform._id, + // }, + // }, + // ], + // }, }, }, ); From 645b98767ded02a15e4dc3a3b7e708601b8c8f3d Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Mon, 15 Apr 2024 11:03:39 +0400 Subject: [PATCH 2/3] disable the tests --- __tests__/unit/models/platform.model.test.ts | 138 +++++++++---------- 1 file changed, 69 insertions(+), 69 deletions(-) diff --git a/__tests__/unit/models/platform.model.test.ts b/__tests__/unit/models/platform.model.test.ts index 2b59e1a..cc640b7 100644 --- a/__tests__/unit/models/platform.model.test.ts +++ b/__tests__/unit/models/platform.model.test.ts @@ -24,84 +24,84 @@ describe('Platform model', () => { await expect(new Platform(platform).validate()).resolves.toBeUndefined(); }); - describe('Middlewares', () => { - test('Pre Remove: should clean up when platform is deleted', async () => { - const user = new User({ discordId: 'discordId' }); - await user.save(); + // describe('Middlewares', () => { + // test('Pre Remove: should clean up when platform 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(); - const platform = new Platform({ name: 'platform', community: community._id }); - await platform.save(); + // const platform = new Platform({ name: 'platform', community: community._id }); + // await platform.save(); - const module1 = new Module({ - name: 'hivemind', - community: community._id, - options: { platforms: [{ platform: platform._id }, { platform: new Types.ObjectId() }] }, - }); - await module1.save(); + // const module1 = new Module({ + // name: 'hivemind', + // community: community._id, + // options: { platforms: [{ platform: platform._id }, { platform: new Types.ObjectId() }] }, + // }); + // await module1.save(); - const module2 = new Module({ - name: 'hivemind', - community: community._id, - options: { - platforms: [ - { platform: new Types.ObjectId() }, - { platform: platform._id }, - { platform: new Types.ObjectId() }, - ], - }, - }); - await module2.save(); + // const module2 = new Module({ + // name: 'hivemind', + // community: community._id, + // options: { + // platforms: [ + // { platform: new Types.ObjectId() }, + // { platform: platform._id }, + // { platform: new Types.ObjectId() }, + // ], + // }, + // }); + // await module2.save(); - let communityDoc = await Community.findById(community.id); - if (communityDoc?.platforms) { - const idAsString = platform.id.toHexString ? platform.id.toHexString() : platform.id; - expect(communityDoc.platforms[0].toHexString()).toBe(idAsString); - } - await platform.remove(); - communityDoc = await Community.findById(community.id); - expect(communityDoc?.platforms).toEqual([]); - expect(communityDoc?.roles).toEqual([]); + // let communityDoc = await Community.findById(community.id); + // if (communityDoc?.platforms) { + // const idAsString = platform.id.toHexString ? platform.id.toHexString() : platform.id; + // expect(communityDoc.platforms[0].toHexString()).toBe(idAsString); + // } + // await platform.remove(); + // communityDoc = await Community.findById(community.id); + // expect(communityDoc?.platforms).toEqual([]); + // expect(communityDoc?.roles).toEqual([]); - const platformDoc = await Platform.findById(platform._id); - expect(platformDoc).toBe(null); + // const platformDoc = await Platform.findById(platform._id); + // expect(platformDoc).toBe(null); - const module1Doc = await Module.findById(module1._id); - const module2Doc = await Module.findById(module2._id); - expect(module1Doc?.options?.platforms.length).toBe(1); - expect(module2Doc?.options?.platforms.length).toBe(2); - }); + // const module1Doc = await Module.findById(module1._id); + // const module2Doc = await Module.findById(module2._id); + // expect(module1Doc?.options?.platforms.length).toBe(1); + // expect(module2Doc?.options?.platforms.length).toBe(2); + // }); - 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(); + // 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 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 communityDoc = await Community.findById(community.id); - if (communityDoc?.platforms && communityDoc?.roles) { - const idAsString = platform.id.toHexString ? platform.id.toHexString() : platform.id; - expect(communityDoc.platforms[0].toHexString()).toBe(idAsString); - // expect(JSON.parse(JSON.stringify(communityDoc.roles))).toEqual([ - // { - // _id: expect.anything(), - // roleType: 'admin', - // source: { - // platform: 'discord', - // identifierType: 'member', - // identifierValues: [user.discordId], - // platformId: platform._id.toHexString(), - // }, - // }, - // ]); - } - }); - }); + // const platform = new Platform({ name: 'platform', community: community._id }); + // await platform.save(); + // const communityDoc = await Community.findById(community.id); + // if (communityDoc?.platforms && communityDoc?.roles) { + // const idAsString = platform.id.toHexString ? platform.id.toHexString() : platform.id; + // expect(communityDoc.platforms[0].toHexString()).toBe(idAsString); + // // expect(JSON.parse(JSON.stringify(communityDoc.roles))).toEqual([ + // // { + // // _id: expect.anything(), + // // roleType: 'admin', + // // source: { + // // platform: 'discord', + // // identifierType: 'member', + // // identifierValues: [user.discordId], + // // platformId: platform._id.toHexString(), + // // }, + // // }, + // // ]); + // } + // }); + // }); }); }); From fa7a3b0173577d381b8d7c8ee71301f909ba4295 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Mon, 15 Apr 2024 11:04:13 +0400 Subject: [PATCH 3/3] disable the tests --- __tests__/unit/models/platform.model.test.ts | 4 +-- __tests__/utils/setupTestDB.ts | 34 ++++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/__tests__/unit/models/platform.model.test.ts b/__tests__/unit/models/platform.model.test.ts index cc640b7..e339f54 100644 --- a/__tests__/unit/models/platform.model.test.ts +++ b/__tests__/unit/models/platform.model.test.ts @@ -1,9 +1,9 @@ import { Platform, Community, User, Module } 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', () => { diff --git a/__tests__/utils/setupTestDB.ts b/__tests__/utils/setupTestDB.ts index 840c340..394512d 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;