diff --git a/leo/commands/login.rs b/leo/commands/login.rs index a72e134df0..4a3db628f5 100644 --- a/leo/commands/login.rs +++ b/leo/commands/login.rs @@ -54,7 +54,7 @@ impl LoginCommand { impl CLI for LoginCommand { // Format: token, username, password type Options = (Option, Option, Option); - type Output = (); + type Output = String; const ABOUT: AboutType = "Login to the package manager (*)"; const ARGUMENTS: &'static [ArgumentType] = &[ @@ -138,6 +138,101 @@ impl CLI for LoginCommand { LoginCommand::write_token(token.as_str())?; log::info!("Successfully logged in"); + Ok(token) + } +} + +#[cfg(test)] +mod tests { + + use super::*; + + use std::{ + fs::{remove_dir, remove_file}, + io, + }; + + const TEST_DIR: &str = ".test"; + + fn setup(suffix: &str) -> Result<(), io::Error> { + let test_dir = format!("{}_{}", TEST_DIR, suffix); + create_dir(&test_dir)?; + Ok(()) + } + + fn clean(suffix: &str) -> Result<(), io::Error> { + let test_dir = format!("{}_{}", TEST_DIR, suffix); + remove_file(&format!( + "{}/{}/{}", + test_dir, LEO_CREDENTIALS_DIR, LEO_CREDENTIALS_FILE + ))?; + remove_dir(&format!("{}/{}", test_dir, LEO_CREDENTIALS_DIR))?; + remove_dir(test_dir)?; + Ok(()) + } + + #[test] + #[ignore] + fn test_credential_dir_exists() -> Result<(), io::Error> { + let suffix = "test1"; + setup(suffix)?; + create_dir(LEO_CREDENTIALS_DIR)?; + + let token = "SOME_TOKEN".to_string(); + let options = (Some(token.clone()), None, None); + LoginCommand::output(options).unwrap(); + + assert_eq!(token, LoginCommand::read_token()?); + clean(suffix)?; + Ok(()) + } + + #[test] + #[ignore] + fn test_credential_file_exists() -> Result<(), io::Error> { + let suffix = "test2"; + setup(suffix)?; + create_dir(LEO_CREDENTIALS_DIR)?; + let old_token = "OLD_TOKEN"; + LoginCommand::write_token(old_token)?; + + let token = "NEW_TOKEN".to_string(); + let options = (Some(token.clone()), None, None); + LoginCommand::output(options).unwrap(); + + assert_eq!(token, LoginCommand::read_token()?); + clean(suffix)?; + Ok(()) + } + + #[test] + #[ignore] + fn test_credential_dir_does_not_exist() -> Result<(), io::Error> { + let suffix = "test3"; + setup(suffix)?; + + let token = "SOME_TOKEN".to_string(); + let options = (Some(token.clone()), None, None); + LoginCommand::output(options).unwrap(); + + assert_eq!(token, LoginCommand::read_token()?); + clean(suffix)?; + Ok(()) + } + + #[test] + #[ignore] + fn test_credential_file_does_not_exist() -> Result<(), io::Error> { + let suffix = "test4"; + setup(suffix)?; + create_dir(LEO_CREDENTIALS_DIR)?; + + let token = "SOME_TOKEN".to_string(); + let options = (Some(token.clone()), None, None); + LoginCommand::output(options).unwrap(); + + assert_eq!(token, LoginCommand::read_token()?); + clean(suffix)?; Ok(()) } }