Skip to content

Commit

Permalink
adjust ICurrentUserFactory Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MajedAlaitwniCap committed Jul 26, 2024
1 parent bed1ac3 commit 533d892
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('AuthenticationService', () => {
describe('generateJwt', () => {
describe('when generating new jwt', () => {
it('should pass the correct parameters', async () => {
const mockCurrentStudentUser = iCurrentUserFactory.buildStudentICurrentUser();
const mockCurrentStudentUser = iCurrentUserFactory.withRoleStudent().build();

await authenticationService.generateJwt(mockCurrentStudentUser);
expect(jwtService.sign).toBeCalledWith(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaseFactory } from '@shared/testing';
import { ObjectId } from 'bson';
import { ICurrentUser } from '../interface';

class CurrentUser implements ICurrentUser {
Expand All @@ -25,47 +26,31 @@ class CurrentUser implements ICurrentUser {
}

export class ICurrentUserFactory extends BaseFactory<CurrentUser, ICurrentUser> {
public buildAdminICurrentUser(): ICurrentUser {
return {
userId: 'mockUserId',
roles: ['admin'],
schoolId: 'mockSchoolId',
accountId: 'mockAccountId',
systemId: 'mockSystemId',
isExternalUser: false,
};
public withRole(role: string): this {
const params = { roles: [role] };
return this.params(params);
}

public buildStudentICurrentUser(): ICurrentUser {
return {
userId: 'mockUserId',
roles: ['student'],
schoolId: 'mockSchoolId',
accountId: 'mockAccountId',
systemId: 'mockSystemId',
isExternalUser: false,
};
public withRoleAdmin() {
return this.withRole('admin');
}

public buildTeacherICurrentUser(): ICurrentUser {
return {
userId: 'mockUserId',
roles: ['teacher'],
schoolId: 'mockSchoolId',
accountId: 'mockAccountId',
systemId: 'mockSystemId',
isExternalUser: false,
};
public withRoleStudent() {
return this.withRole('student');
}

public withRoleTeacher() {
return this.withRole('teacher');
}
}

export const iCurrentUserFactory = ICurrentUserFactory.define(CurrentUser, ({ sequence }) => {
export const iCurrentUserFactory = ICurrentUserFactory.define(CurrentUser, () => {
return {
userId: `mockUserId ${sequence}`,
userId: new ObjectId().toHexString(),
roles: [],
schoolId: `mockSchoolId ${sequence}`,
accountId: `mockAccountId ${sequence}`,
systemId: `mockSystemId ${sequence}`,
schoolId: new ObjectId().toHexString(),
accountId: new ObjectId().toHexString(),
systemId: new ObjectId().toHexString(),
isExternalUser: false,
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,7 @@ class JWTPayload implements JwtPayload {
}
}

export class JwtPayloadFactory extends BaseFactory<JWTPayload, JwtPayload> {
public static buildJWTPayloadCurrentUser(): JwtPayload {
const jwtPayload: JwtPayload = {
accountId: 'dummyAccountId',
systemId: 'dummySystemId',
roles: ['mockRoleId'],
schoolId: 'dummySchoolId',
userId: 'dummyUserId',
support: true,
isExternalUser: true,
sub: 'dummyAccountId',
jti: 'random string',
aud: 'some audience',
iss: 'feathers',
iat: Math.floor(new Date().getTime() / 1000),
exp: Math.floor(new Date().getTime() / 1000) + 3600,
};

return jwtPayload;
}
}
export class JwtPayloadFactory extends BaseFactory<JWTPayload, JwtPayload> {}

export const jwtPayloadFactory = JwtPayloadFactory.define(JWTPayload, ({ sequence }) => {
return {
Expand Down

0 comments on commit 533d892

Please sign in to comment.