2026-03-06 19:51:01 +00:00
|
|
|
import { test } from 'node:test';
|
|
|
|
|
import assert from 'node:assert';
|
|
|
|
|
import { decryptText } from '../services/encryption.js';
|
2026-03-06 19:25:48 +00:00
|
|
|
|
2026-03-06 19:51:01 +00:00
|
|
|
test('decryptText should handle valid payload', () => {
|
|
|
|
|
const payload = 'iv123:tag456:data789';
|
|
|
|
|
assert.throws(() => decryptText(payload), {
|
|
|
|
|
message: 'Invalid encrypted payload format'
|
2026-03-06 19:25:48 +00:00
|
|
|
});
|
2026-03-06 19:29:31 +00:00
|
|
|
});
|
|
|
|
|
|
2026-03-06 19:51:01 +00:00
|
|
|
test('decryptText should throw error for invalid payload format', () => {
|
|
|
|
|
const payload = 'invalid:payload';
|
|
|
|
|
assert.throws(() => decryptText(payload), {
|
|
|
|
|
message: 'Invalid encrypted payload format'
|
2026-03-06 19:29:31 +00:00
|
|
|
});
|
2026-03-06 20:36:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Test profile route POST /phone
|
|
|
|
|
test('POST /phone should update phone number', async () => {
|
|
|
|
|
// Mock request and response objects
|
|
|
|
|
const req = {
|
|
|
|
|
body: { phone: '1234567890' },
|
|
|
|
|
user: { userId: 1 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const res = {
|
|
|
|
|
status: (code) => {
|
|
|
|
|
res.statusCode = code;
|
|
|
|
|
return res;
|
|
|
|
|
},
|
|
|
|
|
json: (data) => {
|
|
|
|
|
res.body = data;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Mock the pool.query function to simulate database update
|
|
|
|
|
const originalQuery = pool.query;
|
|
|
|
|
pool.query = async (sql, params) => {
|
|
|
|
|
if (sql.includes('UPDATE users SET phone_encrypted')) {
|
|
|
|
|
return [{ affectedRows: 1 }];
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await router.post('/phone', req, res);
|
|
|
|
|
assert.strictEqual(res.statusCode, 200);
|
|
|
|
|
assert.deepStrictEqual(res.body, { status: 'updated' });
|
|
|
|
|
} finally {
|
|
|
|
|
pool.query = originalQuery;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Test profile route GET /
|
|
|
|
|
test('GET / should return user profile', async () => {
|
|
|
|
|
const req = {
|
|
|
|
|
user: { userId: 1 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const res = {
|
|
|
|
|
status: (code) => {
|
|
|
|
|
res.statusCode = code;
|
|
|
|
|
return res;
|
|
|
|
|
},
|
|
|
|
|
json: (data) => {
|
|
|
|
|
res.body = data;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Mock the pool.query function to simulate database fetch
|
|
|
|
|
const originalQuery = pool.query;
|
|
|
|
|
pool.query = async (sql, params) => {
|
|
|
|
|
if (sql.includes('SELECT id, name, email, phone_encrypted FROM users')) {
|
|
|
|
|
return [[{
|
|
|
|
|
id: 1,
|
|
|
|
|
name: 'Test User',
|
|
|
|
|
email: 'test@example.com',
|
|
|
|
|
phone_encrypted: 'iv123:tag456:data789'
|
|
|
|
|
}]];
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await router.get('/', req, res);
|
|
|
|
|
assert.strictEqual(res.statusCode, 200);
|
|
|
|
|
assert.deepStrictEqual(res.body, {
|
|
|
|
|
id: 1,
|
|
|
|
|
name: 'Test User',
|
|
|
|
|
email: 'test@example.com',
|
|
|
|
|
phone: null // Decryption error expected
|
|
|
|
|
});
|
|
|
|
|
} finally {
|
|
|
|
|
pool.query = originalQuery;
|
|
|
|
|
}
|
2026-03-06 19:25:48 +00:00
|
|
|
});
|