Skip to content

Instantly share code, notes, and snippets.

@Klerith
Created February 20, 2025 14:50
Show Gist options
  • Save Klerith/c42d9c128d90329b4877c55819fe86f6 to your computer and use it in GitHub Desktop.
Save Klerith/c42d9c128d90329b4877c55819fe86f6 to your computer and use it in GitHub Desktop.
Ejercicio para las pruebas de rutas protegidas
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { getRepositoryToken } from '@nestjs/typeorm';
import * as request from 'supertest';
import { Repository } from 'typeorm';
import { AppModule } from '../../../src/app.module';
import { User } from '../../../src/auth/entities/user.entity';
import { validate } from 'uuid';
const testingUser = {
email: '[email protected]',
password: 'Abc12345',
fullName: 'Testing user',
};
const testingAdminUser = {
email: '[email protected]',
password: 'Abc12345',
fullName: 'Testing admin',
};
describe('AuthModule Private (e2e)', () => {
let app: INestApplication;
let userRepository: Repository<User>;
let token: string;
let adminToken: string;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
}),
);
await app.init();
userRepository = app.get<Repository<User>>(getRepositoryToken(User));
await userRepository.delete({ email: testingUser.email });
await userRepository.delete({ email: testingAdminUser.email });
});
afterAll(async () => {
await app.close();
});
it('should return 401 if no token is provided', async () => {
const response = await request(app.getHttpServer())
.get('/auth/private')
.send();
});
it('should return new token and user if token is provided', async () => {
// Validar que el id es un uuid válido
});
it('should return custom object if token is valid', async () => {
// Validar la respuesta
});
it('should return 401 if admin token is provided', async () => {
const response = await request(app.getHttpServer())
.get('/auth/private3')
});
it('should return user if admin token is provided', async () => {
const response = await request(app.getHttpServer())
.get('/auth/private3')
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment