use cotton_store::{ Driver, membership::{self, Rights}, migrate::migrate_store, }; use rusqlite::types::Value; const DATABASE: &str = "11111111-1111-4111-8111-111111111111"; const NOW: &str = "2026-10-19T12:00:00Z"; fn text(value: &str) -> Value { Value::Text(value.into()) } fn stamp(n: u32) -> String { format!("1700000000000:{n:04}:serving") } fn admit(db: &Driver, n: u32, person: &str, rights: &str, by: &str) { // Synthetic serving facts, as in membership.rs: signatures are checked // by the server writer before this reader sees an admission. db.run("INSERT INTO cotton_admissions(id,workspace_id,person_key,person_name,rights,admitted_by_person,p_sig,admitted_at,_hlc) VALUES(?,?,?,'Synthetic person',?,?,'c2ln',?,?)", &[text(&format!("admission-{n}")),text(DATABASE),text(person),text(rights),text(by),text(NOW),text(&stamp(n))]).unwrap(); } fn state(db: &Driver, n: u32, device: &str) { db.run("INSERT INTO cotton_device_statements(id,workspace_id,person_key,device_key,vault_public_key,device_name,p_sig,d_sig,made_at,_hlc) VALUES(?,?,'alice',?,'synthetic-vault','Synthetic phone','c2ln','c2ln',?,?)", &[text(&format!("statement-{n}")),text(DATABASE),text(device),text(NOW),text(&stamp(n))]).unwrap(); } // RULE: A person revocation permanently ends its old device statements, including after an owner returns and later becomes staff again. // Landing b6bd38377: membership::device -> standing in src/membership.rs // returns since=None whenever last_owned.stamp > ended. A returning owner's // later demotion thus revives a statement denied immediately before it. // auth.rs::row_in_named_file consumes this reader; this guard isolates the // person-life boundary (an additional device-key revocation still bars a key). #[test] fn demoting_a_returning_owner_must_not_revive_devices_from_before_leaves() { let db = Driver::open_in_memory().unwrap(); migrate_store(&db).unwrap(); db.run("INSERT INTO cotton_workspaces(id,name,owner_user_id,created_at) VALUES(?,'Synthetic studio','synthetic-owner',?)", &[text(DATABASE),text(NOW)]).unwrap(); admit(&db, 0, "alice", "owner", "alice"); state(&db, 1, "old-alice-phone"); admit(&db, 2, "ben", "owner", "alice"); db.run("INSERT INTO cotton_revocations(id,workspace_id,subject_table,subject_id,revoked_at,actor,reason,_hlc) VALUES('leaves',?,'person','alice',?,'synthetic-owner','revoked',?)", &[text(DATABASE),text(NOW),text(&stamp(3))]).unwrap(); assert_eq!(membership::device(&db, "old-alice-phone").unwrap(), None); admit(&db, 4, "alice", "owner", "ben"); assert_eq!(membership::device(&db, "old-alice-phone").unwrap(), None); state(&db, 5, "new-alice-phone"); assert_eq!( membership::device(&db, "new-alice-phone").unwrap(), Some(("alice".into(), Rights::Owner)) ); admit(&db, 6, "ben", "owner", "alice"); admit(&db, 7, "alice", "member", "ben"); assert_eq!(membership::of(&db, "alice").unwrap(), Some(Rights::Member)); assert_eq!( membership::device(&db, "new-alice-phone").unwrap(), Some(("alice".into(), Rights::Member)) ); assert_eq!( membership::device(&db, "old-alice-phone").unwrap(), None, "a demotion must preserve the returning owner's membership-life boundary" ); }