nntp-proxy 0.5.1

NNTP proxy server with per-command backend multiplexing, caching, metrics, and TUI dashboard
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//! Configuration validation tests
//!
//! Tests to prevent regression of config field naming and validation issues
//! Ensures configs match what serde expects and validates properly

use anyhow::Result;
use nntp_proxy::config::Config;

/// Test that `connection_keepalive` field name works (not `connection_keepalive_secs`)
/// Regression test for issue #31
#[test]
fn test_connection_keepalive_field_name() -> Result<()> {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"
connection_keepalive = 60
"#;

    let config: Config = toml::from_str(toml)?;
    assert_eq!(config.servers.len(), 1);

    let server = &config.servers[0];
    assert!(server.connection_keepalive.is_some());
    assert_eq!(server.connection_keepalive.unwrap().as_secs(), 60);

    Ok(())
}

/// Test that old incorrect field name `connection_keepalive_secs` is rejected
/// This ensures we catch config errors early
#[test]
fn test_connection_keepalive_secs_rejected() {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"
connection_keepalive_secs = 60
"#;

    // This should either fail to parse or ignore the unknown field
    // depending on serde settings
    let result: Result<Config, _> = toml::from_str(toml);

    // Either it fails, or it succeeds but the field is None (ignored)
    if let Ok(config) = result {
        assert_eq!(config.servers.len(), 1);
        // The unknown field should be ignored, so connection_keepalive should be None
        assert!(
            config.servers[0].connection_keepalive.is_none(),
            "Unknown field connection_keepalive_secs should be ignored, not parsed"
        );
    }
    // If it fails, that's also acceptable behavior
}

/// Test that omitting `connection_keepalive` works (it's optional)
#[test]
fn test_connection_keepalive_optional() -> Result<()> {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"
"#;

    let config: Config = toml::from_str(toml)?;
    assert_eq!(config.servers.len(), 1);
    assert!(config.servers[0].connection_keepalive.is_none());

    Ok(())
}

/// Test that health check uses interval and timeout (not `interval_secs/timeout_secs`)
/// Regression test for issue #31
#[test]
fn test_health_check_field_names() -> Result<()> {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"

[health_check]
interval = 30
timeout = 5
unhealthy_threshold = 3
"#;

    let config: Config = toml::from_str(toml)?;

    let health_check = &config.health_check;
    assert_eq!(health_check.interval.as_secs(), 30);
    assert_eq!(health_check.timeout.as_secs(), 5);
    assert_eq!(health_check.unhealthy_threshold.get(), 3);

    Ok(())
}

/// Test that old incorrect field names `interval_secs/timeout_secs` are rejected
#[test]
fn test_health_check_old_field_names_rejected() {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"

[health_check]
interval_secs = 30
timeout_secs = 5
unhealthy_threshold = 3
"#;

    let result: Result<Config, _> = toml::from_str(toml);

    // Should either fail or use defaults (ignoring unknown fields)
    if let Ok(config) = result {
        // Unknown fields should be ignored, defaults should be used
        assert_eq!(
            config.health_check.interval.as_secs(),
            30,
            "Should use default interval when interval_secs is provided"
        );
        assert_eq!(
            config.health_check.timeout.as_secs(),
            5,
            "Should use default timeout when timeout_secs is provided"
        );
    }
}

/// Test that health check config is optional
#[test]
fn test_health_check_optional() -> Result<()> {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"
"#;

    let config: Config = toml::from_str(toml)?;

    // Should use defaults when not specified
    assert_eq!(config.health_check.interval.as_secs(), 30);
    assert_eq!(config.health_check.timeout.as_secs(), 5);
    assert_eq!(config.health_check.unhealthy_threshold.get(), 3);

    Ok(())
}

/// Test that `client_auth` section is completely optional
/// Regression test for issue #33
#[test]
fn test_client_auth_optional() -> Result<()> {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"
"#;

    let config: Config = toml::from_str(toml)?;
    config.validate()?;

    // client_auth should use defaults (disabled)
    assert!(!config.client_auth.is_enabled());
    assert!(config.client_auth.users.is_empty());
    assert!(config.client_auth.greeting.is_none());

    Ok(())
}

/// Test that `client_auth` with user credentials is enabled
#[test]
fn test_client_auth_enabled_with_credentials() -> Result<()> {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"

[[client_auth.users]]
username = "testuser"
password = "testpass"
"#;

    let config: Config = toml::from_str(toml)?;
    config.validate()?;

    // Should be enabled with user credentials
    assert!(config.client_auth.is_enabled());
    assert_eq!(config.client_auth.users.len(), 1);
    assert_eq!(config.client_auth.users[0].username, "testuser");
    assert_eq!(config.client_auth.users[0].password, "testpass");

    Ok(())
}

/// Test that custom greeting works with client auth
#[test]
fn test_client_auth_custom_greeting() -> Result<()> {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"

[client_auth]
greeting = "200 Custom Greeting"

[[client_auth.users]]
username = "testuser"
password = "testpass"
"#;

    let config: Config = toml::from_str(toml)?;

    assert!(config.client_auth.is_enabled());
    assert_eq!(
        config.client_auth.greeting.as_deref(),
        Some("200 Custom Greeting")
    );

    Ok(())
}

/// Test minimal valid config (regression test for issue #33)
#[test]
fn test_minimal_valid_config() -> Result<()> {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"
"#;

    let config: Config = toml::from_str(toml)?;

    // Validation should pass with minimal config
    config.validate()?;

    assert_eq!(config.servers.len(), 1);
    assert!(!config.client_auth.is_enabled());

    Ok(())
}

/// Test that config requires at least one server
#[test]
fn test_config_requires_server() {
    let toml = r#"
[client_auth]

[[client_auth.users]]
username = "testuser"
password = "testpass"
"#;

    let config: Config = toml::from_str(toml).expect("Should parse");

    // Validation should fail with no servers
    let result = config.validate();
    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("at least one server")
    );
}

/// Test example config files are valid
#[test]
fn test_config_example_is_valid() -> Result<()> {
    let toml = std::fs::read_to_string("config.example.toml")?;
    let config: Config = toml::from_str(&toml)?;
    config.validate()?;

    assert!(!config.servers.is_empty());

    Ok(())
}

/// Test cache config example is valid
#[test]
fn test_cache_config_example_is_valid() -> Result<()> {
    let toml = std::fs::read_to_string("config.cache.toml")?;
    let config: Config = toml::from_str(&toml)?;
    config.validate()?;

    assert!(!config.servers.is_empty());
    assert!(config.cache.is_some());

    Ok(())
}

/// Test that cache config fields are correct
#[test]
fn test_cache_config_field_names() -> Result<()> {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"

[cache]
max_capacity = 10000
ttl = 3600
"#;

    let config: Config = toml::from_str(toml)?;

    let cache = config.cache.expect("Cache config should be present");
    assert_eq!(cache.article_cache_capacity.get(), 10000);
    assert_eq!(cache.article_cache_ttl_secs.as_secs(), 3600);

    Ok(())
}

/// Test TLS configuration fields
#[test]
fn test_tls_config_fields() -> Result<()> {
    let toml = r#"
[[servers]]
host = "secure.example.com"
port = 563
name = "Secure Server"
use_tls = true
tls_verify_cert = true
tls_cert_path = "/path/to/cert.pem"
"#;

    let config: Config = toml::from_str(toml)?;

    let server = &config.servers[0];
    assert!(server.use_tls);
    assert!(server.tls_verify_cert);
    assert_eq!(server.tls_cert_path.as_deref(), Some("/path/to/cert.pem"));

    Ok(())
}

/// Test authentication config fields
#[test]
fn test_server_auth_fields() -> Result<()> {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"
username = "backend_user"
password = "backend_pass"
"#;

    let config: Config = toml::from_str(toml)?;

    let server = &config.servers[0];
    assert_eq!(server.username.as_deref(), Some("backend_user"));
    assert_eq!(server.password.as_deref(), Some("backend_pass"));

    Ok(())
}

/// Test `max_connections` field
#[test]
fn test_max_connections_field() -> Result<()> {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"
max_connections = 20
"#;

    let config: Config = toml::from_str(toml)?;

    let server = &config.servers[0];
    assert_eq!(server.max_connections.get(), 20);

    Ok(())
}

/// Test `max_connections` default value
#[test]
fn test_max_connections_default() -> Result<()> {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"
"#;

    let config: Config = toml::from_str(toml)?;

    let server = &config.servers[0];
    assert_eq!(server.max_connections.get(), 10);

    Ok(())
}

/// Test multiple servers configuration
#[test]
fn test_multiple_servers() -> Result<()> {
    let toml = r#"
[[servers]]
host = "news1.example.com"
port = 119
name = "Server 1"
max_connections = 20

[[servers]]
host = "news2.example.com"
port = 119
name = "Server 2"
max_connections = 15
connection_keepalive = 60

[[servers]]
host = "news3.example.com"
port = 563
name = "Server 3"
use_tls = true
"#;

    let config: Config = toml::from_str(toml)?;
    config.validate()?;

    assert_eq!(config.servers.len(), 3);

    assert_eq!(config.servers[0].name.as_str(), "Server 1");
    assert_eq!(config.servers[0].max_connections.get(), 20);
    assert!(config.servers[0].connection_keepalive.is_none());

    assert_eq!(config.servers[1].name.as_str(), "Server 2");
    assert_eq!(config.servers[1].max_connections.get(), 15);
    assert_eq!(
        config.servers[1].connection_keepalive.unwrap().as_secs(),
        60
    );

    assert_eq!(config.servers[2].name.as_str(), "Server 3");
    assert!(config.servers[2].use_tls);

    Ok(())
}

/// Test that duration fields accept various valid values
#[test]
fn test_duration_field_values() -> Result<()> {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"
connection_keepalive = 0

[health_check]
interval = 1
timeout = 3600
"#;

    let config: Config = toml::from_str(toml)?;

    // 0 should be valid (though not recommended)
    assert_eq!(config.servers[0].connection_keepalive.unwrap().as_secs(), 0);

    // Very small and large values should work
    assert_eq!(config.health_check.interval.as_secs(), 1);
    assert_eq!(config.health_check.timeout.as_secs(), 3600);

    Ok(())
}

/// Test that validation catches invalid port
#[test]
fn test_invalid_port_rejected() {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 0
name = "Test Server"
"#;

    let result: Result<Config, _> = toml::from_str(toml);
    assert!(result.is_err(), "Port 0 should be rejected");
}

/// Test that validation catches invalid `max_connections`
#[test]
fn test_invalid_max_connections_rejected() {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = "Test Server"
max_connections = 0
"#;

    let result: Result<Config, _> = toml::from_str(toml);
    assert!(result.is_err(), "max_connections = 0 should be rejected");
}

/// Test that validation catches empty server name
#[test]
fn test_empty_server_name_rejected() {
    let toml = r#"
[[servers]]
host = "news.example.com"
port = 119
name = ""
"#;

    let result: Result<Config, _> = toml::from_str(toml);
    assert!(result.is_err(), "Empty server name should be rejected");
}

/// Test that validation catches empty hostname
#[test]
fn test_empty_hostname_rejected() {
    let toml = r#"
[[servers]]
host = ""
port = 119
name = "Test Server"
"#;

    let result: Result<Config, _> = toml::from_str(toml);
    assert!(result.is_err(), "Empty hostname should be rejected");
}