Files
codex-proxy-rs/backend/crates/gateway-store/tests/postgres/proxies.rs
T
ZyyandCodex 000107597e fix(accounts): preserve quota across OAuth reauthorization
Keep the last quota observation when the upstream identity is unchanged. Include disabled OAuth accounts in quota maintenance while keeping them out of request selection.

Co-authored-by: Codex <noreply@openai.com>
2026-11-23 17:08:45 +08:00

1531 lines
52 KiB
Rust

//! 验证代理启停、位置、账号绑定、自动解绑与事务锁释放
use gateway_admin::{
model::{
MutationActor, MutationContext, PageSize,
accounts::{BatchUpdateAccounts, UpdateAccount},
proxies::*,
},
ports::{
proxy::ProxyStore,
store::{AccountStore, AdminStoreErrorKind},
},
};
use gateway_core::account::{AccountWeight, OutboundProxy, ProviderAccountId};
use gateway_store::postgres::{
PgProviderAccountRepository, PgProxyRepository, ProviderAccountRepository,
};
use super::{TestDatabase, admin_account_store, provider_accounts::account};
#[tokio::test]
async fn proxy_location_is_shared_preserved_cleared_and_removed_with_binding() {
use gateway_core::account::{ProviderAccountStore, RequestLocation};
let Some(database) = TestDatabase::create("proxy_location").await else {
return;
};
let proxies = PgProxyRepository::new(database.pool.clone());
let accounts = PgProviderAccountRepository::new(database.pool.clone());
let location: RequestLocation = serde_json::from_value(serde_json::json!({"country":"JP", "region":"Tokyo", "city":"Tokyo", "timezone":"Asia/Tokyo"})).unwrap();
let mut saved = proxies
.create(
NewProxy {
auto_location: false,
test: None,
name: "Tokyo".to_owned(),
proxy: OutboundProxy::parse("http://127.0.0.1:18080").unwrap(),
location: Some(location.clone()),
},
&context(),
)
.await
.unwrap()
.record;
assert_eq!(saved.location.as_ref(), Some(&location));
proxies
.record_test(&saved.id, saved.revision, success(), &context())
.await
.unwrap();
for id in ["acct_location_a", "acct_location_b"] {
let mut input = account(id, id);
input.outbound_proxy = Some(saved.proxy.clone());
accounts.insert_provider_account(input).await.unwrap();
let projected = accounts
.get_account(&ProviderAccountId::new(id).unwrap())
.await
.unwrap()
.unwrap();
assert_eq!(projected.request_location(), Some(&location));
}
let directory = accounts.list_accounts().await.unwrap();
assert_eq!(directory.len(), 2);
assert!(
directory
.iter()
.all(|account| account.request_location() == Some(&location))
);
let original_revision = directory[0].revision();
saved = proxies
.update(
UpdateProxy {
max_websocket_connections: None,
enabled: None,
auto_location: None,
test: None,
id: saved.id.clone(),
revision: saved.revision,
name: "Renamed".to_owned(),
proxy: None,
location: None,
},
&context(),
)
.await
.unwrap()
.record;
assert_eq!(saved.location.as_ref(), Some(&location));
assert!(saved.last_test.as_ref().unwrap().success);
let stale_revision = saved.revision;
saved = proxies
.update(
UpdateProxy {
max_websocket_connections: None,
enabled: None,
auto_location: None,
test: None,
id: saved.id.clone(),
revision: saved.revision,
name: saved.name.clone(),
proxy: None,
location: Some(None),
},
&context(),
)
.await
.unwrap()
.record;
assert!(saved.location.is_none());
assert!(saved.last_test.as_ref().unwrap().success);
assert!(
accounts
.list_accounts()
.await
.unwrap()
.iter()
.all(|account| account.request_location().is_none()
&& account.revision() == original_revision)
);
assert!(
proxies
.update(
UpdateProxy {
max_websocket_connections: None,
enabled: None,
auto_location: None,
test: None,
id: saved.id.clone(),
revision: stale_revision,
name: saved.name.clone(),
proxy: None,
location: Some(Some(location.clone())),
},
&context()
)
.await
.is_err()
);
assert!(proxies.get(&saved.id).await.unwrap().location.is_none());
proxies
.update(
UpdateProxy {
max_websocket_connections: None,
enabled: None,
auto_location: None,
test: None,
id: saved.id.clone(),
revision: saved.revision,
name: saved.name.clone(),
proxy: None,
location: Some(Some(location.clone())),
},
&context(),
)
.await
.unwrap();
let id = ProviderAccountId::new("acct_location_a").unwrap();
proxies
.remove_account(&saved.id, &id, &context())
.await
.unwrap();
let removed = accounts.get_account(&id).await.unwrap().unwrap();
assert!(removed.outbound_proxy().is_none());
assert!(removed.request_location().is_none());
let other = accounts
.get_account(&ProviderAccountId::new("acct_location_b").unwrap())
.await
.unwrap()
.unwrap();
assert_eq!(other.request_location(), Some(&location));
database.close().await;
}
fn context() -> MutationContext {
MutationContext {
actor: MutationActor::System,
request_id: "managed-proxy-test".to_owned(),
}
}
#[tokio::test]
async fn disabled_proxy_projects_direct_while_preserving_bindings_and_credentials() {
use gateway_core::{
account::{ProviderAccountStore as _, ProviderRefreshQuery, RequestLocation},
routing::ProviderKind,
};
use std::{
num::NonZeroU32,
time::{Duration, SystemTime},
};
let Some(database) = TestDatabase::create("proxy_enabled").await else {
return;
};
let proxies = PgProxyRepository::new(database.pool.clone());
let accounts = PgProviderAccountRepository::new(database.pool.clone());
let admin = admin_account_store(&database.pool);
let location: RequestLocation = serde_json::from_value(serde_json::json!({
"country":"JP", "region":"Tokyo", "city":"Tokyo", "timezone":"Asia/Tokyo"
}))
.unwrap();
let mut saved = proxies
.create(
NewProxy {
auto_location: false,
test: None,
location: Some(location.clone()),
name: "启停测试".to_owned(),
proxy: OutboundProxy::parse("http://user:secret@127.0.0.1:18080").unwrap(),
},
&context(),
)
.await
.unwrap()
.record;
assert!(saved.enabled);
proxies
.record_test(&saved.id, saved.revision, success(), &context())
.await
.unwrap();
for (id, provider) in [
("acct_enabled_openai", "openai"),
("acct_enabled_xai", "xai"),
] {
let mut seed = account(id, id);
seed.provider_kind = provider.into();
seed.outbound_proxy = Some(saved.proxy.clone());
seed.has_refresh_token = true;
seed.access_token_expires_at = Some(chrono::Utc::now());
accounts.insert_provider_account(seed).await.unwrap();
}
let original: Vec<serde_json::Value> =
sqlx::query_scalar("select to_jsonb(a) from provider_accounts a order by id")
.fetch_all(&database.pool)
.await
.unwrap();
let original_revision = accounts.list_accounts().await.unwrap()[0].revision();
for enabled in [false, true] {
let stale_revision = saved.revision;
saved = proxies
.update(
UpdateProxy {
max_websocket_connections: None,
enabled: Some(enabled),
auto_location: None,
test: None,
location: None,
id: saved.id.clone(),
revision: saved.revision,
name: saved.name.clone(),
proxy: None,
},
&context(),
)
.await
.unwrap()
.record;
assert_eq!(saved.enabled, enabled);
assert_eq!(saved.account_count, 2);
assert_eq!(saved.last_test, Some(success()));
assert_eq!(saved.location.as_ref(), Some(&location));
let current: Vec<serde_json::Value> =
sqlx::query_scalar("select to_jsonb(a) from provider_accounts a order by id")
.fetch_all(&database.pool)
.await
.unwrap();
assert_eq!(current, original);
let mut projected = accounts.list_accounts().await.unwrap();
for (id, provider) in [
("acct_enabled_openai", "openai"),
("acct_enabled_xai", "xai"),
] {
let id = ProviderAccountId::new(id).unwrap();
projected.push(accounts.get_account(&id).await.unwrap().unwrap());
projected.push(accounts.load_current_credential(&id).await.unwrap().account);
let kind = ProviderKind::new(provider).unwrap();
projected.extend(accounts.list_for_provider(&kind, false).await.unwrap());
let now = SystemTime::now();
projected.extend(
accounts
.list_refresh_candidates(ProviderRefreshQuery::new(
kind,
now + Duration::from_secs(300),
now - Duration::from_secs(7200),
now,
vec![],
NonZeroU32::new(10).unwrap(),
))
.await
.unwrap()
.into_iter()
.map(|loaded| loaded.account),
);
let configured = admin
.credential_details(&ProviderKind::new(provider).unwrap(), &id)
.await
.unwrap()
.unwrap()
.credential;
assert_eq!(configured.outbound_proxy.as_ref(), Some(&saved.proxy));
assert_eq!(
configured.outbound_proxy_id.as_deref(),
Some(saved.id.as_str())
);
assert_eq!(
configured.effective_outbound_proxy(),
enabled.then_some(&saved.proxy)
);
}
assert!(projected.iter().all(|account| account.outbound_proxy()
== enabled.then_some(&saved.proxy)
&& account.request_location() == enabled.then_some(&location)
&& account.revision() == original_revision
&& account.enabled()));
assert_eq!(
proxies
.update(
UpdateProxy {
max_websocket_connections: None,
enabled: Some(!enabled),
auto_location: None,
test: None,
location: None,
id: saved.id.clone(),
revision: stale_revision,
name: saved.name.clone(),
proxy: None,
},
&context()
)
.await
.unwrap_err()
.kind(),
AdminStoreErrorKind::Conflict
);
assert_eq!(proxies.get(&saved.id).await.unwrap().enabled, enabled);
let reservation = proxies.reserve_import(&saved.id).await;
assert_eq!(
reservation.is_ok(),
enabled,
"import reservation failed: {:?}",
reservation.as_ref().err()
);
drop(reservation);
if !enabled {
let error = admin
.update_account(
update(
"acct_enabled_openai",
AccountProxySelection::Saved(saved.id.clone()),
),
&context(),
)
.await
.unwrap_err();
assert_eq!(error.kind(), AdminStoreErrorKind::Conflict);
let error = admin
.batch_update_accounts(
BatchUpdateAccounts {
quota_settings: None,
auto_use_quota_credits: None,
account_ids: vec![
"acct_enabled_openai".to_owned(),
"acct_enabled_xai".to_owned(),
],
enabled: Some(false),
concurrency_limit: None,
weight: None,
model_access: None,
group_ids: None,
outbound_proxy: Some(AccountProxySelection::Saved(saved.id.clone())),
},
Default::default(),
&context(),
)
.await
.unwrap_err();
assert_eq!(error.kind(), AdminStoreErrorKind::Conflict);
let unchanged: Vec<serde_json::Value> =
sqlx::query_scalar("select to_jsonb(a) from provider_accounts a order by id")
.fetch_all(&database.pool)
.await
.unwrap();
assert_eq!(unchanged, original);
}
}
database.close().await;
}
fn success() -> ProxyTestResult {
ProxyTestResult {
location: Default::default(),
success: true,
latency_ms: 10,
exit_ip: Some("203.0.113.5".parse().unwrap()),
exit_ipv4: Some("203.0.113.5".parse().unwrap()),
exit_ipv6: None,
message: "Connected".to_owned(),
}
}
fn update(account_id: &str, selection: AccountProxySelection) -> UpdateAccount {
UpdateAccount {
name: None,
notes: None,
model_access: Default::default(),
account_id: account_id.to_owned(),
enabled: true,
concurrency_limit: None,
weight: AccountWeight::DEFAULT,
group_ids: vec![],
outbound_proxy: Some(selection),
}
}
async fn seed_paused_quota(database: &TestDatabase, id: &str) {
let window = serde_json::json!({
"rate_limit": {"primary_window": {"limit_window_seconds": 18000, "used_percent": 99}}
});
sqlx::query(
"update provider_accounts set quota_settings = $2, quota_paused = true,
provider_quota_json = $3, quota_observed_at = now(), updated_at = now() where id = $1",
)
.bind(id)
.bind(serde_json::json!({"codex:18000s": {"limitPercent": 99, "warmup": null}}))
.bind(window)
.execute(&database.pool)
.await
.unwrap();
}
#[tokio::test]
async fn proxy_account_removal_preserves_settings_and_rejects_changed_bindings() {
let Some(database) = TestDatabase::create("proxy_account_remove").await else {
return;
};
let store = PgProxyRepository::new(database.pool.clone());
let context = context();
let saved = store
.create(
NewProxy {
auto_location: false,
test: None,
location: None,
name: "解绑测试".to_owned(),
proxy: OutboundProxy::parse("http://user:secret@127.0.0.1:17890").unwrap(),
},
&context,
)
.await
.unwrap()
.record;
sqlx::query(
"insert into provider_accounts
(id, provider_kind, name, email, authentication_kind, provider_credentials_json,
has_refresh_token, enabled, concurrency_limit, weight, plan_type,
credential_observed_at, created_at, updated_at, outbound_proxy_id, outbound_proxy_url)
values ('acct_remove', 'openai', '解绑账号', 'remove@example.invalid', 'oauth',
'{\"access_token\":\"preserve-test-secret\"}'::jsonb, false, false, 3, 7, 'plus',
now(), now(), now(), $1, $2)",
)
.bind(&saved.id)
.bind(saved.proxy.expose_url())
.execute(&database.pool)
.await
.unwrap();
sqlx::query(
"insert into account_groups (id, name, color, enabled, created_at, updated_at)
values ('grp_00000000000000000000000000000001', '保留分组', '#123456FF', true, now(), now())",
).execute(&database.pool).await.unwrap();
sqlx::query(
"insert into account_group_accounts (account_group_id, provider_account_id, created_at)
values ('grp_00000000000000000000000000000001', 'acct_remove', now())",
)
.execute(&database.pool)
.await
.unwrap();
seed_paused_quota(&database, "acct_remove").await;
let account_id = ProviderAccountId::new("acct_remove").unwrap();
let snapshot = "select to_jsonb(a) - 'updated_at' - 'outbound_proxy_id' - 'outbound_proxy_url'
from provider_accounts a where id = 'acct_remove'";
let before: serde_json::Value = sqlx::query_scalar(snapshot)
.fetch_one(&database.pool)
.await
.unwrap();
let revision: i64 =
sqlx::query_scalar("select config_revision from runtime_settings where id = 1")
.fetch_one(&database.pool)
.await
.unwrap();
let audit_count: i64 = sqlx::query_scalar("select count(*) from admin_audit_events")
.fetch_one(&database.pool)
.await
.unwrap();
assert_eq!(
store
.remove_account("proxy_previous", &account_id, &context)
.await
.unwrap_err()
.kind(),
AdminStoreErrorKind::Conflict
);
assert_eq!(store.get(&saved.id).await.unwrap().account_count, 1);
let unchanged: (Option<String>, Option<String>) = sqlx::query_as(
"select outbound_proxy_id, outbound_proxy_url from provider_accounts where id = 'acct_remove'",
).fetch_one(&database.pool).await.unwrap();
assert_eq!(
unchanged,
(
Some(saved.id.clone()),
Some(saved.proxy.expose_url().to_owned())
)
);
let current_revision: i64 =
sqlx::query_scalar("select config_revision from runtime_settings where id = 1")
.fetch_one(&database.pool)
.await
.unwrap();
assert_eq!(current_revision, revision);
let committed = store
.remove_account(&saved.id, &account_id, &context)
.await
.unwrap();
assert_eq!(committed.get(), u64::try_from(revision + 1).unwrap());
let after: serde_json::Value = sqlx::query_scalar(snapshot)
.fetch_one(&database.pool)
.await
.unwrap();
assert_eq!(after, before);
let direct: (Option<String>, Option<String>) = sqlx::query_as(
"select outbound_proxy_id, outbound_proxy_url from provider_accounts where id = 'acct_remove'",
).fetch_one(&database.pool).await.unwrap();
assert_eq!(direct, (None, None));
let group_count: i64 = sqlx::query_scalar(
"select count(*) from account_group_accounts where provider_account_id = 'acct_remove'",
)
.fetch_one(&database.pool)
.await
.unwrap();
assert_eq!(group_count, 1);
assert_eq!(store.get(&saved.id).await.unwrap().account_count, 0);
assert_eq!(
store
.list_accounts(ProxyAccountListQuery {
proxy_id: saved.id.clone(),
page: 1,
page_size: PageSize::new(20).unwrap(),
search: String::new(),
})
.await
.unwrap()
.total,
0
);
assert_eq!(
store
.remove_account(&saved.id, &account_id, &context)
.await
.unwrap_err()
.kind(),
AdminStoreErrorKind::Conflict
);
let current_audits: i64 = sqlx::query_scalar("select count(*) from admin_audit_events")
.fetch_one(&database.pool)
.await
.unwrap();
assert_eq!(current_audits, audit_count + 1);
let current_revision: i64 =
sqlx::query_scalar("select config_revision from runtime_settings where id = 1")
.fetch_one(&database.pool)
.await
.unwrap();
assert_eq!(u64::try_from(current_revision).unwrap(), committed.get());
database.close().await;
}
#[tokio::test]
async fn proxy_accounts_paginate_thousands_of_accounts_and_search_without_loading_the_catalog() {
let Some(database) = TestDatabase::create("proxy_account_pagination").await else {
return;
};
let store = PgProxyRepository::new(database.pool.clone());
let context = context();
let saved = store
.create(
NewProxy {
auto_location: false,
test: None,
location: None,
name: "分页测试".to_owned(),
proxy: OutboundProxy::parse("http://127.0.0.1:17890").unwrap(),
},
&context,
)
.await
.unwrap()
.record;
let empty = store
.create(
NewProxy {
auto_location: false,
test: None,
location: None,
name: "无关联账号".to_owned(),
proxy: OutboundProxy::parse("http://127.0.0.1:17891").unwrap(),
},
&context,
)
.await
.unwrap()
.record;
// 相同名称验证稳定排序;账号仅写入隔离测试数据库,不接触真实账号池
sqlx::query(
"insert into provider_accounts
(id, provider_kind, name, email, authentication_kind, provider_credentials_json,
has_refresh_token, enabled, credential_observed_at, created_at, updated_at,
outbound_proxy_id, outbound_proxy_url)
select 'acct_page_' || lpad(n::text, 4, '0'),
case when n % 2 = 0 then 'xai' else 'openai' end, '共享账号',
'page_' || lpad(n::text, 4, '0') || '@example.invalid', 'oauth',
'{\"access_token\":\"page-test-secret\"}'::jsonb, false, n % 2 = 0,
now(), now(), now(), $1, $2
from generate_series(1, 1005) as n",
)
.bind(&saved.id)
.bind(saved.proxy.expose_url())
.execute(&database.pool)
.await
.unwrap();
sqlx::query("update provider_accounts set plan_type = 'plus' where id = 'acct_page_0001'")
.execute(&database.pool)
.await
.unwrap();
sqlx::query(
"insert into account_groups (id, name, color, enabled, created_at, updated_at)
values ('grp_00000000000000000000000000000001', '工作组', '#123456FF', false, now(), now()),
('grp_00000000000000000000000000000002', '次页分组', '#654321FF', true, now(), now())",
).execute(&database.pool).await.unwrap();
sqlx::query(
"insert into account_group_accounts (account_group_id, provider_account_id, created_at)
values ('grp_00000000000000000000000000000001', 'acct_page_0001', now()),
('grp_00000000000000000000000000000002', 'acct_page_0021', now())",
)
.execute(&database.pool)
.await
.unwrap();
PgProviderAccountRepository::new(database.pool.clone())
.insert_provider_account(account("acct_unrelated", "unrelated-user"))
.await
.unwrap();
let query = |page, search: &str| ProxyAccountListQuery {
proxy_id: saved.id.clone(),
page,
page_size: PageSize::new(20).unwrap(),
search: search.to_owned(),
};
let first = store.list_accounts(query(1, "")).await.unwrap();
assert_eq!((first.total, first.items.len()), (1005, 20));
assert_eq!(first.items[0].id, "acct_page_0001");
assert_eq!(
first.items[0].email.as_deref(),
Some("page_0001@example.invalid")
);
assert_eq!(first.items[0].provider_kind, "openai");
assert_eq!(first.items[0].authentication_kind, "oauth");
assert_eq!(first.items[0].plan_type.as_deref(), Some("plus"));
assert_eq!(first.items[0].groups.len(), 1);
assert_eq!(first.items[0].groups[0].name, "工作组");
assert_eq!(first.items[0].groups[0].color.as_str(), "#123456FF");
assert!(!first.items[0].groups[0].enabled);
assert!(first.items[1].groups.is_empty());
assert!(!first.items[0].enabled);
let second = store.list_accounts(query(2, "")).await.unwrap();
assert_eq!(second.items[0].id, "acct_page_0021");
assert_eq!(second.items[0].groups[0].name, "次页分组");
assert!(
first
.items
.iter()
.all(|left| second.items.iter().all(|right| left.id != right.id))
);
let last = store.list_accounts(query(51, "")).await.unwrap();
assert_eq!((last.total, last.items.len()), (1005, 5));
assert_eq!(last.items[4].id, "acct_page_1005");
let filtered = store.list_accounts(query(1, "PAGE_004")).await.unwrap();
assert_eq!((filtered.total, filtered.items.len()), (10, 10));
assert_eq!(filtered.items[0].id, "acct_page_0040");
assert_eq!(store.list_accounts(query(1, "%")).await.unwrap().total, 0);
assert!(
store
.list_accounts(query(52, ""))
.await
.unwrap()
.items
.is_empty()
);
assert_eq!(
store
.list_accounts(ProxyAccountListQuery {
proxy_id: empty.id,
..query(1, "")
})
.await
.unwrap()
.total,
0
);
assert_eq!(
store
.list_accounts(ProxyAccountListQuery {
proxy_id: "missing".to_owned(),
..query(1, "")
})
.await
.unwrap_err()
.kind(),
AdminStoreErrorKind::NotFound
);
let catalog = store
.list(ProxyListQuery {
page: 1,
page_size: PageSize::new(20).unwrap(),
search: "分页测试".to_owned(),
})
.await
.unwrap();
assert_eq!(catalog.items[0].account_count, 1005);
database.close().await;
}
#[tokio::test]
async fn rejected_import_reservations_release_proxy_lock_before_returning() {
let Some(database) = TestDatabase::create("rejected_proxy_import").await else {
return;
};
let store = PgProxyRepository::new(database.pool.clone());
let id = "proxy_missing";
// 读取不存在的代理失败后,应立即允许其他事务取得同一资源的独占锁
for _ in 0..64 {
let error = store.reserve_import(id).await.err().unwrap();
assert_eq!(error.kind(), AdminStoreErrorKind::NotFound);
let mut transaction = database.pool.begin().await.unwrap();
let acquired: bool =
sqlx::query_scalar("select pg_try_advisory_xact_lock(hashtextextended($1, 739219))")
.bind(id)
.fetch_one(&mut *transaction)
.await
.unwrap();
assert!(acquired);
transaction.commit().await.unwrap();
}
database.close().await;
}
#[tokio::test]
async fn stale_proxy_mutations_release_locks_before_next_update() {
let Some(database) = TestDatabase::create("stale_proxy_mutation_lock").await else {
return;
};
let store = PgProxyRepository::new(database.pool.clone());
let context = context();
let mut saved = store
.create(
NewProxy {
name: "过期检测结果".to_owned(),
proxy: OutboundProxy::parse("http://127.0.0.1:19090").unwrap(),
auto_location: true,
location: None,
test: None,
},
&context,
)
.await
.unwrap()
.record;
let stale_revision = saved.revision;
saved = store
.record_test(&saved.id, saved.revision, success(), &context)
.await
.unwrap()
.record;
for mutation in ["record_test", "update", "delete"] {
// 暂停连接归还时的后台清理,确保锁由返回错误前的显式回滚释放
let release_gate = std::sync::Arc::new(tokio::sync::Semaphore::new(0));
let rejected_pool = database
.pool
.options()
.clone()
.max_connections(1)
.after_release({
let release_gate = release_gate.clone();
move |_, _| {
let release_gate = release_gate.clone();
Box::pin(async move {
let _permit = release_gate.acquire().await.unwrap();
Ok(true)
})
}
})
.connect_with((*database.pool.connect_options()).clone())
.await
.unwrap();
let stale_writer = PgProxyRepository::new(rejected_pool.clone());
let command = |revision| UpdateProxy {
max_websocket_connections: None,
enabled: None,
id: saved.id.clone(),
revision,
name: saved.name.clone(),
proxy: None,
auto_location: None,
location: None,
test: None,
};
let error = match mutation {
"record_test" => stale_writer
.record_test(&saved.id, stale_revision, success(), &context)
.await
.unwrap_err(),
"update" => stale_writer
.update(command(stale_revision), &context)
.await
.unwrap_err(),
"delete" => stale_writer
.delete(&saved.id, stale_revision, &context)
.await
.unwrap_err(),
_ => unreachable!(),
};
let update = store.update(command(saved.revision), &context).await;
release_gate.add_permits(1);
rejected_pool.close().await;
assert_eq!(error.kind(), AdminStoreErrorKind::Conflict);
saved = update
.unwrap_or_else(|error| panic!("过期 {mutation} 不应阻挡后续编辑: {error:?}"))
.record;
}
database.close().await;
}
#[tokio::test]
async fn import_reservation_blocks_proxy_mutations_until_rotated_credentials_are_committed() {
use gateway_store::postgres::{
ImportProviderAccounts, ProviderAccountAdminRepository, ProviderAccountAdminScope,
};
let Some(database) = TestDatabase::create("proxy_import_reservation").await else {
return;
};
let store = PgProxyRepository::new(database.pool.clone());
let other_process = PgProxyRepository::new(database.pool.clone());
let context = context();
let saved = store
.create(
NewProxy {
auto_location: false,
test: None,
location: None,
name: "导入出口".to_owned(),
proxy: OutboundProxy::parse("http://127.0.0.1:8080")
.unwrap()
.with_max_websocket_connections(24),
},
&context,
)
.await
.unwrap()
.record;
assert!(saved.last_test.is_none());
store
.record_test(
&saved.id,
saved.revision,
ProxyTestResult {
location: Default::default(),
success: false,
..success()
},
&context,
)
.await
.unwrap();
let reservation = store.reserve_import(&saved.id).await.unwrap();
let replacement = UpdateProxy {
max_websocket_connections: None,
enabled: None,
auto_location: None,
test: None,
location: None,
id: saved.id.clone(),
revision: saved.revision,
name: saved.name,
proxy: Some(OutboundProxy::parse("http://127.0.0.1:9090").unwrap()),
};
assert!(
other_process
.update(replacement.clone(), &context)
.await
.is_err()
);
assert!(
other_process
.delete(&saved.id, saved.revision, &context)
.await
.is_err()
);
assert!(
other_process
.record_test(
&saved.id,
saved.revision,
ProxyTestResult {
location: Default::default(),
success: false,
..success()
},
&context
)
.await
.is_err()
);
// 模拟上游已轮换的新凭据;持有保护时,另一个连接仍能完成导入事务
let mut candidate = account("acct_reserved_import", "reserved-import-user");
candidate.outbound_proxy = Some(reservation.binding.proxy.clone());
let repository = PgProviderAccountRepository::new(database.pool.clone());
let audit =
super::provider_accounts::audit("audit_reserved_import", "import", "acct_reserved_import");
tokio::time::timeout(
std::time::Duration::from_secs(5),
repository.import_provider_accounts(ImportProviderAccounts {
settings: None,
outbound_proxy: Some(reservation.binding.clone()),
scope: ProviderAccountAdminScope {
provider_kind: "openai".to_owned(),
},
accounts: vec![candidate],
audit,
}),
)
.await
.unwrap()
.unwrap();
drop(reservation);
tokio::time::timeout(std::time::Duration::from_secs(5), async {
loop {
if other_process
.update(replacement.clone(), &context)
.await
.is_ok()
{
break;
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
})
.await
.unwrap();
assert!(
repository
.load_provider_account("acct_reserved_import")
.await
.unwrap()
.is_some()
);
database.close().await;
}
#[tokio::test]
async fn managed_proxies_persist_bind_update_all_accounts_and_protect_stale_tests() {
let Some(database) = TestDatabase::create("managed_proxies").await else {
return;
};
let accounts = PgProviderAccountRepository::new(database.pool.clone());
for id in ["acct_one", "acct_two"] {
accounts
.insert_provider_account(account(id, id))
.await
.unwrap();
}
let store = PgProxyRepository::new(database.pool.clone());
let admin = admin_account_store(&database.pool);
let context = context();
let old_proxy = OutboundProxy::parse("http://user:secret@127.0.0.1:8080").unwrap();
let created = store
.create(
NewProxy {
auto_location: false,
test: None,
location: None,
name: "Office".to_owned(),
proxy: old_proxy.clone(),
},
&context,
)
.await
.unwrap()
.record;
assert_eq!(store.get(&created.id).await.unwrap().proxy, old_proxy);
assert!(
store
.create(
NewProxy {
auto_location: false,
test: None,
location: None,
name: "Duplicate".to_owned(),
proxy: old_proxy.clone()
},
&context
)
.await
.is_err()
);
seed_paused_quota(&database, "acct_one").await;
let paused_evidence = || async {
use gateway_core::account::ProviderAccountStore;
let id = ProviderAccountId::new("acct_one").unwrap();
assert!(
accounts
.get_account(&id)
.await
.unwrap()
.unwrap()
.quota_paused()
);
let observation = accounts.get_quotas(&[id]).await.unwrap().pop().unwrap();
(observation.observed_at, observation.quota)
};
let original_evidence = paused_evidence().await;
let selection = AccountProxySelection::Saved(created.id.clone());
admin
.update_account(update("acct_one", selection.clone()), &context)
.await
.unwrap();
let read = || async {
sqlx::query_as::<_, (String, Option<String>, Option<String>, i64)>("select id, outbound_proxy_id, outbound_proxy_url, credential_revision from provider_accounts order by id")
.fetch_all(&database.pool).await.unwrap()
};
assert_eq!(paused_evidence().await, original_evidence);
let untested = read().await;
assert_eq!(untested[0].1.as_deref(), Some(created.id.as_str()));
assert_eq!(untested[0].2.as_deref(), Some(old_proxy.expose_url()));
assert!(untested[1].1.is_none() && untested[1].2.is_none());
assert!(untested.iter().all(|row| row.3 == 1));
let tested = store
.record_test(
&created.id,
created.revision,
ProxyTestResult {
location: Default::default(),
success: false,
..success()
},
&context,
)
.await
.unwrap();
assert!(tested.record.last_test_at.is_some());
assert!(!tested.record.last_test.unwrap().success);
for id in ["acct_one", "acct_two"] {
admin
.update_account(update(id, selection.clone()), &context)
.await
.unwrap();
}
assert_eq!(store.get(&created.id).await.unwrap().account_count, 2);
assert!(
read()
.await
.iter()
.all(|row| row.1.as_deref() == Some(created.id.as_str())
&& row.2.as_deref() == Some(old_proxy.expose_url())
&& row.3 == 1)
);
store
.record_test(&created.id, created.revision, success(), &context)
.await
.unwrap();
let renamed = store
.update(
UpdateProxy {
max_websocket_connections: None,
enabled: None,
auto_location: None,
test: None,
id: created.id.clone(),
revision: created.revision,
location: None,
name: "Renamed".to_owned(),
proxy: None,
},
&context,
)
.await
.unwrap()
.record;
assert_eq!(renamed.proxy, old_proxy);
assert_eq!(renamed.last_test, Some(success()));
assert!(read().await.iter().all(|row| row.3 == 1));
let new_proxy = OutboundProxy::parse("socks5h://next:new-secret@127.0.0.1:1080").unwrap();
let edited = store
.update(
UpdateProxy {
max_websocket_connections: None,
enabled: None,
auto_location: None,
test: None,
id: created.id.clone(),
revision: renamed.revision,
location: None,
name: renamed.name,
proxy: Some(new_proxy.clone()),
},
&context,
)
.await
.unwrap()
.record;
assert!(edited.last_test.is_none());
assert!(edited.last_test_at.is_none());
assert_eq!(paused_evidence().await, original_evidence);
assert!(
read()
.await
.iter()
.all(|row| row.2.as_deref() == Some(new_proxy.expose_url()) && row.3 == 1)
);
assert!(
store
.record_test(&created.id, created.revision, success(), &context)
.await
.is_err()
);
assert!(store.get(&created.id).await.unwrap().last_test.is_none());
store
.record_test(&created.id, edited.revision, success(), &context)
.await
.unwrap();
assert_eq!(
store
.delete(&created.id, created.revision, &context)
.await
.unwrap_err()
.kind(),
AdminStoreErrorKind::Conflict
);
assert!(
read()
.await
.iter()
.all(|row| row.1.as_deref() == Some(created.id.as_str()))
);
let preserved: Vec<serde_json::Value> = sqlx::query_scalar("select to_jsonb(a) - 'updated_at' - 'outbound_proxy_id' - 'outbound_proxy_url' from provider_accounts a order by id")
.fetch_all(&database.pool).await.unwrap();
let deleted_revision = store
.delete(&created.id, edited.revision, &context)
.await
.unwrap();
let after_delete: Vec<serde_json::Value> = sqlx::query_scalar("select to_jsonb(a) - 'updated_at' - 'outbound_proxy_id' - 'outbound_proxy_url' from provider_accounts a order by id")
.fetch_all(&database.pool).await.unwrap();
assert_eq!(preserved, after_delete);
assert_eq!(
sqlx::query_scalar::<_, i64>(
"select count(*) from admin_audit_events where config_revision = $1"
)
.bind(i64::try_from(deleted_revision.get()).unwrap())
.fetch_one(&database.pool)
.await
.unwrap(),
3
);
assert!(
read()
.await
.iter()
.all(|row| row.1.is_none() && row.2.is_none() && row.3 == 1)
);
assert!(store.get(&created.id).await.is_err());
assert_eq!(paused_evidence().await, original_evidence);
let audits: Vec<serde_json::Value> =
sqlx::query_scalar("select to_jsonb(a) from admin_audit_events a")
.fetch_all(&database.pool)
.await
.unwrap();
assert!(!serde_json::to_string(&audits).unwrap().contains("secret"));
database.close().await;
}
#[tokio::test]
async fn imported_urls_share_one_proxy_and_invalid_batch_preserves_the_binding() {
let Some(database) = TestDatabase::create("proxy_catalog_import").await else {
return;
};
let accounts = PgProviderAccountRepository::new(database.pool.clone());
let proxy = OutboundProxy::parse("http://user:secret@127.0.0.1:8080").unwrap();
for id in ["acct_one", "acct_two"] {
let mut seed = account(id, id);
seed.outbound_proxy = Some(proxy.clone());
accounts.insert_provider_account(seed).await.unwrap();
}
let store = PgProxyRepository::new(database.pool.clone());
let page = store
.list(ProxyListQuery {
page: 1,
page_size: PageSize::new(20).unwrap(),
search: String::new(),
})
.await
.unwrap();
assert_eq!(page.total, 1);
assert_eq!(page.items[0].account_count, 2);
let admin = admin_account_store(&database.pool);
assert!(
admin
.batch_update_accounts(
BatchUpdateAccounts {
quota_settings: None,
auto_use_quota_credits: None,
model_access: Default::default(),
account_ids: vec!["acct_one".to_owned(), "acct_missing".to_owned()],
enabled: Some(false),
concurrency_limit: Some(None),
weight: Some(AccountWeight::DEFAULT),
group_ids: Some(vec![]),
outbound_proxy: Some(AccountProxySelection::Direct),
},
Default::default(),
&context()
)
.await
.is_err()
);
assert_eq!(
sqlx::query_scalar::<_, i64>("select count(*) from outbound_proxies")
.fetch_one(&database.pool)
.await
.unwrap(),
1
);
assert_eq!(
sqlx::query_as::<_, (bool, String)>(
"select enabled, outbound_proxy_url from provider_accounts where id = 'acct_one'"
)
.fetch_one(&database.pool)
.await
.unwrap(),
(true, proxy.expose_url().to_owned())
);
database.close().await;
}
#[tokio::test]
async fn automatic_proxy_location_projects_to_accounts_and_guards_refresh_results() {
use gateway_core::account::{ProviderAccountStore, RequestLocation};
let Some(database) = TestDatabase::create("auto_proxy_location").await else {
return;
};
let proxies = PgProxyRepository::new(database.pool.clone());
let accounts = PgProviderAccountRepository::new(database.pool.clone());
let manual = RequestLocation::default();
let detected: RequestLocation = serde_json::from_value(serde_json::json!({"country":"JP", "region":"Tokyo", "city":"Tokyo", "timezone":"Asia/Tokyo"})).unwrap();
let detection = ProxyTestResult {
location: ProxyLocationDetection::Detected {
location: detected.clone(),
},
..success()
};
let created = proxies
.create(
NewProxy {
name: "Auto".to_owned(),
proxy: OutboundProxy::parse("http://127.0.0.1:19090").unwrap(),
location: Some(manual.clone()),
auto_location: true,
test: Some(detection.clone()),
},
&context(),
)
.await
.unwrap();
let mut saved = created.record;
assert_eq!(saved.effective_location(), Some(&detected));
assert_eq!(saved.location, Some(manual.clone()));
for id in ["acct_auto_a", "acct_auto_b"] {
let mut input = account(id, id);
input.outbound_proxy = Some(saved.proxy.clone());
accounts.insert_provider_account(input).await.unwrap();
}
let initial_accounts = accounts.list_accounts().await.unwrap();
let credential_revision = initial_accounts[0].revision();
assert!(
initial_accounts
.iter()
.all(|account| account.request_location() == Some(&detected))
);
let single = accounts
.get_account(&ProviderAccountId::new("acct_auto_a").unwrap())
.await
.unwrap()
.unwrap();
assert_eq!(single.request_location(), Some(&detected));
let original_detection = saved.detected_location.clone();
let failed = ProxyTestResult {
location: ProxyLocationDetection::Failed {
message: "查询失败".to_owned(),
},
..success()
};
saved = proxies
.record_test(&saved.id, saved.revision, failed.clone(), &context())
.await
.unwrap()
.record;
assert_eq!(saved.detected_location, original_detection);
let stale = saved.revision;
let changed_ip = ProxyTestResult {
exit_ip: Some("203.0.113.77".parse().unwrap()),
exit_ipv4: Some("203.0.113.77".parse().unwrap()),
..failed
};
let before: i64 =
sqlx::query_scalar("select config_revision from runtime_settings where id = 1")
.fetch_one(&database.pool)
.await
.unwrap();
let changed = proxies
.record_test(&saved.id, saved.revision, changed_ip, &context())
.await
.unwrap();
assert!(changed.config_revision.get() > before as u64);
saved = changed.record;
assert!(saved.detected_location.is_none());
assert!(
accounts
.list_accounts()
.await
.unwrap()
.iter()
.all(|account| account.request_location().is_none())
);
assert_eq!(
proxies
.record_test(&saved.id, stale, detection.clone(), &context())
.await
.unwrap_err()
.kind(),
AdminStoreErrorKind::Conflict
);
saved = proxies
.record_test(&saved.id, saved.revision, detection.clone(), &context())
.await
.unwrap()
.record;
let conflict = ProxyTestResult {
location: ProxyLocationDetection::Conflict,
..success()
};
saved = proxies
.record_test(&saved.id, saved.revision, conflict, &context())
.await
.unwrap()
.record;
assert!(saved.effective_location().is_none());
saved = proxies
.record_test(&saved.id, saved.revision, detection, &context())
.await
.unwrap()
.record;
saved = proxies
.update(
UpdateProxy {
max_websocket_connections: None,
enabled: None,
id: saved.id.clone(),
revision: saved.revision,
name: saved.name.clone(),
proxy: None,
auto_location: None,
location: None,
test: None,
},
&context(),
)
.await
.unwrap()
.record;
assert_eq!(saved.effective_location(), Some(&detected));
let prior_revision = saved.revision;
saved = proxies
.update(
UpdateProxy {
max_websocket_connections: None,
enabled: None,
id: saved.id.clone(),
revision: saved.revision,
name: saved.name.clone(),
proxy: Some(OutboundProxy::parse("http://127.0.0.1:19091").unwrap()),
auto_location: None,
location: None,
test: None,
},
&context(),
)
.await
.unwrap()
.record;
assert!(saved.effective_location().is_none());
assert!(saved.last_test.is_none());
assert_eq!(
proxies
.record_test(&saved.id, prior_revision, success(), &context())
.await
.unwrap_err()
.kind(),
AdminStoreErrorKind::Conflict
);
saved = proxies
.update(
UpdateProxy {
max_websocket_connections: None,
enabled: None,
id: saved.id.clone(),
revision: saved.revision,
name: saved.name.clone(),
proxy: None,
auto_location: Some(false),
location: None,
test: None,
},
&context(),
)
.await
.unwrap()
.record;
assert_eq!(saved.effective_location(), Some(&manual));
assert!(
accounts
.list_accounts()
.await
.unwrap()
.iter()
.all(|account| account.request_location() == Some(&manual)
&& account.revision() == credential_revision)
);
database.close().await;
}
#[tokio::test]
async fn proxy_websocket_capacity_is_shared_by_bound_accounts_and_updates_without_rebinding() {
use gateway_core::account::ProviderAccountStore;
let Some(database) = TestDatabase::create("proxy_ws_capacity").await else {
return;
};
let proxies = PgProxyRepository::new(database.pool.clone());
let accounts = PgProviderAccountRepository::new(database.pool.clone());
let mut saved = proxies
.create(
NewProxy {
auto_location: false,
test: None,
location: None,
name: "Limited".to_owned(),
proxy: OutboundProxy::parse("http://127.0.0.1:18080")
.unwrap()
.with_max_websocket_connections(24),
},
&context(),
)
.await
.unwrap()
.record;
assert_eq!(saved.proxy.max_websocket_connections(), 24);
for id in ["acct_proxy_budget_a", "acct_proxy_budget_b"] {
let mut input = account(id, id);
input.outbound_proxy = Some(saved.proxy.clone());
accounts.insert_provider_account(input).await.unwrap();
}
let mut direct = account("acct_other_proxy", "Other");
direct.outbound_proxy = Some(OutboundProxy::parse("http://127.0.0.1:18081").unwrap());
accounts.insert_provider_account(direct).await.unwrap();
for (change, expected) in [(None, 24), (Some(8), 8), (Some(0), 0)] {
saved = proxies
.update(
UpdateProxy {
max_websocket_connections: change,
enabled: None,
auto_location: None,
test: None,
location: None,
id: saved.id.clone(),
revision: saved.revision,
name: saved.name.clone(),
proxy: None,
},
&context(),
)
.await
.unwrap()
.record;
assert_eq!(saved.proxy.max_websocket_connections(), expected);
for id in ["acct_proxy_budget_a", "acct_proxy_budget_b"] {
let projected = accounts
.get_account(&ProviderAccountId::new(id).unwrap())
.await
.unwrap()
.unwrap();
assert_eq!(
projected
.outbound_proxy()
.unwrap()
.max_websocket_connections(),
expected
);
}
let other = accounts
.get_account(&ProviderAccountId::new("acct_other_proxy").unwrap())
.await
.unwrap()
.unwrap();
assert_eq!(
other.outbound_proxy().unwrap().max_websocket_connections(),
0
);
}
}