auto(agent): add smoke tests for addresses route

This commit is contained in:
OpenClaw 2026-03-06 19:52:57 +00:00
parent 2d68f838ec
commit 9d5f08a885
2 changed files with 73 additions and 53 deletions

View file

@ -1,72 +1,92 @@
const { test } = require('node:test'); import { test } from 'node:test';
const assert = require('node:assert'); import assert from 'node:assert';
const sinon = require('sinon'); import { app } from '../app.js';
const router = require('../routes/addresses'); import { pool } from '../db/connection.js';
import { encryptText } from '../services/encryption.js';
// Mock the database pool // Mock user for testing
const mockPool = { const mockUser = {
query: sinon.stub(), userId: 1,
getConnection: sinon.stub() email: 'test@example.com'
}; };
// Mock the route functions with the mocked pool // Helper to create a test request
const mockReq = { user: { userId: 1 }, pool: mockPool }; function createTestRequest(path, method, body = {}, headers = {}) {
const mockRes = { return {
status: sinon.stub().returnsThis(), method,
json: sinon.stub() path,
headers: {
'Content-Type': 'application/json',
...headers
},
body
}; };
}
test('POST /change-request should validate input and handle existing address check', async () => { test('POST /addresses/change-request - valid address change request', async () => {
// Test valid input // Mock existing address in DB
const validReq = { await pool.query(
...mockReq, `INSERT INTO addresses (user_id, address_encrypted, postal_verified_at)
body: { newAddress: '123 Main St, Anytown, USA' } VALUES (?, ?, CURRENT_TIMESTAMP)`,
}; [mockUser.userId, encryptText('Old Address')]
);
mockPool.query.onCall(0).resolves([{ id: 1 }]); // Existing address check const req = createTestRequest('/addresses/change-request', 'POST', {
mockPool.query.onCall(1).resolves({ insertId: 1 }); // Insert request newAddress: 'New Test Address'
await router.post('/change-request', validReq, mockRes);
assert.strictEqual(mockRes.status.calledWith(201), true);
assert.strictEqual(mockRes.json.calledOnce, true);
}); });
test('POST /change-request should reject invalid input', async () => { const res = await app.request(req);
const invalidReq = {
...mockReq,
body: { newAddress: 'Invalid' } // Too short
};
await router.post('/change-request', invalidReq, mockRes); assert.strictEqual(res.status, 201);
const data = await res.json();
assert.strictEqual(mockRes.status.calledWith(400), true); assert.ok(data.requestId);
assert.strictEqual(data.postalDispatch, 'pending_letter');
assert.ok(data.verificationCode);
}); });
test('POST /verify should validate input and handle verification flow', async () => { test('POST /addresses/change-request - invalid address (too short)', async () => {
const validReq = { const req = createTestRequest('/addresses/change-request', 'POST', {
...mockReq, newAddress: 'Short'
body: { requestId: 1, code: '123456' }
};
mockPool.query.onCall(0).resolves([{ id: 1, user_id: 1, new_address_encrypted: 'encrypted', verification_code_hash: 'hash', status: 'pending_letter' }]);
mockPool.query.onCall(1).resolves(); // Update request
mockPool.query.onCall(2).resolves(); // Insert address
await router.post('/verify', validReq, mockRes);
assert.strictEqual(mockRes.json.calledWith({ status: 'verified' }), true);
}); });
test('POST /verify should reject invalid verification code', async () => { const res = await app.request(req);
const invalidReq = {
...mockReq,
body: { requestId: 1, code: '000000' } // Wrong code
};
mockPool.query.onCall(0).resolves([{ id: 1, user_id: 1, new_address_encrypted: 'encrypted', verification_code_hash: 'hash', status: 'pending_letter' }]); assert.strictEqual(res.status, 400);
const data = await res.json();
await router.post('/verify', invalidReq, mockRes); assert.ok(data.error);
});
assert.strictEqual(mockRes.status.calledWith(400), true);
test('POST /addresses/verify - valid verification', async () => {
// Mock a change request first
const code = '123456';
const codeHash = 'hashed_code';
await pool.query(
`INSERT INTO address_change_requests (user_id, new_address_encrypted, verification_code_hash, status)
VALUES (?, ?, ?, 'pending_letter')`,
[mockUser.userId, encryptText('New Address'), codeHash]
);
const req = createTestRequest('/addresses/verify', 'POST', {
requestId: 1,
code
});
const res = await app.request(req);
assert.strictEqual(res.status, 200);
const data = await res.json();
assert.strictEqual(data.status, 'verified');
});
test('POST /addresses/verify - invalid verification code', async () => {
const req = createTestRequest('/addresses/verify', 'POST', {
requestId: 1,
code: 'wrongcode'
});
const res = await app.request(req);
assert.strictEqual(res.status, 400);
const data = await res.json();
assert.ok(data.error);
}); });

View file

@ -1,2 +1,2 @@
LAST_ROUTE=reviews.js LAST_ROUTE=addresses.js
UPDATED_AT=2026-03-06T19:51:18Z UPDATED_AT=2026-03-06T19:52:37Z