Cotton code
95 lines · 3.2 KB · RustRaw
//! The three functions `shareExpiry.test.ts` drives through routes, asked//! directly.//!//! The TypeScript has no unit file for `sharing/expiry.ts` — every one of its//! rules is asserted through `POST /v1/shares`, and the route suites here do//! the same. These cases pin the arithmetic underneath//! them, because a "whole number of days from 1 to 365" is a sentence four//! surfaces show and the one thing all four must agree on.use super::*;fn at(iso: &str) -> DateTime<Utc> {    DateTime::parse_from_rfc3339(iso)        .expect("a literal instant")        .with_timezone(&Utc)}const T0: &str = "2026-09-06T10:00:00Z";#[test]fn an_absent_field_is_the_seven_day_default_and_a_null_is_until_revoked() {    assert_eq!(parse_expires_in_days(None), Ok(Some(7)));    assert_eq!(parse_expires_in_days(Some(&Value::Null)), Ok(None));}#[test]fn a_whole_number_from_one_to_three_hundred_and_sixty_five_is_taken() {    for days in [1i64, 7, 30, 365] {        assert_eq!(            parse_expires_in_days(Some(&Value::from(days))),            Ok(Some(days))        );    }    // JavaScript reads `7.0` as the integer 7, and so does this: a body a    // browser serialised from a float must not be refused for a difference    // nobody can see.    assert_eq!(        parse_expires_in_days(Some(&serde_json::json!(7.0))),        Ok(Some(7))    );}#[test]fn zero_four_hundred_a_fraction_and_a_string_are_refused_with_one_sentence() {    let sentence =        "expiresInDays must be a whole number of days from 1 to 365, or null for a link that stays open until you revoke it";    for bad in [        serde_json::json!(0),        serde_json::json!(400),        serde_json::json!(2.5),        serde_json::json!("7"),        serde_json::json!(-1),        serde_json::json!(366),        serde_json::json!(true),    ] {        assert_eq!(            parse_expires_in_days(Some(&bad)),            Err(sentence.to_string()),            "{bad}"        );    }}#[test]fn expires_at_from_counts_whole_days_and_answers_nothing_for_until_revoked() {    assert_eq!(        expires_at_from(at(T0), Some(7)).as_deref(),        Some("2026-09-13T10:00:00.000Z")    );    assert_eq!(        expires_at_from(at(T0), Some(365)).as_deref(),        Some("2027-09-06T10:00:00.000Z")    );    assert_eq!(expires_at_from(at(T0), None), None);}#[test]fn nothing_never_expires_and_the_instant_itself_does() {    assert!(!is_expired(None, at(T0)));    // One second before: still open. AT the instant: closed — the TypeScript's    // `at <= now`, which is what makes "opens until 13 September" true of the    // whole of the twelfth.    assert!(!is_expired(Some("2026-09-06T10:00:01.000Z"), at(T0)));    assert!(is_expired(Some("2026-09-06T10:00:00.000Z"), at(T0)));    assert!(is_expired(Some("2026-09-06T09:59:59.000Z"), at(T0)));}#[test]fn a_value_that_is_not_a_date_reads_as_expired() {    // A column somebody edited by hand into junk must CLOSE the link, not open    // it for ever — the same direction every other doubt on this surface takes.    for junk in ["", "not-a-date", "2026-13-45", "yesterday"] {        assert!(is_expired(Some(junk), at(T0)), "{junk}");    }}