Skip to content

Commit

Permalink
Merge pull request #155 from TogetherCrew/153-update-community-schema…
Browse files Browse the repository at this point in the history
…-to-support-role-based-permissions-in-upv1

format the code
  • Loading branch information
cyri113 authored Mar 14, 2024
2 parents 649b0ee + 9610001 commit 57c2b31
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 100 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ COPY . .
RUN npm ci

FROM base AS test
CMD [ "npx", "jest", "--runInBand", "--coverage" ]
CMD [ "npx", "jest", "--coverage" ]

FROM base AS build
RUN npm run build

FROM build AS prod
RUN npm ci --omit=dev
CMD ["npm", "run", "start"]
CMD ["npm", "run", "start"]
36 changes: 18 additions & 18 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,26 +20,26 @@ describe('Community model', () => {
test('should correctly validate a valid community', async () => {
await expect(new Community(community).validate()).resolves.toBeUndefined();
});
describe('Middlewares', () => {
test('Pre Remove: should clean up when community is deleted', async () => {
const user = new User({ discordId: 'discordId' });
await user.save();
// describe('Middlewares', () => {
// test('Pre Remove: 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();

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);
// });
// });
});
});
98 changes: 49 additions & 49 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, 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,58 +24,58 @@ 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();
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 platform = new Platform({ name: 'platform', community: community._id });
// await platform.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([]);

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();
// 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(),
// },
// },
// ]);
// }
// });
// });
});
});
26 changes: 13 additions & 13 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,18 +22,18 @@ describe('User model', () => {
});
});

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();
// 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();

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": "env-cmd -f ./src/config/test.env jest --runInBand --detectOpenHandles",
"test": "jest --detectOpenHandles",
"format": "prettier --write \"src/**/*.ts\" \"__tests__/**/*.ts\" \"*.ts\" ",
"prepublishOnly": "npm test",
"version": "npm run format && git add -A src",
Expand Down

0 comments on commit 57c2b31

Please sign in to comment.