You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.8 KiB
TypeScript
85 lines
2.8 KiB
TypeScript
import { describe, it, beforeEach, afterEach, expect } from 'bun:test';
|
|
import Database from 'bun:sqlite';
|
|
import { initializeDatabase } from '../../../src/db';
|
|
import { TaskService } from '../../../src/tasks/service';
|
|
import { AllowedGroups } from '../../../src/services/allowed-groups';
|
|
|
|
function seedGroup(db: Database, groupId: string) {
|
|
// Intento genérico de seed para la tabla groups con columnas comunes
|
|
const cols = db.query(`PRAGMA table_info(groups)`).all() as any[];
|
|
const colNames = cols.map(c => String(c.name));
|
|
const values: Record<string, any> = {};
|
|
for (const c of colNames) {
|
|
if (c === 'id') values[c] = groupId;
|
|
else if (c === 'name' || c === 'title' || c === 'subject') values[c] = 'Test Group';
|
|
else if (c === 'is_active' || c === 'active') values[c] = 1;
|
|
else if (c.endsWith('_at')) values[c] = new Date().toISOString().replace('T', ' ').replace('Z', '');
|
|
else if (c === 'created_by') values[c] = 'tester';
|
|
// Para otras columnas dejaremos NULL si lo permite
|
|
}
|
|
const colsList = Object.keys(values);
|
|
const placeholders = colsList.map(() => '?').join(', ');
|
|
const sql = `INSERT OR IGNORE INTO groups (${colsList.join(', ')}) VALUES (${placeholders})`;
|
|
db.prepare(sql).run(...colsList.map(k => values[k]));
|
|
}
|
|
|
|
describe('TaskService - gating en creación con group_id (enforce)', () => {
|
|
const envBackup = process.env;
|
|
let memdb: Database;
|
|
|
|
beforeEach(() => {
|
|
process.env = { ...envBackup, NODE_ENV: 'test', GROUP_GATING_MODE: 'enforce' };
|
|
memdb = new Database(':memory:');
|
|
initializeDatabase(memdb);
|
|
(TaskService as any).dbInstance = memdb;
|
|
(AllowedGroups as any).dbInstance = memdb;
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.env = envBackup;
|
|
memdb.close();
|
|
});
|
|
|
|
it('fuerza group_id=null cuando el grupo no está allowed', () => {
|
|
const gid = 'na@g.us';
|
|
seedGroup(memdb, gid);
|
|
AllowedGroups.setStatus(gid, 'blocked');
|
|
|
|
const taskId = TaskService.createTask(
|
|
{
|
|
description: 'Probar gating',
|
|
due_date: null,
|
|
group_id: gid,
|
|
created_by: '34600123456',
|
|
},
|
|
[{ user_id: '34600123456', assigned_by: '34600123456' }]
|
|
);
|
|
|
|
const row = memdb
|
|
.query(`SELECT group_id FROM tasks WHERE id = ?`)
|
|
.get(taskId) as any;
|
|
expect(row?.group_id).toBeNull();
|
|
});
|
|
|
|
it('conserva group_id cuando el grupo está allowed', () => {
|
|
const gid = 'ok@g.us';
|
|
seedGroup(memdb, gid);
|
|
AllowedGroups.setStatus(gid, 'allowed');
|
|
|
|
const taskId = TaskService.createTask(
|
|
{
|
|
description: 'Tarea en grupo allowed',
|
|
due_date: null,
|
|
group_id: gid,
|
|
created_by: '34600123456',
|
|
},
|
|
[{ user_id: '34600123456', assigned_by: '34600123456' }]
|
|
);
|
|
|
|
const row = memdb
|
|
.query(`SELECT group_id FROM tasks WHERE id = ?`)
|
|
.get(taskId) as any;
|
|
expect(String(row?.group_id)).toBe(gid);
|
|
});
|
|
});
|