diff --git a/src/core/identity.rs b/src/core/identity.rs index 8b8589a..1b7e908 100644 --- a/src/core/identity.rs +++ b/src/core/identity.rs @@ -18,7 +18,7 @@ use crate::core::botinstance::ArcBox; use std::rc::Rc; use std::cell::RefCell; -use std::sync::{Arc}; +use std::sync::Arc; use tokio::sync::RwLock; use casual_logger::{Level,Log}; @@ -1082,7 +1082,7 @@ pub enum ChatBadge { Mod, } - +#[derive(Debug,PartialEq, Eq)] pub enum ChangeResult { Success(String), Failed(String), @@ -1528,7 +1528,8 @@ impl IdentityManager { // .write().await // .push(UserRole::Mod(channel.clone())); - self.add_role(trgchatter.clone(), UserRole::Mod(channel.clone())).await; + self.affirm_chatter_in_db(authorizer.clone()).await; + self.add_role(authorizer.clone(), UserRole::Mod(channel.clone())).await; } @@ -2349,3 +2350,267 @@ impl IdentityManager { } + + +#[cfg(test)] +mod core_identity { + + + use casual_logger::Extension; + + use super::*; + + #[test] + fn user_role_identity() { + Log::set_file_ext(Extension::Log); + // Log::set_level(Level::Trace); + // let result = 2 + 2; + // assert_eq!(result, 4); + assert_eq!(UserRole::SupMod(ChType::Channel("strong".to_string())),UserRole::SupMod(ChType::Channel("Strong".to_lowercase()))); + } + + #[tokio::test] + async fn promote_workflow_01() { + Log::set_file_ext(Extension::Log); + // Log::set_level(Level::Trace); + + let test_id_mgr = IdentityManager::init(); + + // [x] Mod Attempts to Promote User + let channel = Some(ChType::Channel("twitchchanneltest".to_string())); + let trgchatter = "regularChatter".to_string(); + let authorizer_badge = &Some(ChatBadge::Mod); + let authorizer = "chatMod".to_string(); + let trg_role = None; + + + let rslt = test_id_mgr.promote(authorizer, authorizer_badge, trgchatter.clone(), channel.clone(), trg_role).await; + + assert_eq!(rslt,ChangeResult::Failed(String::from("You're not permitted to do that"))); + + } + + #[tokio::test] + async fn promote_workflow_02() { + + Log::set_file_ext(Extension::Log); + // Log::set_level(Level::Trace); + + let test_id_mgr = IdentityManager::init(); + + // [x] Broadcaster Promotes Chatter to SupMod + let channel = Some(ChType::Channel("broadcasterer".to_string())); + let trgchatter = "regularChatter".to_string(); + let authorizer_badge = &Some(ChatBadge::Broadcaster); + let authorizer = "broadcasterer".to_string(); + let trg_role = None; + + + let rslt = test_id_mgr.promote(authorizer.clone(), authorizer_badge, trgchatter.clone(), channel.clone(), trg_role.clone()).await; + + assert_eq!(rslt, ChangeResult::Success("Promotion Successful".to_string())); + + let rslt = test_id_mgr.getspecialuserroles(trgchatter.clone(), channel.clone()).await; + + assert!(rslt.contains(&UserRole::Mod(ChType::Channel("broadcasterer".to_string())))); + + let rslt = test_id_mgr.promote(authorizer.clone(), authorizer_badge, trgchatter.clone(), channel.clone(), trg_role.clone()).await; + + assert_eq!(rslt, ChangeResult::Success("Promotion Successful".to_string())); + + let rslt = test_id_mgr.getspecialuserroles(trgchatter.clone(), channel.clone()).await; + + assert!(rslt.contains(&UserRole::SupMod(ChType::Channel("broadcasterer".to_string())))); + + let rslt = test_id_mgr.promote(authorizer.clone(), authorizer_badge, trgchatter.clone(), channel.clone(), trg_role.clone()).await; + + assert_eq!(rslt, ChangeResult::Failed(String::from("Already highest available role"))); + + + } + + + + #[tokio::test] + async fn promote_workflow_03() { + + Log::set_file_ext(Extension::Log); + // Log::set_level(Level::Trace); + + let test_id_mgr = IdentityManager::init(); + + // [x] SupMod Promotes Chatter to SupMod + + // [x] Broadcaster first promotes a SupMod + + let broadcaster = "broadcasterer".to_string(); + let broadcaster_badge = &Some(ChatBadge::Broadcaster); + let channel = Some(ChType::Channel(broadcaster.clone())); + let supchatter = "superModerator".to_string(); + let trg_role = None; + + let rslt = test_id_mgr.promote(broadcaster.clone(), broadcaster_badge, supchatter.clone(), channel.clone(), trg_role.clone()).await; + + assert_eq!(rslt, ChangeResult::Success("Promotion Successful".to_string())); + + + let rslt = test_id_mgr.promote(broadcaster.clone(), broadcaster_badge, supchatter.clone(), channel.clone(), trg_role.clone()).await; + + assert_eq!(rslt, ChangeResult::Success("Promotion Successful".to_string())); + + let rslt = test_id_mgr.getspecialuserroles(supchatter.clone(), channel.clone()).await; + + assert!(rslt.contains(&UserRole::SupMod(channel.unwrap()))); + + + + // [x] SupMod Attempts to Promote Chatter to SupMod + + // let broadcaster = "broadcasterer".to_string(); + let authorizer = supchatter; + let authorizer_badge = &Some(ChatBadge::Broadcaster); + let channel = Some(ChType::Channel(broadcaster.clone())); + let trgchatter = "regularChatter".to_string(); + let trg_role = None; + + let rslt = test_id_mgr.promote(authorizer.clone(), authorizer_badge, trgchatter.clone(), channel.clone(), trg_role.clone()).await; + + assert_eq!(rslt, ChangeResult::Success("Promotion Successful".to_string())); + + let rslt = test_id_mgr.getspecialuserroles(trgchatter.clone(), channel.clone()).await; + + // assert!(rslt.contains(&UserRole::Mod(ChType::Channel("broadcasterer".to_string())))); + assert!(rslt.contains(&UserRole::Mod(channel.clone().unwrap()))); + + let rslt = test_id_mgr.promote(authorizer.clone(), authorizer_badge, trgchatter.clone(), channel.clone(), trg_role.clone()).await; + + // assert_eq!(rslt, ChangeResult::Success("Promotion Successful".to_string())); + assert_eq!(rslt, ChangeResult::Failed("You're not permitted to do that".to_string())); + + } + + #[tokio::test] + async fn promote_workflow_04() { + + Log::set_file_ext(Extension::Log); + // Log::set_level(Level::Trace); + + let test_id_mgr = IdentityManager::init(); + + // [x] BotAdmin Promotes Chatter to SupMod + + // [x] Create BotAdmin first + + let botadmin = "botadministrator".to_string(); + let botadmin_badge = &None; + + test_id_mgr.affirm_chatter_in_db(botadmin.clone()).await; + test_id_mgr.add_role(botadmin.clone(), UserRole::BotAdmin).await; + + let rslt = test_id_mgr.getspecialuserroles(botadmin.clone(), None).await; + + assert!(rslt.contains(&UserRole::BotAdmin)); + + + // [x] SupMod Attempts to Promote Chatter to SupMod + + // let broadcaster = "broadcasterer".to_string(); + let authorizer = botadmin; + let authorizer_badge = botadmin_badge; + let channel = Some(ChType::Channel("somechannel".to_string())); + let trgchatter = "regularChatter".to_string(); + let trg_role = None; + + let rslt = test_id_mgr.promote(authorizer.clone(), authorizer_badge, trgchatter.clone(), channel.clone(), trg_role.clone()).await; + + assert_eq!(rslt, ChangeResult::Success("Promotion Successful".to_string())); + + let rslt = test_id_mgr.getspecialuserroles(trgchatter.clone(), channel.clone()).await; + + assert!(rslt.contains(&UserRole::Mod(channel.clone().unwrap()))); + + let rslt = test_id_mgr.promote(authorizer.clone(), authorizer_badge, trgchatter.clone(), channel.clone(), trg_role.clone()).await; + + // assert_eq!(rslt, ChangeResult::Success("Promotion Successful".to_string())); + assert_eq!(rslt, ChangeResult::Success("Promotion Successful".to_string())); + + let rslt = test_id_mgr.getspecialuserroles(trgchatter.clone(), channel.clone()).await; + + assert!(rslt.contains(&UserRole::SupMod(channel.clone().unwrap()))); + + let rslt = test_id_mgr.promote(authorizer.clone(), authorizer_badge, trgchatter.clone(), channel.clone(), trg_role.clone()).await; + + assert_eq!(rslt, ChangeResult::Failed(String::from("Already highest available role"))); + + + } + + + #[tokio::test] + async fn demote_workflow_01() { + + Log::set_file_ext(Extension::Log); + // Log::set_level(Level::Trace); + + // [x] SupMod demotes a mod + + // [x] create a SupMod first + + let test_id_mgr = IdentityManager::init(); + + let supmod = "supmoder".to_string(); + // let supmod_badge = &None; + let channel = Some(ChType::Channel("somechannel".to_string())); + + test_id_mgr.affirm_chatter_in_db(supmod.clone()).await; + test_id_mgr.add_role(supmod.clone(), UserRole::SupMod(channel.clone().unwrap())).await; + + let rslt = test_id_mgr.getspecialuserroles(supmod.clone(), channel.clone()).await; + + assert!(rslt.contains(&UserRole::SupMod(channel.clone().unwrap()))); + + // [x] Create regular mod + + let regmod = "moder".to_string(); + // let supmod_badge = &None; + // let channel = Some(ChType::Channel("somechannel".to_string())); + + test_id_mgr.affirm_chatter_in_db(regmod.clone()).await; + test_id_mgr.add_role(regmod.clone(), UserRole::Mod(channel.clone().unwrap())).await; + + let rslt = test_id_mgr.getspecialuserroles(regmod.clone(), channel.clone()).await; + + assert!(rslt.contains(&UserRole::Mod(channel.clone().unwrap()))); + + // [x] Regular mod attempts to demote a supmod + + let authorizer = regmod.clone(); + let authorizer_badge = &None; + let trgchatter = supmod.clone(); + + + let rslt = test_id_mgr.demote(authorizer.clone(), authorizer_badge, trgchatter.clone(), channel.clone()).await; + + assert_eq!(rslt, ChangeResult::Failed("You're not permitted to do that".to_string())); + + // [x] SupMod demotes regular mod + + let authorizer = supmod; + let authorizer_badge = &None; + let trgchatter = regmod; + + let rslt = test_id_mgr.demote(authorizer.clone(), authorizer_badge, trgchatter.clone(), channel.clone()).await; + + assert_eq!(rslt, ChangeResult::Success("Demoted successfully".to_string())); + + + let rslt = test_id_mgr.demote(authorizer.clone(), authorizer_badge, trgchatter.clone(), channel.clone()).await; + + assert_eq!(rslt, ChangeResult::Failed("Target chatter does not have a role that can be demoted".to_string())); + + + + } + + +}