auto(agent): add smoke tests for addresses route
This commit is contained in:
parent
2d68f838ec
commit
9d5f08a885
2 changed files with 73 additions and 53 deletions
|
|
@ -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',
|
||||||
test('POST /change-request should validate input and handle existing address check', async () => {
|
...headers
|
||||||
// Test valid input
|
},
|
||||||
const validReq = {
|
body
|
||||||
...mockReq,
|
|
||||||
body: { newAddress: '123 Main St, Anytown, USA' }
|
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
mockPool.query.onCall(0).resolves([{ id: 1 }]); // Existing address check
|
test('POST /addresses/change-request - valid address change request', async () => {
|
||||||
mockPool.query.onCall(1).resolves({ insertId: 1 }); // Insert request
|
// Mock existing address in DB
|
||||||
|
await pool.query(
|
||||||
|
`INSERT INTO addresses (user_id, address_encrypted, postal_verified_at)
|
||||||
|
VALUES (?, ?, CURRENT_TIMESTAMP)`,
|
||||||
|
[mockUser.userId, encryptText('Old Address')]
|
||||||
|
);
|
||||||
|
|
||||||
await router.post('/change-request', validReq, mockRes);
|
const req = createTestRequest('/addresses/change-request', 'POST', {
|
||||||
|
newAddress: 'New Test Address'
|
||||||
|
});
|
||||||
|
|
||||||
assert.strictEqual(mockRes.status.calledWith(201), true);
|
const res = await app.request(req);
|
||||||
assert.strictEqual(mockRes.json.calledOnce, true);
|
|
||||||
|
assert.strictEqual(res.status, 201);
|
||||||
|
const data = await res.json();
|
||||||
|
assert.ok(data.requestId);
|
||||||
|
assert.strictEqual(data.postalDispatch, 'pending_letter');
|
||||||
|
assert.ok(data.verificationCode);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('POST /change-request should reject invalid input', async () => {
|
test('POST /addresses/change-request - invalid address (too short)', async () => {
|
||||||
const invalidReq = {
|
const req = createTestRequest('/addresses/change-request', 'POST', {
|
||||||
...mockReq,
|
newAddress: 'Short'
|
||||||
body: { newAddress: 'Invalid' } // Too short
|
});
|
||||||
};
|
|
||||||
|
|
||||||
await router.post('/change-request', invalidReq, mockRes);
|
const res = await app.request(req);
|
||||||
|
|
||||||
assert.strictEqual(mockRes.status.calledWith(400), true);
|
assert.strictEqual(res.status, 400);
|
||||||
|
const data = await res.json();
|
||||||
|
assert.ok(data.error);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('POST /verify should validate input and handle verification flow', async () => {
|
test('POST /addresses/verify - valid verification', async () => {
|
||||||
const validReq = {
|
// Mock a change request first
|
||||||
...mockReq,
|
const code = '123456';
|
||||||
body: { requestId: 1, 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]
|
||||||
|
);
|
||||||
|
|
||||||
mockPool.query.onCall(0).resolves([{ id: 1, user_id: 1, new_address_encrypted: 'encrypted', verification_code_hash: 'hash', status: 'pending_letter' }]);
|
const req = createTestRequest('/addresses/verify', 'POST', {
|
||||||
mockPool.query.onCall(1).resolves(); // Update request
|
requestId: 1,
|
||||||
mockPool.query.onCall(2).resolves(); // Insert address
|
code
|
||||||
|
});
|
||||||
|
|
||||||
await router.post('/verify', validReq, mockRes);
|
const res = await app.request(req);
|
||||||
|
|
||||||
assert.strictEqual(mockRes.json.calledWith({ status: 'verified' }), true);
|
assert.strictEqual(res.status, 200);
|
||||||
|
const data = await res.json();
|
||||||
|
assert.strictEqual(data.status, 'verified');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('POST /verify should reject invalid verification code', async () => {
|
test('POST /addresses/verify - invalid verification code', async () => {
|
||||||
const invalidReq = {
|
const req = createTestRequest('/addresses/verify', 'POST', {
|
||||||
...mockReq,
|
requestId: 1,
|
||||||
body: { requestId: 1, code: '000000' } // Wrong code
|
code: 'wrongcode'
|
||||||
};
|
});
|
||||||
|
|
||||||
mockPool.query.onCall(0).resolves([{ id: 1, user_id: 1, new_address_encrypted: 'encrypted', verification_code_hash: 'hash', status: 'pending_letter' }]);
|
const res = await app.request(req);
|
||||||
|
|
||||||
await router.post('/verify', invalidReq, mockRes);
|
assert.strictEqual(res.status, 400);
|
||||||
|
const data = await res.json();
|
||||||
assert.strictEqual(mockRes.status.calledWith(400), true);
|
assert.ok(data.error);
|
||||||
});
|
});
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue