[Fix] 白名单相关Bug修复及用例新增 (#317)

* feat: 添加问卷信息字段、去掉C端获取问卷信息的敏感字段

* feat: 白名单验证接口

* test: 白名单验证单元测试、参数类型优化

* test: 增加白名单验证单元测试

* feat: 提交问卷时校验白名单

* test: 提交问卷验证verifyId

* test: verifyId不匹配测试

* feat: 注册entity、出参调整

* style: lint

* fix: verifyId校验问题

* test: 出参数据结构调整

* fix: 成员信息查询问题

* test: whitelist service测试用例
This commit is contained in:
Stahsf 2024-06-29 14:05:16 +08:00 committed by GitHub
parent 99e21def1c
commit 5a8e821159
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 81 additions and 4 deletions

View File

@ -0,0 +1,76 @@
import { Test, TestingModule } from '@nestjs/testing';
import { MongoRepository } from 'typeorm';
import { ObjectId } from 'mongodb';
import { getRepositoryToken } from '@nestjs/typeorm';
import { WhitelistService } from '../services/whitelist.service';
import { WhitelistVerify } from 'src/models/whitelistVerify.entity';
describe('WhitelistService', () => {
let service: WhitelistService;
let whitelistRepository: MongoRepository<WhitelistVerify>;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
WhitelistService,
{
provide: getRepositoryToken(WhitelistVerify),
useValue: {
create: jest.fn(),
save: jest.fn(),
findOne: jest.fn(),
},
},
],
}).compile();
service = module.get<WhitelistService>(WhitelistService);
whitelistRepository = module.get<MongoRepository<WhitelistVerify>>(
getRepositoryToken(WhitelistVerify),
);
});
describe('create a verifyId', () => {
it('should create a verifyId successfully', async () => {
const surveyPath = 'GiWfCGPb';
jest.spyOn(whitelistRepository, 'create').mockImplementation((data) => {
return {
...data,
} as WhitelistVerify;
});
jest.spyOn(whitelistRepository, 'save').mockImplementation((data) => {
return Promise.resolve({
_id: new ObjectId(),
...data,
} as WhitelistVerify);
});
const result = await service.create(surveyPath);
expect(result.surveyPath).toBe(surveyPath);
expect(whitelistRepository.create).toHaveBeenCalledWith({
surveyPath,
});
});
});
describe('check if verifyId is correct ', () => {
it('should check if verifyId is correct successfully', async () => {
const mockId = new ObjectId();
const mockWhitelist = new WhitelistVerify();
const surveyPath = 'GiWfCGPb';
mockWhitelist._id = mockId;
mockWhitelist.surveyPath = surveyPath;
jest
.spyOn(whitelistRepository, 'findOne')
.mockImplementation(() => Promise.resolve(mockWhitelist));
const result = await service.match(surveyPath, mockId.toString());
expect(result).not.toBeNull();
expect(whitelistRepository.findOne).toHaveBeenCalledWith({
where: { _id: mockId, surveyPath },
});
});
});
});

View File

@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { MongoRepository } from 'typeorm';
import { WhitelistVerify } from 'src/models/whitelistVerify.entity';
import { ObjectId } from 'mongodb';
@Injectable()
export class WhitelistService {
@ -23,7 +24,7 @@ export class WhitelistService {
return await this.whitelistVerifyRepo.findOne({
where: {
surveyPath,
_id: verifyId,
_id: new ObjectId(verifyId),
},
});
}

View File

@ -177,7 +177,7 @@ describe('ResponseSchemaController', () => {
controller.whitelistValidate(surveyPath, {
password: '123456',
}),
).resolves.toBe(id);
).resolves.toEqual({ code: 200, data: { verifyId: id } });
});
it('whitelistValidate should throw WHITELIST_ERROR code when mobile or email is incorrect', async () => {
@ -266,6 +266,6 @@ describe('ResponseSchemaController', () => {
password: '123456',
value: '13500000000',
}),
).resolves.toBe(id);
).resolves.toEqual({ code: 200, data: { verifyId: id } });
});
});

View File

@ -118,7 +118,7 @@ export class ResponseSchemaController {
}
const workspaceMember = await this.workspaceMemberService.findAllByUserId(
{ userId: user._id },
{ userId: user._id.toString() },
);
if (!workspaceMember.length) {
throw new HttpException('验证失败', EXCEPTION_CODE.WHITELIST_ERROR);