Skip to content

Commit

Permalink
feat: 完成分题统计接口
Browse files Browse the repository at this point in the history
  • Loading branch information
luch1994 committed Jun 11, 2024
1 parent 7bb7b0c commit e71ed94
Show file tree
Hide file tree
Showing 11 changed files with 1,442 additions and 15 deletions.
63 changes: 61 additions & 2 deletions server/src/guards/__test/survey.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { NoPermissionException } from 'src/exceptions/noPermissionException';
import { SurveyMeta } from 'src/models/surveyMeta.entity';
import { WorkspaceMember } from 'src/models/workspaceMember.entity';
import { Collaborator } from 'src/models/collaborator.entity';
import { SURVEY_PERMISSION } from 'src/enums/surveyPermission';

describe('SurveyGuard', () => {
let guard: SurveyGuard;
Expand Down Expand Up @@ -81,7 +82,19 @@ describe('SurveyGuard', () => {
);
});

it('should allow access if user is the owner of the survey', async () => {
it('should allow access if user is the owner of the survey by ownerId', async () => {
const context = createMockExecutionContext();
const surveyMeta = { ownerId: 'testUserId', workspaceId: null };
jest.spyOn(reflector, 'get').mockReturnValue('params.surveyId');
jest
.spyOn(surveyMetaService, 'getSurveyById')
.mockResolvedValue(surveyMeta as SurveyMeta);

const result = await guard.canActivate(context);
expect(result).toBe(true);
});

it('should allow access if user is the owner of the survey by username', async () => {
const context = createMockExecutionContext();
const surveyMeta = { owner: 'testUser', workspaceId: null };
jest.spyOn(reflector, 'get').mockReturnValue('params.surveyId');
Expand All @@ -108,7 +121,35 @@ describe('SurveyGuard', () => {
expect(result).toBe(true);
});

it('should throw NoPermissionException if user has no permissions', async () => {
it('should throw NoPermissionException if user is not a workspace member', async () => {
const context = createMockExecutionContext();
const surveyMeta = { owner: 'anotherUser', workspaceId: 'workspaceId' };
jest.spyOn(reflector, 'get').mockReturnValue('params.surveyId');
jest
.spyOn(surveyMetaService, 'getSurveyById')
.mockResolvedValue(surveyMeta as SurveyMeta);
jest.spyOn(workspaceMemberService, 'findOne').mockResolvedValue(null);

await expect(guard.canActivate(context)).rejects.toThrow(
NoPermissionException,
);
});

it('should throw NoPermissionException if no permissions are provided', async () => {
const context = createMockExecutionContext();
const surveyMeta = { owner: 'anotherUser', workspaceId: null };
jest.spyOn(reflector, 'get').mockReturnValueOnce('params.surveyId');
jest.spyOn(reflector, 'get').mockReturnValueOnce(null);
jest
.spyOn(surveyMetaService, 'getSurveyById')
.mockResolvedValue(surveyMeta as SurveyMeta);

await expect(guard.canActivate(context)).rejects.toThrow(
NoPermissionException,
);
});

it('should throw NoPermissionException if user has no matching permissions', async () => {
const context = createMockExecutionContext();
const surveyMeta = { owner: 'anotherUser', workspaceId: null };
jest.spyOn(reflector, 'get').mockReturnValueOnce('params.surveyId');
Expand All @@ -125,6 +166,24 @@ describe('SurveyGuard', () => {
);
});

it('should allow access if user has the required permissions', async () => {
const context = createMockExecutionContext();
const surveyMeta = { owner: 'anotherUser', workspaceId: null };
jest.spyOn(reflector, 'get').mockReturnValueOnce('params.surveyId');
jest
.spyOn(reflector, 'get')
.mockReturnValueOnce([SURVEY_PERMISSION.SURVEY_CONF_MANAGE]);
jest
.spyOn(surveyMetaService, 'getSurveyById')
.mockResolvedValue(surveyMeta as SurveyMeta);
jest.spyOn(collaboratorService, 'getCollaborator').mockResolvedValue({
permissions: [SURVEY_PERMISSION.SURVEY_CONF_MANAGE],
} as Collaborator);

const result = await guard.canActivate(context);
expect(result).toBe(true);
});

function createMockExecutionContext(): ExecutionContext {
return {
switchToHttp: jest.fn().mockReturnValue({
Expand Down
130 changes: 128 additions & 2 deletions server/src/modules/auth/__test/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { User } from 'src/models/user.entity';
import { HttpException } from 'src/exceptions/httpException';
import { hash256 } from 'src/utils/hash256';
import { RECORD_STATUS } from 'src/enums';
import { ObjectId } from 'mongodb';

describe('UserService', () => {
let service: UserService;
Expand All @@ -21,6 +22,7 @@ describe('UserService', () => {
create: jest.fn(),
save: jest.fn(),
findOne: jest.fn(),
find: jest.fn(),
},
},
],
Expand All @@ -32,6 +34,10 @@ describe('UserService', () => {
);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

it('should create a user', async () => {
const userInfo = {
username: 'testUser',
Expand Down Expand Up @@ -102,7 +108,7 @@ describe('UserService', () => {
expect(user).toEqual({ ...userInfo, password: hashedPassword });
});

it('should return undefined when user is not found by credentials', async () => {
it('should return null when user is not found by credentials', async () => {
const userInfo = {
username: 'nonExistingUser',
password: 'nonExistingPassword',
Expand All @@ -129,18 +135,138 @@ describe('UserService', () => {
const userInfo = {
username: username,
password: 'existingPassword',
} as User;
curStatus: { status: 'ACTIVE' },
} as unknown as User;

jest.spyOn(userRepository, 'findOne').mockResolvedValue(userInfo);

const user = await service.getUserByUsername(username);

expect(userRepository.findOne).toHaveBeenCalledWith({
where: {
username: username,
'curStatus.status': { $ne: RECORD_STATUS.REMOVED },
},
});
expect(user).toEqual(userInfo);
});

it('should return null when user is not found by username', async () => {
const username = 'nonExistingUser';

const findOneSpy = jest
.spyOn(userRepository, 'findOne')
.mockResolvedValue(null);

const user = await service.getUserByUsername(username);

expect(findOneSpy).toHaveBeenCalledWith({
where: {
username: username,
'curStatus.status': { $ne: RECORD_STATUS.REMOVED },
},
});
expect(user).toBe(null);
});

it('should return a user by id', async () => {
const id = '60c72b2f9b1e8a5f4b123456';
const userInfo = {
_id: new ObjectId(id),
username: 'testUser',
curStatus: { status: 'ACTIVE' },
} as unknown as User;

jest.spyOn(userRepository, 'findOne').mockResolvedValue(userInfo);

const user = await service.getUserById(id);

expect(userRepository.findOne).toHaveBeenCalledWith({
where: {
_id: new ObjectId(id),
'curStatus.status': { $ne: RECORD_STATUS.REMOVED },
},
});
expect(user).toEqual(userInfo);
});

it('should return null when user is not found by id', async () => {
const id = '60c72b2f9b1e8a5f4b123456';

const findOneSpy = jest
.spyOn(userRepository, 'findOne')
.mockResolvedValue(null);

const user = await service.getUserById(id);

expect(findOneSpy).toHaveBeenCalledWith({
where: {
_id: new ObjectId(id),
'curStatus.status': { $ne: RECORD_STATUS.REMOVED },
},
});
expect(user).toBe(null);
});

it('should return a list of users by username', async () => {
const username = 'test';
const userList = [
{ _id: new ObjectId(), username: 'testUser1', createDate: new Date() },
{ _id: new ObjectId(), username: 'testUser2', createDate: new Date() },
];

jest
.spyOn(userRepository, 'find')
.mockResolvedValue(userList as unknown as User[]);

const result = await service.getUserListByUsername({
username,
skip: 0,
take: 10,
});

expect(userRepository.find).toHaveBeenCalledWith({
where: {
username: new RegExp(username),
'curStatus.status': { $ne: RECORD_STATUS.REMOVED },
},
skip: 0,
take: 10,
select: ['_id', 'username', 'createDate'],
});
expect(result).toEqual(userList);
});

it('should return a list of users by ids', async () => {
const idList = ['60c72b2f9b1e8a5f4b123456', '60c72b2f9b1e8a5f4b123457'];
const userList = [
{
_id: new ObjectId(idList[0]),
username: 'testUser1',
createDate: new Date(),
},
{
_id: new ObjectId(idList[1]),
username: 'testUser2',
createDate: new Date(),
},
];

jest
.spyOn(userRepository, 'find')
.mockResolvedValue(userList as unknown as User[]);

const result = await service.getUserListByIds({ idList });

expect(userRepository.find).toHaveBeenCalledWith({
where: {
_id: {
$in: idList.map((id) => new ObjectId(id)),
},
'curStatus.status': { $ne: RECORD_STATUS.REMOVED },
},
select: ['_id', 'username', 'createDate'],
});
expect(result).toEqual(userList);
});
});
Loading

0 comments on commit e71ed94

Please sign in to comment.