Skip to main content

pacenotes_shared/
key_status.rs

1/// The body of `GET /whoami` for a live access key; every other key is a 401,
2/// so the status alone tells a mistyped key from a good one, which `/packages`
3/// cannot, since it falls back to the public listing.
4#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
5pub struct KeyStatus {
6    /// Unix seconds; `None` never expires. Lets the installer warn before the key stops working.
7    pub expires_at: Option<i64>,
8}
9
10#[cfg(test)]
11mod tests {
12    #[test]
13    fn key_status_round_trips_with_and_without_expiry() {
14        for status in [
15            super::KeyStatus { expires_at: None },
16            super::KeyStatus {
17                expires_at: Some(1_900_000_000),
18            },
19        ] {
20            let json = serde_json::to_string(&status).unwrap();
21            let back: super::KeyStatus = serde_json::from_str(&json).unwrap();
22            assert_eq!(back, status);
23        }
24    }
25
26    /// `null` is the wire form of "never", not an absent field.
27    #[test]
28    fn a_key_that_never_expires_says_so_explicitly() {
29        assert_eq!(
30            serde_json::to_string(&super::KeyStatus { expires_at: None }).unwrap(),
31            r#"{"expires_at":null}"#
32        );
33    }
34}