import { INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { eq } from 'drizzle-orm'; 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 { privilegeDetails, privilegeKeys, privileges, users, } from '../src/database/schema'; import { PRIVILEGE_ACTIONS } from '../src/modules/privileges/privilege-action'; import { registerAndActivate } from './helpers/activate-user'; describe('Company settings (e2e)', () => { let app: INestApplication; let db: DrizzleDB; const password = 'password123'; const adminUsername = `set_admin_${Date.now()}`; const otherUsername = `set_other_${Date.now()}`; let adminAccessToken: string; let adminUserId: string; let otherAccessToken: string; 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); const admin = await registerAndActivate(app, db, adminUsername, password); adminAccessToken = admin.accessToken; adminUserId = admin.userId; const other = await registerAndActivate(app, db, otherUsername, password); otherAccessToken = other.accessToken; const now = Date.now(); const [priv] = await db .insert(privileges) .values({ name: 'Settings Admin', code: `SET_ADMIN_${now}`, status: 'active', createdAt: now, updatedAt: now, createdBy: adminUserId, updatedBy: adminUserId, }) .returning(); const keys = await db.select().from(privilegeKeys); const detailRows = keys.flatMap((key) => PRIVILEGE_ACTIONS.map((action) => ({ privilegeId: priv.id, privilegeKeyId: key.id, action, value: true, })), ); await db.insert(privilegeDetails).values(detailRows); await db .update(users) .set({ privilegeId: priv.id, updatedAt: Date.now() }) .where(eq(users.id, adminUserId)); }); afterAll(async () => { await app.close(); }); it('forbids settings without permission', async () => { await request(app.getHttpServer()) .get('/settings') .set('Authorization', `Bearer ${otherAccessToken}`) .expect(403); }); it('upserts the cycle start date', async () => { const updated = await request(app.getHttpServer()) .patch('/settings') .set('Authorization', `Bearer ${adminAccessToken}`) .send({ cycleStartDate: '2026-01-05' }) .expect(200); expect((updated.body as { cycleStartDate: number }).cycleStartDate).toBe( Date.parse('2026-01-04T17:00:00.000Z'), ); const got = await request(app.getHttpServer()) .get('/settings') .set('Authorization', `Bearer ${adminAccessToken}`) .expect(200); expect((got.body as { cycleStartDate: number }).cycleStartDate).toBe( (updated.body as { cycleStartDate: number }).cycleStartDate, ); }); it('upserts timeline tracking settings', async () => { const updated = await request(app.getHttpServer()) .patch('/settings') .set('Authorization', `Bearer ${adminAccessToken}`) .send({ gpsIntervalSeconds: 15, checkoutWarningRadiusMeters: 300, }) .expect(200); expect((updated.body as { gpsIntervalSeconds: number }).gpsIntervalSeconds).toBe( 15, ); expect( (updated.body as { checkoutWarningRadiusMeters: number }) .checkoutWarningRadiusMeters, ).toBe(300); }); });