use endr::MemoryStorageBackend; use mofo::Mofo; use crew_rs::{ CrewManager, SimpleCredentialSource, ADMIN_INVITATION_ROLE, ADMIN_ROLE, CONTENT_SECRET, PARTICIPATION_SECRET, READER_ROLE, READ_INVITATION_ROLE, SET_WRITE_ACCESS_INFO_ID, WRITER_ROLE, WRITE_INVITATION_ROLE, MemberCredential, CrewChange, AddRole, }; const USE_ADMIN_FROM_PARENTS: bool = false; #[tokio::test] async fn reader_should_have_reader_role_in_child_crew() { let background = Mofo::new(); let endr = endr::Node::new(background.clone(), MemoryStorageBackend::new()); let crew_manager: CrewManager = CrewManager::new( endr.clone(), background.clone(), Box::new(SimpleCredentialSource::new()), ); background .run_until(async { let (crew_1, _) = crew_manager.create_crew().await; let reader = MemberCredential::new_random(); crew_1.make_changes([ CrewChange::AddRole(AddRole { to: reader.pub_id(), role: READER_ROLE.to_owned(), }) ]).await.unwrap(); let (crew_2, _) = crew_manager.create_crew_with_parents([crew_1.id()], USE_ADMIN_FROM_PARENTS).await; loop { match crew_2.current_state().unwrap().roles_of(&reader.signer().pub_id()).contains(READER_ROLE) { true => break, false => { println!("Waiting"); tokio::time::sleep(std::time::Duration::from_millis(1)).await; } } } }).await; } #[tokio::test] async fn reader_should_have_reader_role_in_grand_child_crew() { let background = Mofo::new(); let endr = endr::Node::new(background.clone(), MemoryStorageBackend::new()); let crew_manager: CrewManager = CrewManager::new( endr.clone(), background.clone(), Box::new(SimpleCredentialSource::new()), ); background .run_until(async { let (crew_1, _) = crew_manager.create_crew().await; let reader = MemberCredential::new_random(); crew_1.make_changes([ CrewChange::AddRole(AddRole { to: reader.pub_id(), role: READER_ROLE.to_owned(), }) ]).await.unwrap(); let (crew_2, _) = crew_manager.create_crew_with_parents([crew_1.id()], USE_ADMIN_FROM_PARENTS).await; let (crew_3, _) = crew_manager.create_crew_with_parents([crew_2.id()], USE_ADMIN_FROM_PARENTS).await; loop { match crew_3.current_state().unwrap().roles_of(&reader.signer().pub_id()).contains(READER_ROLE) { true => break, false => { println!("Waiting"); tokio::time::sleep(std::time::Duration::from_millis(1)).await; } } } }).await; } #[tokio::test] async fn a_reader_in_a_parent_crew_can_access_content_secret_of_child_crew() { let background = Mofo::new(); let endr = endr::Node::new(background.clone(), MemoryStorageBackend::new()); let crew_manager: CrewManager = CrewManager::new( endr.clone(), background.clone(), Box::new(SimpleCredentialSource::new()), ); let crew_manager2: CrewManager = CrewManager::new( endr, background.clone(), Box::new(SimpleCredentialSource::new()), ); background .run_until(async { let (crew_1, _) = crew_manager.create_crew().await; let invitation = crew_1.create_invitation(ADMIN_INVITATION_ROLE).await.unwrap(); let (crew_1_joined, _) = crew_manager2.join_crew(&invitation).await; crew_1_joined.get_shared_secret(CONTENT_SECRET).unwrap(); let (crew_2, _) = crew_manager.create_crew_with_parents(Some(crew_1.id()), USE_ADMIN_FROM_PARENTS).await; let crew_2_joined = crew_manager2.load_crew(crew_2.id()).await; loop { match crew_2_joined.get_shared_secret(CONTENT_SECRET) { Ok(_) => break, Err(err) => { println!("Waiting: {}", err); tokio::time::sleep(std::time::Duration::from_millis(1)).await; } } } }).await; } #[tokio::test] async fn a_writer_in_a_parent_crew_can_access_content_and_participation_secret_of_child_crew() { let background = Mofo::new(); let endr = endr::Node::new(background.clone(), MemoryStorageBackend::new()); let crew_manager: CrewManager = CrewManager::new( endr.clone(), background.clone(), Box::new(SimpleCredentialSource::new()), ); let crew_manager2: CrewManager = CrewManager::new( endr, background.clone(), Box::new(SimpleCredentialSource::new()), ); background .run_until(async { let (crew_1, _) = crew_manager.create_crew().await; let invitation = crew_1.create_invitation(WRITE_INVITATION_ROLE).await.unwrap(); let (crew_1_joined, _) = crew_manager2.join_crew(&invitation).await; crew_1_joined.get_shared_secret(CONTENT_SECRET).unwrap(); crew_1_joined.get_shared_secret(PARTICIPATION_SECRET).unwrap(); let (crew_2, _) = crew_manager.create_crew_with_parents(Some(crew_1.id()), USE_ADMIN_FROM_PARENTS).await; let crew_2_joined = crew_manager2.load_crew(crew_2.id()).await; loop { match crew_2_joined.get_shared_secret(CONTENT_SECRET).and_then(|_| crew_2_joined.get_shared_secret(PARTICIPATION_SECRET)) { Ok(_) => break, Err(err) => { println!("Waiting: {}", err); tokio::time::sleep(std::time::Duration::from_millis(1)).await; } } } }).await; } #[tokio::test] async fn an_admin_in_a_parent_crew_can_access_content_and_participation_secret_of_child_crew() { let background = Mofo::new(); let endr = endr::Node::new(background.clone(), MemoryStorageBackend::new()); let crew_manager: CrewManager = CrewManager::new( endr.clone(), background.clone(), Box::new(SimpleCredentialSource::new()), ); let crew_manager2: CrewManager = CrewManager::new( endr, background.clone(), Box::new(SimpleCredentialSource::new()), ); background .run_until(async { let (crew_1, _) = crew_manager.create_crew().await; let invitation = crew_1.create_invitation(ADMIN_INVITATION_ROLE).await.unwrap(); let (crew_1_joined, _) = crew_manager2.join_crew(&invitation).await; crew_1_joined.get_shared_secret(CONTENT_SECRET).unwrap(); crew_1_joined.get_shared_secret(PARTICIPATION_SECRET).unwrap(); let (crew_2, _) = crew_manager.create_crew_with_parents(Some(crew_1.id()), USE_ADMIN_FROM_PARENTS).await; let crew_2_joined = crew_manager2.load_crew(crew_2.id()).await; loop { match crew_2_joined.get_shared_secret(CONTENT_SECRET).and_then(|_| crew_2_joined.get_shared_secret(PARTICIPATION_SECRET)) { Ok(_) => break, Err(err) => { println!("Waiting: {}", err); tokio::time::sleep(std::time::Duration::from_millis(1)).await; } } } }).await; } #[tokio::test] async fn a_reader_in_a_grand_parent_crew_can_access_content_secret_of_grand_child_crew() { let background = Mofo::new(); let endr = endr::Node::new(background.clone(), MemoryStorageBackend::new()); let crew_manager: CrewManager = CrewManager::new( endr.clone(), background.clone(), Box::new(SimpleCredentialSource::new()), ); let crew_manager2: CrewManager = CrewManager::new( endr, background.clone(), Box::new(SimpleCredentialSource::new()), ); background .run_until(async { let (crew_1, _) = crew_manager.create_crew().await; let invitation = crew_1.create_invitation(READ_INVITATION_ROLE).await.unwrap(); let (crew_1_joined, _) = crew_manager2.join_crew(&invitation).await; crew_1_joined.get_shared_secret(CONTENT_SECRET).unwrap(); let (crew_2, _) = crew_manager.create_crew_with_parents(Some(crew_1.id()), USE_ADMIN_FROM_PARENTS).await; let (crew_3, _) = crew_manager.create_crew_with_parents(Some(crew_2.id()), USE_ADMIN_FROM_PARENTS).await; let crew_3_joined = crew_manager2.load_crew(crew_3.id()).await; loop { match crew_3_joined.get_shared_secret(CONTENT_SECRET) { Ok(_) => break, Err(err) => { println!("Waiting: {}", err); tokio::time::sleep(std::time::Duration::from_millis(1)).await; } } } }).await; }