- Introduced new columns `status`, `created_by`, and `updated_by` in the `users` table to track user status and ownership. - Updated the `employees` table to include a foreign key reference to the `users` table via `user_id`. - Created migration script `0012_users_primary.sql` to apply these changes to the database schema. - Enhanced the `EmployeesService` and `EmployeesRepository` to support user assignments and related data retrieval. - Updated DTOs and service methods to reflect the new user and employee relationships. - Added unit tests to validate the new functionality and ensure data integrity. - Modified existing controllers to accommodate the new fields and relationships in user and employee management.
123 lines
3.3 KiB
TypeScript
123 lines
3.3 KiB
TypeScript
import { INestApplication } from '@nestjs/common';
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
import request from 'supertest';
|
|
import { App } from 'supertest/types';
|
|
import { AppModule } from '../src/app.module';
|
|
import { configureApp } from '../src/common/configure-app';
|
|
import { DRIZZLE, type DrizzleDB } from '../src/database/database.module';
|
|
import { registerAndActivate } from './helpers/activate-user';
|
|
|
|
describe('Auth (e2e)', () => {
|
|
let app: INestApplication<App>;
|
|
let db: DrizzleDB;
|
|
|
|
const username = `user_${Date.now()}`;
|
|
const password = 'password123';
|
|
|
|
beforeAll(async () => {
|
|
const moduleFixture: TestingModule = await Test.createTestingModule({
|
|
imports: [AppModule],
|
|
}).compile();
|
|
|
|
app = moduleFixture.createNestApplication();
|
|
configureApp(app, {
|
|
NODE_ENV: 'test',
|
|
SWAGGER_ENABLED: 'false',
|
|
});
|
|
await app.init();
|
|
db = app.get(DRIZZLE);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
it('GET / remains public', async () => {
|
|
await request(app.getHttpServer())
|
|
.get('/')
|
|
.expect(200)
|
|
.expect('Hello World!');
|
|
});
|
|
|
|
it('GET /auth/me without token returns 401', async () => {
|
|
await request(app.getHttpServer()).get('/auth/me').expect(401);
|
|
});
|
|
|
|
it('register creates a draft user without tokens; login works after activate', async () => {
|
|
const register = await request(app.getHttpServer())
|
|
.post('/auth/register')
|
|
.send({ username, password })
|
|
.expect(201);
|
|
|
|
expect(register.body).toMatchObject({
|
|
username: username.toLowerCase(),
|
|
status: 'draft',
|
|
});
|
|
expect(register.body).not.toHaveProperty('accessToken');
|
|
|
|
await request(app.getHttpServer())
|
|
.post('/auth/login')
|
|
.send({ username, password })
|
|
.expect(401);
|
|
|
|
const activated = await registerAndActivate(
|
|
app,
|
|
db,
|
|
`${username}_active`,
|
|
password,
|
|
);
|
|
|
|
const me = await request(app.getHttpServer())
|
|
.get('/auth/me')
|
|
.set('Authorization', `Bearer ${activated.accessToken}`)
|
|
.expect(200);
|
|
|
|
expect(me.body).toMatchObject({
|
|
username: `${username}_active`.toLowerCase(),
|
|
isSuperadmin: false,
|
|
privilege: null,
|
|
permissions: {},
|
|
});
|
|
|
|
const refreshed = await request(app.getHttpServer())
|
|
.post('/auth/refresh')
|
|
.send({ refreshToken: activated.refreshToken })
|
|
.expect(200);
|
|
|
|
const { accessToken: nextAccess, refreshToken: nextRefresh } =
|
|
refreshed.body as {
|
|
accessToken: string;
|
|
refreshToken: string;
|
|
};
|
|
|
|
await request(app.getHttpServer())
|
|
.get('/auth/me')
|
|
.set('Authorization', `Bearer ${nextAccess}`)
|
|
.expect(200);
|
|
|
|
await request(app.getHttpServer())
|
|
.post('/auth/revoke')
|
|
.send({ refreshToken: nextRefresh })
|
|
.expect(204);
|
|
|
|
await request(app.getHttpServer())
|
|
.get('/auth/me')
|
|
.set('Authorization', `Bearer ${nextAccess}`)
|
|
.expect(401);
|
|
});
|
|
|
|
it('login rejects invalid credentials', async () => {
|
|
await request(app.getHttpServer())
|
|
.post('/auth/login')
|
|
.send({ username, password: 'wrongpass' })
|
|
.expect(401);
|
|
});
|
|
|
|
it('duplicate register returns 409', async () => {
|
|
await request(app.getHttpServer())
|
|
.post('/auth/register')
|
|
.send({ username, password })
|
|
.expect(409);
|
|
});
|
|
});
|