HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-172-31-4-197 6.8.0-1036-aws #38~22.04.1-Ubuntu SMP Fri Aug 22 15:44:33 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/api-management/test/users/user.controller.findAll.e2e-spec.ts
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { UserModule } from '../../src/app/user/user.module';
import { UserSchema } from '../../src/app/user/schemas/user.schema';
import mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { MongooseModule } from '@nestjs/mongoose';

jest.setTimeout(30000);

describe('UserController (e2e)', () => {
  let app: INestApplication;
  let mongoServer;
  let userModel = mongoose.model('User', UserSchema);
  let user;

  const seederUser = {
    tipoIdentificacion: [
      {
        _id: new mongoose.Types.ObjectId(),
        nombre: 'Cédula de ciudadanía',
      },
    ],
    identificacion: '123456789',
    nombres: 'Juan',
    apellidos: 'Pérez',
    email: 'juan.perez@example.com',
    password: 'securepassword123',
    roles: [
      {
        _id: new mongoose.Types.ObjectId(),
        nombre: 'Administrador',
      },
    ],
  };

  beforeAll(async () => {
    mongoServer = await MongoMemoryServer.create();
    const uri = mongoServer.getUri();
    await mongoose.connect(uri);
    userModel = mongoose.model('User', UserSchema);
  });

  afterAll(async () => {
    await mongoose.disconnect();
    await mongoServer.stop();
  });

  beforeEach(async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [
        MongooseModule.forRootAsync({
          useFactory: () => ({
            uri: mongoServer.getUri(),
            useNewUrlParser: true,
            useUnifiedTopology: true,
          }),
        }),
        UserModule,
      ],
    }).compile();

    user = await userModel.create(seederUser);
    app = moduleRef.createNestApplication();
    await app.init();
  });

  afterEach(async () => {
    await userModel.deleteMany({});
    if (app) await app.close();
  });

  describe('GET users', () => {
    it('debe retornar una lista de usuarios', async () => {
      const page: number = 1;
      const pageSize: number = 5;

      const expectResponse = {
        data: [
          {
            _id: user._id.toString(),
            tipoIdentificacion: [
              {
                _id: user.tipoIdentificacion[0]._id.toString(),
                nombre: user.tipoIdentificacion[0].nombre,
              },
            ],
            identificacion: user.identificacion,
            nombres: user.nombres,
            apellidos: user.apellidos,
            email: user.email,
            roles: [
              {
                _id: user.roles[0]._id.toString(),
                nombre: user.roles[0].nombre,
              },
            ],
          },
        ],
        message: 'Consulta exitosa',
        summary: 'Los elementos fueron encontrados',
        totalRecords: 1,
        rows: 1,
      };

      const response = await request(app.getHttpServer())
        .get(`/users?page=${page}&pageSize=${pageSize}`)
        .set({
          'Content-Type': 'application/json',
          Accept: 'application/json',
        })
        .expect(200);

      expect(response.body).toEqual(expectResponse);
    });
  });
});