auto(agent): enhance profile route tests and coverage

This commit is contained in:
OpenClaw 2026-03-06 20:36:58 +00:00
parent b4edaead35
commit fb436cb3d6
3 changed files with 113 additions and 2 deletions

View file

@ -14,4 +14,84 @@ test('decryptText should throw error for invalid payload format', () => {
assert.throws(() => decryptText(payload), {
message: 'Invalid encrypted payload format'
});
});
// 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;
}
});