This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into alewis/warn-node-modules
- Loading branch information
Showing
4 changed files
with
63 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,11 @@ use serde::{Deserialize, Serialize}; | |
use crate::terminal::emoji; | ||
use config::{Config, Environment, File}; | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct GlobalUser { | ||
pub email: Option<String>, | ||
pub api_key: Option<String>, | ||
pub api_token: Option<String>, | ||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] | ||
#[serde(untagged)] | ||
pub enum GlobalUser { | ||
TokenAuth { api_token: String }, | ||
GlobalKeyAuth { email: String, api_key: String }, | ||
} | ||
|
||
impl GlobalUser { | ||
|
@@ -23,16 +23,12 @@ impl GlobalUser { | |
|
||
impl From<GlobalUser> for Credentials { | ||
fn from(user: GlobalUser) -> Credentials { | ||
if let Some(token) = user.api_token { | ||
return Credentials::UserAuthToken { token: token }; | ||
} | ||
|
||
// fallback to email and global API key. | ||
// If either of the fields below are None, just substitute in an empty string | ||
// and these credentials will trigger the appropriate "missing field" response from the API. | ||
Credentials::UserAuthKey { | ||
key: user.api_key.unwrap_or("".to_string()), | ||
email: user.email.unwrap_or("".to_string()), | ||
match user { | ||
GlobalUser::TokenAuth { api_token } => Credentials::UserAuthToken { token: api_token }, | ||
GlobalUser::GlobalKeyAuth { email, api_key } => Credentials::UserAuthKey { | ||
key: api_key, | ||
email, | ||
}, | ||
} | ||
} | ||
} | ||
|
@@ -89,3 +85,28 @@ pub fn get_global_config_dir() -> Result<PathBuf, failure::Error> { | |
info!("Using global config dir: {:?}", global_config_dir); | ||
Ok(global_config_dir) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::env; | ||
|
||
#[test] | ||
fn it_can_prioritize_token_input() { | ||
// Set all CF_API_TOKEN, CF_EMAIL, and CF_API_KEY. | ||
// This test evaluates whether the GlobalUser returned is | ||
// a GlobalUser::TokenAuth (expected behavior; token | ||
// should be prioritized over email + global API key pair.) | ||
env::set_var("CF_API_TOKEN", "foo"); | ||
env::set_var("CF_EMAIL", "[email protected]"); | ||
env::set_var("CF_API_KEY", "bar"); | ||
|
||
let user = get_global_config().unwrap(); | ||
assert_eq!( | ||
user, | ||
GlobalUser::TokenAuth { | ||
api_token: "foo".to_string() | ||
} | ||
); | ||
} | ||
} |