feat(cardav): implement POST /accounts/:id/test endpoint
Add connection test route. Loads account from DB and delegates to CardDAVSync.testConnection() to verify credentials and discover addressbooks without saving. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
import { createLogger } from '../logger.js';
|
import { createLogger } from '../logger.js';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
|
import * as db from '../db.js';
|
||||||
import * as CardDAVSync from '../services/cardav-sync.js';
|
import * as CardDAVSync from '../services/cardav-sync.js';
|
||||||
import { str, collectErrors, MAX_TITLE } from '../middleware/validate.js';
|
import { str, collectErrors, MAX_TITLE } from '../middleware/validate.js';
|
||||||
|
|
||||||
@@ -76,4 +77,30 @@ router.delete('/accounts/:id', async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST /api/v1/contacts/cardav/accounts/:id/test
|
||||||
|
* Connection testen (ohne Account zu speichern).
|
||||||
|
* Response: { data: { ok, addressbooks } }
|
||||||
|
*/
|
||||||
|
router.post('/accounts/:id/test', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const id = parseInt(req.params.id, 10);
|
||||||
|
if (!id || id < 1) return res.status(400).json({ error: 'Invalid ID', code: 400 });
|
||||||
|
|
||||||
|
const account = db.get().prepare('SELECT * FROM carddav_accounts WHERE id = ?').get(id);
|
||||||
|
if (!account) return res.status(404).json({ error: 'Account nicht gefunden', code: 404 });
|
||||||
|
|
||||||
|
const result = await CardDAVSync.testConnection(
|
||||||
|
account.carddav_url,
|
||||||
|
account.username,
|
||||||
|
account.password
|
||||||
|
);
|
||||||
|
|
||||||
|
res.json({ data: result });
|
||||||
|
} catch (err) {
|
||||||
|
log.error('Error testing CardDAV connection:', err);
|
||||||
|
res.status(500).json({ error: 'Interner Fehler', code: 500 });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
@@ -1726,4 +1726,41 @@ describe('CardDAV API Routes', () => {
|
|||||||
assert.ok(res.data.error.includes('Invalid ID'));
|
assert.ok(res.data.error.includes('Invalid ID'));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Connection & Discovery', () => {
|
||||||
|
it('POST /accounts/:id/test - should test connection', async () => {
|
||||||
|
// Insert test account directly into DB
|
||||||
|
const result = apiTestDb.prepare(`
|
||||||
|
INSERT INTO carddav_accounts (name, carddav_url, username, password, created_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?)
|
||||||
|
`).run('Test Connection Account', 'https://example.com/carddav-test', 'testuser-connection', 'testpass', '2026-05-04T10:00:00Z');
|
||||||
|
|
||||||
|
const accountId = result.lastInsertRowid;
|
||||||
|
|
||||||
|
const cardavRouter = await import('./server/routes/cardav.js');
|
||||||
|
|
||||||
|
// Test connection
|
||||||
|
const req = {
|
||||||
|
params: { id: String(accountId) },
|
||||||
|
query: {},
|
||||||
|
body: {}
|
||||||
|
};
|
||||||
|
const res = {
|
||||||
|
statusCode: 200,
|
||||||
|
status(code) { this.statusCode = code; return this; },
|
||||||
|
json(data) { this.data = data; return this; },
|
||||||
|
};
|
||||||
|
|
||||||
|
const testHandler = cardavRouter.default.stack.find(
|
||||||
|
layer => layer.route?.path === '/accounts/:id/test' && layer.route.methods.post
|
||||||
|
)?.route?.stack[0]?.handle;
|
||||||
|
|
||||||
|
assert.ok(testHandler, 'POST /accounts/:id/test handler should exist');
|
||||||
|
await testHandler(req, res);
|
||||||
|
|
||||||
|
assert.strictEqual(res.statusCode, 200);
|
||||||
|
assert.ok('ok' in res.data.data);
|
||||||
|
assert.ok(Array.isArray(res.data.data.addressbooks));
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user