This adds some tests for invalid emails & passwords. More work is needed to handle invalid ID names.
QPAPAIEPNPSN67CML4ZKDLS6RG5FUOVNLCIUSYOGWP5BU25NIB5QC
)?;
identity.run(SubCommand::New)?;
Ok(())
}
#[test]
fn new_email_invalid() -> Result<(), Error> {
let identity = Identity::new(
id_name("new_email_invalid"),
None,
Some(
Interaction::new(
prompt::EMAIL,
InteractionType::Input(String::from("BAD-EMAIL")),
)
.with_second_attempt(SecondAttempt::new(
InteractionType::Input(default::EMAIL.to_string()),
"Invalid email address",
)?)?,
),
None,
None,
None,
None,
#[test]
fn new_expiry_invalid() -> Result<(), Error> {
let identity = Identity::new(
id_name("new_expiry_invalid"),
None,
None,
Some(
Interaction::new(
prompt::EXPIRY_DATE,
InteractionType::Input(String::from("BAD-EXPIRY")),
)
.with_second_attempt(SecondAttempt::new(
InteractionType::Input(default::EXPIRY.to_string()),
"Invalid date",
)?)?,
),
None,
None,
None,
)?;
identity.run(SubCommand::New)?;
Ok(())
}
#[test]
fn new_password_invalid() -> Result<(), Error> {
let identity = Identity::new(
id_name("new_password_invalid"),
None,
None,
None,
None,
None,
Some(
Interaction::new(
prompt::PASSWORD,
InteractionType::Password {
input: default::PASSWORD.to_string(),
confirm: Some(prompt::PASSWORD_REPROMPT.to_string()),
},
)
.with_second_attempt(SecondAttempt::new(
InteractionType::Password {
input: "Good-Password".to_string(),
confirm: Some(prompt::PASSWORD_REPROMPT.to_string()),
},
"Password mismatch",
)?)?,
),
)?;
identity.run(SubCommand::New)?;
Ok(())
}
// If there is a second attempt, first send try the invalid password
if let Some(second_attempt) = &self.second_attempt {
let invalid_password = self.input.as_string();
let valid_password = self.valid_input().as_string();
println!("Sending valid password: {valid_password}");
session.send(&valid_password)?;
session.send_control(ControlCode::CarriageReturn)?;
// If there is a second attempt, send the invalid password
if let Some(second_attempt) = self.invalid_input() {
let confirm_prompt = confirm.as_ref().unwrap();
println!("Expecting password re-prompt: {confirm_prompt}");
session.expect(confirm_prompt)?;
let invalid_password = second_attempt.as_string();
// Sometimes the password needs to match, eg in password confirmation
if let Some(confirm_prompt) = confirm {
session.send(confirm_prompt)?;
session.send_control(ControlCode::CarriageReturn)?;
let error_message = self.second_attempt.clone().unwrap().error_message;
println!("Expecting error message: {error_message}");
session.expect(&error_message)?;
}
let valid_password = second_attempt.input.as_string();
// Sometimes the password needs to be confirmed
if let Some(confirm_prompt) = confirm {
// In the case of invalid input, we have to send twice
if self.invalid_input().is_some() {
println!("Expecting prompt message: {}", self.prompt_message);
session.expect(&self.prompt_message)?;