/** * Requires the real, migrated, seeded Manager database (see SETUP.md) reachable via DATABASE_URL. * Run with: npm run test:integration */ import request from 'supertest'; import { createApp } from '../../src/app'; import { prisma } from '../../src/lib/prisma'; const app = createApp(); const OPERATOR_EMAIL = 'joosth2002@icloud.com'; const OPERATOR_PASSWORD = 'ChangeMe123!'; describe('Manager auth + handoff (real DB)', () => { afterAll(async () => { await prisma.$disconnect(); }); it('logs in with the seeded operator credentials', async () => { const response = await request(app).post('/api/auth/login').send({ email: OPERATOR_EMAIL, password: OPERATOR_PASSWORD }); expect(response.status).toBe(200); expect(response.body.requiresTwoFactor).toBe(false); expect(response.body.accessToken).toBeDefined(); expect(response.body.person.email).toBe(OPERATOR_EMAIL); }); it('rejects the wrong password', async () => { const response = await request(app).post('/api/auth/login').send({ email: OPERATOR_EMAIL, password: 'wrong' }); expect(response.status).toBe(401); }); it('lists the seeded demo company as a membership and issues a working handoff token', async () => { const login = await request(app).post('/api/auth/login').send({ email: OPERATOR_EMAIL, password: OPERATOR_PASSWORD }); const accessToken = login.body.accessToken as string; const myCompanies = await request(app).get('/api/companies').set('Authorization', `Bearer ${accessToken}`); expect(myCompanies.status).toBe(200); expect(myCompanies.body.length).toBeGreaterThanOrEqual(1); const demo = myCompanies.body.find((c: { slug: string }) => c.slug === 'demo'); expect(demo).toBeDefined(); const handoff = await request(app) .post(`/api/companies/${demo.companyId}/handoff`) .set('Authorization', `Bearer ${accessToken}`); expect(handoff.status).toBe(200); expect(handoff.body.token).toBeDefined(); expect(handoff.body.redirectUrl).toBe('http://localhost:5173'); }); it('refuses a handoff for a company the person has no membership in', async () => { const login = await request(app).post('/api/auth/login').send({ email: OPERATOR_EMAIL, password: OPERATOR_PASSWORD }); const accessToken = login.body.accessToken as string; const response = await request(app) .post('/api/companies/999999/handoff') .set('Authorization', `Bearer ${accessToken}`); expect(response.status).toBe(403); }); });