diff --git a/crates/ghostlight-dungeon/src/catalog.rs b/crates/ghostlight-dungeon/src/catalog.rs index 8746f0f..6ec466d 100644 --- a/crates/ghostlight-dungeon/src/catalog.rs +++ b/crates/ghostlight-dungeon/src/catalog.rs @@ -490,4 +490,78 @@ mod sign_in_tests { let mut reopened = Catalog::open(directory.path().join("catalog.cc")).unwrap(); assert_eq!(reopened.sign_in("google:deru", None, now).unwrap().unwrap(), first); } + + #[test] + fn only_the_minted_token_redeems_its_invite_and_the_catalog_keeps_its_hash_alone() { + let (_d, mut catalog) = catalog(); + let now = Utc::now(); + let token = catalog.mint_invite(now, Duration::hours(1)).unwrap(); + assert_eq!(token.len(), 64); + assert!(token.chars().all(|c| c.is_ascii_hexdigit())); + assert_ne!(token, catalog.mint_invite(now, Duration::hours(1)).unwrap()); + for wrong in ["", "x", &token[1..], &format!("{token}0"), &token.to_uppercase()] { + assert_eq!( + catalog.sign_in("google:deru", Some(wrong), now).unwrap(), + Err(SignInRefusal::NoInvite), + "{wrong:?} must not redeem a live invite" + ); + } + assert!(catalog.state.invites.contains_key(&token_hash(&token))); + assert!(!catalog.state.invites.contains_key(&token), "the raw token is never stored"); + assert!(catalog.sign_in("google:deru", Some(&token), now).unwrap().is_ok()); + } + + #[test] + fn the_token_hash_is_sha256_hex() { + assert_eq!( + token_hash("abc"), + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" + ); + assert_ne!(token_hash("abc"), token_hash("abd")); + } + + #[test] + fn an_invite_dies_at_its_expiry_instant_and_spent_or_dead_invites_are_pruned() { + let (_d, mut catalog) = catalog(); + let now = Utc::now(); + let short = catalog.mint_invite(now, Duration::hours(1)).unwrap(); + let long = catalog.mint_invite(now, Duration::hours(3)).unwrap(); + let at_expiry = now + Duration::hours(1); + assert_eq!( + catalog.sign_in("a", Some(&short), at_expiry).unwrap(), + Err(SignInRefusal::NoInvite), + "an invite is not live at the instant it expires" + ); + assert!( + catalog.sign_in("a", Some(&short), at_expiry - Duration::seconds(1)).unwrap().is_ok(), + "and is live a second before" + ); + assert_eq!(catalog.state.invites.len(), 1, "the spent invite is gone"); + // Redeeming the long invite after the short one died prunes nothing live. + assert!(catalog.sign_in("b", Some(&long), at_expiry).unwrap().is_ok()); + assert!(catalog.state.invites.is_empty()); + // A mint prunes dead invites. + let stale = catalog.mint_invite(now, Duration::hours(1)).unwrap(); + let later = now + Duration::hours(2); + let fresh = catalog.mint_invite(later, Duration::hours(1)).unwrap(); + assert_eq!(catalog.state.invites.len(), 1); + assert!(!catalog.state.invites.contains_key(&token_hash(&stale))); + assert!(catalog.state.invites.contains_key(&token_hash(&fresh))); + assert_eq!(catalog.state.invites[&token_hash(&fresh)], later + Duration::hours(1)); + } + + #[test] + fn a_new_account_is_bound_to_its_identity_and_returns_it_as_the_principal() { + let (_d, mut catalog) = catalog(); + let now = Utc::now(); + let token = catalog.mint_invite(now, Duration::hours(1)).unwrap(); + let principal = catalog.sign_in("google:deru", Some(&token), now).unwrap().unwrap(); + assert_eq!(principal, "google:deru"); + let account_id = &catalog.state.identities["google:deru"]; + let account = &catalog.state.accounts[account_id]; + assert_eq!(&account.account_id, account_id); + assert_eq!(account.identities, vec!["google:deru".to_owned()]); + assert_eq!(account.created_at, now); + assert!(account_id.starts_with("account:")); + } }