Skip to content
Snippets Groups Projects
auth.service.spec.ts 5.49 KiB
Newer Older
import { JwtService } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { Test } from '@nestjs/testing';
import { JwtServiceMock } from '../../test/mock/services/jwt.mock.service';
import { UsersServiceMock } from '../../test/mock/services/user.mock.service';
import { ConfigurationModule } from '../configuration/configuration.module';
import { MailerModule } from '../mailer/mailer.module';
Hugo SUBTIL's avatar
Hugo SUBTIL committed
import { UsersService } from '../users/services/users.service';
import { AuthService } from './auth.service';
import { LoginDto } from './login-dto';

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

  beforeEach(async () => {
    const module = await Test.createTestingModule({
      imports: [PassportModule, MailerModule, ConfigurationModule],
          provide: UsersService,
          useClass: UsersServiceMock,
        },
        {
          provide: JwtService,
          useClass: JwtServiceMock,
    }).compile();

    authService = module.get<AuthService>(AuthService);
  });

  it('should be defined', () => {
    expect(authService).toBeDefined();

  describe('validateUser', () => {
    it('should validateUser', async () => {
      const result = {
        _id: 'tsfsf6296',
        validationToken:
          'cf1c74c22cedb6b575945098db42d2f493fb759c9142c6aff7980f252886f36ee086574ee99a06bc99119079257116c959c8ec870949cebdef2b293666dbca42',
        emailVerified: false,
        email: 'jacques.dupont@mii.com',
        role: 0,
      const loginDto: LoginDto = { email: 'jacques.dupont@mii.com', password: 'test1A!!' }; //NOSONAR
      expect(await authService.validateUser(loginDto)).toStrictEqual(result);
    });

    it('should not validateUser', async () => {
      const loginDto: LoginDto = { email: 'tom.dupont@mii.com', password: 'test1A!!!' }; //NOSONAR
      expect(await authService.validateUser(loginDto)).toBe(null);
    });
  });

  describe('login', () => {
Bastien DUMONT's avatar
Bastien DUMONT committed
    let _createToken;
    beforeAll(() => {
      _createToken = jest.spyOn(AuthService.prototype as any, '_createToken');
    });
    it('should login user pauline.dupont@mii.com', async () => {
      // Token creation mock
      const token = {
        accessToken:
          'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InBhdWxpbmUuZHVwb250QG1paS5jb20iLCJyb2xlIjowLCJpYXQiOjE2MjAwNDg5MDYsImV4cCI6MTYyMDEzNTMwNn0.jbLazQNJzU_X9Yp1S7XH1rYD5W7yyd1pdGebmkyTMB4',
        expiresAt: '2021-05-04T15:35:06.663+02:00',
      };
      _createToken.mockImplementation(() => token);

      const loginDto: LoginDto = { email: 'pauline.dupont@mii.com', password: 'test1A!!' }; //NOSONAR
      expect(await authService.login(loginDto)).toStrictEqual({
        accessToken:
          'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InBhdWxpbmUuZHVwb250QG1paS5jb20iLCJyb2xlIjowLCJpYXQiOjE2MjAwNDg5MDYsImV4cCI6MTYyMDEzNTMwNn0.jbLazQNJzU_X9Yp1S7XH1rYD5W7yyd1pdGebmkyTMB4',
        expiresAt: '2021-05-04T15:35:06.663+02:00',
        username: 'pauline.dupont@mii.com',
        name: 'DUPONT',
        surname: 'Pauline',
      });
    it('should login not login user jacques.dupont@mii.com because email is not verified', async () => {
      // Token creation mock
      const token = {
        accessToken:
          'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InBhdWxpbmUuZHVwb250QG1paS5jb20iLCJyb2xlIjowLCJpYXQiOjE2MjAwNDg5MDYsImV4cCI6MTYyMDEzNTMwNn0.jbLazQNJzU_X9Yp1S7XH1rYD5W7yyd1pdGebmkyTMB4',
        expiresAt: '2021-05-04T15:35:06.663+02:00',
      };
      _createToken.mockImplementation(() => token);

      const loginDto: LoginDto = { email: 'jacques.dupont@mii.com', password: 'test1A!!' }; //NOSONAR
      try {
        await authService.login(loginDto);
      } catch (e) {
        expect(e.response).toBe('Unverified user');
        expect(e.message).toBe('Unverified user');
        expect(e.status).toBe(418);
    it('should login not login user toto@mii.com because email does not exist', async () => {
      // Token creation mock
      const token = {
        accessToken:
          'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InBhdWxpbmUuZHVwb250QG1paS5jb20iLCJyb2xlIjowLCJpYXQiOjE2MjAwNDg5MDYsImV4cCI6MTYyMDEzNTMwNn0.jbLazQNJzU_X9Yp1S7XH1rYD5W7yyd1pdGebmkyTMB4',
        expiresAt: '2021-05-04T15:35:06.663+02:00',
      };
      _createToken.mockImplementation(() => token);

      const loginDto: LoginDto = { email: 'toto@mii.com', password: 'test1A!!' }; //NOSONAR
      try {
        await authService.login(loginDto);
      } catch (e) {
        expect(e.response).toBe('Invalid credentials');
        expect(e.message).toBe('Invalid credentials');
        expect(e.status).toBe(401);
      }
    it('should login not login user toto@mii.com because password is invalid', async () => {
      // Token creation mock
      const token = {
        accessToken:
          'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InBhdWxpbmUuZHVwb250QG1paS5jb20iLCJyb2xlIjowLCJpYXQiOjE2MjAwNDg5MDYsImV4cCI6MTYyMDEzNTMwNn0.jbLazQNJzU_X9Yp1S7XH1rYD5W7yyd1pdGebmkyTMB4',
        expiresAt: '2021-05-04T15:35:06.663+02:00',
      };
      _createToken.mockImplementation(() => token);

      const loginDto: LoginDto = { email: 'jacques.dupont@mii.com', password: '1' }; //NOSONAR
      try {
        await authService.login(loginDto);
      } catch (e) {
        expect(e.response).toBe('Invalid credentials');
        expect(e.message).toBe('Invalid credentials');
        expect(e.status).toBe(401);
      }