diff --git a/server/src/modules/auth/__test/whitelist.service.spec.ts b/server/src/modules/auth/__test/whitelist.service.spec.ts new file mode 100644 index 00000000..892fdd8b --- /dev/null +++ b/server/src/modules/auth/__test/whitelist.service.spec.ts @@ -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; + + 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); + whitelistRepository = module.get>( + 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 }, + }); + }); + }); +}); diff --git a/server/src/modules/auth/services/whitelist.service.ts b/server/src/modules/auth/services/whitelist.service.ts index e534e36d..2334b38e 100644 --- a/server/src/modules/auth/services/whitelist.service.ts +++ b/server/src/modules/auth/services/whitelist.service.ts @@ -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), }, }); } diff --git a/server/src/modules/surveyResponse/__test/responseSchema.controller.spec.ts b/server/src/modules/surveyResponse/__test/responseSchema.controller.spec.ts index d74c43eb..acf5e533 100644 --- a/server/src/modules/surveyResponse/__test/responseSchema.controller.spec.ts +++ b/server/src/modules/surveyResponse/__test/responseSchema.controller.spec.ts @@ -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 } }); }); }); diff --git a/server/src/modules/surveyResponse/controllers/responseSchema.controller.ts b/server/src/modules/surveyResponse/controllers/responseSchema.controller.ts index 6755f09d..4f18f11c 100644 --- a/server/src/modules/surveyResponse/controllers/responseSchema.controller.ts +++ b/server/src/modules/surveyResponse/controllers/responseSchema.controller.ts @@ -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);