ic-bn-lib 0.10.2

Internet Computer Boundary Nodes shared modules
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
#![allow(clippy::option_if_let_else)]

use axum::{Json, http::StatusCode, response::IntoResponse};
use candid::Principal;
use derive_new::new;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[cfg(feature = "custom-domains-openapi")]
use utoipa::ToSchema;

use crate::custom_domains::base::{
    traits::{repository::RepositoryError, validation::ValidationError},
    types::domain::RegistrationStatus,
};

/// Generic API response structure for all endpoints.
#[derive(Serialize)]
#[cfg_attr(feature = "custom-domains-openapi", derive(ToSchema))]
pub struct ApiResponse<T> {
    /// Status of the response ("success" or "error")
    status: String,
    /// Optional human-readable message
    #[serde(skip_serializing_if = "Option::is_none")]
    message: Option<String>,
    /// Optional response data payload
    #[serde(skip_serializing_if = "Option::is_none")]
    data: Option<T>,
    /// Optional error details
    #[serde(skip_serializing_if = "Option::is_none")]
    errors: Option<String>,
}

/// API error types with associated details.
#[derive(Serialize, Deserialize, Debug, Clone, Error)]
pub enum ApiError {
    /// Invalid request data (400)
    #[error("bad_request: {0}")]
    BadRequest(String),
    /// Resource not found (404)
    #[error("not_found: {0}")]
    NotFound(String),
    /// Resource conflict (409)
    #[error("conflict: {0}")]
    Conflict(String),
    /// Request validation failed (422)
    #[error("unprocessable_entity: {0}")]
    UnprocessableEntity(String),
    /// Server error (500)
    #[error(
        "internal_server_error: An unexpected error occurred. Please try again later or contact support."
    )]
    InternalServerError(String),
}

/// Response data payload for domain creation/update.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(feature = "custom-domains-openapi", derive(ToSchema))]
pub struct CreateOrUpdateResponse {
    /// The domain name
    pub domain: String,
    /// Associated canister ID
    #[cfg_attr(
        feature = "custom-domains-openapi",
        schema(value_type = String, example = "rrkah-fqaaa-aaaaa-aaaaq-cai")
    )]
    pub canister_id: Principal,
}

/// Error response data payload.
#[derive(Serialize, Deserialize, Debug, Clone, new)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(feature = "custom-domains-openapi", derive(ToSchema))]
pub struct ErrorResponse {
    /// The domain name
    pub domain: String,
}

/// Delete response data payload.
#[derive(Serialize, Deserialize, Debug, Clone, new)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(feature = "custom-domains-openapi", derive(ToSchema))]
pub struct DeleteResponse {
    /// The domain name
    pub domain: String,
}

/// Get domains status response data payload.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(feature = "custom-domains-openapi", derive(ToSchema))]
pub struct GetStatusResponse {
    /// The domain name
    pub domain: String,
    /// Associated canister ID
    #[cfg_attr(
        feature = "custom-domains-openapi",
        schema(value_type = Option<String>, example = "rrkah-fqaaa-aaaaa-aaaaq-cai", nullable = true)
    )]
    pub canister_id: Option<Principal>,
    /// Domain registration status
    pub registration_status: RegistrationStatus,
}

/// Response data payload for domain-related endpoints.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(feature = "custom-domains-openapi", derive(ToSchema))]
pub struct ValidateResponse {
    /// The domain name
    pub domain: String,
    /// Associated canister ID
    #[cfg_attr(
        feature = "custom-domains-openapi",
        schema(value_type = String, example = "rrkah-fqaaa-aaaaa-aaaaq-cai")
    )]
    pub canister_id: Principal,
    /// Domain validation status
    pub validation_status: ValidationStatus,
}

impl From<RepositoryError> for ApiError {
    fn from(err: RepositoryError) -> Self {
        match err {
            RepositoryError::CertificateAlreadyIssued(domain) => Self::Conflict(format!(
                "Certificate for {domain} already exists; reissuance is not permitted."
            )),
            RepositoryError::AnotherTaskInProgress(domain) => Self::Conflict(format!(
                "Another task for {domain} is already in progress. Please retry after it completes."
            )),
            RepositoryError::DomainNotFound(domain) => {
                Self::NotFound(format!("Domain {domain} not found."))
            }
            RepositoryError::MissingCertificateForUpdate(domain) => Self::BadRequest(format!(
                "Cannot update domain-to-canister mapping: no valid certificate found for domain {domain}."
            )),
            _ => Self::InternalServerError("".to_string()),
        }
    }
}

// All validation errors should be converted to BadRequest
impl From<ValidationError> for ApiError {
    fn from(value: ValidationError) -> Self {
        Self::BadRequest(value.to_string())
    }
}

/// Creates a success response with the given data and message.
pub fn success_response<T: Serialize>(
    code: StatusCode,
    data: T,
    message: Option<String>,
) -> axum::response::Response {
    let json: Json<ApiResponse<T>> = Json(ApiResponse {
        status: "success".to_string(),
        message,
        data: Some(data),
        errors: None,
    });

    (code, json).into_response()
}

/// Creates an error response with the given error, data, and message.
pub fn error_response<T: Serialize>(
    error: ApiError,
    data: T,
    message: Option<String>,
) -> axum::response::Response {
    let code = match error {
        ApiError::BadRequest { .. } => StatusCode::BAD_REQUEST,
        ApiError::NotFound { .. } => StatusCode::NOT_FOUND,
        ApiError::Conflict { .. } => StatusCode::CONFLICT,
        ApiError::UnprocessableEntity { .. } => StatusCode::UNPROCESSABLE_ENTITY,
        ApiError::InternalServerError { .. } => StatusCode::INTERNAL_SERVER_ERROR,
    };

    let json: Json<ApiResponse<T>> = Json(ApiResponse {
        status: "error".to_string(),
        message,
        data: Some(data),
        errors: Some(error.to_string()),
    });

    (code, json).into_response()
}

/// Domain validation status for API responses.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(feature = "custom-domains-openapi", derive(ToSchema))]
pub enum ValidationStatus {
    /// Domain validation passed
    Valid,
    /// Domain validation failed with error details
    Invalid(String),
}

#[cfg(test)]
mod test {
    use std::str::FromStr;

    use axum::{body::to_bytes, http::header::CONTENT_TYPE};
    use fqdn::FQDN;
    use serde_json::{Value, json};

    use super::*;
    use crate::principal;

    const CANISTER_ID: &str = "rrkah-fqaaa-aaaaa-aaaaq-cai";
    const BODY_LIMIT: usize = 8 * 1024;
    /// The exact text `ApiError::InternalServerError` is allowed to expose.
    const MASKED_INTERNAL: &str = "internal_server_error: An unexpected error occurred. Please try again later or contact support.";

    fn fqdn(s: &str) -> FQDN {
        FQDN::from_str(s).unwrap()
    }

    async fn into_json(resp: axum::response::Response) -> (StatusCode, Value) {
        let status = resp.status();
        let body = to_bytes(resp.into_body(), BODY_LIMIT).await.unwrap();
        (status, serde_json::from_slice(&body).unwrap())
    }

    fn keys(v: &Value) -> Vec<&str> {
        let mut k = v
            .as_object()
            .unwrap()
            .keys()
            .map(String::as_str)
            .collect::<Vec<_>>();
        k.sort_unstable();
        k
    }

    #[tokio::test]
    async fn success_response_omits_message_and_errors_when_none() {
        // `message: None` and the always-`None` `errors` must not appear at all
        // (not as `null`) because of `skip_serializing_if`.
        let resp = success_response(StatusCode::OK, json!({"a": 1}), None);
        let (status, body) = into_json(resp).await;

        assert_eq!(status, StatusCode::OK);
        assert_eq!(keys(&body), vec!["data", "status"]);
        assert_eq!(body["status"], "success");
        assert_eq!(body["data"], json!({"a": 1}));
    }

    #[tokio::test]
    async fn success_response_uses_given_code_and_message_and_json_content_type() {
        let resp = success_response(
            StatusCode::ACCEPTED,
            DeleteResponse::new("example.org".to_string()),
            Some("accepted".to_string()),
        );

        assert_eq!(
            resp.headers().get(CONTENT_TYPE).unwrap(),
            "application/json"
        );

        let (status, body) = into_json(resp).await;
        assert_eq!(status, StatusCode::ACCEPTED);
        assert_eq!(keys(&body), vec!["data", "message", "status"]);
        assert_eq!(body["status"], "success");
        assert_eq!(body["message"], "accepted");
        assert_eq!(body["data"]["domain"], "example.org");
    }

    #[tokio::test]
    async fn error_response_maps_every_variant_to_its_status_code() {
        let cases = [
            (
                ApiError::BadRequest("bad".to_string()),
                StatusCode::BAD_REQUEST,
            ),
            (
                ApiError::NotFound("gone".to_string()),
                StatusCode::NOT_FOUND,
            ),
            (ApiError::Conflict("dupe".to_string()), StatusCode::CONFLICT),
            (
                ApiError::UnprocessableEntity("nope".to_string()),
                StatusCode::UNPROCESSABLE_ENTITY,
            ),
            (
                ApiError::InternalServerError("boom".to_string()),
                StatusCode::INTERNAL_SERVER_ERROR,
            ),
        ];

        for (err, expected_code) in cases {
            let expected_errors = err.to_string();
            let resp = error_response(
                err,
                ErrorResponse::new("example.org".to_string()),
                Some("failed".to_string()),
            );
            let (status, body) = into_json(resp).await;

            assert_eq!(status, expected_code);
            assert_eq!(keys(&body), vec!["data", "errors", "message", "status"]);
            assert_eq!(body["status"], "error");
            assert_eq!(body["message"], "failed");
            assert_eq!(body["data"]["domain"], "example.org");
            assert_eq!(body["errors"], expected_errors);
        }
    }

    #[tokio::test]
    async fn error_response_omits_message_when_none() {
        let resp = error_response(
            ApiError::NotFound("gone".to_string()),
            ErrorResponse::new("example.org".to_string()),
            None,
        );
        let (status, body) = into_json(resp).await;

        assert_eq!(status, StatusCode::NOT_FOUND);
        assert_eq!(keys(&body), vec!["data", "errors", "status"]);
        assert_eq!(body["errors"], "not_found: gone");
    }

    #[test]
    fn api_error_display_is_prefixed_with_snake_case_kind() {
        assert_eq!(
            ApiError::BadRequest("Domain is too long".to_string()).to_string(),
            "bad_request: Domain is too long"
        );
        assert_eq!(
            ApiError::NotFound("Domain x not found.".to_string()).to_string(),
            "not_found: Domain x not found."
        );
        assert_eq!(
            ApiError::Conflict("busy".to_string()).to_string(),
            "conflict: busy"
        );
        assert_eq!(
            ApiError::UnprocessableEntity("invalid".to_string()).to_string(),
            "unprocessable_entity: invalid"
        );
    }

    #[test]
    fn api_error_display_masks_internal_server_error_details() {
        // The inner string must never reach the client.
        let err = ApiError::InternalServerError(
            "postgres://user:hunter2@db/internal exploded".to_string(),
        );
        let rendered = err.to_string();

        assert_eq!(rendered, MASKED_INTERNAL);
        assert!(!rendered.contains("hunter2"));
        assert!(!rendered.contains("postgres"));
    }

    #[test]
    fn api_error_json_shape_is_externally_tagged_and_round_trips() {
        let err = ApiError::UnprocessableEntity("dns is wrong".to_string());
        assert_eq!(
            serde_json::to_value(&err).unwrap(),
            json!({"UnprocessableEntity": "dns is wrong"})
        );

        let back: ApiError = serde_json::from_value(json!({"NotFound": "nope"})).unwrap();
        assert!(matches!(back, ApiError::NotFound(ref x) if x == "nope"));
        assert_eq!(back.to_string(), "not_found: nope");

        assert!(serde_json::from_str::<ApiError>(r#"{"Teapot":"x"}"#).is_err());
    }

    #[test]
    fn repository_error_converts_to_expected_api_error() {
        let err = ApiError::from(RepositoryError::CertificateAlreadyIssued(fqdn(
            "example.org",
        )));
        assert!(matches!(err, ApiError::Conflict(_)));
        assert_eq!(
            err.to_string(),
            "conflict: Certificate for example.org already exists; reissuance is not permitted."
        );

        let err = ApiError::from(RepositoryError::AnotherTaskInProgress(fqdn("example.org")));
        assert!(matches!(err, ApiError::Conflict(_)));
        assert_eq!(
            err.to_string(),
            "conflict: Another task for example.org is already in progress. Please retry after it completes."
        );

        let err = ApiError::from(RepositoryError::DomainNotFound(fqdn("example.org")));
        assert!(matches!(err, ApiError::NotFound(_)));
        assert_eq!(err.to_string(), "not_found: Domain example.org not found.");

        let err = ApiError::from(RepositoryError::MissingCertificateForUpdate(fqdn(
            "example.org",
        )));
        assert!(matches!(err, ApiError::BadRequest(_)));
        assert_eq!(
            err.to_string(),
            "bad_request: Cannot update domain-to-canister mapping: no valid certificate found for domain example.org."
        );
    }

    #[test]
    fn repository_error_catch_all_becomes_masked_internal_error() {
        // Variants without an explicit mapping must fall through to a 500 that
        // carries no details from the original error.
        let unmapped = [
            RepositoryError::Unauthorized,
            RepositoryError::NonExistingTaskSubmitted(1_234_567_890),
            RepositoryError::InternalError(anyhow::anyhow!("secret db failure")),
        ];

        for repo_err in unmapped {
            let original = repo_err.to_string();
            let err = ApiError::from(repo_err);

            assert!(matches!(err, ApiError::InternalServerError(ref x) if x.is_empty()));
            assert_eq!(err.to_string(), MASKED_INTERNAL);
            assert!(!err.to_string().contains(&original));
        }
    }

    #[test]
    fn validation_error_always_converts_to_bad_request_with_full_text() {
        let err = ApiError::from(ValidationError::MultipleDnsTxtCanisterId {
            src: "_canister-id.example.org.".to_string(),
            records: vec!["a".to_string(), "b".to_string()],
        });
        assert!(matches!(err, ApiError::BadRequest(_)));
        assert_eq!(
            err.to_string(),
            r#"bad_request: multiple DNS TXT records for canister id at _canister-id.example.org.: ["a", "b"]"#
        );

        // Even an unexpected/internal validation failure is surfaced as 400.
        let err = ApiError::from(ValidationError::UnexpectedError(anyhow::anyhow!("uh oh")));
        assert!(matches!(err, ApiError::BadRequest(_)));
        assert_eq!(err.to_string(), "bad_request: uh oh");
    }

    #[test]
    fn validation_status_serializes_snake_case_and_round_trips() {
        assert_eq!(
            serde_json::to_value(ValidationStatus::Valid).unwrap(),
            json!("valid")
        );
        assert_eq!(
            serde_json::to_value(ValidationStatus::Invalid("no cname".to_string())).unwrap(),
            json!({"invalid": "no cname"})
        );

        let back: ValidationStatus = serde_json::from_value(json!("valid")).unwrap();
        assert!(matches!(back, ValidationStatus::Valid));

        let back: ValidationStatus =
            serde_json::from_value(json!({"invalid": "no cname"})).unwrap();
        assert!(matches!(back, ValidationStatus::Invalid(ref x) if x == "no cname"));

        // Unknown variants and the non-renamed spellings must be rejected.
        assert!(serde_json::from_value::<ValidationStatus>(json!("Valid")).is_err());
        assert!(serde_json::from_value::<ValidationStatus>(json!("bogus")).is_err());
        assert!(serde_json::from_value::<ValidationStatus>(json!({"invalid": 1})).is_err());
    }

    #[test]
    fn create_or_update_response_round_trips_via_textual_principal() {
        let resp = CreateOrUpdateResponse {
            domain: "example.org".to_string(),
            canister_id: principal!(CANISTER_ID),
        };

        let value = serde_json::to_value(&resp).unwrap();
        assert_eq!(
            value,
            json!({"domain": "example.org", "canister_id": CANISTER_ID})
        );

        let back: CreateOrUpdateResponse = serde_json::from_value(value).unwrap();
        assert_eq!(back.domain, "example.org");
        assert_eq!(back.canister_id, principal!(CANISTER_ID));
    }

    #[test]
    fn create_or_update_response_rejects_malformed_payloads() {
        // Missing field
        let err = serde_json::from_str::<CreateOrUpdateResponse>(r#"{"domain":"example.org"}"#)
            .unwrap_err();
        assert!(
            err.to_string().contains("canister_id"),
            "unexpected error: {err}"
        );

        // Unparseable principal
        let err = serde_json::from_str::<CreateOrUpdateResponse>(
            r#"{"domain":"example.org","canister_id":"not-a-principal"}"#,
        )
        .unwrap_err();
        assert!(err.is_data(), "unexpected error: {err}");

        // Wrong JSON type for the domain
        assert!(
            serde_json::from_str::<CreateOrUpdateResponse>(
                r#"{"domain":42,"canister_id":"rrkah-fqaaa-aaaaa-aaaaq-cai"}"#
            )
            .is_err()
        );

        // A principal encoded as raw bytes is not accepted by the JSON (human-readable) format
        assert!(
            serde_json::from_str::<CreateOrUpdateResponse>(
                r#"{"domain":"example.org","canister_id":[0,0,0,0,0,0,0,0,1,1]}"#
            )
            .is_err()
        );
    }

    #[test]
    fn get_status_response_nulls_missing_canister_id_and_masks_failure_reason() {
        let resp = GetStatusResponse {
            domain: "example.org".to_string(),
            canister_id: None,
            registration_status: RegistrationStatus::Failed("ACME order 4711 rejected".to_string()),
        };

        let raw = serde_json::to_string(&resp).unwrap();
        assert!(
            !raw.contains("4711"),
            "internal failure reason leaked: {raw}"
        );

        let value: Value = serde_json::from_str(&raw).unwrap();
        assert_eq!(
            keys(&value),
            vec!["canister_id", "domain", "registration_status"]
        );
        assert!(value["canister_id"].is_null());
        assert_eq!(
            value["registration_status"]["failed"],
            "An unexpected error occurred during registration. Please try again later or contact support."
        );

        // The masking makes the round-trip deliberately lossy.
        let back: GetStatusResponse = serde_json::from_value(value).unwrap();
        assert_eq!(
            back.registration_status,
            RegistrationStatus::Failed(
                "An unexpected error occurred during registration. Please try again later or contact support."
                    .to_string()
            )
        );
    }

    #[test]
    fn get_status_response_round_trips_non_failed_statuses() {
        for (status, wire) in [
            (RegistrationStatus::Registering, "registering"),
            (RegistrationStatus::Registered, "registered"),
            (RegistrationStatus::Expired, "expired"),
        ] {
            let resp = GetStatusResponse {
                domain: "тест.unicode.org".to_string(),
                canister_id: Some(principal!(CANISTER_ID)),
                registration_status: status.clone(),
            };

            let value = serde_json::to_value(&resp).unwrap();
            assert_eq!(value["registration_status"], json!(wire));
            assert_eq!(value["canister_id"], json!(CANISTER_ID));

            let back: GetStatusResponse = serde_json::from_value(value).unwrap();
            assert_eq!(back.domain, "тест.unicode.org");
            assert_eq!(back.canister_id, Some(principal!(CANISTER_ID)));
            assert_eq!(back.registration_status, status);
        }
    }

    #[test]
    fn delete_and_error_responses_expose_only_the_domain() {
        let value = serde_json::to_value(DeleteResponse::new("example.org".to_string())).unwrap();
        assert_eq!(value, json!({"domain": "example.org"}));

        let value = serde_json::to_value(ErrorResponse::new("example.org".to_string())).unwrap();
        assert_eq!(value, json!({"domain": "example.org"}));

        let back: DeleteResponse = serde_json::from_value(json!({"domain": "a.org"})).unwrap();
        assert_eq!(back.domain, "a.org");
        assert!(serde_json::from_str::<ErrorResponse>("{}").is_err());
    }

    #[test]
    fn validate_response_nests_validation_status() {
        let resp = ValidateResponse {
            domain: "example.org".to_string(),
            canister_id: principal!(CANISTER_ID),
            validation_status: ValidationStatus::Invalid("missing CNAME".to_string()),
        };

        let value = serde_json::to_value(&resp).unwrap();
        assert_eq!(
            value,
            json!({
                "domain": "example.org",
                "canister_id": CANISTER_ID,
                "validation_status": {"invalid": "missing CNAME"},
            })
        );

        let back: ValidateResponse = serde_json::from_value(value).unwrap();
        assert!(
            matches!(back.validation_status, ValidationStatus::Invalid(ref x) if x == "missing CNAME")
        );
    }

    #[tokio::test]
    async fn api_error_serialization_keeps_internal_details_that_display_hides() {
        // `Serialize` is derived and therefore does NOT mask the inner string - only
        // `Display` does. `error_response` renders via `Display`, so the HTTP body
        // must stay clean even though the enum itself round-trips losslessly.
        let secret = "postgres://user:hunter2@db exploded";
        let err = ApiError::InternalServerError(secret.to_string());

        assert_eq!(
            serde_json::to_value(&err).unwrap(),
            json!({"InternalServerError": secret})
        );

        let back: ApiError =
            serde_json::from_value(json!({"InternalServerError": secret})).unwrap();
        assert!(matches!(back, ApiError::InternalServerError(ref x) if x == secret));
        assert_eq!(back.to_string(), MASKED_INTERNAL);

        let resp = error_response(err, ErrorResponse::new("example.org".to_string()), None);
        let (status, body) = into_json(resp).await;
        assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
        assert_eq!(body["errors"], MASKED_INTERNAL);
        assert!(!body.to_string().contains("hunter2"), "leaked: {body}");
    }

    #[test]
    fn get_status_response_defaults_a_missing_canister_id_to_none() {
        // `canister_id` has no `#[serde(default)]`, but an absent `Option` field is
        // still `None`; an explicit `null` must behave identically.
        let back: GetStatusResponse =
            serde_json::from_str(r#"{"domain":"example.org","registration_status":"registered"}"#)
                .unwrap();
        assert_eq!(back.domain, "example.org");
        assert_eq!(back.canister_id, None);
        assert_eq!(back.registration_status, RegistrationStatus::Registered);

        let back: GetStatusResponse = serde_json::from_str(
            r#"{"domain":"example.org","canister_id":null,"registration_status":"registered"}"#,
        )
        .unwrap();
        assert_eq!(back.canister_id, None);
    }

    #[test]
    fn get_status_response_rejects_malformed_payloads() {
        // The status is mandatory.
        let err =
            serde_json::from_str::<GetStatusResponse>(r#"{"domain":"example.org"}"#).unwrap_err();
        assert!(
            err.to_string().contains("registration_status"),
            "unexpected error: {err}"
        );

        // Unknown variants, the non-renamed spelling, a wrongly typed `Failed`
        // payload, and the bare-string form of the newtype variant.
        for bad in [
            r#""pending""#,
            r#""Registered""#,
            r#""failed""#,
            r#"{"failed":1}"#,
            r#"{"Failed":"x"}"#,
        ] {
            assert!(
                serde_json::from_str::<GetStatusResponse>(&format!(
                    r#"{{"domain":"example.org","registration_status":{bad}}}"#
                ))
                .is_err(),
                "expected {bad} to be rejected"
            );
        }
    }

    #[test]
    fn validate_response_requires_every_field() {
        for bad in [
            r#"{"canister_id":"rrkah-fqaaa-aaaaa-aaaaq-cai","validation_status":"valid"}"#,
            r#"{"domain":"example.org","validation_status":"valid"}"#,
            r#"{"domain":"example.org","canister_id":"rrkah-fqaaa-aaaaa-aaaaq-cai"}"#,
            r#"{}"#,
        ] {
            assert!(
                serde_json::from_str::<ValidateResponse>(bad).is_err(),
                "expected {bad} to be rejected"
            );
        }
    }
}