auto(agent): Improve address change request and verification tests
This commit is contained in:
parent
44e52191d1
commit
53a2968bd2
1 changed files with 49 additions and 22 deletions
|
|
@ -1,45 +1,72 @@
|
||||||
const { test } = require('node:test');
|
const { test } = require('node:test');
|
||||||
const assert = require('node:assert');
|
const assert = require('node:assert');
|
||||||
const sinon = require('sinon');
|
const sinon = require('sinon');
|
||||||
const { getAddresses, createAddress } = require('../routes/addresses');
|
const router = require('../routes/addresses');
|
||||||
|
|
||||||
// Mock the database pool
|
// Mock the database pool
|
||||||
const mockPool = {
|
const mockPool = {
|
||||||
query: sinon.stub()
|
query: sinon.stub(),
|
||||||
|
getConnection: sinon.stub()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Mock the route functions with the mocked pool
|
// Mock the route functions with the mocked pool
|
||||||
const mockGetAddresses = (req, res) => getAddresses({ ...req, pool: mockPool }, res);
|
const mockReq = { user: { userId: 1 }, pool: mockPool };
|
||||||
const mockCreateAddress = (req, res) => createAddress({ ...req, pool: mockPool }, res);
|
const mockRes = {
|
||||||
|
|
||||||
test('GET /addresses should return addresses', async () => {
|
|
||||||
const mockResponse = {
|
|
||||||
status: sinon.stub().returnsThis(),
|
status: sinon.stub().returnsThis(),
|
||||||
json: sinon.stub()
|
json: sinon.stub()
|
||||||
};
|
};
|
||||||
|
|
||||||
mockPool.query.resolves([{ id: 1, street: 'Test Street', city: 'Test City' }]);
|
test('POST /change-request should validate input and handle existing address check', async () => {
|
||||||
|
// Test valid input
|
||||||
await mockGetAddresses({}, mockResponse);
|
const validReq = {
|
||||||
|
...mockReq,
|
||||||
assert.strictEqual(mockResponse.status.calledWith(200), true);
|
body: { newAddress: '123 Main St, Anytown, USA' }
|
||||||
assert.strictEqual(mockResponse.json.calledOnce, true);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('POST /addresses should create a new address', async () => {
|
|
||||||
const mockRequest = {
|
|
||||||
body: { street: 'New Street', city: 'New City' }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const mockResponse = {
|
mockPool.query.onCall(0).resolves([{ id: 1 }]); // Existing address check
|
||||||
status: sinon.stub().returnsThis(),
|
mockPool.query.onCall(1).resolves({ insertId: 1 }); // Insert request
|
||||||
json: sinon.stub()
|
|
||||||
|
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 invalidReq = {
|
||||||
|
...mockReq,
|
||||||
|
body: { newAddress: 'Invalid' } // Too short
|
||||||
};
|
};
|
||||||
|
|
||||||
mockPool.query.resolves([{ id: 2, street: 'New Street', city: 'New City' }]);
|
await router.post('/change-request', invalidReq, mockRes);
|
||||||
|
|
||||||
await mockCreateAddress(mockRequest, mockResponse);
|
assert.strictEqual(mockRes.status.calledWith(400), true);
|
||||||
|
});
|
||||||
assert.strictEqual(mockResponse.status.calledWith(201), true);
|
|
||||||
assert.strictEqual(mockResponse.json.calledOnce, true);
|
test('POST /verify should validate input and handle verification flow', async () => {
|
||||||
|
const validReq = {
|
||||||
|
...mockReq,
|
||||||
|
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 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' }]);
|
||||||
|
|
||||||
|
await router.post('/verify', invalidReq, mockRes);
|
||||||
|
|
||||||
|
assert.strictEqual(mockRes.status.calledWith(400), true);
|
||||||
});
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue