Files
TX/artifacts/api-server/tests/delete-force-warnings.test.mjs
T
riyadhafraa efb3f512b7 Improve app deletion tests with defensive cleanup and order tracking
Update delete-force-warnings.test.mjs to include user_app_orders in cleanup, add a defensive sweep for orphaned apps, and ensure app IDs are tracked for all app deletion test cases.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 77cfe984-2c65-4152-bb7a-0df28274fe66
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 5b25e846-3b26-42aa-9202-268dd811b0d2
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/c3c252e4-c83d-40ca-9fff-99a3ea60701e/77cfe984-2c65-4152-bb7a-0df28274fe66/UXp27BF
Replit-Helium-Checkpoint-Created: true
2026-04-28 21:03:02 +00:00

408 lines
13 KiB
JavaScript

import { test, before, after } from "node:test";
import assert from "node:assert/strict";
import pg from "pg";
const API_BASE = process.env.TEST_API_BASE ?? "http://localhost:8080";
const DATABASE_URL = process.env.DATABASE_URL;
if (!DATABASE_URL) {
throw new Error("DATABASE_URL must be set to run these tests");
}
const TEST_PASSWORD = "TestPass123!";
const TEST_PASSWORD_HASH =
"$2b$10$Bs636ukPMyz01nKrsi.5m.JlDXSN22AVCvn8cgPWWDbo5yJRQX2vu";
const pool = new pg.Pool({ connectionString: DATABASE_URL });
let adminId;
let adminUsername;
let adminCookie;
let createdUserIds = [];
let createdAppIds = [];
let createdServiceIds = [];
let createdGroupIds = [];
async function loginAndGetCookie(username, password) {
const res = await fetch(`${API_BASE}/api/auth/login`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password }),
});
assert.equal(res.status, 200, `login expected 200, got ${res.status}`);
const setCookie = res.headers.get("set-cookie");
return setCookie
.split(",")
.map((c) => c.split(";")[0].trim())
.find((c) => c.startsWith("connect.sid="));
}
before(async () => {
const stamp = `${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 6)}`;
adminUsername = `del_admin_${stamp}`;
const a = await pool.query(
`INSERT INTO users (username, email, password_hash, display_name_en, preferred_language, is_active)
VALUES ($1, $2, $3, 'Delete Admin', 'en', true) RETURNING id`,
[adminUsername, `${adminUsername}@example.com`, TEST_PASSWORD_HASH],
);
adminId = a.rows[0].id;
await pool.query(
`INSERT INTO user_roles (user_id, role_id) SELECT $1, id FROM roles WHERE name = 'admin'`,
[adminId],
);
await pool.query(
`INSERT INTO user_groups (user_id, group_id)
SELECT $1, id FROM groups WHERE name IN ('Everyone', 'Admins')
ON CONFLICT DO NOTHING`,
[adminId],
);
adminCookie = await loginAndGetCookie(adminUsername, TEST_PASSWORD);
});
after(async () => {
if (createdServiceIds.length) {
await pool.query(
`DELETE FROM service_orders WHERE service_id = ANY($1::int[])`,
[createdServiceIds],
);
await pool.query(`DELETE FROM services WHERE id = ANY($1::int[])`, [
createdServiceIds,
]);
}
if (createdAppIds.length) {
await pool.query(`DELETE FROM app_opens WHERE app_id = ANY($1::int[])`, [
createdAppIds,
]);
await pool.query(
`DELETE FROM app_permissions WHERE app_id = ANY($1::int[])`,
[createdAppIds],
);
await pool.query(`DELETE FROM group_apps WHERE app_id = ANY($1::int[])`, [
createdAppIds,
]);
await pool.query(
`DELETE FROM user_app_orders WHERE app_id = ANY($1::int[])`,
[createdAppIds],
);
await pool.query(`DELETE FROM apps WHERE id = ANY($1::int[])`, [
createdAppIds,
]);
}
// Defensive sweep: kill any leftover apps whose slug matches a test prefix
// (clean_app_*, busy_app_*, force_app_*). This guards against the case
// where a test crashes between INSERT and the createdAppIds.push above.
const sweep = await pool.query(
`SELECT id FROM apps
WHERE slug LIKE 'clean_app_%'
OR slug LIKE 'busy_app_%'
OR slug LIKE 'force_app_%'`,
);
if (sweep.rowCount > 0) {
const ids = sweep.rows.map((r) => r.id);
await pool.query(`DELETE FROM app_opens WHERE app_id = ANY($1::int[])`, [ids]);
await pool.query(`DELETE FROM app_permissions WHERE app_id = ANY($1::int[])`, [ids]);
await pool.query(`DELETE FROM group_apps WHERE app_id = ANY($1::int[])`, [ids]);
await pool.query(`DELETE FROM user_app_orders WHERE app_id = ANY($1::int[])`, [ids]);
await pool.query(`DELETE FROM apps WHERE id = ANY($1::int[])`, [ids]);
}
if (createdGroupIds.length) {
await pool.query(`DELETE FROM group_apps WHERE group_id = ANY($1::int[])`, [
createdGroupIds,
]);
await pool.query(`DELETE FROM groups WHERE id = ANY($1::int[])`, [
createdGroupIds,
]);
}
for (const uid of [adminId, ...createdUserIds]) {
if (uid !== undefined) {
await pool.query(`DELETE FROM messages WHERE sender_id = $1`, [uid]);
await pool.query(`DELETE FROM conversations WHERE created_by = $1`, [
uid,
]);
await pool.query(`DELETE FROM notes WHERE user_id = $1`, [uid]);
await pool.query(`DELETE FROM service_orders WHERE user_id = $1`, [uid]);
await pool.query(`DELETE FROM user_groups WHERE user_id = $1`, [uid]);
await pool.query(`DELETE FROM user_roles WHERE user_id = $1`, [uid]);
await pool.query(`DELETE FROM users WHERE id = $1`, [uid]);
}
}
await pool.end();
});
// ---------- Users ----------
test("DELETE /api/users/:id without dependents succeeds (no force needed)", async () => {
const username = `del_clean_${Date.now().toString(36)}`;
const r = await pool.query(
`INSERT INTO users (username, email, password_hash, display_name_en, preferred_language, is_active)
VALUES ($1, $2, $3, 'Clean User', 'en', true) RETURNING id`,
[username, `${username}@example.com`, TEST_PASSWORD_HASH],
);
const uid = r.rows[0].id;
const res = await fetch(`${API_BASE}/api/users/${uid}`, {
method: "DELETE",
headers: { Cookie: adminCookie },
});
assert.equal(res.status, 204);
const check = await pool.query(`SELECT id FROM users WHERE id = $1`, [uid]);
assert.equal(check.rowCount, 0);
});
test("DELETE /api/users/:id with dependents returns 409 with counts", async () => {
const username = `del_busy_${Date.now().toString(36)}`;
const r = await pool.query(
`INSERT INTO users (username, email, password_hash, display_name_en, preferred_language, is_active)
VALUES ($1, $2, $3, 'Busy User', 'en', true) RETURNING id`,
[username, `${username}@example.com`, TEST_PASSWORD_HASH],
);
const uid = r.rows[0].id;
createdUserIds.push(uid);
await pool.query(
`INSERT INTO notes (user_id, title, content) VALUES ($1, 'Note A', 'x'), ($1, 'Note B', 'y')`,
[uid],
);
const conv = await pool.query(
`INSERT INTO conversations (created_by, name_en, is_group) VALUES ($1, 'Test Conv', true) RETURNING id`,
[uid],
);
const convId = conv.rows[0].id;
await pool.query(
`INSERT INTO messages (conversation_id, sender_id, content) VALUES ($1, $2, 'hello')`,
[convId, uid],
);
const res = await fetch(`${API_BASE}/api/users/${uid}`, {
method: "DELETE",
headers: { Cookie: adminCookie },
});
assert.equal(res.status, 409);
const body = await res.json();
assert.equal(body.noteCount, 2);
assert.equal(body.conversationCount, 1);
assert.equal(body.messageCount, 1);
const stillThere = await pool.query(`SELECT id FROM users WHERE id = $1`, [
uid,
]);
assert.equal(stillThere.rowCount, 1);
});
test("DELETE /api/users/:id?force=true deletes user and writes audit log", async () => {
const username = `del_force_${Date.now().toString(36)}`;
const r = await pool.query(
`INSERT INTO users (username, email, password_hash, display_name_en, preferred_language, is_active)
VALUES ($1, $2, $3, 'Force User', 'en', true) RETURNING id`,
[username, `${username}@example.com`, TEST_PASSWORD_HASH],
);
const uid = r.rows[0].id;
await pool.query(`INSERT INTO notes (user_id, title) VALUES ($1, 'n')`, [
uid,
]);
const conv = await pool.query(
`INSERT INTO conversations (created_by, name_en, is_group) VALUES ($1, 'Conv', true) RETURNING id`,
[uid],
);
await pool.query(
`INSERT INTO messages (conversation_id, sender_id, content) VALUES ($1, $2, 'hi')`,
[conv.rows[0].id, uid],
);
const res = await fetch(`${API_BASE}/api/users/${uid}?force=true`, {
method: "DELETE",
headers: { Cookie: adminCookie },
});
assert.equal(res.status, 204);
const check = await pool.query(`SELECT id FROM users WHERE id = $1`, [uid]);
assert.equal(check.rowCount, 0);
const audit = await pool.query(
`SELECT * FROM audit_logs WHERE action = 'user.force_delete' AND target_id = $1`,
[uid],
);
assert.equal(audit.rowCount, 1);
assert.equal(audit.rows[0].actor_user_id, adminId);
});
// ---------- Apps ----------
test("DELETE /api/apps/:id without dependents succeeds (no force needed)", async () => {
const slug = `clean_app_${Date.now().toString(36)}`;
const a = await pool.query(
`INSERT INTO apps (slug, name_ar, name_en, route, sort_order)
VALUES ($1, 'تطبيق', 'App Clean', '/x', 999) RETURNING id`,
[slug],
);
const id = a.rows[0].id;
createdAppIds.push(id);
const res = await fetch(`${API_BASE}/api/apps/${id}`, {
method: "DELETE",
headers: { Cookie: adminCookie },
});
assert.equal(res.status, 204);
});
test("DELETE /api/apps/:id with dependents returns 409 with counts", async () => {
const slug = `busy_app_${Date.now().toString(36)}`;
const a = await pool.query(
`INSERT INTO apps (slug, name_ar, name_en, route, sort_order)
VALUES ($1, 'تطبيق', 'App Busy', '/y', 999) RETURNING id`,
[slug],
);
const appId = a.rows[0].id;
createdAppIds.push(appId);
const g = await pool.query(
`INSERT INTO groups (name, description_en) VALUES ($1, 'g') RETURNING id`,
[`gApp_${Date.now().toString(36)}`],
);
const groupId = g.rows[0].id;
createdGroupIds.push(groupId);
await pool.query(
`INSERT INTO group_apps (group_id, app_id) VALUES ($1, $2)`,
[groupId, appId],
);
await pool.query(
`INSERT INTO app_opens (user_id, app_id) VALUES ($1, $2), ($1, $2)`,
[adminId, appId],
);
const res = await fetch(`${API_BASE}/api/apps/${appId}`, {
method: "DELETE",
headers: { Cookie: adminCookie },
});
assert.equal(res.status, 409);
const body = await res.json();
assert.equal(body.groupCount, 1);
assert.equal(body.openCount, 2);
const stillThere = await pool.query(`SELECT id FROM apps WHERE id = $1`, [
appId,
]);
assert.equal(stillThere.rowCount, 1);
});
test("DELETE /api/apps/:id?force=true deletes app and writes audit log", async () => {
const slug = `force_app_${Date.now().toString(36)}`;
const a = await pool.query(
`INSERT INTO apps (slug, name_ar, name_en, route, sort_order)
VALUES ($1, 'تطبيق', 'App Force', '/z', 999) RETURNING id`,
[slug],
);
const appId = a.rows[0].id;
createdAppIds.push(appId);
const g = await pool.query(
`INSERT INTO groups (name, description_en) VALUES ($1, 'g') RETURNING id`,
[`gAppF_${Date.now().toString(36)}`],
);
const groupId = g.rows[0].id;
createdGroupIds.push(groupId);
await pool.query(
`INSERT INTO group_apps (group_id, app_id) VALUES ($1, $2)`,
[groupId, appId],
);
await pool.query(
`INSERT INTO app_opens (user_id, app_id) VALUES ($1, $2)`,
[adminId, appId],
);
const res = await fetch(`${API_BASE}/api/apps/${appId}?force=true`, {
method: "DELETE",
headers: { Cookie: adminCookie },
});
assert.equal(res.status, 204);
const check = await pool.query(`SELECT id FROM apps WHERE id = $1`, [appId]);
assert.equal(check.rowCount, 0);
const audit = await pool.query(
`SELECT * FROM audit_logs WHERE action = 'app.force_delete' AND target_id = $1`,
[String(appId)],
);
assert.equal(audit.rowCount, 1);
});
// ---------- Services ----------
test("DELETE /api/services/:id without dependents succeeds (no force needed)", async () => {
const s = await pool.query(
`INSERT INTO services (name_ar, name_en, price, is_available) VALUES ('خ', 'Clean Svc', 1.00, true) RETURNING id`,
);
const id = s.rows[0].id;
const res = await fetch(`${API_BASE}/api/services/${id}`, {
method: "DELETE",
headers: { Cookie: adminCookie },
});
assert.equal(res.status, 204);
});
test("DELETE /api/services/:id with orders returns 409 with counts", async () => {
const s = await pool.query(
`INSERT INTO services (name_ar, name_en, price, is_available) VALUES ('خ', 'Busy Svc', 2.00, true) RETURNING id`,
);
const sid = s.rows[0].id;
createdServiceIds.push(sid);
await pool.query(
`INSERT INTO service_orders (user_id, service_id, status) VALUES ($1, $2, 'pending'), ($1, $2, 'pending'), ($1, $2, 'completed')`,
[adminId, sid],
);
const res = await fetch(`${API_BASE}/api/services/${sid}`, {
method: "DELETE",
headers: { Cookie: adminCookie },
});
assert.equal(res.status, 409);
const body = await res.json();
assert.equal(body.orderCount, 3);
const stillThere = await pool.query(`SELECT id FROM services WHERE id = $1`, [
sid,
]);
assert.equal(stillThere.rowCount, 1);
});
test("DELETE /api/services/:id?force=true deletes service + orders and writes audit log", async () => {
const s = await pool.query(
`INSERT INTO services (name_ar, name_en, price, is_available) VALUES ('خ', 'Force Svc', 3.00, true) RETURNING id`,
);
const sid = s.rows[0].id;
await pool.query(
`INSERT INTO service_orders (user_id, service_id, status) VALUES ($1, $2, 'pending')`,
[adminId, sid],
);
const res = await fetch(`${API_BASE}/api/services/${sid}?force=true`, {
method: "DELETE",
headers: { Cookie: adminCookie },
});
assert.equal(res.status, 204);
const check = await pool.query(`SELECT id FROM services WHERE id = $1`, [
sid,
]);
assert.equal(check.rowCount, 0);
const ordersGone = await pool.query(
`SELECT id FROM service_orders WHERE service_id = $1`,
[sid],
);
assert.equal(ordersGone.rowCount, 0);
const audit = await pool.query(
`SELECT * FROM audit_logs WHERE action = 'service.force_delete' AND target_id = $1`,
[String(sid)],
);
assert.equal(audit.rowCount, 1);
});