Skip to content

Commit

Permalink
Merge branch 'main' into EW-777
Browse files Browse the repository at this point in the history
  • Loading branch information
MajedAlaitwniCap authored Jan 15, 2025
2 parents 821722c + 149779e commit 6fe76a8
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { defaultMikroOrmOptions } from '@shared/common/defaultMikroOrmOptions';
import { ALL_ENTITIES } from '@shared/domain/entity/all-entities';
import { createConfigModuleOptions, DB_PASSWORD, DB_URL, DB_USERNAME } from '@src/config';
import { CoreModule } from '@src/core';
import { AuthenticationApiModule } from '../authentication/authentication-api.module';
import { AuthorizationModule } from '../authorization';
import { serverConfig } from '../server';
import { config } from './board-collaboration.config';
Expand Down Expand Up @@ -50,7 +49,6 @@ const testConfig = () => {
imports: [
...imports,
ConfigModule.forRoot(createConfigModuleOptions(testConfig)),
AuthenticationApiModule,
MongoMemoryDatabaseModule.forRoot({
...defaultMikroOrmOptions,
entities: ALL_ENTITIES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { Test } from '@nestjs/testing';

import { MongoIoAdapter } from '@infra/socketio';
import { InputFormat } from '@shared/domain/types/input-format.types';
import { cleanupCollections, courseFactory, userFactory } from '@shared/testing';
import { cleanupCollections, courseFactory, schoolEntityFactory, UserAndAccountTestFactory } from '@shared/testing';
import { JwtAuthenticationFactory } from '@shared/testing/factory/jwt-authentication.factory';
import { getSocketApiClient, waitForEvent } from '@shared/testing/test-socket-api-client';
import { Socket } from 'socket.io-client';
import { BoardCollaborationTestModule } from '../../board-collaboration.app.module';
Expand Down Expand Up @@ -48,14 +49,34 @@ describe(BoardCollaborationGateway.name, () => {

const setup = async () => {
await cleanupCollections(em);
const user = userFactory.buildWithId();
const unauthorizedUser = userFactory.buildWithId();
const school = schoolEntityFactory.build();
const { teacherUser, teacherAccount } = UserAndAccountTestFactory.buildTeacher({ school });

const course = courseFactory.build({ teachers: [user] });
await em.persistAndFlush([user, unauthorizedUser, course]);
const teacherAuthJwt = JwtAuthenticationFactory.createJwt({
accountId: teacherAccount.id,
userId: teacherUser.id,
schoolId: teacherUser.school.id,
roles: [teacherUser.roles[0].id],
support: false,
isExternalUser: false,
});

const { studentUser, studentAccount } = UserAndAccountTestFactory.buildStudent({ school });

const studentAuthJwt = JwtAuthenticationFactory.createJwt({
accountId: studentAccount.id,
userId: studentUser.id,
schoolId: studentUser.school.id,
roles: [studentUser.roles[0].id],
support: false,
isExternalUser: false,
});

const course = courseFactory.build({ teachers: [teacherUser] });
await em.persistAndFlush([teacherUser, teacherAccount, studentUser, studentAccount, course]);

ioClient = await getSocketApiClient(app, user);
unauthorizedIoClient = await getSocketApiClient(app, unauthorizedUser);
ioClient = await getSocketApiClient(app, teacherAuthJwt);
unauthorizedIoClient = await getSocketApiClient(app, studentAuthJwt);

const columnBoardNode = columnBoardEntityFactory.buildWithId({
context: { id: course.id, type: BoardExternalReferenceType.Course },
Expand Down
6 changes: 2 additions & 4 deletions apps/server/src/modules/school/api/school.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { CurrentUser, ICurrentUser, JwtAuthentication } from '@infra/auth-guard'
import { Body, Controller, ForbiddenException, Get, NotFoundException, Param, Patch, Query } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ApiValidationError } from '@shared/common';
import { PaginationParams } from '@shared/controller';
import { SchoolQueryParams, SchoolRemoveSystemUrlParams, SchoolUpdateBodyParams, SchoolUrlParams } from './dto/param';
import { SchoolForExternalInviteResponse, SchoolResponse, SchoolSystemResponse } from './dto/response';
import { SchoolExistsResponse } from './dto/response/school-exists.response';
Expand Down Expand Up @@ -98,10 +97,9 @@ export class SchoolController {
@JwtAuthentication()
public async getTeachers(
@Param() urlParams: SchoolUrlParams,
@CurrentUser() user: ICurrentUser,
@Query() pagination: PaginationParams
@CurrentUser() user: ICurrentUser
): Promise<SchoolUserListResponse> {
const res = await this.schoolUc.getSchoolTeachers(urlParams.schoolId, user.userId, pagination);
const res = await this.schoolUc.getSchoolTeachers(urlParams.schoolId, user.userId);
return res;
}
}
13 changes: 4 additions & 9 deletions apps/server/src/modules/school/api/school.uc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AuthorizationContextBuilder, AuthorizationService } from '@modules/authorization';
import { Injectable } from '@nestjs/common';
import { PaginationParams } from '@shared/controller';
import { Page, UserDO } from '@shared/domain/domainobject';
import { User } from '@shared/domain/entity';
import { Permission, RoleName, SortOrder } from '@shared/domain/interface';
Expand Down Expand Up @@ -133,11 +132,7 @@ export class SchoolUc {
return dto;
}

public async getSchoolTeachers(
schoolId: EntityId,
userId: EntityId,
pagination?: PaginationParams
): Promise<SchoolUserListResponse> {
public async getSchoolTeachers(schoolId: EntityId, userId: EntityId): Promise<SchoolUserListResponse> {
const [school, user] = await Promise.all([
this.schoolService.getSchoolById(schoolId),
this.authorizationService.getUserWithPermissions(userId),
Expand All @@ -149,12 +144,12 @@ export class SchoolUc {

let result: Page<UserDO>;
if (isUserOfSchool) {
result = await this.userService.findBySchoolRole(schoolId, RoleName.TEACHER, { pagination });
result = await this.userService.findBySchoolRole(schoolId, RoleName.TEACHER);
} else {
result = await this.userService.findPublicTeachersBySchool(schoolId, { pagination });
result = await this.userService.findPublicTeachersBySchool(schoolId);
}

const responseDto = SchoolUserResponseMapper.mapToListResponse(result, pagination);
const responseDto = SchoolUserResponseMapper.mapToListResponse(result);
return responseDto;
}

Expand Down
11 changes: 0 additions & 11 deletions apps/server/src/modules/school/api/test/school-users.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,6 @@ describe('School Controller (API)', () => {
])
);
});

it('should paginate', async () => {
const { loggedInClient, school } = await setup();

const response = await loggedInClient.get(`${school.id}/teachers`).query({ skip: 1, limit: 1 });
const body = response.body as SchoolUserListResponse;

expect(body.data).toHaveLength(1);
expect(body.total).toEqual(4);
expect(body.skip).toEqual(1);
});
});
});
});
27 changes: 2 additions & 25 deletions apps/server/src/shared/testing/test-socket-api-client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { EntityManager } from '@mikro-orm/mongodb';
import { INestApplication } from '@nestjs/common';

import { User } from '@shared/domain/entity';
import { accountFactory } from '@modules/account/testing';
import { LocalAuthorizationBodyParams } from '@modules/authentication/controllers/dto';
import { Socket, io } from 'socket.io-client';
import request from 'supertest';

export async function waitForEvent(socket: Socket, eventName: string): Promise<unknown> {
return new Promise((resolve) => {
Expand All @@ -15,33 +10,15 @@ export async function waitForEvent(socket: Socket, eventName: string): Promise<u
});
}

export async function getSocketApiClient(app: INestApplication, user: User) {
const em = app.get<EntityManager>(EntityManager);
const defaultPassword = 'DummyPasswd!1';
const defaultPasswordHash = '$2a$10$/DsztV5o6P5piW2eWJsxw.4nHovmJGBA.QNwiTmuZ/uvUc40b.Uhu';
const account = accountFactory.buildWithId({
userId: user.id,
username: user.email,
password: defaultPasswordHash,
});

await em.persistAndFlush([account]);

const params: LocalAuthorizationBodyParams = {
username: user.email,
password: defaultPassword,
};
const response = await request(app.getHttpServer()).post(`/authentication/local`).send(params).expect(200);
const jwt = (response.body as { accessToken: string }).accessToken;

export async function getSocketApiClient(app: INestApplication, authValue?: string): Promise<Socket> {
const url = await app.getUrl();

const ioClient = io(url, {
autoConnect: false,
path: '/board-collaboration',
transports: ['websocket', 'polling'],
extraHeaders: {
Cookie: `jwt=${jwt}`,
Cookie: `jwt=${authValue || ''}`,
},
});

Expand Down
11 changes: 9 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"setup:idm": "npm run setup:idm:seed && npm run setup:idm:configure",
"build": "npm run nest:build",
"postinstall": "node ./esbuild/esmodules-bundler.js",
"copy-files": "copyfiles src/**/*.yaml build",
"inspect": "npm run build && node --inspect=5959 build/src/",
"inspect-container": "npm run build && node --inspect=0.0.0.0:5959 build/src/",
"start": "npm run nest:start:prod",
Expand Down Expand Up @@ -188,7 +187,6 @@
"commander": "^8.1.0",
"compression": "^1.6.2",
"cors": "^2.8.1",
"cross-env": "^7.0.0",
"crypto-js": "^4.2.0",
"disposable-email-domains": "^1.0.56",
"es6-promisify": "^7.0.0",
Expand All @@ -198,7 +196,6 @@
"feathers-hooks-common": "^8.1.1",
"feathers-swagger": "^3.0.0",
"file-type": "^18.5.0",
"freeport": "^1.0.5",
"gm": "^1.25.0",
"html-entities": "^2.3.2",
"i18next": "^23.3.0",
Expand Down Expand Up @@ -257,6 +254,8 @@
"yaml": "^2.5.0"
},
"devDependencies": {
"cross-env": "^7.0.0",
"freeport": "^1.0.5",
"@aws-sdk/client-s3": "^3.617.0",
"@faker-js/faker": "^8.0.2",
"@feathersjs/adapter-tests": "^5.0.29",
Expand Down

0 comments on commit 6fe76a8

Please sign in to comment.