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
762
763
764
765
766
767
768
769
770
//! Mocked tests for `/auth/tokens/` and its RRset scoping policies.
#![allow(clippy::expect_used)]

mod common;

use common::*;

use desec::api::tokens::{NewTokenPolicy, TokenPolicyPatch, TokenUpdate};
use desec::{DjangoDuration, RecordType, Secret, Subname};
use serde_json::json;
use uuid::Uuid;
use wiremock::matchers::{body_json, header, method, path, query_param, query_param_is_missing};
use wiremock::{Mock, ResponseTemplate};

const TOKEN_ID: &str = "3a6b94b5-d20e-40bd-a7cc-521f5c79fab3";
const OTHER_TOKEN_ID: &str = "f7ab039b-07b8-493d-ac61-4ddcf903d4de";
const POLICY_ID: &str = "7aed3f71-bc81-4f7e-90ae-8f0df0d1c211";
const OTHER_POLICY_ID: &str = "1b4a6f26-6f8d-4a25-9f3a-2a0dcb3e7c11";

fn token_id() -> Uuid {
    TOKEN_ID.parse().expect("valid uuid")
}

fn policy_id() -> Uuid {
    POLICY_ID.parse().expect("valid uuid")
}

fn tokens_path() -> String {
    "/api/v1/auth/tokens/".to_owned()
}

fn token_path() -> String {
    format!("/api/v1/auth/tokens/{TOKEN_ID}/")
}

fn policies_path() -> String {
    format!("/api/v1/auth/tokens/{TOKEN_ID}/policies/rrsets/")
}

fn policy_path() -> String {
    format!("/api/v1/auth/tokens/{TOKEN_ID}/policies/rrsets/{POLICY_ID}/")
}

#[tokio::test]
async fn creating_a_token_discloses_the_secret_but_keeps_it_out_of_debug() {
    let (server, client) = mock().await;
    Mock::given(method("POST"))
        .and(path(tokens_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({"name": "my token"})))
        .respond_with(ResponseTemplate::new(201).set_body_json(token_json(
            TOKEN_ID,
            "my token",
            Some(TOKEN),
        )))
        .expect(1)
        .mount(&server)
        .await;

    let token = client
        .tokens()
        .create(&TokenUpdate::new().name("my token"))
        .await
        .expect("a token");

    assert_eq!(token.token.as_ref().map(Secret::expose), Some(TOKEN));
    // Tokens get logged for auditing, so the one disclosure must not become two.
    assert!(!format!("{token:?}").contains(TOKEN));
}

#[tokio::test]
async fn a_provisioning_token_gets_its_domain_permissions_at_creation() {
    let (server, client) = mock().await;
    Mock::given(method("POST"))
        .and(path(tokens_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({
            "name": "provisioning",
            "perm_create_domain": true,
            "perm_delete_domain": true,
        })))
        .respond_with(ResponseTemplate::new(201).set_body_json(token_json(
            TOKEN_ID,
            "provisioning",
            Some(TOKEN),
        )))
        .expect(1)
        .mount(&server)
        .await;

    client
        .tokens()
        .create(
            &TokenUpdate::new()
                .name("provisioning")
                .perm_create_domain(true)
                .perm_delete_domain(true),
        )
        .await
        .expect("a token");
}

#[tokio::test]
async fn token_lifetimes_go_on_the_wire_as_django_durations() {
    let (server, client) = mock().await;
    Mock::given(method("POST"))
        .and(path(tokens_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({
            "max_age": "7 00:00:00",
            "max_unused_period": "01:00:00",
        })))
        .respond_with(ResponseTemplate::new(201).set_body_json(token_json(
            TOKEN_ID,
            "",
            Some(TOKEN),
        )))
        .expect(1)
        .mount(&server)
        .await;

    client
        .tokens()
        .create(
            &TokenUpdate::new()
                .max_age(DjangoDuration::days(7))
                .max_unused_period(DjangoDuration::hours(1)),
        )
        .await
        .expect("a token");
}

#[tokio::test]
async fn creating_a_token_can_restrict_subnets_and_enable_auto_policy() {
    let (server, client) = mock().await;
    Mock::given(method("POST"))
        .and(path(tokens_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({
            "allowed_subnets": ["203.0.113.0/24", "2001:db8::/32"],
            "auto_policy": true,
        })))
        .respond_with(ResponseTemplate::new(201).set_body_json(token_json(
            TOKEN_ID,
            "",
            Some(TOKEN),
        )))
        .expect(1)
        .mount(&server)
        .await;

    client
        .tokens()
        .create(
            &TokenUpdate::new()
                .allowed_subnets(["203.0.113.0/24", "2001:db8::/32"])
                .auto_policy(true),
        )
        .await
        .expect("a token");
}

#[tokio::test]
async fn creating_a_token_without_any_settings_posts_an_empty_object() {
    let (server, client) = mock().await;
    Mock::given(method("POST"))
        .and(path(tokens_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({})))
        .respond_with(ResponseTemplate::new(201).set_body_json(token_json(
            TOKEN_ID,
            "",
            Some(TOKEN),
        )))
        .expect(1)
        .mount(&server)
        .await;

    client
        .tokens()
        .create(&TokenUpdate::new())
        .await
        .expect("a token");
}

#[tokio::test]
async fn listing_tokens_sends_an_empty_cursor_and_yields_no_secrets() {
    let (server, client) = mock().await;
    // Omitting `cursor` is what makes the API answer `400 Pagination required`.
    Mock::given(method("GET"))
        .and(path(tokens_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(query_param("cursor", ""))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!([
            token_json(TOKEN_ID, "one", None),
            token_json(OTHER_TOKEN_ID, "two", None),
        ])))
        .expect(1)
        .mount(&server)
        .await;

    let page = client
        .tokens()
        .list()
        .send()
        .await
        .expect("a page of tokens");

    assert_eq!(page.items.len(), 2);
    assert!(page.items.iter().all(|token| token.token.is_none()));
    assert!(!page.has_next());
}

#[tokio::test]
async fn getting_a_token_addresses_it_by_lowercase_hyphenated_uuid() {
    let (server, client) = mock().await;
    Mock::given(method("GET"))
        .and(path(token_path()))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(token_json(TOKEN_ID, "my token", None)),
        )
        .expect(1)
        .mount(&server)
        .await;

    // Parsed from the uppercase spelling, to pin that the path is not built from Debug.
    let id: Uuid = TOKEN_ID.to_uppercase().parse().expect("valid uuid");
    let token = client.tokens().get(id).await.expect("a token");

    assert_eq!(token.id, token_id());
    assert_eq!(token.name, "my token");
}

#[tokio::test]
async fn try_get_maps_a_missing_token_onto_none() {
    let (server, client) = mock().await;
    Mock::given(method("GET"))
        .and(path(token_path()))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(ResponseTemplate::new(404).set_body_json(json!({"detail": "Not found."})))
        .expect(1)
        .mount(&server)
        .await;

    assert!(
        client
            .tokens()
            .try_get(token_id())
            .await
            .expect("a lookup")
            .is_none()
    );
}

#[tokio::test]
async fn renaming_a_token_leaves_its_permissions_untouched() {
    let (server, client) = mock().await;
    // An omitted field means "leave alone", so a rename must not carry permissions along.
    Mock::given(method("PATCH"))
        .and(path(token_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({"name": "x"})))
        .respond_with(ResponseTemplate::new(200).set_body_json(token_json(TOKEN_ID, "x", None)))
        .expect(1)
        .mount(&server)
        .await;

    client
        .tokens()
        .patch(token_id(), &TokenUpdate::new().name("x"))
        .await
        .expect("a token");
}

#[tokio::test]
async fn clearing_a_token_lifetime_sends_an_explicit_null() {
    let (server, client) = mock().await;
    // Without the null a lifetime could be set but never removed.
    Mock::given(method("PATCH"))
        .and(path(token_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({"max_age": null})))
        .respond_with(ResponseTemplate::new(200).set_body_json(token_json(TOKEN_ID, "", None)))
        .expect(1)
        .mount(&server)
        .await;

    client
        .tokens()
        .patch(token_id(), &TokenUpdate::new().clear_max_age())
        .await
        .expect("a token");
}

#[tokio::test]
async fn patching_nothing_sends_an_empty_object() {
    let (server, client) = mock().await;
    Mock::given(method("PATCH"))
        .and(path(token_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({})))
        .respond_with(ResponseTemplate::new(200).set_body_json(token_json(TOKEN_ID, "", None)))
        .expect(1)
        .mount(&server)
        .await;

    client
        .tokens()
        .patch(token_id(), &TokenUpdate::new())
        .await
        .expect("a token");
}

#[tokio::test]
async fn revoking_domain_creation_sends_false_rather_than_omitting_it() {
    let (server, client) = mock().await;
    Mock::given(method("PATCH"))
        .and(path(token_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({"perm_create_domain": false})))
        .respond_with(ResponseTemplate::new(200).set_body_json(token_json(TOKEN_ID, "", None)))
        .expect(1)
        .mount(&server)
        .await;

    client
        .tokens()
        .patch(token_id(), &TokenUpdate::new().perm_create_domain(false))
        .await
        .expect("a token");
}

#[tokio::test]
async fn replacing_a_token_uses_put() {
    let (server, client) = mock().await;
    Mock::given(method("PUT"))
        .and(path(token_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({"name": "reset"})))
        .respond_with(ResponseTemplate::new(200).set_body_json(token_json(TOKEN_ID, "reset", None)))
        .expect(1)
        .mount(&server)
        .await;

    client
        .tokens()
        .replace(token_id(), &TokenUpdate::new().name("reset"))
        .await
        .expect("a token");
}

#[tokio::test]
async fn deleting_a_token_accepts_a_204() {
    let (server, client) = mock().await;
    Mock::given(method("DELETE"))
        .and(path(token_path()))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(ResponseTemplate::new(204))
        .expect(1)
        .mount(&server)
        .await;

    client
        .tokens()
        .delete(token_id())
        .await
        .expect("a deletion");
}

#[tokio::test]
async fn patching_without_permission_surfaces_as_forbidden() {
    let (server, client) = mock().await;
    Mock::given(method("PATCH"))
        .and(path(token_path()))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(ResponseTemplate::new(403).set_body_json(json!({
            "detail": "You do not have permission to perform this action.",
        })))
        .expect(1)
        .mount(&server)
        .await;

    let err = client
        .tokens()
        .patch(token_id(), &TokenUpdate::new().name("x"))
        .await
        .expect_err("forbidden");

    assert!(err.is_forbidden(), "{err:?}");
    assert_eq!(
        err.api_error().expect("an api error").detail(),
        Some("You do not have permission to perform this action.")
    );
}

#[tokio::test]
async fn listing_policies_sends_no_cursor_because_the_endpoint_is_unpaginated() {
    let (server, client) = mock().await;
    // Upstream sets `pagination_class = None` here, so a cursor would be a stray parameter.
    Mock::given(method("GET"))
        .and(path(policies_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(query_param_is_missing("cursor"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!([
            policy_json(POLICY_ID, None, None, None, true),
            policy_json(
                OTHER_POLICY_ID,
                Some("example.com"),
                Some("www"),
                Some("A"),
                false,
            ),
        ])))
        .expect(1)
        .mount(&server)
        .await;

    let policies = client
        .tokens()
        .policies(token_id())
        .list()
        .await
        .expect("the policies");

    assert_eq!(policies.len(), 2);
    assert!(policies[0].is_default());
    assert!(!policies[1].is_default());
}

#[tokio::test]
async fn the_default_policy_posts_all_three_selectors_as_null() {
    let (server, client) = mock().await;
    // The all-null policy *is* the default one; dropping a null would scope it by accident.
    Mock::given(method("POST"))
        .and(path(policies_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({
            "domain": null,
            "subname": null,
            "type": null,
            "perm_write": true,
        })))
        .respond_with(
            ResponseTemplate::new(201)
                .set_body_json(policy_json(POLICY_ID, None, None, None, true)),
        )
        .expect(1)
        .mount(&server)
        .await;

    let policy = client
        .tokens()
        .policies(token_id())
        .create(&NewTokenPolicy::default_policy(true))
        .await
        .expect("the policy");

    assert!(policy.is_default());
}

#[tokio::test]
async fn a_domain_scoped_policy_keeps_its_wildcard_selectors_on_the_wire() {
    let (server, client) = mock().await;
    Mock::given(method("POST"))
        .and(path(policies_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({
            "domain": "example.com",
            "subname": null,
            "type": "TXT",
            "perm_write": true,
        })))
        .respond_with(ResponseTemplate::new(201).set_body_json(policy_json(
            POLICY_ID,
            Some("example.com"),
            None,
            Some("TXT"),
            true,
        )))
        .expect(1)
        .mount(&server)
        .await;

    let policy = client
        .tokens()
        .policies(token_id())
        .create(&NewTokenPolicy::for_domain("example.com", true).record_type(RecordType::TXT))
        .await
        .expect("the policy");

    assert_eq!(policy.record_type, Some(RecordType::TXT));
}

#[tokio::test]
async fn an_apex_scoped_policy_sends_the_apex_as_an_empty_string() {
    let (server, client) = mock().await;
    // The apex is `@` in a path but `""` in a body, and this is a body.
    Mock::given(method("POST"))
        .and(path(policies_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({
            "domain": "example.com",
            "subname": "",
            "type": null,
            "perm_write": false,
        })))
        .respond_with(ResponseTemplate::new(201).set_body_json(policy_json(
            POLICY_ID,
            Some("example.com"),
            Some(""),
            None,
            false,
        )))
        .expect(1)
        .mount(&server)
        .await;

    let policy = client
        .tokens()
        .policies(token_id())
        .create(&NewTokenPolicy::for_domain("example.com", false).subname(Subname::apex()))
        .await
        .expect("the policy");

    assert_eq!(policy.subname.as_ref().map(Subname::as_payload), Some(""));
}

#[tokio::test]
async fn a_second_default_policy_is_rejected_under_non_field_errors() {
    let (server, client) = mock().await;
    Mock::given(method("POST"))
        .and(path(policies_path()))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(ResponseTemplate::new(400).set_body_json(json!({
            "non_field_errors": ["Cannot create multiple default policies."],
        })))
        .expect(1)
        .mount(&server)
        .await;

    let err = client
        .tokens()
        .policies(token_id())
        .create(&NewTokenPolicy::default_policy(true))
        .await
        .expect_err("the policy is rejected");

    assert!(err.is_validation(), "{err:?}");
    assert_eq!(
        err.api_error().expect("an api error").non_field_errors(),
        vec!["Cannot create multiple default policies."]
    );
}

#[tokio::test]
async fn getting_a_policy_addresses_it_below_its_token() {
    let (server, client) = mock().await;
    Mock::given(method("GET"))
        .and(path(policy_path()))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(ResponseTemplate::new(200).set_body_json(policy_json(
            POLICY_ID,
            Some("example.com"),
            None,
            None,
            true,
        )))
        .expect(1)
        .mount(&server)
        .await;

    let policy = client
        .tokens()
        .policies(token_id())
        .get(policy_id())
        .await
        .expect("the policy");

    assert_eq!(policy.id, policy_id());
    assert_eq!(policy.domain.as_deref(), Some("example.com"));
}

#[tokio::test]
async fn try_get_maps_a_missing_policy_onto_none() {
    let (server, client) = mock().await;
    Mock::given(method("GET"))
        .and(path(policy_path()))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(ResponseTemplate::new(404).set_body_json(json!({"detail": "Not found."})))
        .expect(1)
        .mount(&server)
        .await;

    assert!(
        client
            .tokens()
            .policies(token_id())
            .try_get(policy_id())
            .await
            .expect("a lookup")
            .is_none()
    );
}

#[tokio::test]
async fn revoking_write_permission_on_a_policy_sends_only_perm_write_false() {
    let (server, client) = mock().await;
    // Omitting the field preserves the old value, so a dropped `false` never revokes.
    Mock::given(method("PATCH"))
        .and(path(policy_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({"perm_write": false})))
        .respond_with(ResponseTemplate::new(200).set_body_json(policy_json(
            POLICY_ID,
            Some("example.com"),
            None,
            None,
            false,
        )))
        .expect(1)
        .mount(&server)
        .await;

    let policy = client
        .tokens()
        .policies(token_id())
        .patch(policy_id(), &TokenPolicyPatch::new().perm_write(false))
        .await
        .expect("the policy");

    assert!(!policy.perm_write);
}

#[tokio::test]
async fn rescoping_a_policy_does_not_resend_its_write_permission() {
    let (server, client) = mock().await;
    // Sending all four fields would turn this PATCH into a PUT and silently revoke write.
    Mock::given(method("PATCH"))
        .and(path(policy_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({"domain": "x"})))
        .respond_with(ResponseTemplate::new(200).set_body_json(policy_json(
            POLICY_ID,
            Some("x"),
            None,
            None,
            true,
        )))
        .expect(1)
        .mount(&server)
        .await;

    let policy = client
        .tokens()
        .policies(token_id())
        .patch(policy_id(), &TokenPolicyPatch::new().domain("x"))
        .await
        .expect("the policy");

    assert!(policy.perm_write);
}

#[tokio::test]
async fn widening_a_policy_to_any_domain_sends_a_null_domain() {
    let (server, client) = mock().await;
    Mock::given(method("PATCH"))
        .and(path(policy_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({"domain": null})))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(policy_json(POLICY_ID, None, None, None, true)),
        )
        .expect(1)
        .mount(&server)
        .await;

    client
        .tokens()
        .policies(token_id())
        .patch(policy_id(), &TokenPolicyPatch::new().any_domain())
        .await
        .expect("the policy");
}

#[tokio::test]
async fn replacing_a_policy_uses_put() {
    let (server, client) = mock().await;
    Mock::given(method("PUT"))
        .and(path(policy_path()))
        .and(header("authorization", auth_header().as_str()))
        .and(body_json(json!({
            "domain": "example.com",
            "subname": null,
            "type": null,
            "perm_write": false,
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(policy_json(
            POLICY_ID,
            Some("example.com"),
            None,
            None,
            false,
        )))
        .expect(1)
        .mount(&server)
        .await;

    client
        .tokens()
        .policies(token_id())
        .replace(
            policy_id(),
            &NewTokenPolicy::for_domain("example.com", false),
        )
        .await
        .expect("the policy");
}

#[tokio::test]
async fn deleting_a_policy_accepts_a_204() {
    let (server, client) = mock().await;
    Mock::given(method("DELETE"))
        .and(path(policy_path()))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(ResponseTemplate::new(204))
        .expect(1)
        .mount(&server)
        .await;

    client
        .tokens()
        .policies(token_id())
        .delete(policy_id())
        .await
        .expect("a deletion");
}

#[tokio::test]
async fn a_policy_scoped_to_a_subname_with_any_type_round_trips() {
    let (server, client) = mock().await;
    Mock::given(method("GET"))
        .and(path(policy_path()))
        .and(header("authorization", auth_header().as_str()))
        .respond_with(ResponseTemplate::new(200).set_body_json(policy_json(
            POLICY_ID,
            Some("example.com"),
            Some("_acme-challenge"),
            None,
            true,
        )))
        .expect(1)
        .mount(&server)
        .await;

    let policy = client
        .tokens()
        .policies(token_id())
        .get(policy_id())
        .await
        .expect("the policy");

    assert_eq!(
        policy.subname.as_ref().map(Subname::as_payload),
        Some("_acme-challenge")
    );
    assert_eq!(policy.record_type, None);
    assert!(!policy.is_default());
}