diff --git a/backend/src/__tests__/addresses.test.js b/backend/src/__tests__/addresses.test.js index 5be0e36..ad099e3 100644 --- a/backend/src/__tests__/addresses.test.js +++ b/backend/src/__tests__/addresses.test.js @@ -1,45 +1,72 @@ const { test } = require('node:test'); const assert = require('node:assert'); const sinon = require('sinon'); -const { getAddresses, createAddress } = require('../routes/addresses'); +const router = require('../routes/addresses'); // Mock the database pool const mockPool = { - query: sinon.stub() + query: sinon.stub(), + getConnection: sinon.stub() }; // Mock the route functions with the mocked pool -const mockGetAddresses = (req, res) => getAddresses({ ...req, pool: mockPool }, res); -const mockCreateAddress = (req, res) => createAddress({ ...req, pool: mockPool }, res); +const mockReq = { user: { userId: 1 }, pool: mockPool }; +const mockRes = { + status: sinon.stub().returnsThis(), + json: sinon.stub() +}; -test('GET /addresses should return addresses', async () => { - const mockResponse = { - status: sinon.stub().returnsThis(), - json: sinon.stub() +test('POST /change-request should validate input and handle existing address check', async () => { + // Test valid input + const validReq = { + ...mockReq, + body: { newAddress: '123 Main St, Anytown, USA' } }; - mockPool.query.resolves([{ id: 1, street: 'Test Street', city: 'Test City' }]); + mockPool.query.onCall(0).resolves([{ id: 1 }]); // Existing address check + mockPool.query.onCall(1).resolves({ insertId: 1 }); // Insert request - await mockGetAddresses({}, mockResponse); + await router.post('/change-request', validReq, mockRes); - assert.strictEqual(mockResponse.status.calledWith(200), true); - assert.strictEqual(mockResponse.json.calledOnce, true); + assert.strictEqual(mockRes.status.calledWith(201), true); + assert.strictEqual(mockRes.json.calledOnce, true); }); -test('POST /addresses should create a new address', async () => { - const mockRequest = { - body: { street: 'New Street', city: 'New City' } +test('POST /change-request should reject invalid input', async () => { + const invalidReq = { + ...mockReq, + body: { newAddress: 'Invalid' } // Too short }; - const mockResponse = { - status: sinon.stub().returnsThis(), - json: sinon.stub() + await router.post('/change-request', invalidReq, mockRes); + + assert.strictEqual(mockRes.status.calledWith(400), true); +}); + +test('POST /verify should validate input and handle verification flow', async () => { + const validReq = { + ...mockReq, + body: { requestId: 1, code: '123456' } }; - mockPool.query.resolves([{ id: 2, street: 'New Street', city: 'New City' }]); + 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 mockCreateAddress(mockRequest, mockResponse); + await router.post('/verify', validReq, mockRes); - assert.strictEqual(mockResponse.status.calledWith(201), true); - assert.strictEqual(mockResponse.json.calledOnce, true); + 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); }); \ No newline at end of file