Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.1 kB
1
Indexable
Never
// CAMINHO: SRC/TESTS/login.service.spec.ts

import { expect } from 'chai';
import * as sinon from 'sinon';
import loginSrv from '../services/login.service';
// import genToken from '../services/login.service';
import Users from '../database/models/Users.model';
import * as bcrypt from 'bcryptjs';

describe('loginSrv', () => {
  afterEach(() => {
    sinon.restore();
  });

  it('should return status 400 and error message if email or password is not provided', async () => {
    const result = await loginSrv.loginSrv('', '');
    expect(result).to.deep.equal({ status: 400, message: 'All fields must be filled' });
  });

  it('should return status 401 and error message if email is invalid', async () => {
    const result = await loginSrv.loginSrv('invalidemail', 'password');
    expect(result).to.deep.equal({ status: 401, message: 'Invalid email or password' });
  });

  it('should return status 401 and error message if user does not exist', async () => {
    sinon.stub(Users, 'findOne').resolves(null);
    const result = await loginSrv.loginSrv('validemail@example.com', 'password');
    expect(result).to.deep.equal({ status: 401, message: 'Invalid email or password' });
  });

  it('should return status 401 and error message if password is incorrect', async () => {
    const user: any = { email: 'validemail@example.com', password: 'hashedpassword' };
    sinon.stub(Users, 'findOne').resolves(user);
    sinon.stub(bcrypt, 'compare').resolves(false);
    const result = await loginSrv.loginSrv('validemail@example.com', 'incorrectpassword');
    expect(result).to.deep.equal({ status: 401, message: 'Invalid email or password' });
  });

  it('should return status 401 and error message if password is less than 6 characters', async () => {
    const user: any = { email: 'validemail@example.com', password: 'hjkl' };
    sinon.stub(Users, 'findOne').resolves(user);
    sinon.stub(bcrypt, 'compare').resolves(true);
    const result = await loginSrv.loginSrv('validemail@example.com', 'hjkl');
    expect(result).to.deep.equal({ status: 401, message: 'Invalid email or password' });
  });

  it('should return status 200 and a token if login is successful', async () => {
    const user: any = { email: 'validemail@example.com', password: 'hashedpassword', username: 'username' };
    sinon.stub(Users, 'findOne').resolves(user);
    sinon.stub(bcrypt, 'compare').resolves(true);
    const mock: any = { status: 200, token: 'token' }
    sinon.stub(loginSrv, 'loginSrv').returns(mock);
    const result = await loginSrv.loginSrv('validemail@example.com', 'password');
    expect(result).to.deep.equal(mock);
  });
  
  it('should return the role if the user exists', async () => {
    const user: any = { username: 'username', role: 'admin' };
    sinon.stub(Users, 'findOne').resolves(user);
    const result = await loginSrv.getRoleSrv('username');
    expect(result).to.equal('admin');
  });

  it('should return null if the user does not exist', async () => {
    sinon.stub(Users, 'findOne').resolves(null);
    const result = await loginSrv.getRoleSrv('username');
    expect(result).to.be.null;
  });
});
Leave a Comment