Skip to content
Snippets Groups Projects
auth.service.spec.ts 5.49 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { JwtService } from '@nestjs/jwt';
    
    import { PassportModule } from '@nestjs/passport';
    
    import { Test, TestingModule } 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 service: AuthService;
    
      beforeEach(async () => {
        const module: TestingModule = await Test.createTestingModule({
    
          imports: [PassportModule, MailerModule, ConfigurationModule],
    
              provide: UsersService,
              useClass: UsersServiceMock,
            },
            {
              provide: JwtService,
              useClass: JwtServiceMock,
    
        }).compile();
    
        service = module.get<AuthService>(AuthService);
      });
    
      it('should be defined', () => {
        expect(service).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 service.validateUser(loginDto)).toStrictEqual(result);
    
        });
    
        it('should not validateUser', async () => {
    
          const loginDto: LoginDto = { email: 'tom.dupont@mii.com', password: 'test1A!!!' }; //NOSONAR
          expect(await service.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 service.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 service.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 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 service.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 service.login(loginDto);
          } catch (e) {
            expect(e.response).toBe('Invalid credentials');
            expect(e.message).toBe('Invalid credentials');
            expect(e.status).toBe(401);
          }