|
| 1 | +use super::{CommandContext, CommandHandler}; |
| 2 | +use std::error::Error; |
| 3 | + |
| 4 | +pub struct DeleteCommand { |
| 5 | + pub nickname: String, |
| 6 | +} |
| 7 | + |
| 8 | +impl CommandHandler for DeleteCommand { |
| 9 | + async fn execute(&self, context: CommandContext<'_>) -> Result<(), Box<dyn Error>> { |
| 10 | + if context |
| 11 | + .config_manager |
| 12 | + .remove_client(context.config, &self.nickname) |
| 13 | + { |
| 14 | + let config_path = context.config_manager.get_config_path(); |
| 15 | + match context |
| 16 | + .config_manager |
| 17 | + .save_config(&config_path, context.config) |
| 18 | + { |
| 19 | + Ok(()) => println!("Client '{}' removed", self.nickname), |
| 20 | + Err(e) => println!("Failed to update config file.\n{e}"), |
| 21 | + } |
| 22 | + } else { |
| 23 | + println!("Client '{}' doesn't exist", self.nickname); |
| 24 | + } |
| 25 | + Ok(()) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +#[cfg(test)] |
| 30 | +mod tests { |
| 31 | + use std::collections::HashMap; |
| 32 | + |
| 33 | + use crate::{ |
| 34 | + commands::{CommandContext, CommandHandler, delete::DeleteCommand}, |
| 35 | + config::ConfigManager, |
| 36 | + oauth::TokenManager, |
| 37 | + types::{AuthConfig, ConfigFile, CredentialsProvider}, |
| 38 | + }; |
| 39 | + |
| 40 | + struct MockCredentialsProvider; |
| 41 | + |
| 42 | + impl CredentialsProvider for MockCredentialsProvider { |
| 43 | + fn get_credentials(&self) -> Result<(String, String), Box<dyn std::error::Error>> { |
| 44 | + Ok(("user".into(), "pass".into())) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + #[test] |
| 49 | + fn test_delete_client() { |
| 50 | + let mut config = ConfigFile { |
| 51 | + clients: [ |
| 52 | + ( |
| 53 | + "test1".to_string(), |
| 54 | + AuthConfig { |
| 55 | + auth_url: "https://auth1.com".to_string(), |
| 56 | + client_id: "client_id_1".to_string(), |
| 57 | + refresh_token: Some("token1".to_string()), |
| 58 | + }, |
| 59 | + ), |
| 60 | + ( |
| 61 | + "test2".to_string(), |
| 62 | + AuthConfig { |
| 63 | + auth_url: "https://auth2.com".to_string(), |
| 64 | + client_id: "client_id_2".to_string(), |
| 65 | + refresh_token: None, |
| 66 | + }, |
| 67 | + ), |
| 68 | + ] |
| 69 | + .into_iter() |
| 70 | + .collect(), |
| 71 | + }; |
| 72 | + |
| 73 | + let config_manager = ConfigManager::new(); |
| 74 | + config_manager.remove_client(&mut config, "test1"); |
| 75 | + |
| 76 | + let target = ConfigFile { |
| 77 | + clients: [( |
| 78 | + "test2".to_string(), |
| 79 | + AuthConfig { |
| 80 | + auth_url: "https://auth2.com".to_string(), |
| 81 | + client_id: "client_id_2".to_string(), |
| 82 | + refresh_token: None, |
| 83 | + }, |
| 84 | + )] |
| 85 | + .into_iter() |
| 86 | + .collect(), |
| 87 | + }; |
| 88 | + |
| 89 | + assert_eq!(config, target); |
| 90 | + } |
| 91 | + |
| 92 | + #[tokio::test] |
| 93 | + async fn test_delete_command_existing_client() { |
| 94 | + let config_manager = ConfigManager::new(); |
| 95 | + let token_manager = TokenManager::new(); |
| 96 | + let mut config = ConfigFile { |
| 97 | + clients: { |
| 98 | + let mut clients = HashMap::new(); |
| 99 | + clients.insert( |
| 100 | + "test_client".to_string(), |
| 101 | + AuthConfig { |
| 102 | + auth_url: "https://example.com".to_string(), |
| 103 | + client_id: "client123".to_string(), |
| 104 | + refresh_token: Some("refresh123".to_string()), |
| 105 | + }, |
| 106 | + ); |
| 107 | + clients |
| 108 | + }, |
| 109 | + }; |
| 110 | + |
| 111 | + let delete_command = DeleteCommand { |
| 112 | + nickname: "test_client".to_string(), |
| 113 | + }; |
| 114 | + |
| 115 | + let mock_credentials_provider = MockCredentialsProvider; |
| 116 | + let context = CommandContext { |
| 117 | + config: &mut config, |
| 118 | + config_manager: &config_manager, |
| 119 | + token_manager: &token_manager, |
| 120 | + credentials_provider: &mock_credentials_provider, |
| 121 | + }; |
| 122 | + |
| 123 | + let result = delete_command.execute(context).await; |
| 124 | + assert!(result.is_ok()); |
| 125 | + assert!(!config.clients.contains_key("test_client")); |
| 126 | + } |
| 127 | + |
| 128 | + #[tokio::test] |
| 129 | + async fn test_delete_command_nonexistent_client() { |
| 130 | + let config_manager = ConfigManager::new(); |
| 131 | + let token_manager = TokenManager::new(); |
| 132 | + let mut config = ConfigFile::default(); |
| 133 | + |
| 134 | + let delete_command = DeleteCommand { |
| 135 | + nickname: "nonexistent_client".to_string(), |
| 136 | + }; |
| 137 | + |
| 138 | + let mock_credentials_provider = MockCredentialsProvider; |
| 139 | + let context = CommandContext { |
| 140 | + config: &mut config, |
| 141 | + config_manager: &config_manager, |
| 142 | + token_manager: &token_manager, |
| 143 | + credentials_provider: &mock_credentials_provider, |
| 144 | + }; |
| 145 | + |
| 146 | + let result = delete_command.execute(context).await; |
| 147 | + assert!(result.is_ok()); // Command succeeds but prints message that client doesn't exist |
| 148 | + } |
| 149 | +} |
0 commit comments