quicknode-cli 0.1.9

Command-line interface for the Quicknode SDK
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
//! Integration tests for `qn endpoint …` against wiremock.

mod common;

use common::run_qn;
use serde_json::json;
use wiremock::matchers::{body_json, header, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

fn endpoint_payload(id: &str) -> serde_json::Value {
    json!({
        "id": id,
        "name": "test-name",
        "label": "test-label",
        "status": "active",
        "chain": "ethereum",
        "network": "mainnet",
        "is_dedicated": false,
        "is_flat_rate": false,
        "http_url": format!("https://{id}.example/v1/abc"),
        "wss_url": null,
        "tags": [],
        "is_multichain": false,
    })
}

/// Subprocess test: confirms `--format json` output is valid JSON over the wire.
#[tokio::test]
async fn list_endpoints_json_output_is_valid_json() {
    use assert_cmd::Command;
    use std::process::Stdio;
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v0/endpoints"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": [endpoint_payload("ep-1")],
            "pagination": { "total": 1, "limit": 20, "offset": 0 }
        })))
        .mount(&server)
        .await;
    let _ = Stdio::null();

    let output = Command::cargo_bin("qn")
        .unwrap()
        .env_remove("HOME")
        .env("HOME", std::env::temp_dir())
        .args([
            "--api-key",
            "test",
            "--base-url",
            &server.uri(),
            "--no-input",
            "--format",
            "json",
            "endpoint",
            "list",
        ])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    let v: serde_json::Value = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("invalid JSON: {e}\nstdout was:\n{stdout}"));
    assert_eq!(v["data"][0]["id"].as_str(), Some("ep-1"));
}

/// Subprocess test: each non-table format produces *some* stdout containing the
/// endpoint ID. The exact byte content is format-specific; we don't want to
/// over-fit on serializer details. The point is: the format flag is wired
/// through, exit 0, and the ID shows up somewhere.
#[tokio::test]
async fn list_endpoints_all_formats_render() {
    use assert_cmd::Command;
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v0/endpoints"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": [endpoint_payload("ep-1")],
            "pagination": { "total": 1, "limit": 20, "offset": 0 }
        })))
        .mount(&server)
        .await;

    for fmt in ["json", "yaml", "md", "toon", "table"] {
        let output = Command::cargo_bin("qn")
            .unwrap()
            .env_remove("HOME")
            .env("HOME", std::env::temp_dir())
            .args([
                "--api-key",
                "test",
                "--base-url",
                &server.uri(),
                "--no-input",
                "--no-color",
                "-o",
                fmt,
                "endpoint",
                "list",
            ])
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "format={fmt} exit={:?} stderr={}",
            output.status.code(),
            String::from_utf8_lossy(&output.stderr)
        );
        let stdout = String::from_utf8(output.stdout).unwrap();
        assert!(
            stdout.contains("ep-1"),
            "format={fmt} stdout did not contain ep-1:\n{stdout}"
        );
    }
}

/// `--wide -o md` adds HTTP/WSS columns to the markdown table.
#[tokio::test]
async fn list_endpoints_wide_md_includes_urls() {
    use assert_cmd::Command;
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v0/endpoints"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": [endpoint_payload("ep-1")],
            "pagination": { "total": 1, "limit": 20, "offset": 0 }
        })))
        .mount(&server)
        .await;

    let output = Command::cargo_bin("qn")
        .unwrap()
        .env_remove("HOME")
        .env("HOME", std::env::temp_dir())
        .args([
            "--api-key",
            "test",
            "--base-url",
            &server.uri(),
            "--no-input",
            "--no-color",
            "-o",
            "md",
            "-w",
            "endpoint",
            "list",
        ])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "stderr={}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        stdout.contains("HTTP") && stdout.contains("WSS"),
        "expected HTTP/WSS headers in -w output:\n{stdout}"
    );
    assert!(
        stdout.contains("ep-1.example"),
        "expected http_url in -w output:\n{stdout}"
    );
}

#[tokio::test]
async fn list_endpoints_happy_path() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v0/endpoints"))
        .and(header("x-api-key", "test"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": [endpoint_payload("ep-1"), endpoint_payload("ep-2")],
            "pagination": { "total": 2, "limit": 20, "offset": 0 }
        })))
        .mount(&server)
        .await;

    let out = run_qn(&server.uri(), &["endpoint", "list"]).await;
    assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}

#[tokio::test]
async fn requests_carry_the_cli_user_agent() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v0/endpoints"))
        .and(header("user-agent", qn::context::user_agent().as_str()))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": [],
            "pagination": { "total": 0, "limit": 20, "offset": 0 }
        })))
        .expect(1)
        .mount(&server)
        .await;

    let out = run_qn(&server.uri(), &["endpoint", "list"]).await;
    assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}

#[tokio::test]
async fn list_endpoints_404_renders_clean_error() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v0/endpoints"))
        .respond_with(ResponseTemplate::new(404).set_body_string("{\"message\":\"nope\"}"))
        .mount(&server)
        .await;

    let out = run_qn(&server.uri(), &["endpoint", "list"]).await;
    assert_eq!(out.exit_code, 2);
    assert!(out.stderr.contains("not found"), "stderr={}", out.stderr);
}

#[tokio::test]
async fn list_endpoints_401_maps_to_unauthorized() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v0/endpoints"))
        .respond_with(ResponseTemplate::new(401).set_body_string("denied"))
        .mount(&server)
        .await;
    let out = run_qn(&server.uri(), &["endpoint", "list"]).await;
    assert_eq!(out.exit_code, 2);
    assert!(out.stderr.contains("unauthorized"), "stderr={}", out.stderr);
}

#[tokio::test]
async fn create_endpoint_sends_chain_and_network() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v0/endpoints"))
        .and(body_json(
            json!({ "chain": "ethereum", "network": "mainnet" }),
        ))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": {
                "id": "ep-new",
                "chain": "ethereum",
                "network": "mainnet",
                "http_url": "https://ep-new.example",
                "tags": []
            }
        })))
        .mount(&server)
        .await;

    let out = run_qn(
        &server.uri(),
        &[
            "endpoint",
            "create",
            "--chain",
            "ethereum",
            "--network",
            "mainnet",
        ],
    )
    .await;
    assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}

#[tokio::test]
async fn show_endpoint_passes_id_in_path() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v0/endpoints/ep-show"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": {
                "id": "ep-show",
                "chain": "solana",
                "network": "mainnet",
                "http_url": "https://ep-show.example",
                "tags": []
            }
        })))
        .mount(&server)
        .await;
    let out = run_qn(&server.uri(), &["endpoint", "show", "ep-show"]).await;
    assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}

#[tokio::test]
async fn update_endpoint_label() {
    let server = MockServer::start().await;
    Mock::given(method("PATCH"))
        .and(path("/v0/endpoints/ep-1"))
        .and(body_json(json!({ "label": "new label" })))
        .respond_with(ResponseTemplate::new(200))
        .mount(&server)
        .await;
    let out = run_qn(
        &server.uri(),
        &["endpoint", "update", "ep-1", "--label", "new label"],
    )
    .await;
    assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}

#[tokio::test]
async fn archive_endpoint_requires_yes_in_no_tty() {
    let server = MockServer::start().await;
    Mock::given(method("DELETE"))
        .and(path("/v0/endpoints/ep-1"))
        .respond_with(ResponseTemplate::new(200))
        .mount(&server)
        .await;

    let no_yes = run_qn(&server.uri(), &["endpoint", "archive", "ep-1"]).await;
    assert_eq!(no_yes.exit_code, 5);
    assert!(no_yes.stderr.contains("confirmation"), "{}", no_yes.stderr);

    let with_yes = run_qn(&server.uri(), &["endpoint", "archive", "ep-1", "--yes"]).await;
    assert_eq!(with_yes.exit_code, 0, "stderr={}", with_yes.stderr);
}

#[tokio::test]
async fn pause_resume_send_status_update() {
    let server = MockServer::start().await;
    Mock::given(method("PATCH"))
        .and(path("/v0/endpoints/ep-1/status"))
        .and(body_json(json!({ "status": "paused" })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({"data":"ok"})))
        .mount(&server)
        .await;
    Mock::given(method("PATCH"))
        .and(path("/v0/endpoints/ep-1/status"))
        .and(body_json(json!({ "status": "active" })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({"data":"ok"})))
        .mount(&server)
        .await;
    assert_eq!(
        run_qn(&server.uri(), &["endpoint", "pause", "ep-1"])
            .await
            .exit_code,
        0
    );
    assert_eq!(
        run_qn(&server.uri(), &["endpoint", "resume", "ep-1"])
            .await
            .exit_code,
        0
    );
}

#[tokio::test]
async fn urls_endpoint() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v0/endpoints/ep-1/urls"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": {
                "http_url": "https://ep-1.example",
                "wss_url": "wss://ep-1.example",
                "multichain_urls": null
            }
        })))
        .mount(&server)
        .await;
    let out = run_qn(&server.uri(), &["endpoint", "urls", "ep-1"]).await;
    assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}

#[tokio::test]
async fn multichain_enable_disable() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v0/endpoints/ep-1/enable_multichain"))
        .respond_with(ResponseTemplate::new(200))
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/v0/endpoints/ep-1/disable_multichain"))
        .respond_with(ResponseTemplate::new(200))
        .mount(&server)
        .await;
    assert_eq!(
        run_qn(&server.uri(), &["endpoint", "enable-multichain", "ep-1"])
            .await
            .exit_code,
        0
    );
    assert_eq!(
        run_qn(&server.uri(), &["endpoint", "disable-multichain", "ep-1"])
            .await
            .exit_code,
        0
    );
}

#[tokio::test]
async fn endpoint_tag_add() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v0/endpoints/ep-1/tags"))
        .and(body_json(json!({ "label": "prod" })))
        .respond_with(ResponseTemplate::new(200))
        .mount(&server)
        .await;
    let out = run_qn(&server.uri(), &["endpoint", "tag", "add", "ep-1", "prod"]).await;
    assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}

#[tokio::test]
async fn endpoint_security_token_create() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v0/endpoints/ep-1/security/tokens"))
        .respond_with(ResponseTemplate::new(200))
        .mount(&server)
        .await;
    let out = run_qn(
        &server.uri(),
        &["endpoint", "security", "token", "create", "ep-1"],
    )
    .await;
    assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}

#[tokio::test]
async fn endpoint_security_token_delete_with_yes() {
    let server = MockServer::start().await;
    Mock::given(method("DELETE"))
        .and(path("/v0/endpoints/ep-1/security/tokens/tok-1"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "data": true })))
        .expect(1)
        .mount(&server)
        .await;
    let out = run_qn(
        &server.uri(),
        &[
            "endpoint", "security", "token", "delete", "ep-1", "tok-1", "--yes",
        ],
    )
    .await;
    assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}

#[tokio::test]
async fn endpoint_security_token_delete_without_yes_sends_nothing() {
    let server = MockServer::start().await;
    Mock::given(method("DELETE"))
        .and(path("/v0/endpoints/ep-1/security/tokens/tok-1"))
        .respond_with(ResponseTemplate::new(200))
        .expect(0)
        .mount(&server)
        .await;
    let out = run_qn(
        &server.uri(),
        &["endpoint", "security", "token", "delete", "ep-1", "tok-1"],
    )
    .await;
    assert_eq!(out.exit_code, 5, "stderr={}", out.stderr);
}

#[tokio::test]
async fn rate_limit_delete_override_with_yes() {
    let server = MockServer::start().await;
    Mock::given(method("DELETE"))
        .and(path("/v0/endpoints/ep-1/rate-limits/ovr-1"))
        .respond_with(ResponseTemplate::new(200))
        .expect(1)
        .mount(&server)
        .await;
    let out = run_qn(
        &server.uri(),
        &[
            "endpoint",
            "rate-limit",
            "delete-override",
            "ep-1",
            "ovr-1",
            "--yes",
        ],
    )
    .await;
    assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}

#[tokio::test]
async fn rate_limit_delete_override_without_yes_sends_nothing() {
    let server = MockServer::start().await;
    Mock::given(method("DELETE"))
        .and(path("/v0/endpoints/ep-1/rate-limits/ovr-1"))
        .respond_with(ResponseTemplate::new(200))
        .expect(0)
        .mount(&server)
        .await;
    let out = run_qn(
        &server.uri(),
        &["endpoint", "rate-limit", "delete-override", "ep-1", "ovr-1"],
    )
    .await;
    assert_eq!(out.exit_code, 5, "stderr={}", out.stderr);
}

#[tokio::test]
async fn endpoint_security_set_options_sends_partial_payload() {
    let server = MockServer::start().await;
    Mock::given(method("PATCH"))
        .and(path("/v0/endpoints/ep-1/security_options"))
        .and(body_json(json!({
            "options": { "tokens": "enabled", "jwts": "disabled" }
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "data": [] })))
        .mount(&server)
        .await;
    let out = run_qn(
        &server.uri(),
        &[
            "endpoint",
            "security",
            "set-options",
            "ep-1",
            "--tokens",
            "enabled",
            "--jwts",
            "disabled",
        ],
    )
    .await;
    assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}

#[tokio::test]
async fn endpoint_method_ratelimit_create() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v0/endpoints/ep-1/method-rate-limits"))
        .and(body_json(json!({
            "interval": "second",
            "methods": ["eth_call"],
            "rate": 10
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "data": {
                "id": "rl-1",
                "interval": "second",
                "methods": ["eth_call"],
                "rate": 10,
                "status": "enabled",
                "created": "2026-01-01T00:00:00Z"
            }
        })))
        .mount(&server)
        .await;
    let out = run_qn(
        &server.uri(),
        &[
            "endpoint",
            "rate-limit",
            "method-create",
            "ep-1",
            "--interval",
            "second",
            "--method",
            "eth_call",
            "--rate",
            "10",
        ],
    )
    .await;
    assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}

#[tokio::test]
async fn endpoint_ratelimit_set_omits_unset_fields() {
    let server = MockServer::start().await;
    Mock::given(method("PATCH"))
        .and(path("/v0/endpoints/ep-1/rate-limits"))
        .and(body_json(json!({ "rate_limits": { "rps": 100 } })))
        .respond_with(ResponseTemplate::new(200))
        .mount(&server)
        .await;
    let out = run_qn(
        &server.uri(),
        &["endpoint", "rate-limit", "set", "ep-1", "--rps", "100"],
    )
    .await;
    assert_eq!(out.exit_code, 0, "stderr={}", out.stderr);
}

// ---- Required-args enforcement (clap-level, exits 1 before any HTTP) ---- //

#[tokio::test]
async fn endpoint_create_no_flags_fails_before_api_call() {
    // No mocks mounted; if the CLI tries to make a request, wiremock would 404.
    // clap rejects the invocation *before* any HTTP call.
    let server = MockServer::start().await;
    let out = run_qn(&server.uri(), &["endpoint", "create"]).await;
    assert_eq!(out.exit_code, 1, "stderr={}", out.stderr);
    assert!(
        out.stderr.contains("--chain") && out.stderr.contains("--network"),
        "stderr={}",
        out.stderr
    );
    assert_eq!(server.received_requests().await.unwrap().len(), 0);
}

#[tokio::test]
async fn endpoint_create_only_chain_fails_before_api_call() {
    let server = MockServer::start().await;
    let out = run_qn(
        &server.uri(),
        &["endpoint", "create", "--chain", "ethereum"],
    )
    .await;
    assert_eq!(out.exit_code, 1, "stderr={}", out.stderr);
    assert!(
        out.stderr.contains("--network <NETWORK>"),
        "should call out --network; stderr={}",
        out.stderr
    );
    assert_eq!(server.received_requests().await.unwrap().len(), 0);
}

#[tokio::test]
async fn endpoint_update_no_flags_fails_before_api_call() {
    let server = MockServer::start().await;
    let out = run_qn(&server.uri(), &["endpoint", "update", "ep-1"]).await;
    assert_eq!(out.exit_code, 1, "stderr={}", out.stderr);
    assert!(out.stderr.contains("--label"), "stderr={}", out.stderr);
    assert_eq!(server.received_requests().await.unwrap().len(), 0);
}

#[tokio::test]
async fn stream_create_missing_flags_fails_before_api_call() {
    let server = MockServer::start().await;
    let out = run_qn(&server.uri(), &["stream", "create", "--name", "x"]).await;
    assert_eq!(out.exit_code, 1, "stderr={}", out.stderr);
    assert!(
        out.stderr.contains("--network") && out.stderr.contains("--webhook"),
        "stderr={}",
        out.stderr
    );
    assert_eq!(server.received_requests().await.unwrap().len(), 0);
}

#[tokio::test]
async fn endpoint_security_set_options_no_flags_fails_before_api_call() {
    let server = MockServer::start().await;
    let out = run_qn(
        &server.uri(),
        &["endpoint", "security", "set-options", "ep-1"],
    )
    .await;
    assert_eq!(out.exit_code, 1, "stderr={}", out.stderr);
    assert!(out.stderr.contains("--tokens"), "stderr={}", out.stderr);
    assert_eq!(server.received_requests().await.unwrap().len(), 0);
}