Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 134 #138

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('[Repositories] AuthRepository', () => {
email: true,
id: true,
name: true,
password: true,
username: true,
},
where: { username: user.username },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class AuthRepository {
email: true,
id: true,
name: true,
password: true,
username: true,
},
where: {
Expand Down
111 changes: 56 additions & 55 deletions src/features/auth/services/auth-login-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,12 @@
import { BcryptAdapter } from '@/shared/infra/crypto/bcrypt-adapter';
import { JWTHelper } from '@/shared/infra/jwt/jwt';
import { BcryptAdapter } from '@/shared/infra/crypto/bcrypt-adapter.js';
import { JWTHelper } from '@/shared/infra/jwt/jwt.js';
import { UserMock } from '@/shared/test-helpers/mocks/user.mock.js';

import type { AuthRepository } from '../repositories/auth-repository/auth-repository.js';
import { AuthLoginService } from './auth-login-service';
import { AuthRepository } from '../repositories/auth-repository/auth-repository.js';
import { AuthLoginService } from './auth-login-service.js';

const makeSut = () => {
class AuthRepositoryStub implements AuthRepository {
findUserByCredentials({
password,
username,
}: {
password: string;
username: string;
}): Promise<{
email: string;
id: string;
name: null | string;
username: string;
} | null> {
password;
return Promise.resolve({
email: '[email protected]',
id: 'valid_id',
name: 'john',
username,
});
}

findUserById(id: string): Promise<{
email: string;
id: string;
name: null | string;
username: string;
} | null> {
throw new Error('Method not implemented.' + id);
}

findUserByUsername(username: string): Promise<{
email: string;
id: string;
name: null | string;
username: string;
} | null> {
throw new Error('Method not implemented. ' + username);
}
}

const authRepository = new AuthRepositoryStub();
const authRepository = new AuthRepository();
const bcryptAdapter = new BcryptAdapter();
const jwt = new JWTHelper('secret-test-key');
const authLoginService = new AuthLoginService(
Expand All @@ -55,37 +15,78 @@ const makeSut = () => {
jwt
);

return { authLoginService, authRepository };
return {
authLoginService,
authRepository,
bcryptAdapter,
};
};

describe('Auth Login Service Sut', () => {
it('should return token if login is correctly', async () => {
const { authLoginService, authRepository } = makeSut();
const { authLoginService, authRepository, bcryptAdapter } = makeSut();

const userMock = UserMock.create();

const userCredentials = {
password: 'password',
username: 'usernmae',
password: userMock.password,
username: userMock.username,
};

vi.spyOn(authRepository, 'findUserByCredentials');
vi.spyOn(bcryptAdapter, 'compare').mockResolvedValueOnce(true);

vi.spyOn(authRepository, 'findUserByUsername').mockResolvedValueOnce({
email: userMock.email,
id: userMock.id,
name: userMock.name,
password: userMock.password,
username: userMock.username,
});

const token = authLoginService.execute(userCredentials);

expect(token).toBeTruthy();
});

it('should return return error if credentials are wrong', async () => {
it('should return return error if username dont exists', async () => {
const { authLoginService, authRepository } = makeSut();

const userMock = UserMock.create();

const userCredentials = {
password: 'wrong-password',
username: 'usernmae',
password: userMock.password,
username: 'inexistent username',
};

vi.spyOn(authRepository, 'findUserByCredentials').mockResolvedValue(null);
vi.spyOn(authRepository, 'findUserByUsername').mockResolvedValue(null);

const response = authLoginService.execute(userCredentials);

await expect(response).rejects.toThrowError();
});

it('should return return error if password are wrong', async () => {
const { authLoginService, authRepository, bcryptAdapter } = makeSut();

const userMock = UserMock.create();

const userCredentials = {
password: 'wrong password',
username: userMock.username,
};

const response = authLoginService.execute(userCredentials);

vi.spyOn(authRepository, 'findUserByUsername').mockResolvedValueOnce({
email: userMock.email,
id: userMock.id,
name: userMock.name,
password: userMock.password,
username: userMock.username,
});

vi.spyOn(bcryptAdapter, 'compare').mockResolvedValueOnce(false);

await expect(response).rejects.toThrowError();
});
});
13 changes: 7 additions & 6 deletions src/features/auth/services/auth-login-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ export class AuthLoginService implements Service<Input, Output> {
) {}

async execute({ password, username }: AuthLoginModel) {
const hashedPassword = await this.crypto.encrypt(password);

const user = await this.authRepository.findUserByCredentials({
password: hashedPassword,
username,
});
const user = await this.authRepository.findUserByUsername(username);

if (!user) {
throw new InvalidCredentialsError();
}

const isValidPassword = await this.crypto.compare(password, user.password);

if (!isValidPassword) {
throw new InvalidCredentialsError();
}

const token = this.jwt.createToken({ userId: user.id });

return {
Expand Down
Loading