Skip to content

Commit

Permalink
working on member activity tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Behzad-rabiei committed Mar 6, 2024
1 parent bbe09bc commit c65498d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 34 deletions.
6 changes: 6 additions & 0 deletions __tests__/fixtures/discord/channels.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const discordChannel1: IChannel = {
},
],
deletedAt: null,
type: 0
};

export const discordChannel2: IChannel = {
Expand All @@ -35,6 +36,8 @@ export const discordChannel2: IChannel = {
},
],
deletedAt: null,
type: 0

};

export const discordChannel3: IChannel = {
Expand All @@ -56,20 +59,23 @@ export const discordChannel3: IChannel = {
},
],
deletedAt: null,
type: 0
};

export const discordChannel4: IChannel = {
channelId: '345678901234567000',
name: 'Channel 4',
parentId: null,
deletedAt: null,
type: 0
};

export const discordChannel5: IChannel = {
channelId: '345678901234567333',
name: 'Channel 5',
parentId: '987654321098765432',
deletedAt: new Date(),
type: 0
};

export const insertChannels = async function <Type>(channels: Array<Type>, connection: Connection) {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/fixtures/discord/guildMember.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const discordGuildMember4 = {
};

export const discordGuildMember5 = {
discordId: '444444444',
discordId: '66666666',
username: 'User4',
avatar: 'AvatarLink',
roles: ['652345789987654321'],
Expand Down
48 changes: 20 additions & 28 deletions __tests__/integration/memberActivity.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import request from 'supertest';
import httpStatus from 'http-status';
import app from '../../src/app';
import setupTestDB from '../utils/setupTestDB';
import setupTestDB, { cleanUpTenantDatabases } from '../utils/setupTestDB';
import { userOne, insertUsers } from '../fixtures/user.fixture';
import { userOneAccessToken } from '../fixtures/token.fixture';
import {
Expand Down Expand Up @@ -30,16 +30,8 @@ import { Connection } from 'mongoose';
setupTestDB();

describe('member-activity routes', () => {
let connection: Connection;
beforeAll(async () => {
connection = await DatabaseManager.getInstance().getTenantDb('connection-1');
});
afterAll(async () => {
await connection.close();
});
beforeEach(async () => {
await connection.collection('guildmembers').deleteMany({});
await connection.collection('roles').deleteMany({});
cleanUpTenantDatabases()
userOne.communities = [communityOne._id];
communityOne.users = [userOne._id];
communityOne.platforms = [platformOne._id, platformTwo._id, platformFour._id];
Expand All @@ -48,9 +40,9 @@ describe('member-activity routes', () => {
platformFour.community = communityOne._id;
});
describe('POST /api/v1/member-activity/:platformId/active-members-composition-line-graph', () => {
let connection: Connection;
beforeEach(async () => {
await connection.collection('guildmembers').deleteMany({});
await connection.collection('roles').deleteMany({});
connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id);
});
test('should return 200 and active members composition line graph data if req data is ok', async () => {
await insertCommunities([communityOne]);
Expand Down Expand Up @@ -143,9 +135,9 @@ describe('member-activity routes', () => {
});

describe('POST /api/v1/member-activity/:platformId/disengaged-members-composition-line-graph', () => {
let connection: Connection;
beforeEach(async () => {
await connection.collection('guildmembers').deleteMany({});
await connection.collection('roles').deleteMany({});
connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id);
});
test('should return 200 and disengaged members composition line graph data if req data is ok', async () => {
await insertCommunities([communityOne]);
Expand Down Expand Up @@ -234,9 +226,9 @@ describe('member-activity routes', () => {
});

describe('POST /api/v1/member-activity/:platformId/active-members-onboarding-line-graph', () => {
let connection: Connection;
beforeEach(async () => {
await connection.collection('guildmembers').deleteMany({});
await connection.collection('roles').deleteMany({});
connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id);
});
test('should return 200 and active members onboarding line graph data if req data is ok', async () => {
await insertCommunities([communityOne]);
Expand Down Expand Up @@ -322,9 +314,9 @@ describe('member-activity routes', () => {
});

describe('POST /api/v1/member-activity/:platformId/inactive-members-line-graph', () => {
let connection: Connection;
beforeEach(async () => {
await connection.collection('guildmembers').deleteMany({});
await connection.collection('roles').deleteMany({});
connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id);
});
test('should return 200 and inactive members line graph data if req data is ok', async () => {
await insertCommunities([communityOne]);
Expand Down Expand Up @@ -482,9 +474,9 @@ describe('member-activity routes', () => {
// })

describe('GET /api/v1/member-activity/:platformId/fragmentation-score', () => {
let connection: Connection;
beforeEach(async () => {
await connection.collection('guildmembers').deleteMany({});
await connection.collection('roles').deleteMany({});
connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id);
});
test('should return 200 and fragmentation score if req data is ok', async () => {
await insertCommunities([communityOne]);
Expand Down Expand Up @@ -639,9 +631,9 @@ describe('member-activity routes', () => {
});

describe('GET /api/v1/member-activity/:platformId/decentralisation-score', () => {
let connection: Connection;
beforeEach(async () => {
await connection.collection('guildmembers').deleteMany({});
await connection.collection('roles').deleteMany({});
connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id);
});
test('should return 200 and decentralisation score if req data is ok', async () => {
await insertCommunities([communityOne]);
Expand Down Expand Up @@ -816,9 +808,9 @@ describe('member-activity routes', () => {
});

describe('POST /api/v1/member-activity/:platformId/active-members-composition-table', () => {
let connection: Connection;
beforeEach(async () => {
await connection.collection('guildmembers').deleteMany({});
await connection.collection('roles').deleteMany({});
connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id);
});
test('should return 200 and apply the default query options', async () => {
await insertCommunities([communityOne]);
Expand Down Expand Up @@ -1266,9 +1258,9 @@ describe('member-activity routes', () => {
});

describe('POST /api/v1/member-activity/:platformId/active-members-onboarding-table', () => {
let connection: Connection;
beforeEach(async () => {
await connection.collection('guildmembers').deleteMany({});
await connection.collection('roles').deleteMany({});
connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id);
});
test('should return 200 and apply the default query options', async () => {
await insertUsers([userOne]);
Expand Down Expand Up @@ -1719,9 +1711,9 @@ describe('member-activity routes', () => {
});

describe('POST /api/v1/member-activity/:platformId/disengaged-members-composition-table', () => {
let connection: Connection;
beforeEach(async () => {
await connection.collection('guildmembers').deleteMany({});
await connection.collection('roles').deleteMany({});
connection = await DatabaseManager.getInstance().getTenantDb(platformOne.metadata?.id);
});
test('should return 200 and apply the default query options', async () => {
await insertUsers([userOne]);
Expand Down
23 changes: 20 additions & 3 deletions __tests__/utils/setupTestDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ import mongoose from 'mongoose';
import config from '../../src/config';
import RabbitMQ, { MBConnection, Queue } from '@togethercrew.dev/tc-messagebroker';
import Redis from 'ioredis';
import { platformOne, platformTwo, platformThree, platformFour } from '../fixtures/platform.fixture';
import { DatabaseManager } from '@togethercrew.dev/db';

export const cleanUpTenantDatabases = async () => {
const tenantIds = [platformOne.metadata?.id, platformTwo.metadata?.id, platformThree.metadata?.id, platformFour.metadata?.id];

for (const tenantId of tenantIds) {
const connection = await DatabaseManager.getInstance().getTenantDb(tenantId);
await Promise.all(
Object.values(connection.collections).map(async (collection) => collection.deleteMany({})),
);
}
};

const setupTestDB = () => {
beforeAll(async () => {
mongoose.set('strictQuery', false);
Expand All @@ -11,9 +25,12 @@ const setupTestDB = () => {
});

beforeEach(async () => {
await Promise.all(
Object.values(mongoose.connection.collections).map(async (collection) => collection.deleteMany({})),
);
await Promise.all([
// Clean up the main database
...Object.values(mongoose.connection.collections).map(async (collection) => collection.deleteMany({})),
// Clean up tenant databases
cleanUpTenantDatabases(),
]);
});

afterAll(async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/platform.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ const requestAccess = catchAsync(async function (req: ISessionRequest, res: Resp
discord.scopes.connectGuild,
permissionsValueNumber,
state,
id, // Pass the Guild ID for which access is requested
true, // Assuming you want to disable guild selection
id,
true,
);
res.redirect(discordUrl);
}
Expand Down

0 comments on commit c65498d

Please sign in to comment.