Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the CLI respect the --token flag #4032

Merged
merged 3 commits into from
Jun 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 133 additions & 2 deletions lib/registry/src/wasmer_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,17 @@ impl WasmerEnv {

/// The API token for the active registry.
pub fn token(&self) -> Option<String> {
if let Some(token) = &self.token {
theduke marked this conversation as resolved.
Show resolved Hide resolved
return Some(token.clone());
}

// Fall back to the config file

let config = self.config().ok()?;
let login = config.registry.current_login()?;
Some(login.token.clone())
let registry_endpoint = self.registry_endpoint().ok()?;
config
.registry
.get_login_token_for_registry(registry_endpoint.as_str())
}
}

Expand Down Expand Up @@ -140,3 +148,126 @@ impl From<&str> for Registry {
Registry(value.to_string())
}
}

#[cfg(test)]
mod tests {
use tempfile::TempDir;

use super::*;

const WASMER_TOML: &str = r#"
telemetry_enabled = false
update_notifications_enabled = false

[registry]
active_registry = "https://registry.wasmer.io/graphql"

[[registry.tokens]]
registry = "https://registry.wasmer.wtf/graphql"
token = "dev-token"

[[registry.tokens]]
registry = "https://registry.wasmer.io/graphql"
token = "prod-token"
"#;

#[test]
fn load_defaults_from_config() {
let temp = TempDir::new().unwrap();
std::fs::write(temp.path().join("wasmer.toml"), WASMER_TOML).unwrap();

let env = WasmerEnv {
wasmer_dir: temp.path().to_path_buf(),
registry: None,
cache_dir: None,
token: None,
};

assert_eq!(
env.registry_endpoint().unwrap().as_str(),
"https://registry.wasmer.io/graphql"
);
assert_eq!(env.token().unwrap(), "prod-token");
assert_eq!(env.cache_dir(), temp.path().join("cache"));
}

#[test]
fn override_token() {
let temp = TempDir::new().unwrap();
std::fs::write(temp.path().join("wasmer.toml"), WASMER_TOML).unwrap();

let env = WasmerEnv {
wasmer_dir: temp.path().to_path_buf(),
registry: None,
cache_dir: None,
token: Some("asdf".to_string()),
};

assert_eq!(
env.registry_endpoint().unwrap().as_str(),
"https://registry.wasmer.io/graphql"
);
assert_eq!(env.token().unwrap(), "asdf");
assert_eq!(env.cache_dir(), temp.path().join("cache"));
}

#[test]
fn override_registry() {
let temp = TempDir::new().unwrap();
std::fs::write(temp.path().join("wasmer.toml"), WASMER_TOML).unwrap();
let env = WasmerEnv {
wasmer_dir: temp.path().to_path_buf(),
registry: Some(Registry::from("wasmer.wtf")),
cache_dir: None,
token: None,
};

assert_eq!(
env.registry_endpoint().unwrap().as_str(),
"https://registry.wasmer.wtf/graphql"
);
assert_eq!(env.token().unwrap(), "dev-token");
assert_eq!(env.cache_dir(), temp.path().join("cache"));
}

#[test]
fn override_registry_and_token() {
let temp = TempDir::new().unwrap();
std::fs::write(temp.path().join("wasmer.toml"), WASMER_TOML).unwrap();

let env = WasmerEnv {
wasmer_dir: temp.path().to_path_buf(),
registry: Some(Registry::from("wasmer.wtf")),
cache_dir: None,
token: Some("asdf".to_string()),
};

assert_eq!(
env.registry_endpoint().unwrap().as_str(),
"https://registry.wasmer.wtf/graphql"
);
assert_eq!(env.token().unwrap(), "asdf");
assert_eq!(env.cache_dir(), temp.path().join("cache"));
}

#[test]
fn override_cache_dir() {
let temp = TempDir::new().unwrap();
std::fs::write(temp.path().join("wasmer.toml"), WASMER_TOML).unwrap();
let expected_cache_dir = temp.path().join("some-other-cache");

let env = WasmerEnv {
wasmer_dir: temp.path().to_path_buf(),
registry: None,
cache_dir: Some(expected_cache_dir.clone()),
token: None,
};

assert_eq!(
env.registry_endpoint().unwrap().as_str(),
"https://registry.wasmer.io/graphql"
);
assert_eq!(env.token().unwrap(), "prod-token");
assert_eq!(env.cache_dir(), expected_cache_dir);
}
}