Skip to content

Commit

Permalink
Add proper failure when login fails
Browse files Browse the repository at this point in the history
  • Loading branch information
fschutt committed Nov 24, 2022
1 parent 2beef7c commit 98f8d86
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
9 changes: 7 additions & 2 deletions lib/cli/src/commands/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ impl Login {
/// execute [List]
pub fn execute(&self) -> Result<(), anyhow::Error> {
let token = self.get_token_or_ask_user()?;
wasmer_registry::login::login_and_save_token(&self.registry, &token)
.map_err(|e| anyhow::anyhow!("{e}"))
match wasmer_registry::login::login_and_save_token(&self.registry, &token)? {
Some(s) => println!("Login for WAPM user {:?} saved", s),
None => println!("Login for WAPM user saved"),
}
Ok(())
}
}

Expand All @@ -61,6 +64,7 @@ fn test_login_2() {
let login = Login {
registry: "wapm.dev".to_string(),
token: None,
debug: false,
};

assert_eq!(
Expand All @@ -71,6 +75,7 @@ fn test_login_2() {
let login = Login {
registry: "wapm.dev".to_string(),
token: Some("abc".to_string()),
debug: false,
};

assert_eq!(login.get_token_or_ask_user().unwrap(), "abc");
Expand Down
23 changes: 10 additions & 13 deletions lib/registry/src/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,26 @@ pub fn login_and_save_token(
#[cfg(test)] test_name: &str,
registry: &str,
token: &str,
) -> Result<(), anyhow::Error> {
) -> Result<Option<String>, anyhow::Error> {
let registry = format_graphql(registry);
#[cfg(test)]
let mut config = PartialWapmConfig::from_file(test_name).map_err(|e| anyhow::anyhow!("{e}"))?;
let mut config = PartialWapmConfig::from_file(test_name)
.map_err(|e| anyhow::anyhow!("config from file: {e}"))?;
#[cfg(not(test))]
let mut config = PartialWapmConfig::from_file().map_err(|e| anyhow::anyhow!("{e}"))?;
let mut config =
PartialWapmConfig::from_file().map_err(|e| anyhow::anyhow!("config from file: {e}"))?;
config.registry.set_current_registry(&registry);
config.registry.set_login_token_for_registry(
&config.registry.get_current_registry(),
token,
UpdateRegistry::Update,
);
#[cfg(test)]
let path =
PartialWapmConfig::get_file_location(test_name).map_err(|e| anyhow::anyhow!("{e}"))?;
let path = PartialWapmConfig::get_file_location(test_name)
.map_err(|e| anyhow::anyhow!("get file location: {e}"))?;
#[cfg(not(test))]
let path = PartialWapmConfig::get_file_location().map_err(|e| anyhow::anyhow!("{e}"))?;
let path = PartialWapmConfig::get_file_location()
.map_err(|e| anyhow::anyhow!("get file location: {e}"))?;
config.save(&path)?;
let username = crate::utils::get_username_registry_token(&registry, token);
if let Some(s) = username.ok().and_then(|o| o) {
println!("Login for WAPM user {:?} saved", s);
} else {
println!("Login for WAPM user saved");
}
Ok(())
crate::utils::get_username_registry_token(&registry, token)
}
23 changes: 18 additions & 5 deletions tests/integration/cli/tests/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,31 @@ fn login_works() -> anyhow::Result<()> {
.arg(wapm_dev_token)
.output()?;

let stdout = std::str::from_utf8(&output.stdout)
.expect("stdout is not utf8! need to handle arbitrary bytes");

let stderr = std::str::from_utf8(&output.stderr)
.expect("stderr is not utf8! need to handle arbitrary bytes");

if !output.status.success() {
bail!(
"wasmer login failed with: stdout: {}\n\nstderr: {}",
std::str::from_utf8(&output.stdout)
.expect("stdout is not utf8! need to handle arbitrary bytes"),
std::str::from_utf8(&output.stderr)
.expect("stderr is not utf8! need to handle arbitrary bytes")
stdout,
stderr
);
}

let stdout_output = std::str::from_utf8(&output.stdout).unwrap();
assert_eq!(stdout_output, "Login for WAPM user \"ciuser\" saved\n");
let expected = "Login for WAPM user \"ciuser\" saved\n";
if stdout_output != expected {
println!("expected:");
println!("{expected}");
println!("got:");
println!("{stdout}");
println!("-----");
println!("{stderr}");
panic!("stdout incorrect");
}

Ok(())
}

0 comments on commit 98f8d86

Please sign in to comment.