Files
goodgo-platform/e2e/fixtures/auth.fixture.ts
Ho Ngoc Hai c658e540f0 fix(api): remove type-only imports of injectable classes to fix NestJS DI
Type-only imports (`import { type X }`) strip runtime type metadata
needed by NestJS dependency injection via reflect-metadata. This caused
`UnknownDependenciesException` errors where constructor parameters
resolved to `Function` instead of the actual class.

Fixed 129 files across all modules:
- Services (LoggerService, PrismaService, CacheService, etc.)
- CQRS buses (EventBus, QueryBus, CommandBus)
- DTOs used with @Body()/@Query() decorators in controllers
- Payment gateway services and search repositories

Also fixed E2E test infrastructure:
- auth.fixture.ts: use destructuring pattern for Playwright fixture
- global-teardown.ts: correct column names (Lead.agentId, Transaction.buyerId)
- inquiries.spec.ts: flexible response property checks
- payments-callback.spec.ts: accept 500 for unknown provider

All 111 API E2E tests now pass.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-12 20:43:35 +07:00

87 lines
2.6 KiB
TypeScript

import { test as base, type APIRequestContext } from '@playwright/test';
/** Shape returned by POST /auth/register and POST /auth/login */
export interface TokenPair {
accessToken: string;
refreshToken: string;
}
let _counter = 0;
/** Generates a unique test user payload for each test run. */
export function createTestUser(suffix = `${Date.now()}${(++_counter).toString().padStart(4, '0')}${Math.random().toString(36).slice(2, 6)}`) {
// Use last 8 digits of the combined suffix for the phone number
const phoneSuffix = suffix.replace(/\D/g, '').slice(-8).padStart(8, '0');
return {
phone: `09${phoneSuffix}`,
password: 'Test@1234!',
fullName: `Test User ${suffix}`,
email: `testuser${suffix}@goodgo.test`,
};
}
/** Registers a new user via the API and returns the token pair. */
export async function registerUser(
request: APIRequestContext,
user = createTestUser(),
): Promise<TokenPair & { user: ReturnType<typeof createTestUser> }> {
const res = await request.post('auth/register', { data: user });
if (!res.ok()) {
const body = await res.text();
throw new Error(`Register failed (${res.status()}): ${body}`);
}
const tokens: TokenPair = await res.json();
return { ...tokens, user };
}
/** Logs in an existing user and returns the token pair. */
export async function loginUser(
request: APIRequestContext,
phone: string,
password: string,
): Promise<TokenPair> {
const res = await request.post('auth/login', {
data: { phone, password },
});
if (!res.ok()) {
const body = await res.text();
throw new Error(`Login failed (${res.status()}): ${body}`);
}
return res.json();
}
/**
* Extended test fixture that provides a pre-authenticated API context.
*
* Usage:
* import { test } from '../fixtures/auth.fixture';
* test('my test', async ({ authedRequest, testTokens }) => { ... });
*/
export const test = base.extend<{
testUser: ReturnType<typeof createTestUser>;
testTokens: TokenPair;
authedRequest: APIRequestContext;
}>({
testUser: async ({}, use) => {
await use(createTestUser());
},
testTokens: async ({ request, testUser }, use) => {
const { accessToken, refreshToken } = await registerUser(request, testUser);
await use({ accessToken, refreshToken });
},
authedRequest: async ({ playwright, testTokens, baseURL }, use) => {
const ctx = await playwright.request.newContext({
baseURL,
extraHTTPHeaders: {
Authorization: `Bearer ${testTokens.accessToken}`,
},
});
await use(ctx);
await ctx.dispose();
},
});
export { expect } from '@playwright/test';