From 8b4c21ba58604332c307cd94e0d4bc8cd4d32e83 Mon Sep 17 00:00:00 2001 From: ModulatingForce <116608425+modulatingforce@users.noreply.github.com> Date: Fri, 22 Mar 2024 17:06:09 -0400 Subject: [PATCH] unit tests --- src/core/botmodules.rs | 904 ++++++++++++++++++++++++++++++++++++++++- src/core/identity.rs | 6 +- 2 files changed, 886 insertions(+), 24 deletions(-) diff --git a/src/core/botmodules.rs b/src/core/botmodules.rs index cc4b1aa..a3866dd 100644 --- a/src/core/botmodules.rs +++ b/src/core/botmodules.rs @@ -28,7 +28,7 @@ use std::sync::Arc; // use futures::stream::iter; use twitch_irc::message::PrivmsgMessage; -// use casual_logger::Log; +use casual_logger::Log; use tokio::sync::RwLock; @@ -38,7 +38,7 @@ use self::bot_actions::actions_util::BotAR; use crate::core::bot_actions::actions_util; use crate::core::botinstance::{BotInstance, ChType,ChangeResult}; use crate::core::botlog; -use crate::core::identity::{self, Permissible}; +use crate::core::identity::{self, Permissible,IdentityManager}; use crate::core::bot_actions; pub use ChType::Channel; @@ -598,7 +598,8 @@ impl ModulesManager { trg_module: ModType, // channel: Option, trg_level: StatusLvl, - bot: BotAR, + // bot: BotAR, + id: Arc>, ) -> ChangeResult { @@ -631,8 +632,20 @@ impl ModulesManager { */ - let botlock = bot.read().await; - let id = botlock.get_identity(); + // [x] Validate in trg_module first + + // let botlock = bot.read().await; + // let modmgr = Arc::clone(&botlock.botmodules); + let modlist = self.moduleslist().await; + let rslt = modlist.get(&trg_module); + + if let None = rslt { + return ChangeResult::Failed("Module doesn't exist".to_string()); + } + + + // let botlock = bot.read().await; + // let id = botlock.get_identity(); let mut idlock = id.write().await; // if trg_level = StatusLvl::Instance , the temp_chnl = the broadcaster's or the chatter's @@ -657,17 +670,37 @@ impl ModulesManager { identity::UserRole::Broadcaster, ]).await; + + // botlog::debug( + // &format!("FAILURE BEFORE Let statements involves : + // admin_level_access : {:?} ; chnl_elevated_access : {:?}", + // admin_level_access , chnl_elevated_access), + // Some("botmodules.rs > exec_enable()".to_string()), + // None, + // ); + + + if let Permissible::Allow = admin_level_access { if let Permissible::Block = chnl_elevated_access { + + botlog::debug( + &format!("?? REACHED INNER TIER : + admin_level_access : {:?} ; chnl_elevated_access : {:?}", + admin_level_access , chnl_elevated_access), + Some("botmodules.rs > exec_enable()".to_string()), + None, + ); match trg_level { StatusLvl::Instance => { self.set_instance_enabled(trg_module.clone()).await; - ChangeResult::Success("Enabled at Instance Level".to_string()) + return ChangeResult::Success("Enabled at Instance Level".to_string()); }, StatusLvl::Ch(_) => { - ChangeResult::Failed("Promote yourself Temporarily First".to_string()) + return ChangeResult::Failed("Promote yourself Temporarily First".to_string()); }, }; + } } @@ -684,11 +717,11 @@ impl ModulesManager { if let Permissible::Allow = chnl_elevated_access { match trg_level.clone() { StatusLvl::Instance => { - ChangeResult::Failed("You're not allowed".to_string()) + return ChangeResult::Failed("You're not allowed".to_string()); }, StatusLvl::Ch(in_chnl) => { self.set_ch_enabled(trg_module.clone(), in_chnl).await; - ChangeResult::Success("Enabled at Channel Level".to_string()) + return ChangeResult::Success("Enabled at Channel Level".to_string()); }, }; } @@ -709,17 +742,39 @@ impl ModulesManager { match trg_level { StatusLvl::Instance => { self.set_instance_enabled(trg_module.clone()).await; - ChangeResult::Success("Enabled at Instance Level".to_string()) + return ChangeResult::Success("Enabled at Instance Level".to_string()); }, StatusLvl::Ch(in_chnl) => { self.set_ch_enabled(trg_module.clone(), in_chnl).await; - ChangeResult::Success("Enabled at Channel Level".to_string()) + return ChangeResult::Success("Enabled at Channel Level".to_string()); }, }; } } + // Respond in case of General Chatter + // The below should NOT be required , as current internal logic would prevent + // a BotCommand to be ran by a Chatter if it requires any special roles and + // that chatter does not have htose roles + // However, below is added to satisfy unit tests + + + if let Permissible::Block = admin_level_access { + if let Permissible::Block = chnl_elevated_access { + match trg_level { + StatusLvl::Instance => { + return ChangeResult::Failed("You're not allowed".to_string()); + }, + StatusLvl::Ch(_) => { + return ChangeResult::Failed("You're not allowed".to_string()); + }, + }; + } + } + + + // ======================= @@ -779,8 +834,17 @@ impl ModulesManager { // } + botlog::debug( + &format!("FAILURE involves : + admin_level_access : {:?} ; chnl_elevated_access : {:?}", + admin_level_access , chnl_elevated_access), + Some("botmodules.rs > exec_enable()".to_string()), + None, + ); + Log::flush(); + ChangeResult::Failed("ERROR : Not implemented yet".to_string()) } @@ -793,7 +857,8 @@ impl ModulesManager { // channel: Option, trg_level: StatusLvl, force: bool, - bot: BotAR, + // bot: BotAR, + id: Arc>, ) -> ChangeResult { @@ -818,10 +883,21 @@ impl ModulesManager { 3c. , and is -f (forced) , return a Success */ + // [x] Validate in trg_module first + + // let botlock = bot.read().await; + // let modmgr = Arc::clone(&botlock.botmodules); + let modlist = self.moduleslist().await; + let rslt = modlist.get(&trg_module); + + if let None = rslt { + return ChangeResult::Failed("Module doesn't exist".to_string()); + } + - let botlock = bot.read().await; - let id = botlock.get_identity(); + // let botlock = bot.read().await; + // let id = botlock.get_identity(); let mut idlock = id.write().await; // if trg_level = StatusLvl::Instance , the temp_chnl = the broadcaster's or the chatter's @@ -866,10 +942,10 @@ impl ModulesManager { match trg_level { StatusLvl::Instance => { self.set_instance_disabled(trg_module.clone()).await; - ChangeResult::Success("Disabled at Instance Level".to_string()) + return ChangeResult::Success("Disabled at Instance Level".to_string()); }, StatusLvl::Ch(_) => { - ChangeResult::Failed("Promote yourself Temporarily First".to_string()) + return ChangeResult::Failed("Promote yourself Temporarily First".to_string()); }, }; } @@ -893,11 +969,11 @@ impl ModulesManager { } else { match trg_level.clone() { StatusLvl::Instance => { - ChangeResult::Failed("You're not allowed".to_string()) + return ChangeResult::Failed("You're not allowed".to_string()); }, StatusLvl::Ch(in_chnl) => { self.set_ch_disabled(trg_module.clone(), in_chnl).await; - ChangeResult::Success("Disabled at Channel Level".to_string()) + return ChangeResult::Success("Disabled at Channel Level".to_string()); }, }; } @@ -923,11 +999,11 @@ impl ModulesManager { match trg_level { StatusLvl::Instance => { self.set_instance_disabled(trg_module.clone()).await; - ChangeResult::Success("Disabled at Instance Level".to_string()) + return ChangeResult::Success("Disabled at Instance Level".to_string()); }, StatusLvl::Ch(in_chnl) => { self.set_ch_disabled(trg_module.clone(), in_chnl).await; - ChangeResult::Success("Disabled at Channel Level".to_string()) + return ChangeResult::Success("Disabled at Channel Level".to_string()); }, }; } @@ -935,6 +1011,29 @@ impl ModulesManager { } + + // Respond in case of General Chatter + // The below should NOT be required , as current internal logic would prevent + // a BotCommand to be ran by a Chatter if it requires any special roles and + // that chatter does not have htose roles + // However, below is added to satisfy unit tests + + + if let Permissible::Block = admin_level_access { + if let Permissible::Block = chnl_elevated_access { + match trg_level { + StatusLvl::Instance => { + return ChangeResult::Failed("You're not allowed".to_string()); + }, + StatusLvl::Ch(_) => { + return ChangeResult::Failed("You're not allowed".to_string()); + }, + }; + } + } + + + ChangeResult::Failed("ERROR : Not implemented yet".to_string()) } @@ -1301,8 +1400,8 @@ mod core_modulesmanager { fn case_insensitive_test() { Log::set_file_ext(Extension::Log); assert_eq!( - BotModule("Test".to_string()), - BotModule("Test".to_lowercase()) + BotModule("TEST".to_string()), + BotModule("test".to_string()) ); } @@ -1545,4 +1644,765 @@ mod core_modulesmanager { } + + + /* + 1. Create new ModulesManager & Identity Manager + 2. modmgr.affirm_in_statusdb(Experiments01,Custom) + + 3. affirm when BotAdmin attempts to exec_enable on the following + a. Channel Level , where they are not a Mod + b. Channel Level , when they are a Mod + c. Instance Level + 4. affirm when BotAdmin attempts to exec_disable on the following + a. Channel Level , where they are not a Mod + b. Channel Level , when they are a Mod + c. Instance Level + d. force disable + + + 1. Create new ModulesManager & Identity Manager + 2. modmgr.affirm_in_statusdb(Experiments01,Custom) + + 3. affirm when Mod attempts to exec_enable on the following + a. Channel Level , where they are not a Mod + b. Channel Level , when they are a Mod + c. Instance Level + 4. affirm when Mod attempts to exec_disable on the following + a. Channel Level , where they are not a Mod + b. Channel Level , when they are a Mod + c. Instance Level + d. force disable + + */ + + async fn inner_enable_disable_complex( + requestor:String, + channel:ChType, + idmgr:IdentityManager, + modsmgr:Arc) + { + + /* + Parent Tests would involve : + - Testing with a BotAdmin User + - Testing with a Mod User + - Testing with a Regular Chatter + */ + + enum TestScenarios { + BotadminUser, + ModUser, + RegularChatter, + // ModuleDoesNotExist, // preferring instead to handle in it's own smaller test + } + + + // let channel = ChType::Channel("somechannel".to_string()); + + + // let mut idlock = idmgr.write().await; + let mut idlock = idmgr.clone(); + + // if trg_level = StatusLvl::Instance , the temp_chnl = the broadcaster's or the chatter's + + // let arb_chnl = match trg_level.clone() { + // StatusLvl::Instance => ChType::Channel(requestor.to_lowercase()), + // StatusLvl::Ch(a) => a, + // }; + + let requestor_badge = None; // If they are a Mod on the Given Channel already, that can be evaluated without the current badge + + const OF_CMD_CHANNEL:ChType = Channel(String::new()); + + let (admin_level_access,_) = idlock.can_user_run(requestor.clone(), channel.clone(), requestor_badge.clone(), + vec![ + identity::UserRole::BotAdmin, + ]).await; + + + let (chnl_elevated_access,_) = idlock.can_user_run(requestor.clone(), channel.clone(), requestor_badge.clone(), + vec![ + identity::UserRole::Mod(OF_CMD_CHANNEL), + identity::UserRole::SupMod(OF_CMD_CHANNEL), + identity::UserRole::Broadcaster, + ]).await; + + + let current_test_scenario = + match admin_level_access { + Permissible::Allow => { + match chnl_elevated_access { + Permissible::Allow => { TestScenarios::BotadminUser }, + Permissible::Block => { TestScenarios::BotadminUser } + } + }, + Permissible::Block => { + match chnl_elevated_access { + Permissible::Allow => { TestScenarios::ModUser }, + Permissible::Block => { TestScenarios::RegularChatter } + } + } + }; + + + + // [x] 2. modmgr.affirm_in_statusdb(Experiments01,Custom) + + let in_module = BotModule("Experiments01".to_string()); + let in_modgroup = ModGroup::Custom; + + // match current_test_scenario { + // TestScenarios::BotadminUser => modsmgr.affirm_in_statusdb(in_module.clone(), in_modgroup.clone()).await, + // _ => self::panic!("Scenario not handled"), + // } + + modsmgr.affirm_in_statusdb(in_module.clone(), in_modgroup.clone()).await; + + let in_module = match current_test_scenario { + // TestScenarios::ModuleDoesNotExist => BotModule("NonExisting_Module".to_string()), + _ => in_module, + }; + + + /* + [x] 3. affirm when BotAdmin attempts to exec_enable on the following + a. Channel Level , where they are not a Mod + */ + + + // [-] requestor_badge: Option, + + // [x] trg_module: ModType, + let trg_module = in_module; + + // [x] trg_level: StatusLvl, + + let trg_level = StatusLvl::Ch(channel.clone()); // setting to Channel Level + + + // [x] id: Arc>, + let id = Arc::new(RwLock::new(idmgr.clone())); + + + let rslt = modsmgr.exec_enable(requestor.clone(), + None, + trg_module.clone(), + trg_level.clone(), + id.clone()).await; + + // assert_eq!(rslt,ChangeResult::Failed("Promote yourself Temporarily First".to_string())); + + match current_test_scenario { + TestScenarios::BotadminUser => + assert_eq!(rslt,ChangeResult::Failed("Promote yourself Temporarily First".to_string())), + TestScenarios::ModUser => + assert_eq!(rslt,ChangeResult::Success("Enabled at Channel Level".to_string())), + TestScenarios::RegularChatter => + assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + // TestScenarios::ModuleDoesNotExist => + // assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + // _ => + // self::panic!("Scenario not handled"), + } + + + /* + [x] 3. affirm when BotAdmin attempts to exec_enable on the following + b. Channel Level , when they are a Mod + */ + + // [x] requestor_badge: Option, + // let requestor_badge = ChatBadge::Mod; // setting badge to Mod + let requestor_badge = match current_test_scenario { + TestScenarios::BotadminUser => + Some(ChatBadge::Mod), // setting badge to Mod -- for the Problem Scenario . They are both BotAdmin & Mod + TestScenarios::ModUser => + Some(ChatBadge::Mod), // setting badge to Mod + TestScenarios::RegularChatter => + None, // setting badge to None + // TestScenarios::ModuleDoesNotExist => + // None, + // _ => + // self::panic!("Scenario not handled"), + } ; + + + let rslt = modsmgr.exec_enable(requestor.clone(), + requestor_badge, + trg_module.clone(), + trg_level.clone(), + id.clone()).await; + + // assert_eq!(rslt,ChangeResult::Success("Enabled at Channel Level".to_string())); + + match current_test_scenario { + TestScenarios::BotadminUser => + assert_eq!(rslt,ChangeResult::Success("Enabled at Channel Level".to_string())), + TestScenarios::ModUser => + assert_eq!(rslt,ChangeResult::Success("Enabled at Channel Level".to_string())), + TestScenarios::RegularChatter => + assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + // TestScenarios::ModuleDoesNotExist => + // assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + // _ => + // self::panic!("Scenario not handled"), + } + + /* + [x] 3. affirm when BotAdmin attempts to exec_enable on the following + c. Instance Level + */ + + let trg_level = StatusLvl::Instance; // setting to Instance level + + let requestor_badge = match current_test_scenario { + TestScenarios::BotadminUser => + None, + TestScenarios::ModUser => + Some(ChatBadge::Mod), + TestScenarios::RegularChatter => + None, // setting badge to None + // TestScenarios::ModuleDoesNotExist => + // None, // setting badge to None + // _ => + // self::panic!("Scenario not handled"), + }; + + let rslt = modsmgr.exec_enable(requestor.clone(), + requestor_badge, // passing based on scenario + trg_module.clone(), + trg_level.clone(), + id.clone()).await; + + // assert_eq!(rslt,ChangeResult::Success("Enabled at Instance Level".to_string())); + + match current_test_scenario { + TestScenarios::BotadminUser => + assert_eq!(rslt,ChangeResult::Success("Enabled at Instance Level".to_string())), + TestScenarios::ModUser => + assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + TestScenarios::RegularChatter => + assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + // TestScenarios::ModuleDoesNotExist => + // assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + // _ => + // self::panic!("Scenario not handled"), + } + + /* + [x] 4. affirm when BotAdmin attempts to exec_disable on the following + a. Channel Level , where they are not a Mod + */ + + let trg_level = StatusLvl::Ch(channel.clone()); // setting to Channel Level + + let rslt: ChangeResult = modsmgr.exec_disable(requestor.clone(), + None, // Does not have a ChatBadge like Mod + trg_module.clone(), + trg_level.clone(), + false, + id.clone()).await; + + // assert_eq!(rslt,ChangeResult::Success("Disabled at Channel Level".to_string())); + match current_test_scenario { + TestScenarios::BotadminUser => + assert_eq!(rslt,ChangeResult::Success("Disabled at Channel Level".to_string())), + TestScenarios::ModUser => + assert_eq!(rslt,ChangeResult::Success("Disabled at Channel Level".to_string())), + TestScenarios::RegularChatter => + assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + // TestScenarios::ModuleDoesNotExist => + // assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + // _ => + // self::panic!("Scenario not handled"), + } + + + /* + [x] 4. affirm when BotAdmin attempts to exec_disable on the following + b. Channel Level , when they are a Mod + */ + + + let trg_level = StatusLvl::Ch(channel.clone()); // setting to Channel Level + // let requestor_badge = ChatBadge::Mod; // setting badge to Mod + let requestor_badge = match current_test_scenario { + TestScenarios::BotadminUser => + None, + TestScenarios::ModUser => + Some(ChatBadge::Mod), + TestScenarios::RegularChatter => + None, // setting badge to None + // TestScenarios::ModuleDoesNotExist => + // None, + // _ => + // self::panic!("Scenario not handled"), + }; + + let rslt: ChangeResult = modsmgr.exec_disable(requestor.clone(), + requestor_badge, + trg_module.clone(), + trg_level.clone(), + false, + id.clone()).await; + + // assert_eq!(rslt,ChangeResult::Success("Disabled at Channel Level".to_string())); + match current_test_scenario { + TestScenarios::BotadminUser => + assert_eq!(rslt,ChangeResult::Success("Disabled at Channel Level".to_string())), + TestScenarios::ModUser => + assert_eq!(rslt,ChangeResult::Success("Disabled at Channel Level".to_string())), + TestScenarios::RegularChatter => + assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + // TestScenarios::ModuleDoesNotExist => + // assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + // _ => + // self::panic!("Scenario not handled"), + } + + + /* + [x] 4. affirm when BotAdmin attempts to exec_disable on the following + c. Instance Level + */ + + let trg_level = StatusLvl::Instance; // setting to Instance level + // let requestor_badge = ChatBadge::Mod; // setting badge to Mod + + let rslt: ChangeResult = modsmgr.exec_disable(requestor.clone(), + None, // Does not have a ChatBadge like Mod + trg_module.clone(), + trg_level.clone(), + false, + id.clone()).await; + + // assert_eq!(rslt,ChangeResult::Success("Disabled at Instance Level".to_string())); + match current_test_scenario { + TestScenarios::BotadminUser => + assert_eq!(rslt,ChangeResult::Success("Disabled at Instance Level".to_string())), + TestScenarios::ModUser => + assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + TestScenarios::RegularChatter => + assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + // TestScenarios::ModuleDoesNotExist => + // assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + // _ => + // self::panic!("Scenario not handled"), + } + + /* + [ ] 4. affirm when BotAdmin attempts to exec_disable on the following + d. force disable + */ + + let trg_level = StatusLvl::Instance; // setting to Instance level + // let requestor_badge = ChatBadge::Mod; // setting badge to Mod + + let rslt: ChangeResult = modsmgr.exec_disable(requestor.clone(), + None, // Does not have a ChatBadge like Mod + trg_module.clone(), + trg_level.clone(), + true, // force flag - true + id.clone()).await; + + // assert_eq!(rslt,ChangeResult::Success("Forced Disable".to_string())); + match current_test_scenario { + TestScenarios::BotadminUser => + assert_eq!(rslt,ChangeResult::Success("Forced Disable".to_string())), + TestScenarios::ModUser => + assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + TestScenarios::RegularChatter => + assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + // TestScenarios::ModuleDoesNotExist => + // assert_eq!(rslt,ChangeResult::Failed("You're not allowed".to_string())), + // _ => + // self::panic!("Scenario not handled"), + } + + + } + + + #[tokio::test] + async fn enable_disable_bot_admin_workflow() { + Log::set_file_ext(Extension::Log); + /* + + 1. Create new ModulesManager & Identity Manager + 2. modmgr.affirm_in_statusdb(Experiments01,Custom) + + 3. affirm when BotAdmin attempts to exec_enable on the following + a. Channel Level , where they are not a Mod + b. Channel Level , when they are a Mod + c. Instance Level + 4. affirm when BotAdmin attempts to exec_disable on the following + a. Channel Level , where they are not a Mod + b. Channel Level , when they are a Mod + c. Instance Level + d. force disable + + */ + + + // [x] 1. Create new ModulesManager & Identity Manager + let idmgr = IdentityManager::init(); + let modsmgr = ModulesManager::init().await; + + // // [x] 2. modmgr.affirm_in_statusdb(Experiments01,Custom) + + // let in_module = BotModule("Experiments01".to_string()); + // let in_modgroup = ModGroup::Custom; + + // modsmgr.affirm_in_statusdb(in_module.clone(), in_modgroup.clone()).await; + + + /* + [x] 3. affirm when BotAdmin attempts to exec_enable on the following + a. Channel Level , where they are not a Mod + */ + + // [x] Create BotAdmin first + + let requestor = "botadministrator".to_string(); + // let botadmin_badge = &None; + + idmgr.affirm_chatter_in_db(requestor.clone()).await; + idmgr + .add_role(requestor.clone(), identity::UserRole::BotAdmin) + .await; + + let rslt = idmgr + .getspecialuserroles(requestor.clone(), None) + .await; + + assert!(rslt.contains(&identity::UserRole::BotAdmin)); + + let channel = ChType::Channel("somechannel".to_string()); + + + inner_enable_disable_complex(requestor, channel, idmgr, modsmgr).await; + + + /* + pub async fn exec_enable( + &self, + requestor: String, + requestor_badge: Option, + trg_module: ModType, + trg_level: StatusLvl, + id: Arc>, + ) -> ChangeResult + + */ + + /* + [x] 3. affirm when BotAdmin attempts to exec_enable on the following + a. Channel Level , where they are not a Mod + */ + + + // [-] requestor_badge: Option, + + // [x] trg_module: ModType, + // let trg_module = in_module; + + // [x] trg_level: StatusLvl, + + // let channel = ChType::Channel("somechannel".to_string()); + // let trg_level = StatusLvl::Ch(channel.clone()); // setting to Channel Level + + + // // [x] id: Arc>, + // let id = Arc::new(RwLock::new(idmgr)); + + + // let rslt = modsmgr.exec_enable(requestor.clone(), + // None, + // trg_module.clone(), + // trg_level.clone(), + // id.clone()).await; + + // assert_eq!(rslt,ChangeResult::Failed("Promote yourself Temporarily First".to_string())); + + // /* + // [x] 3. affirm when BotAdmin attempts to exec_enable on the following + // b. Channel Level , when they are a Mod + // */ + + // // [x] requestor_badge: Option, + // let requestor_badge = ChatBadge::Mod; // setting badge to Mod + + // let rslt = modsmgr.exec_enable(requestor.clone(), + // Some(requestor_badge), + // trg_module.clone(), + // trg_level.clone(), + // id.clone()).await; + + // assert_eq!(rslt,ChangeResult::Success("Enabled at Channel Level".to_string())); + + // /* + // [x] 3. affirm when BotAdmin attempts to exec_enable on the following + // c. Instance Level + // */ + + // let trg_level = StatusLvl::Instance; // setting to Instance level + + // let rslt = modsmgr.exec_enable(requestor.clone(), + // None, // setting them back to Non-Mod + // trg_module.clone(), + // trg_level.clone(), + // id.clone()).await; + + // assert_eq!(rslt,ChangeResult::Success("Enabled at Instance Level".to_string())); + + // /* + // [x] 4. affirm when BotAdmin attempts to exec_disable on the following + // a. Channel Level , where they are not a Mod + // */ + + // let trg_level = StatusLvl::Ch(channel.clone()); // setting to Channel Level + + // let rslt: ChangeResult = modsmgr.exec_disable(requestor.clone(), + // None, // Does not have a ChatBadge like Mod + // trg_module.clone(), + // trg_level.clone(), + // false, + // id.clone()).await; + + // assert_eq!(rslt,ChangeResult::Success("Disabled at Channel Level".to_string())); + + + // /* + // [x] 4. affirm when BotAdmin attempts to exec_disable on the following + // b. Channel Level , when they are a Mod + // */ + + + // let trg_level = StatusLvl::Ch(channel.clone()); // setting to Channel Level + // let requestor_badge = ChatBadge::Mod; // setting badge to Mod + + // let rslt: ChangeResult = modsmgr.exec_disable(requestor.clone(), + // Some(requestor_badge), + // trg_module.clone(), + // trg_level.clone(), + // false, + // id.clone()).await; + + // assert_eq!(rslt,ChangeResult::Success("Disabled at Channel Level".to_string())); + + // /* + // [x] 4. affirm when BotAdmin attempts to exec_disable on the following + // c. Instance Level + // */ + + // let trg_level = StatusLvl::Instance; // setting to Instance level + // // let requestor_badge = ChatBadge::Mod; // setting badge to Mod + + // let rslt: ChangeResult = modsmgr.exec_disable(requestor.clone(), + // None, // Does not have a ChatBadge like Mod + // trg_module.clone(), + // trg_level.clone(), + // false, + // id.clone()).await; + + // assert_eq!(rslt,ChangeResult::Success("Disabled at Instance Level".to_string())); + + // /* + // [ ] 4. affirm when BotAdmin attempts to exec_disable on the following + // d. force disable + // */ + + // let trg_level = StatusLvl::Instance; // setting to Instance level + // // let requestor_badge = ChatBadge::Mod; // setting badge to Mod + + // let rslt: ChangeResult = modsmgr.exec_disable(requestor.clone(), + // None, // Does not have a ChatBadge like Mod + // trg_module.clone(), + // trg_level.clone(), + // true, // force flag - true + // id.clone()).await; + + // assert_eq!(rslt,ChangeResult::Success("Forced Disable".to_string())); + + + + + } + + + + #[tokio::test] + async fn enable_disable_mod_workflow() { + Log::set_file_ext(Extension::Log); + + /* + 1. Create new ModulesManager & Identity Manager + 2. modmgr.affirm_in_statusdb(Experiments01,Custom) + + 3. affirm when Mod attempts to exec_enable on the following + a. Channel Level , where they are not a Mod + b. Channel Level , when they are a Mod + c. Instance Level + 4. affirm when Mod attempts to exec_disable on the following + a. Channel Level , where they are not a Mod + b. Channel Level , when they are a Mod + c. Instance Level + d. force disable + */ + + + // [x] 1. Create new ModulesManager & Identity Manager + let idmgr = IdentityManager::init(); + let modsmgr = ModulesManager::init().await; + + + let requestor = "mod_user".to_string(); + // let botadmin_badge = &None; + let channel = ChType::Channel("somechannel".to_string()); + + + idmgr.affirm_chatter_in_db(requestor.clone()).await; + idmgr + .add_role(requestor.clone(), identity::UserRole::Mod(channel.clone())) + .await; + + let rslt = idmgr + .getspecialuserroles( + requestor.clone(), + Some(channel.clone()) // None if BotAdmin ; Otherwise, pass Some(Channel) + ) + .await; + + assert!(rslt.contains(&identity::UserRole::Mod(channel.clone()))); + + + inner_enable_disable_complex(requestor, channel, idmgr, modsmgr).await; + + + + } + + #[tokio::test] + async fn enable_disable_chatter_workflow() { + Log::set_file_ext(Extension::Log); + + + /* + 1. Create new ModulesManager & Identity Manager + 2. modmgr.affirm_in_statusdb(Experiments01,Custom) + + 3. affirm when Mod attempts to exec_enable on the following + a. Channel Level , where they are not a Mod + b. Channel Level , when they are a Mod + c. Instance Level + 4. affirm when Mod attempts to exec_disable on the following + a. Channel Level , where they are not a Mod + b. Channel Level , when they are a Mod + c. Instance Level + d. force disable + */ + + + // [x] 1. Create new ModulesManager & Identity Manager + let idmgr = IdentityManager::init(); + let modsmgr = ModulesManager::init().await; + + + let requestor = "regular_user".to_string(); + // let botadmin_badge = &None; + let channel = ChType::Channel("somechannel".to_string()); + + + idmgr.affirm_chatter_in_db(requestor.clone()).await; + // idmgr + // .add_role(requestor.clone(), identity::UserRole::Mod(channel.clone())) + // .await; + + let rslt = idmgr + .getspecialuserroles( + requestor.clone(), + Some(channel.clone()) // None if BotAdmin ; Otherwise, pass Some(Channel) + ) + .await; + + assert!(!rslt.contains(&identity::UserRole::Mod(channel.clone())) || + !rslt.contains(&identity::UserRole::BotAdmin)); + + + inner_enable_disable_complex(requestor, channel, idmgr, modsmgr).await; + + } + + + #[tokio::test] + async fn enable_disable_modulenotexist_workflow() { + Log::set_file_ext(Extension::Log); + + + // [x] 1. Create new ModulesManager & Identity Manager + let idmgr = IdentityManager::init(); + let modsmgr = ModulesManager::init().await; + + + let requestor = "regular_user".to_string(); + // let botadmin_badge = &None; + let channel = ChType::Channel("somechannel".to_string()); + + + idmgr.affirm_chatter_in_db(requestor.clone()).await; + // idmgr + // .add_role(requestor.clone(), identity::UserRole::Mod(channel.clone())) + // .await; + + let rslt = idmgr + .getspecialuserroles( + requestor.clone(), + Some(channel.clone()) // None if BotAdmin ; Otherwise, pass Some(Channel) + ) + .await; + + assert!(!rslt.contains(&identity::UserRole::Mod(channel.clone())) || + !rslt.contains(&identity::UserRole::BotAdmin)); + + // After above, regular chatter is created + + // [x] 2. modmgr.affirm_in_statusdb(Existing_Module,Custom) + + let in_module = BotModule("Existing_Module".to_string()); + let in_modgroup = ModGroup::Custom; + + modsmgr.affirm_in_statusdb(in_module.clone(), in_modgroup.clone()).await; + + + let trg_level = StatusLvl::Ch(channel.clone()); // setting to Channel Level + + + // [x] Test with Non Existing module > exec + + let trg_module = BotModule("Non_Existent_Module".to_string()); + + let rslt = modsmgr.exec_enable(requestor.clone(), + None, + trg_module.clone(), + trg_level.clone(), + Arc::new(RwLock::new(idmgr.clone()))).await; + + assert_eq!(rslt,ChangeResult::Failed("Module doesn't exist".to_string())); + + // [ ] Test with Non Existing module > disable + + let trg_module = BotModule("Non_Existent_Module".to_string()); + + let rslt = modsmgr.exec_disable(requestor.clone(), + None, + trg_module.clone(), + trg_level.clone(), + false, + Arc::new(RwLock::new(idmgr))).await; + + assert_eq!(rslt,ChangeResult::Failed("Module doesn't exist".to_string())); + + } + } diff --git a/src/core/identity.rs b/src/core/identity.rs index 84905cc..accda4f 100644 --- a/src/core/identity.rs +++ b/src/core/identity.rs @@ -630,7 +630,8 @@ impl IdentityManager { } } - async fn add_role(&self, trgchatter: String, trg_role: UserRole) { + // => 03.22 - Force - Made public because botmodules unit tests + pub async fn add_role(&self, trgchatter: String, trg_role: UserRole) { let mut srulock = self.special_roles_users.write().await; let mut usrrolelock = srulock .get_mut(&trgchatter) @@ -652,7 +653,8 @@ impl IdentityManager { } } - async fn affirm_chatter_in_db(&self, trgchatter: String) { + // => 03.22 - Force - Made public because botmodules unit tests + pub async fn affirm_chatter_in_db(&self, trgchatter: String) { let mut srulock = self.special_roles_users.write().await; srulock .entry(trgchatter.clone())