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, }; #[tokio::test] async fn crew_manager_can_create_crew() { let background = Mofo::new(); let endr = endr::Node::new(background.clone(), MemoryStorageBackend::new()); let crew_manager: CrewManager = CrewManager::new( endr, background.clone(), Box::new(SimpleCredentialSource::new()), ); background .run_until(async { let (crew, _) = crew_manager.create_crew().await; println!("{:#?}", crew.current_state()); assert!(crew.get_shared_secret(CONTENT_SECRET).is_ok()); assert!(crew.get_shared_secret(PARTICIPATION_SECRET).is_ok()); assert!(crew .get_entrusted_info(PARTICIPATION_SECRET, SET_WRITE_ACCESS_INFO_ID) .is_ok()); }) .await; } #[tokio::test] async fn crew_manager_can_invite_reader_to_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, _) = crew_manager.create_crew().await; let invitation = crew.create_invitation(READ_INVITATION_ROLE).await.unwrap(); let (crew2, _) = crew_manager2.join_crew(&invitation).await; crew2.get_shared_secret(CONTENT_SECRET).unwrap(); assert!(crew2 .current_state() .unwrap() .roles .iter() .any(|(_, role)| role == READER_ROLE)) }) .await; } #[tokio::test] async fn crew_manager_can_invite_writer_to_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, _) = crew_manager.create_crew().await; let invitation = crew.create_invitation(WRITE_INVITATION_ROLE).await.unwrap(); let (crew2, _) = crew_manager2.join_crew(&invitation).await; crew2.get_shared_secret(CONTENT_SECRET).unwrap(); crew2.get_shared_secret(PARTICIPATION_SECRET).unwrap(); assert!(crew2 .current_state() .unwrap() .roles .iter() .any(|(_, role)| role == WRITER_ROLE)) }) .await; } #[tokio::test] async fn crew_manager_can_invite_admin_to_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, _) = crew_manager.create_crew().await; let invitation = crew.create_invitation(ADMIN_INVITATION_ROLE).await.unwrap(); let (crew2, _) = crew_manager2.join_crew(&invitation).await; crew2.get_shared_secret(CONTENT_SECRET).unwrap(); crew2.get_shared_secret(PARTICIPATION_SECRET).unwrap(); assert_eq!( crew2 .current_state() .unwrap() .roles .iter() .filter(|(_, role)| role == ADMIN_ROLE) .count(), 2 ) }) .await; }