Skip to content

Commit

Permalink
add middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Behzad-rabiei committed Mar 14, 2024
1 parent 8c0317c commit cdc5650
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 75 deletions.
41 changes: 20 additions & 21 deletions __tests__/unit/models/community.mode.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);
});
});
});
});
59 changes: 41 additions & 18 deletions __tests__/unit/models/platform.model.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);
});
});
});
});
27 changes: 13 additions & 14 deletions __tests__/unit/models/user.model.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);

// });
// });
});
});
});
34 changes: 17 additions & 17 deletions __tests__/utils/setupTestDB.ts
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
},
};
34 changes: 33 additions & 1 deletion src/models/schemas/Platform.schema.ts
Original file line number Diff line number Diff line change
@@ -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<IPlatform, PlatformModel>(
{
Expand Down Expand Up @@ -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;

0 comments on commit cdc5650

Please sign in to comment.