Skip to content

Commit f52a8ba

Browse files
committed
test: fix auth tests after changes
1 parent bae6356 commit f52a8ba

1 file changed

Lines changed: 40 additions & 18 deletions

File tree

packages/apps/reputation-oracle/server/src/modules/auth/auth.service.spec.ts

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
UserEntity,
4343
UserRepository,
4444
UserService,
45+
type OperatorUserEntity,
4546
} from '../user';
4647
import { Web3Service } from '../web3/web3.service';
4748
import {
@@ -163,6 +164,7 @@ describe('AuthService', () => {
163164
email: signInDto.email,
164165
password: MOCK_HASHED_PASSWORD,
165166
status: UserStatus.ACTIVE,
167+
role: UserRole.WORKER,
166168
};
167169

168170
let findOneByEmailMock: any;
@@ -185,7 +187,15 @@ describe('AuthService', () => {
185187

186188
const result = await authService.signin(signInDto);
187189

188-
expect(findOneByEmailMock).toHaveBeenCalledWith(signInDto.email);
190+
expect(findOneByEmailMock).toHaveBeenCalledWith(signInDto.email, {
191+
relations: {
192+
kyc: true,
193+
siteKeys: true,
194+
userQualifications: {
195+
qualification: true,
196+
},
197+
},
198+
});
189199
expect(authService.auth).toHaveBeenCalledWith(userEntity);
190200
expect(result).toStrictEqual({
191201
accessToken: MOCK_ACCESS_TOKEN,
@@ -200,7 +210,15 @@ describe('AuthService', () => {
200210
new AuthError(AuthErrorMessage.INVALID_CREDENTIALS),
201211
);
202212

203-
expect(findOneByEmailMock).toHaveBeenCalledWith(signInDto.email);
213+
expect(findOneByEmailMock).toHaveBeenCalledWith(signInDto.email, {
214+
relations: {
215+
kyc: true,
216+
siteKeys: true,
217+
userQualifications: {
218+
qualification: true,
219+
},
220+
},
221+
});
204222
});
205223
});
206224

@@ -216,6 +234,7 @@ describe('AuthService', () => {
216234
id: 1,
217235
email: userCreateDto.email,
218236
password: MOCK_HASHED_PASSWORD,
237+
role: UserRole.WORKER,
219238
};
220239

221240
let createUserMock: any;
@@ -233,15 +252,14 @@ describe('AuthService', () => {
233252
});
234253

235254
it('should create a new user and return the user entity', async () => {
236-
const result = await authService.signup(userCreateDto);
255+
await authService.signup(userCreateDto);
237256

238257
expect(userService.createWorkerUser).toHaveBeenCalledWith(userCreateDto);
239258
expect(tokenRepository.createUnique).toHaveBeenCalledWith({
240259
type: TokenType.EMAIL,
241-
user: userEntity,
260+
userId: userEntity.id,
242261
expiresAt: expect.any(Date),
243262
});
244-
expect(result).toBe(userEntity);
245263
});
246264

247265
it("should call emailService sendEmail if user's email is valid", async () => {
@@ -411,7 +429,7 @@ describe('AuthService', () => {
411429
const tokenEntity: Partial<TokenEntity> = {
412430
uuid: v4(),
413431
type: TokenType.EMAIL,
414-
user: userEntity as UserEntity,
432+
userId: userEntity.id,
415433
};
416434

417435
let findTokenMock: any;
@@ -459,7 +477,9 @@ describe('AuthService', () => {
459477
new Date().setDate(new Date().getDate() + 1),
460478
);
461479
findTokenMock.mockResolvedValue(tokenEntity as TokenEntity);
462-
userService.updatePassword = jest.fn();
480+
userService.updatePassword = jest
481+
.fn()
482+
.mockResolvedValueOnce(userEntity);
463483
emailService.sendEmail = jest.fn();
464484

465485
const updatePasswordMock = jest.spyOn(userService, 'updatePassword');
@@ -485,7 +505,7 @@ describe('AuthService', () => {
485505
const tokenEntity: Partial<TokenEntity> = {
486506
uuid: v4(),
487507
type: TokenType.EMAIL,
488-
user: userEntity as UserEntity,
508+
userId: userEntity.id,
489509
};
490510

491511
let findTokenMock: any;
@@ -506,6 +526,7 @@ describe('AuthService', () => {
506526
new AuthError(AuthErrorMessage.INVALID_REFRESH_TOKEN),
507527
);
508528
});
529+
509530
it('should throw an error if token is expired', () => {
510531
tokenEntity.expiresAt = new Date(new Date().getDate() - 1);
511532
findTokenMock.mockResolvedValue(tokenEntity as TokenEntity);
@@ -521,12 +542,16 @@ describe('AuthService', () => {
521542
new Date().setDate(new Date().getDate() + 1),
522543
);
523544
findTokenMock.mockResolvedValue(tokenEntity as TokenEntity);
524-
userRepository.updateOne = jest.fn();
545+
userRepository.updateOneById = jest.fn();
525546

526547
await authService.emailVerification({ token: 'token' });
527548

528-
expect(userRepository.updateOne).toHaveBeenCalled();
529-
expect(tokenEntity.user?.status).toBe(UserStatus.ACTIVE);
549+
expect(userRepository.updateOneById).toHaveBeenCalledWith(
550+
userEntity.id,
551+
{
552+
status: UserStatus.ACTIVE,
553+
},
554+
);
530555
});
531556
});
532557

@@ -609,28 +634,25 @@ describe('AuthService', () => {
609634
nonce,
610635
};
611636

612-
let getByAddressMock: any;
613637
let updateNonceMock: any;
614638

615639
beforeEach(() => {
616-
getByAddressMock = jest.spyOn(userRepository, 'findOneByAddress');
640+
jest
641+
.spyOn(userService, 'findOperatorUser')
642+
.mockResolvedValue(userEntity as OperatorUserEntity);
617643
updateNonceMock = jest.spyOn(userService, 'updateNonce');
618644

619645
jest.spyOn(authService, 'auth').mockResolvedValue({
620646
accessToken: MOCK_ACCESS_TOKEN,
621647
refreshToken: MOCK_REFRESH_TOKEN,
622648
});
623-
jest
624-
.spyOn(userRepository, 'findOneByAddress')
625-
.mockResolvedValue({ nonce: nonce } as any);
626649
});
627650

628651
afterEach(() => {
629652
jest.clearAllMocks();
630653
});
631654

632655
it('should sign in the user, reset nonce and return the JWT', async () => {
633-
getByAddressMock.mockResolvedValue(userEntity as UserEntity);
634656
updateNonceMock.mockResolvedValue({
635657
...userEntity,
636658
nonce: nonce1,
@@ -649,7 +671,7 @@ describe('AuthService', () => {
649671
signature,
650672
});
651673

652-
expect(userRepository.findOneByAddress).toHaveBeenCalledWith(
674+
expect(userService.findOperatorUser).toHaveBeenCalledWith(
653675
MOCK_ADDRESS,
654676
);
655677
expect(userService.updateNonce).toHaveBeenCalledWith(userEntity);

0 commit comments

Comments
 (0)