Initial commit: DashCaddy v1.0
Full codebase including API server (32 modules + routes), dashboard frontend, DashCA certificate distribution, installer script, and deployment skills.
This commit is contained in:
134
dashcaddy-api/__tests__/tailscale.test.js
Normal file
134
dashcaddy-api/__tests__/tailscale.test.js
Normal file
@@ -0,0 +1,134 @@
|
||||
/**
|
||||
* Tailscale Route Tests
|
||||
*
|
||||
* Tests Tailscale status, configuration, and connection-checking endpoints.
|
||||
* The Tailscale routes are mounted without a prefix on the API router, so:
|
||||
* - GET /api/status — Tailscale status (returns null status if not installed)
|
||||
* - POST /api/config — NOTE: shadowed by config/settings.js which also defines POST /config;
|
||||
* we test it here but it may hit the DashCaddy config route instead.
|
||||
* - GET /api/check-connection — Check if request comes from Tailscale IP
|
||||
* - POST /api/tailscale/oauth-config — OAuth credential setup (requires live API)
|
||||
*/
|
||||
|
||||
const request = require('supertest');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
|
||||
const testServicesFile = path.join(os.tmpdir(), `tailscale-services-${Date.now()}.json`);
|
||||
const testConfigFile = path.join(os.tmpdir(), `tailscale-config-${Date.now()}.json`);
|
||||
|
||||
process.env.SERVICES_FILE = testServicesFile;
|
||||
process.env.CONFIG_FILE = testConfigFile;
|
||||
process.env.ENABLE_HEALTH_CHECKER = 'false';
|
||||
process.env.NODE_ENV = 'test';
|
||||
|
||||
fs.writeFileSync(testServicesFile, '[]', 'utf8');
|
||||
fs.writeFileSync(testConfigFile, '{}', 'utf8');
|
||||
|
||||
const app = require('../server');
|
||||
|
||||
describe('Tailscale Routes', () => {
|
||||
afterAll(() => {
|
||||
try { fs.unlinkSync(testServicesFile); } catch (e) { /* ignore */ }
|
||||
try { fs.unlinkSync(testConfigFile); } catch (e) { /* ignore */ }
|
||||
});
|
||||
|
||||
describe('GET /api/status (Tailscale status)', () => {
|
||||
test('should return 200 with status data', async () => {
|
||||
const res = await request(app).get('/api/status');
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body.success).toBe(true);
|
||||
|
||||
// If Tailscale is not installed in test env, expect installed: false
|
||||
if (!res.body.installed) {
|
||||
expect(res.body.installed).toBe(false);
|
||||
expect(res.body.connected).toBe(false);
|
||||
expect(res.body.message).toBeDefined();
|
||||
} else {
|
||||
// If installed, expect richer data
|
||||
expect(res.body).toHaveProperty('connected');
|
||||
expect(res.body).toHaveProperty('self');
|
||||
expect(res.body).toHaveProperty('config');
|
||||
expect(res.body).toHaveProperty('devices');
|
||||
expect(res.body).toHaveProperty('deviceCount');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/check-connection', () => {
|
||||
test('should return 200 with connection info', async () => {
|
||||
const res = await request(app).get('/api/check-connection');
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body.success).toBe(true);
|
||||
expect(res.body).toHaveProperty('isTailscale');
|
||||
expect(typeof res.body.isTailscale).toBe('boolean');
|
||||
expect(res.body).toHaveProperty('clientIP');
|
||||
});
|
||||
|
||||
test('should detect non-Tailscale IP for localhost requests', async () => {
|
||||
const res = await request(app).get('/api/check-connection');
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
// Supertest connects via loopback, not a 100.x.x.x address
|
||||
expect(res.body.isTailscale).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/devices (Tailscale devices)', () => {
|
||||
test('should return 200 with devices array', async () => {
|
||||
const res = await request(app).get('/api/devices');
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body.success).toBe(true);
|
||||
expect(res.body).toHaveProperty('devices');
|
||||
expect(Array.isArray(res.body.devices)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/tailscale/oauth-config', () => {
|
||||
test('should reject missing required fields', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/tailscale/oauth-config')
|
||||
.send({});
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
});
|
||||
|
||||
test('should reject partial credentials', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/tailscale/oauth-config')
|
||||
.send({ clientId: 'test-id' });
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/tailscale/api-devices', () => {
|
||||
test('should return 400 when OAuth is not configured', async () => {
|
||||
const res = await request(app).get('/api/tailscale/api-devices');
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/tailscale/sync', () => {
|
||||
test('should return 400 when OAuth is not configured', async () => {
|
||||
const res = await request(app).post('/api/tailscale/sync');
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/protect-service', () => {
|
||||
test('should reject missing subdomain', async () => {
|
||||
const res = await request(app)
|
||||
.post('/api/protect-service')
|
||||
.send({});
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user