All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 27m55s
84 lines
3.3 KiB
TypeScript
84 lines
3.3 KiB
TypeScript
// src/tests/e2e/admin-dashboard.e2e.test.ts
|
|
import { describe, it, expect, afterAll } from 'vitest';
|
|
import * as apiClient from '../../services/apiClient';
|
|
import { getPool } from '../../services/db/connection.db';
|
|
import { cleanupDb } from '../utils/cleanup';
|
|
|
|
/**
|
|
* @vitest-environment node
|
|
*/
|
|
|
|
describe('E2E Admin Dashboard Flow', () => {
|
|
// Use a unique email for every run to avoid collisions
|
|
const uniqueId = Date.now();
|
|
const adminEmail = `e2e-admin-${uniqueId}@example.com`;
|
|
const adminPassword = 'StrongPassword123!';
|
|
|
|
let authToken: string;
|
|
let adminUserId: string | null = null;
|
|
|
|
afterAll(async () => {
|
|
// Safety cleanup: Ensure the user is deleted from the DB if the test fails mid-way.
|
|
await cleanupDb({
|
|
userIds: [adminUserId],
|
|
});
|
|
});
|
|
|
|
it('should allow an admin to log in and access dashboard features', async () => {
|
|
// 1. Register a new user (initially a regular user)
|
|
const registerResponse = await apiClient.registerUser(adminEmail, adminPassword, 'E2E Admin User');
|
|
|
|
expect(registerResponse.status).toBe(201);
|
|
const registerData = await registerResponse.json();
|
|
const registeredUser = registerData.userprofile.user;
|
|
adminUserId = registeredUser.user_id;
|
|
expect(adminUserId).toBeDefined();
|
|
|
|
// 2. Promote the user to 'admin' via direct DB access
|
|
// (This simulates an existing admin or a manual promotion, as there is no public "register as admin" endpoint)
|
|
await getPool().query(`UPDATE public.profiles SET role = 'admin' WHERE user_id = $1`, [
|
|
adminUserId,
|
|
]);
|
|
|
|
// 3. Login to get the access token (now with admin privileges)
|
|
const loginResponse = await apiClient.loginUser(adminEmail, adminPassword, false);
|
|
|
|
expect(loginResponse.status).toBe(200);
|
|
const loginData = await loginResponse.json();
|
|
authToken = loginData.token;
|
|
expect(authToken).toBeDefined();
|
|
// Verify the role returned in the login response is now 'admin'
|
|
expect(loginData.userprofile.role).toBe('admin');
|
|
|
|
// 4. Fetch System Stats (Protected Admin Route)
|
|
const statsResponse = await apiClient.getApplicationStats(authToken);
|
|
|
|
expect(statsResponse.status).toBe(200);
|
|
const statsData = await statsResponse.json();
|
|
expect(statsData).toHaveProperty('userCount');
|
|
expect(statsData).toHaveProperty('flyerCount');
|
|
|
|
// 5. Fetch User List (Protected Admin Route)
|
|
const usersResponse = await apiClient.authedGet('/admin/users', { tokenOverride: authToken });
|
|
|
|
expect(usersResponse.status).toBe(200);
|
|
const usersData = await usersResponse.json();
|
|
expect(Array.isArray(usersData)).toBe(true);
|
|
// The list should contain the admin user we just created
|
|
const self = usersData.find((u: any) => u.user_id === adminUserId);
|
|
expect(self).toBeDefined();
|
|
|
|
// 6. Check Queue Status (Protected Admin Route)
|
|
const queueResponse = await apiClient.authedGet('/admin/queues/status', {
|
|
tokenOverride: authToken,
|
|
});
|
|
|
|
expect(queueResponse.status).toBe(200);
|
|
const queueData = await queueResponse.json();
|
|
expect(Array.isArray(queueData)).toBe(true);
|
|
// Verify that the 'flyer-processing' queue is present in the status report
|
|
const flyerQueue = queueData.find((q: any) => q.name === 'flyer-processing');
|
|
expect(flyerQueue).toBeDefined();
|
|
expect(flyerQueue.counts).toBeDefined();
|
|
});
|
|
}); |