desec 0.0.2

deSEC.io DNS API client library
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//! Mocked coverage of the `/domains/{name}/rrsets/` endpoints, apex traps included.
#![allow(clippy::expect_used)]

mod common;

use common::*;

use desec::api::rrsets::{BulkPatch, BulkPut, MAX_TTL, NewRrset, RrsetPatch};
use desec::{RecordType, Subname};
use wiremock::matchers::{any, body_json, header, method, path, query_param};
use wiremock::{Mock, ResponseTemplate};

fn www() -> Subname {
    "www".parse().expect("`www` is a valid subname")
}

#[tokio::test]
async fn creating_one_rrset_sends_all_four_fields() {
    let (server, client) = mock().await;
    Mock::given(method("POST"))
        .and(path("/api/v1/domains/example.com/rrsets/"))
        .and(header("authorization", auth_header().as_str()))
        .and(header("content-type", "application/json"))
        .and(body_json(serde_json::json!({
            "subname": "www",
            "type": "A",
            "ttl": 3600,
            "records": ["127.0.0.1"],
        })))
        .respond_with(ResponseTemplate::new(201).set_body_json(rrset_json(
            "example.com",
            "www",
            "A",
            3600,
            &["127.0.0.1"],
        )))
        .expect(1)
        .mount(&server)
        .await;

    let rrset = client
        .rrsets("example.com")
        .create(&NewRrset::new(www(), RecordType::A, 3600, ["127.0.0.1"]))
        .await
        .expect("creation succeeds");

    assert_eq!(rrset.domain, "example.com");
    assert_eq!(rrset.subname, www());
    assert_eq!(rrset.record_type, RecordType::A);
    assert_eq!(rrset.name, "www.example.com.");
    assert_eq!(rrset.ttl, 3600);
    assert_eq!(rrset.records, vec!["127.0.0.1"]);
}

#[tokio::test]
async fn an_apex_creation_spells_the_subname_as_an_empty_string() {
    let (server, client) = mock().await;
    Mock::given(method("POST"))
        .and(path("/api/v1/domains/example.com/rrsets/"))
        .and(header("authorization", auth_header().as_str()))
        // A body `subname` of `"@"` would create a literal `@` label, not the apex.
        .and(body_json(serde_json::json!({
            "subname": "",
            "type": "MX",
            "ttl": 3600,
            "records": ["10 mx.example.com."],
        })))
        .respond_with(ResponseTemplate::new(201).set_body_json(rrset_json(
            "example.com",
            "",
            "MX",
            3600,
            &["10 mx.example.com."],
        )))
        .expect(1)
        .mount(&server)
        .await;

    let rrset = client
        .rrsets("example.com")
        .create(&NewRrset::at_apex(
            RecordType::MX,
            3600,
            ["10 mx.example.com."],
        ))
        .await
        .expect("creation succeeds");
    assert!(rrset.subname.is_apex());
    assert_eq!(rrset.name, "example.com.");
}

#[tokio::test]
async fn a_bulk_creation_posts_a_json_array() {
    let (server, client) = mock().await;
    Mock::given(method("POST"))
        .and(path("/api/v1/domains/example.com/rrsets/"))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(serde_json::json!([
            {"subname": "www", "type": "A", "ttl": 3600, "records": ["127.0.0.1"]},
            {"subname": "", "type": "TXT", "ttl": 600, "records": ["\"v=spf1 -all\""]},
        ])))
        .respond_with(ResponseTemplate::new(201).set_body_json(serde_json::json!([
            rrset_json("example.com", "www", "A", 3600, &["127.0.0.1"]),
            rrset_json("example.com", "", "TXT", 600, &["\"v=spf1 -all\""]),
        ])))
        .expect(1)
        .mount(&server)
        .await;

    let rrsets = client
        .rrsets("example.com")
        .create_bulk(&[
            NewRrset::new(www(), RecordType::A, 3600, ["127.0.0.1"]),
            NewRrset::at_apex(RecordType::TXT, 600, ["\"v=spf1 -all\""]),
        ])
        .await
        .expect("creation succeeds");

    assert_eq!(rrsets.len(), 2);
    assert_eq!(rrsets[0].subname, www());
    assert!(rrsets[1].subname.is_apex());
}

#[tokio::test]
async fn a_failed_bulk_creation_keeps_the_failing_position() {
    let (server, client) = mock().await;
    Mock::given(method("POST"))
        .and(path("/api/v1/domains/example.com/rrsets/"))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(serde_json::json!([
            {"subname": "www", "type": "A", "ttl": 3600, "records": ["127.0.0.1"]},
            {"subname": "bad", "type": "A", "ttl": 3600, "records": ["not-an-address"]},
        ])))
        .respond_with(ResponseTemplate::new(400).set_body_json(serde_json::json!([
            {},
            {"records": ["Invalid record."]},
        ])))
        .expect(1)
        .mount(&server)
        .await;

    let err = client
        .rrsets("example.com")
        .create_bulk(&[
            NewRrset::new(www(), RecordType::A, 3600, ["127.0.0.1"]),
            NewRrset::new(
                "bad".parse().expect("valid subname"),
                RecordType::A,
                3600,
                ["not-an-address"],
            ),
        ])
        .await
        .expect_err("the second item is invalid");

    assert!(err.is_validation(), "{err:?}");
    let api = err.api_error().expect("a 400 carries an error document");
    let items = api.bulk_items().expect("a bulk failure is an array");
    assert_eq!(items.len(), 2);
    assert_eq!(items[0].messages(), Vec::<&str>::new());
    assert_eq!(items[1].messages(), vec!["Invalid record."]);
    assert_eq!(
        api.messages(),
        vec![("1.records".to_owned(), "Invalid record.")]
    );
}

#[tokio::test]
async fn listing_rrsets_sends_an_empty_cursor() {
    let (server, client) = mock().await;
    Mock::given(method("GET"))
        .and(path("/api/v1/domains/example.com/rrsets/"))
        .and(header("authorization", auth_header().as_str()))
        // Omitting `cursor` is what turns a large zone's list into a 400.
        .and(query_param("cursor", ""))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!([
            rrset_json("example.com", "www", "A", 3600, &["127.0.0.1"]),
            rrset_json("example.com", "", "MX", 3600, &["10 mx.example.com."]),
        ])))
        .expect(1)
        .mount(&server)
        .await;

    let page = client
        .rrsets("example.com")
        .list()
        .send()
        .await
        .expect("the list succeeds");
    assert_eq!(page.items.len(), 2);
    assert!(!page.has_next());
}

#[tokio::test]
async fn a_subname_and_a_type_filter_ride_on_the_same_request() {
    let (server, client) = mock().await;
    Mock::given(method("GET"))
        .and(path("/api/v1/domains/example.com/rrsets/"))
        .and(header("authorization", auth_header().as_str()))
        .and(query_param("subname", "www"))
        .and(query_param("type", "A"))
        .and(query_param("cursor", ""))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(serde_json::json!([rrset_json(
                "example.com",
                "www",
                "A",
                3600,
                &["127.0.0.1"]
            )])),
        )
        .expect(1)
        .mount(&server)
        .await;

    let page = client
        .rrsets("example.com")
        .list()
        .subname(&www())
        .record_type(&RecordType::A)
        .send()
        .await
        .expect("the filtered list succeeds");
    assert_eq!(page.items.len(), 1);
}

#[tokio::test]
async fn the_apex_subname_filter_is_the_empty_string() {
    let (server, client) = mock().await;
    Mock::given(method("GET"))
        .and(path("/api/v1/domains/example.com/rrsets/"))
        .and(header("authorization", auth_header().as_str()))
        // `subname=@` would filter for a literal `@` label and match nothing.
        .and(query_param("subname", ""))
        .and(query_param("cursor", ""))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(serde_json::json!([rrset_json(
                "example.com",
                "",
                "MX",
                3600,
                &["10 mx.example.com."]
            )])),
        )
        .expect(1)
        .mount(&server)
        .await;

    let page = client
        .rrsets("example.com")
        .list()
        .subname(&Subname::apex())
        .send()
        .await
        .expect("the filtered list succeeds");
    assert_eq!(page.items.len(), 1);
    assert!(page.items[0].subname.is_apex());
}

#[tokio::test]
async fn fetching_one_rrset_addresses_it_by_subname_and_type() {
    let (server, client) = mock().await;
    Mock::given(method("GET"))
        .and(path("/api/v1/domains/example.com/rrsets/www/A/"))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(ResponseTemplate::new(200).set_body_json(rrset_json(
            "example.com",
            "www",
            "A",
            3600,
            &["127.0.0.1"],
        )))
        .expect(1)
        .mount(&server)
        .await;

    let rrset = client
        .rrsets("example.com")
        .get(&www(), &RecordType::A)
        .await
        .expect("the RRset exists");
    assert_eq!(rrset.records, vec!["127.0.0.1"]);
}

#[tokio::test]
async fn the_apex_is_reached_through_an_at_sign_path_segment() {
    let (server, client) = mock().await;
    // An empty path segment collapses under HTTP normalization, so `/rrsets//A/` never
    // reaches the apex; `@` is the only spelling that does.
    Mock::given(method("GET"))
        .and(path("/api/v1/domains/example.com/rrsets/@/A/"))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(ResponseTemplate::new(200).set_body_json(rrset_json(
            "example.com",
            "",
            "A",
            3600,
            &["127.0.0.1"],
        )))
        .expect(1)
        .mount(&server)
        .await;

    let rrset = client
        .rrsets("example.com")
        .get(&Subname::apex(), &RecordType::A)
        .await
        .expect("the apex RRset exists");
    assert!(rrset.subname.is_apex());
    assert_eq!(rrset.name, "example.com.");
}

#[tokio::test]
async fn try_get_maps_an_absent_rrset_onto_none() {
    let (server, client) = mock().await;
    Mock::given(method("GET"))
        .and(path("/api/v1/domains/example.com/rrsets/www/AAAA/"))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(
            ResponseTemplate::new(404).set_body_json(serde_json::json!({"detail": "Not found."})),
        )
        .expect(1)
        .mount(&server)
        .await;

    assert!(
        client
            .rrsets("example.com")
            .try_get(&www(), &RecordType::AAAA)
            .await
            .expect("a 404 is not an error here")
            .is_none()
    );
}

#[tokio::test]
async fn a_ttl_only_patch_never_mentions_records() {
    let (server, client) = mock().await;
    Mock::given(method("PATCH"))
        .and(path("/api/v1/domains/example.com/rrsets/www/A/"))
        .and(header("authorization", auth_header().as_str()))
        // `records: null` is a 400 upstream rather than "leave unchanged", so the key has
        // to be absent for a TTL-only update to work at all.
        .and(body_json(serde_json::json!({"ttl": 86400})))
        .respond_with(ResponseTemplate::new(200).set_body_json(rrset_json(
            "example.com",
            "www",
            "A",
            86400,
            &["127.0.0.1"],
        )))
        .expect(1)
        .mount(&server)
        .await;

    let rrset = client
        .rrsets("example.com")
        .patch(&www(), &RecordType::A, &RrsetPatch::new().ttl(86_400))
        .await
        .expect("the patch succeeds");
    assert_eq!(rrset.ttl, 86_400);
    assert_eq!(rrset.records, vec!["127.0.0.1"]);
}

#[tokio::test]
async fn a_records_only_patch_never_mentions_the_ttl() {
    let (server, client) = mock().await;
    Mock::given(method("PATCH"))
        .and(path("/api/v1/domains/example.com/rrsets/www/A/"))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(serde_json::json!({"records": ["1.2.3.4"]})))
        .respond_with(ResponseTemplate::new(200).set_body_json(rrset_json(
            "example.com",
            "www",
            "A",
            3600,
            &["1.2.3.4"],
        )))
        .expect(1)
        .mount(&server)
        .await;

    let rrset = client
        .rrsets("example.com")
        .patch(
            &www(),
            &RecordType::A,
            &RrsetPatch::new().records(["1.2.3.4"]),
        )
        .await
        .expect("the patch succeeds");
    assert_eq!(rrset.ttl, 3600);
    assert_eq!(rrset.records, vec!["1.2.3.4"]);
}

#[tokio::test]
async fn a_subname_read_from_the_api_can_be_written_straight_back() {
    let (server, client) = mock().await;
    // The API answers with `""` and the URL needs `@`. Feeding a response's `subname`
    // back into a write is exactly where a plain String loses the apex.
    Mock::given(method("GET"))
        .and(path("/api/v1/domains/example.com/rrsets/@/A/"))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(ResponseTemplate::new(200).set_body_json(rrset_json(
            "example.com",
            "",
            "A",
            3600,
            &["127.0.0.1"],
        )))
        .expect(1)
        .mount(&server)
        .await;
    Mock::given(method("PATCH"))
        .and(path("/api/v1/domains/example.com/rrsets/@/A/"))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(serde_json::json!({"ttl": 600})))
        .respond_with(ResponseTemplate::new(200).set_body_json(rrset_json(
            "example.com",
            "",
            "A",
            600,
            &["127.0.0.1"],
        )))
        .expect(1)
        .mount(&server)
        .await;

    let rrsets = client.rrsets("example.com");
    let existing = rrsets
        .get(&Subname::apex(), &RecordType::A)
        .await
        .expect("the apex RRset exists");
    assert_eq!(existing.subname.as_payload(), "");

    let updated = rrsets
        .patch(
            &existing.subname,
            &existing.record_type,
            &RrsetPatch::new().ttl(600),
        )
        .await
        .expect("the patch reaches the apex");
    assert_eq!(updated.ttl, 600);
}

#[tokio::test]
async fn an_empty_record_list_is_how_a_patch_deletes() {
    let (server, client) = mock().await;
    Mock::given(method("PATCH"))
        .and(path("/api/v1/domains/example.com/rrsets/www/A/"))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(serde_json::json!({"records": []})))
        .respond_with(ResponseTemplate::new(200).set_body_json(rrset_json(
            "example.com",
            "www",
            "A",
            3600,
            &[],
        )))
        .expect(1)
        .mount(&server)
        .await;

    let rrset = client
        .rrsets("example.com")
        .patch(
            &www(),
            &RecordType::A,
            &RrsetPatch::new().records(Vec::<String>::new()),
        )
        .await
        .expect("the deletion succeeds");
    assert!(rrset.records.is_empty());
}

#[tokio::test]
async fn a_replacement_body_agrees_with_its_own_path() {
    let (server, client) = mock().await;
    Mock::given(method("PUT"))
        .and(path("/api/v1/domains/example.com/rrsets/www/A/"))
        .and(header("authorization", auth_header().as_str()))
        // A body `subname` that disagrees with the path is a 400, so both come from the
        // same argument.
        .and(body_json(serde_json::json!({
            "subname": "www",
            "type": "A",
            "ttl": 3600,
            "records": ["1.2.3.4", "5.6.7.8"],
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(rrset_json(
            "example.com",
            "www",
            "A",
            3600,
            &["1.2.3.4", "5.6.7.8"],
        )))
        .expect(1)
        .mount(&server)
        .await;

    let rrset = client
        .rrsets("example.com")
        .replace(&www(), &RecordType::A, 3600, ["1.2.3.4", "5.6.7.8"])
        .await
        .expect("the replacement succeeds");
    assert_eq!(rrset.subname.as_payload(), "www");
    assert_eq!(rrset.records.len(), 2);
}

#[tokio::test]
async fn an_apex_replacement_uses_at_sign_in_the_path_and_empty_in_the_body() {
    let (server, client) = mock().await;
    Mock::given(method("PUT"))
        .and(path("/api/v1/domains/example.com/rrsets/@/A/"))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(serde_json::json!({
            "subname": "",
            "type": "A",
            "ttl": 3600,
            "records": ["127.0.0.1"],
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(rrset_json(
            "example.com",
            "",
            "A",
            3600,
            &["127.0.0.1"],
        )))
        .expect(1)
        .mount(&server)
        .await;

    let rrset = client
        .rrsets("example.com")
        .replace(&Subname::apex(), &RecordType::A, 3600, ["127.0.0.1"])
        .await
        .expect("the replacement succeeds");
    assert!(rrset.subname.is_apex());
}

#[tokio::test]
async fn deleting_one_rrset_accepts_an_empty_204() {
    let (server, client) = mock().await;
    Mock::given(method("DELETE"))
        .and(path("/api/v1/domains/example.com/rrsets/www/A/"))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(ResponseTemplate::new(204))
        .expect(1)
        .mount(&server)
        .await;

    client
        .rrsets("example.com")
        .delete(&www(), &RecordType::A)
        .await
        .expect("deletion succeeds");
}

#[tokio::test]
async fn a_mixed_bulk_patch_identifies_every_item_it_touches() {
    let (server, client) = mock().await;
    Mock::given(method("PATCH"))
        .and(path("/api/v1/domains/example.com/rrsets/"))
        .and(header("authorization", auth_header().as_str()))
        // Every item carries `subname` and `type`; without them the API cannot tell which
        // RRset an update refers to.
        .and(body_json(serde_json::json!([
            {"subname": "www", "type": "A", "ttl": 600},
            {"subname": "old", "type": "TXT", "records": []},
        ])))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!([
            rrset_json("example.com", "www", "A", 600, &["127.0.0.1"]),
            rrset_json("example.com", "old", "TXT", 3600, &[]),
        ])))
        .expect(1)
        .mount(&server)
        .await;

    let rrsets = client
        .rrsets("example.com")
        .patch_bulk(&[
            BulkPatch::new(www(), RecordType::A).ttl(600),
            BulkPatch::delete("old".parse().expect("valid subname"), RecordType::TXT),
        ])
        .await
        .expect("the bulk patch succeeds");
    assert_eq!(rrsets.len(), 2);
    assert_eq!(rrsets[0].ttl, 600);
    assert!(rrsets[1].records.is_empty());
}

#[tokio::test]
async fn a_bulk_replacement_sends_every_field_of_every_item() {
    let (server, client) = mock().await;
    Mock::given(method("PUT"))
        .and(path("/api/v1/domains/example.com/rrsets/"))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(serde_json::json!([
            {"subname": "www", "type": "A", "ttl": 3600, "records": ["127.0.0.1"]},
            {"subname": "", "type": "MX", "ttl": 600, "records": ["10 mx.example.com."]},
        ])))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!([
            rrset_json("example.com", "www", "A", 3600, &["127.0.0.1"]),
            rrset_json("example.com", "", "MX", 600, &["10 mx.example.com."]),
        ])))
        .expect(1)
        .mount(&server)
        .await;

    let rrsets = client
        .rrsets("example.com")
        .replace_bulk(&[
            BulkPut::new(www(), RecordType::A, 3600, ["127.0.0.1"]),
            BulkPut::new(Subname::apex(), RecordType::MX, 600, ["10 mx.example.com."]),
        ])
        .await
        .expect("the bulk replacement succeeds");
    assert_eq!(rrsets.len(), 2);
}

#[tokio::test]
async fn a_bulk_deletion_patches_rather_than_puts() {
    let (server, client) = mock().await;
    // PUT would reject the same batch for lacking a `ttl` on records it is removing.
    Mock::given(method("PATCH"))
        .and(path("/api/v1/domains/example.com/rrsets/"))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(serde_json::json!([
            {"subname": "a", "type": "A", "records": []},
            {"subname": "", "type": "TXT", "records": []},
        ])))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!([])))
        .expect(1)
        .mount(&server)
        .await;

    let rrsets = client
        .rrsets("example.com")
        .delete_bulk([
            ("a".parse().expect("valid subname"), RecordType::A),
            (Subname::apex(), RecordType::TXT),
        ])
        .await
        .expect("the bulk deletion succeeds");
    assert!(rrsets.is_empty());
}

#[tokio::test]
async fn a_ttl_outside_the_accepted_range_never_reaches_the_network() {
    let (server, client) = mock().await;
    Mock::given(any())
        .respond_with(ResponseTemplate::new(500))
        .expect(0)
        .mount(&server)
        .await;

    let rrsets = client.rrsets("example.com");
    for ttl in [0, MAX_TTL + 1] {
        let err = rrsets
            .create(&NewRrset::new(www(), RecordType::A, ttl, ["127.0.0.1"]))
            .await
            .expect_err("the TTL is out of range");
        assert!(err.is_validation(), "ttl {ttl}: {err:?}");
        assert_eq!(err.status(), None, "ttl {ttl}: no response was involved");

        let err = rrsets
            .patch(&www(), &RecordType::A, &RrsetPatch::new().ttl(ttl))
            .await
            .expect_err("the TTL is out of range");
        assert!(err.is_validation(), "ttl {ttl}: {err:?}");
    }
}

#[tokio::test]
async fn a_wildcard_label_survives_both_the_path_and_the_body() {
    let (server, client) = mock().await;
    Mock::given(method("POST"))
        .and(path("/api/v1/domains/example.com/rrsets/"))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(serde_json::json!({
            "subname": "*.wild",
            "type": "A",
            "ttl": 3600,
            "records": ["127.0.0.1"],
        })))
        .respond_with(ResponseTemplate::new(201).set_body_json(rrset_json(
            "example.com",
            "*.wild",
            "A",
            3600,
            &["127.0.0.1"],
        )))
        .expect(1)
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/api/v1/domains/example.com/rrsets/*.wild/A/"))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(ResponseTemplate::new(200).set_body_json(rrset_json(
            "example.com",
            "*.wild",
            "A",
            3600,
            &["127.0.0.1"],
        )))
        .expect(1)
        .mount(&server)
        .await;

    let wildcard: Subname = "*.wild".parse().expect("a leftmost wildcard is valid");
    let rrsets = client.rrsets("example.com");
    let created = rrsets
        .create(&NewRrset::new(
            wildcard.clone(),
            RecordType::A,
            3600,
            ["127.0.0.1"],
        ))
        .await
        .expect("creation succeeds");
    assert_eq!(created.subname, wildcard);

    let fetched = rrsets
        .get(&wildcard, &RecordType::A)
        .await
        .expect("the RRset exists");
    assert_eq!(fetched.subname.as_path(), "*.wild");
}

#[tokio::test]
async fn a_record_type_this_crate_does_not_name_still_round_trips() {
    let (server, client) = mock().await;
    // A type added upstream must not break a client that has not been rebuilt.
    Mock::given(method("GET"))
        .and(path("/api/v1/domains/example.com/rrsets/pay/WALLET/"))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(ResponseTemplate::new(200).set_body_json(rrset_json(
            "example.com",
            "pay",
            "WALLET",
            3600,
            &["bc1qexample"],
        )))
        .expect(1)
        .mount(&server)
        .await;

    let unknown = RecordType::Other("WALLET".to_owned());
    let rrset = client
        .rrsets("example.com")
        .get(&"pay".parse().expect("valid subname"), &unknown)
        .await
        .expect("an unknown type deserializes");
    assert_eq!(rrset.record_type, unknown);
    assert_eq!(rrset.record_type.as_str(), "WALLET");
    assert_eq!(
        serde_json::to_value(&rrset.record_type).expect("serializes"),
        serde_json::json!("WALLET")
    );
}