Skip to content

Commit

Permalink
Improve AuthService coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBartusek committed Apr 11, 2024
1 parent 4ea61c7 commit 8fde341
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion apps/api/src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,30 @@ import * as bcrypt from 'bcrypt';
import { Types } from 'mongoose';
import { UsersService } from '../models/users/users.service';
import { AuthService } from './auth.service';
import { MockUserRepository } from '../models/users/mocks/mock-user-repository';

describe('AuthService', () => {
let service: AuthService;

const mockUserRepository = new MockUserRepository();

const mockUsersService = {
findOne: jest.fn(),
findById: jest.fn(),
findOneByIdAndUpdate: jest.fn(),
setConfirmed: jest.fn(),
delete: jest.fn((id) => mockUserRepository.deleteOneById(id)),
updateEmail: jest.fn(async (id, email) => {
const user = await mockUserRepository.findOne();
return {
...user,
_id: id,
profile: {
...user.profile,
email,
},
};
}),
create: jest.fn((data) => {
return {
_id: new Types.ObjectId(),
Expand All @@ -27,6 +43,7 @@ describe('AuthService', () => {
};

const userUpdateSpy = jest.spyOn(mockUsersService, 'findOneByIdAndUpdate');
const userSetConfirmedSpy = jest.spyOn(mockUsersService, 'setConfirmed');

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand Down Expand Up @@ -82,7 +99,7 @@ describe('AuthService', () => {
expect(service.validateUserByUsername('test', 'password')).resolves.toBeNull();
});

it('should validate a user with invalid credentials', async () => {
it('should not validate a user with invalid credentials', async () => {
const invalidPassword = 'other-password';
const hashedValidPassword = await bcrypt.hash('password', 4);

Expand Down Expand Up @@ -117,6 +134,31 @@ describe('AuthService', () => {
expect.objectContaining({ profile: expect.objectContaining({ username: 'test' }) }),
);
});

it('should not validate a user that does not exist', async () => {
mockUsersService.findById.mockResolvedValue(null);

const result = await service.validateUserByUserId(new Types.ObjectId(), 'password');

expect(result).toBeNull();
});

it('should not validate a user with invalid credentials', async () => {
const invalidPassword = 'other-password';
const hashedValidPassword = await bcrypt.hash('password', 4);
mockUsersService.findById.mockResolvedValue({
profile: {
username: 'test',
},
auth: {
password: hashedValidPassword,
},
});

const result = await service.validateUserByUserId(new Types.ObjectId(), invalidPassword);

expect(result).toBeNull();
});
});

describe('updateUserPassword', () => {
Expand All @@ -130,4 +172,19 @@ describe('AuthService', () => {
expect(userUpdateSpy).toBeCalledWith(userId, { 'auth.password': expect.any(String) });
});
});

it('should update email', async () => {
const userId = new Types.ObjectId();

const user = await service.updateUserEmail(userId, '[email protected]');

expect(userSetConfirmedSpy).toBeCalledWith(userId, false);
expect(user.profile.email).toBe('[email protected]');
});

it('should delete user', async () => {
const userId = new Types.ObjectId();
const user = await service.deleteUserAccount(userId);
expect(user._id).toBe(userId);
});
});

0 comments on commit 8fde341

Please sign in to comment.