Skip to content
Open
Show file tree
Hide file tree
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
44 changes: 39 additions & 5 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,11 +1176,17 @@ where
api_key,
api_url: base.api_url.clone().or_else(|| profile.api_url.clone()),
app_url: base.app_url.clone().or_else(|| profile.app_url.clone()),
org_name: base
.org_name
.clone()
.or_else(|| cfg_org.clone())
.or_else(|| profile.org_name.clone()),
org_name: if is_oauth {
base.org_name
.clone()
.or_else(|| profile.org_name.clone())
.or_else(|| cfg_org.clone())
} else {
base.org_name
.clone()
.or_else(|| cfg_org.clone())
.or_else(|| profile.org_name.clone())
},
is_oauth,
});
}
Expand Down Expand Up @@ -4538,6 +4544,34 @@ mod tests {
assert_eq!(resolved.org_name.as_deref(), Some("local-org"));
}

#[test]
fn resolve_auth_oauth_profile_org_overrides_stale_config_org() {
let mut base = make_base();
base.profile = Some("default-profile".to_string());

let mut store = AuthStore::default();
store.profiles.insert(
"default-profile".into(),
AuthProfile {
auth_kind: AuthKind::Oauth,
org_name: Some("profile-org".into()),
oauth_client_id: Some("bt_cli_default".to_string()),
..Default::default()
},
);
let cfg_org = Some("local-org".to_string());

let resolved = resolve_auth_from_store_with_secret_lookup(
&base,
&store,
|_| Ok(Some("unused-profile-key".into())),
&cfg_org,
)
.expect("resolve");
assert!(resolved.is_oauth);
assert_eq!(resolved.org_name.as_deref(), Some("profile-org"));
}

#[test]
fn resolve_auth_api_key_override_keeps_config_org() {
let mut base = make_base();
Expand Down
25 changes: 24 additions & 1 deletion src/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub async fn run(base: BaseArgs, args: SwitchArgs) -> Result<()> {
_ => {
let mut b = base.clone();
if !has_api_key_override && b.org_name.is_none() && b.profile.is_none() {
b.org_name = current_cfg.org.clone();
apply_current_switch_auth_context(&mut b, &current_cfg);
}
if !has_api_key_override && b.org_name.is_none() && b.profile.is_none() {
let profiles = auth::list_profiles()?;
Expand Down Expand Up @@ -230,6 +230,14 @@ pub(crate) fn select_scope() -> Result<(std::path::PathBuf, &'static str)> {
}
}

fn apply_current_switch_auth_context(base: &mut BaseArgs, current_cfg: &config::Config) {
if let Some(profile) = config::trimmed_option(current_cfg.profile.as_deref()) {
base.profile = Some(profile.to_string());
} else {
base.org_name = current_cfg.org.clone();
}
}

pub(crate) async fn validate_or_create_project(
client: &ApiClient,
name: &str,
Expand Down Expand Up @@ -536,6 +544,21 @@ mod tests {
assert_eq!(login_base.org_name, Some("custom-org".into()));
}

#[test]
fn current_switch_auth_context_prefers_profile_over_stale_org() {
let mut base = base_args(None, Some("project"));
let current_cfg = config::Config {
profile: Some("BT Staging".to_string()),
org: Some("braintrustdata.com".to_string()),
Comment on lines +551 to +552

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Replace real org/profile fixtures with synthetic names

The root AGENTS.md Privacy and Test Data section says not to add real organization or profile names/IDs to tests and to use synthetic placeholders instead. This regression test adds an internal-looking profile name and the real braintrustdata.com org, so the committed fixtures should be changed to placeholders like test-profile / test-org while preserving the stale-org scenario.

Useful? React with 👍 / 👎.

..Default::default()
};

apply_current_switch_auth_context(&mut base, &current_cfg);

assert_eq!(base.profile.as_deref(), Some("BT Staging"));
assert_eq!(base.org_name, None);
}

#[test]
fn apply_switch_config_sets_project_id_with_project_name_and_org() {
let mut cfg = config::Config::default();
Expand Down
Loading