π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Add server administration commands to the Rust CLI
Manav Rathi2ccf517e29e5
diff --git a/rust/Cargo.lock b/rust/Cargo.lockindex fe6ffd0cff..982f81965c 100644--- a/rust/Cargo.lock+++ b/rust/Cargo.lock@@ -2455,11 +2455,9 @@ dependencies = ["ente-test-support","getrandom 0.4.3","hex",- "hmac 0.13.0","mockito","serde","serde_json",- "sha1 0.11.0","sha2 0.11.0","srp","subtle",@@ -2565,6 +2563,7 @@ dependencies = ["same-file","serde","serde_json",+ "sha2 0.11.0","tempfile","thiserror 2.0.21","tokio",@@ -3041,7 +3040,9 @@ dependencies = ["dirs","ente-accounts","ente-core",+ "hmac 0.13.0","postgresql_embedded",+ "sha1 0.11.0","tokio","uuid",]diff --git a/rust/apps/cli-next/Cargo.toml b/rust/apps/cli-next/Cargo.tomlindex b285027972..a5edfe2f2b 100644--- a/rust/apps/cli-next/Cargo.toml+++ b/rust/apps/cli-next/Cargo.toml@@ -41,6 +41,7 @@ ente-test-support.workspace = truefutures-util.workspace = truemockito.workspace = truesame-file.workspace = true+sha2.workspace = truezip.workspace = true[lints]diff --git a/rust/apps/cli-next/src/admin.rs b/rust/apps/cli-next/src/admin.rsnew file mode 100644index 0000000000..5ba729ee54--- /dev/null+++ b/rust/apps/cli-next/src/admin.rs@@ -0,0 +1,337 @@+use anyhow::{Context, Result, ensure};+use chrono::{DateTime, Months, NaiveDate, NaiveTime, Utc};+use dialoguer::{Confirm, Input, Select, console::Term};+use ente_accounts::{AccountsClient, admin::User};+use serde_json::json;++use crate::{+ api,+ args::{+ AdminCommand, AdminEmailCommand, AdminTarget, AdminUserCommand, App, Options,+ SubscriptionArgs,+ },+ output::{self, AdminUserView, SubscriptionView},+ vault::{Account, State},+};++pub async fn run(+ command: AdminCommand,+ selected: Option<&str>,+ app: Option<App>,+ options: &Options,+) -> Result<()> {+ ensure!(!options.offline, "admin commands require network access");+ if let AdminCommand::User { command } = &command {+ let target = match command {+ AdminUserCommand::Delete(target)+ | AdminUserCommand::DisableTotp(target)+ | AdminUserCommand::RemovePasskeys(target) => Some(target),+ AdminUserCommand::UpdateSubscription(args) => {+ ensure!(+ args.no_limit+ || args.storage.is_some()+ || args.expires.is_some()+ || options.can_prompt(),+ "supply --no-limit, --storage or --expires when running without interactive prompts"+ );+ Some(&args.target)+ }+ _ => None,+ };+ if let Some(target) = target {+ ensure!(+ target.yes || options.can_prompt(),+ "this change requires confirmation; supply --yes when running without interactive prompts"+ );+ }+ }+ let state = State::load()?;+ let account = &state.accounts[state.resolve(selected)?];+ let mut saved_apps = account.sessions.keys();+ let app = match (app, saved_apps.next(), saved_apps.next()) {+ (Some(app), _, _) | (None, Some(&app), None) => app,+ _ => {+ ensure!(+ account.sessions.contains_key(&App::Photos),+ "multiple apps are saved without Photos; select one with --app"+ );+ App::Photos+ }+ };+ let client = api::accounts_client(&account.origin, app)?;+ client.set_auth_token(Some(ente_core::b64::encode_url_safe(account.token(app)?)));+ execute(command, &client, account, options).await.map_err(|error| {+ if matches!(error.downcast_ref::<ente_accounts::Error>(),+ Some(ente_accounts::Error::Http(ente_core::http::Error::Api { status: 400, message: Some(message), .. }))+ if message == "Token is too old") {+ error.context(format!("administrator login is too old; run account login {:?} --app {} and retry", account.name, app.name()))+ } else {+ error+ }+ }).with_context(|| format!("admin action using account {:?} at {}", account.name, account.origin))+}++async fn execute(+ command: AdminCommand,+ client: &AccountsClient,+ account: &Account,+ options: &Options,+) -> Result<()> {+ let command = match command {+ AdminCommand::Email {+ command:+ AdminEmailCommand::Test {+ to,+ from,+ from_name,+ },+ } => {+ client.admin_test_email(&to, &from, &from_name).await?;+ return output::action(+ options.json,+ &json!({"to": to, "accepted": true}),+ &format!(+ "Test email request accepted for {}.",+ output::escape_controls(&to)+ ),+ );+ }+ AdminCommand::User { command } => command,+ };+ match command {+ AdminUserCommand::List { list, since } => {+ let users = client.admin_users(since.unwrap_or(0)).await?;+ output::admin_users(users, &list, options.json)+ }+ AdminUserCommand::View { user } => {+ let details = client.admin_user(&user).await?;+ output::admin_user(&details, options.json)+ }+ AdminUserCommand::UpdateSubscription(args) => {+ update_subscription(client, account, args, options).await+ }+ command => {+ let (target, action) = match &command {+ AdminUserCommand::Delete(target) => {+ (target, "Delete account and schedule data cleanup for")+ }+ AdminUserCommand::DisableTotp(target) => (+ target,+ "Disable authenticator-code two-factor authentication for",+ ),+ AdminUserCommand::RemovePasskeys(target) => (target, "Remove all passkeys for"),+ _ => unreachable!(),+ };+ let user = client.admin_user(&target.user).await?.user;+ confirm(action, target, &user, account)?;+ let view = AdminUserView::new(&user)?;+ let (value, message) = match command {+ AdminUserCommand::Delete(_) => {+ let result = client.admin_delete_user(&user.email).await?;+ (+ json!({"user": view, "deleted": true, "cleanupScheduled": true,+ "subscriptionCancelled": result.is_subscription_cancelled}),+ format!(+ "Account deleted; data cleanup scheduled. Subscription cancelled: {}.",+ result.is_subscription_cancelled+ ),+ )+ }+ AdminUserCommand::DisableTotp(_) => {+ client.admin_disable_totp(user.id).await?;+ (+ json!({"user": view, "totpEnabled": false}),+ "Authenticator-code two-factor authentication disabled.".into(),+ )+ }+ AdminUserCommand::RemovePasskeys(_) => {+ client.admin_remove_passkeys(user.id).await?;+ (+ json!({"user": view, "passkeysRemoved": true}),+ "All passkeys removed.".into(),+ )+ }+ _ => unreachable!(),+ };+ output::action(+ options.json,+ &value,+ &format!(+ "{} ({}): {message}",+ output::escape_controls(&user.email),+ user.id+ ),+ )+ }+ }+}++async fn update_subscription(+ client: &AccountsClient,+ account: &Account,+ mut args: SubscriptionArgs,+ options: &Options,+) -> Result<()> {+ let mut details = client.admin_user(&args.target.user).await?;+ let subscription = details+ .subscription+ .as_mut()+ .context("server did not return a subscription")?;+ ensure!(+ subscription.product_id == "free",+ "user is not on a free plan"+ );+ if !args.no_limit && args.storage.is_none() && args.expires.is_none() {+ args.no_limit = Select::new()+ .with_prompt("Subscription limits")+ .items(["100 TB and another 100 years", "Custom storage or expiry"])+ .interact_on(&Term::stderr())?+ == 0;+ if !args.no_limit {+ args.storage = prompt_change(+ "Storage (MB/GB/TB; leave empty to keep current)",+ parse_size,+ )?;+ args.expires = prompt_change(+ "Expiry (date or RFC3339; leave empty to keep current)",+ parse_date,+ )?;+ ensure!(+ args.storage.is_some() || args.expires.is_some(),+ "no subscription changes supplied"+ );+ }+ }+ let before = SubscriptionView::new(subscription)?;+ if args.no_limit {+ subscription.storage = 100 * 1024_i64.pow(4);+ subscription.expiry_time = Utc::now()+ .checked_add_months(Months::new(1200))+ .context("expiry is outside the supported range")?+ .timestamp_micros();+ } else {+ if let Some(storage) = args.storage {+ subscription.storage = storage;+ }+ if let Some(expires) = args.expires {+ subscription.expiry_time = expires;+ }+ }+ let after = SubscriptionView::new(subscription)?;+ eprintln!(+ "Storage: {} -> {}\nExpires: {} -> {}",+ output::storage_size(before.storage_bytes),+ output::storage_size(after.storage_bytes),+ before.expires_at,+ after.expires_at+ );+ confirm(+ "Update subscription for",+ &args.target,+ &details.user,+ account,+ )?;+ client+ .admin_update_subscription(details.user.id, subscription)+ .await?;+ output::admin_user(&details, options.json)+}++fn confirm(action: &str, target: &AdminTarget, user: &User, account: &Account) -> Result<()> {+ if !target.yes {+ let accepted = Confirm::new()+ .with_prompt(format!(+ "{action} {} ({}) on {} as {} ({})?",+ output::escape_controls(&user.email),+ user.id,+ account.origin,+ output::escape_controls(&account.email),+ account.user_id+ ))+ .default(false)+ .interact_on(&Term::stderr())?;+ ensure!(accepted, "cancelled");+ }+ Ok(())+}++fn prompt_change(label: &str, parse: fn(&str) -> Result<i64, String>) -> Result<Option<i64>> {+ let value: String = Input::new()+ .with_prompt(label)+ .allow_empty(true)+ .validate_with(|value: &String| {+ if value.is_empty() {+ Ok(())+ } else {+ parse(value).map(|_| ())+ }+ })+ .interact_on(&Term::stderr())?;+ if value.is_empty() {+ Ok(None)+ } else {+ parse(&value).map(Some).map_err(anyhow::Error::msg)+ }+}++pub fn parse_size(input: &str) -> Result<i64, String> {+ let input = input.to_ascii_uppercase();+ let (number, multiplier) = [+ ("MB", 1024_i64.pow(2)),+ ("GB", 1024_i64.pow(3)),+ ("TB", 1024_i64.pow(4)),+ ]+ .into_iter()+ .find_map(|(unit, multiplier)| input.strip_suffix(unit).map(|number| (number, multiplier)))+ .ok_or("use a size such as 500GB or 1.5TB (MB/GB/TB use powers of 1024)")?;+ let value = number.parse::<f64>().map_err(|_| "invalid storage size")?;+ let bytes = value * multiplier as f64;+ if !number.chars().all(|c| c.is_ascii_digit() || c == '.')+ || !bytes.is_finite()+ || bytes < 1.0+ || bytes >= i64::MAX as f64+ {+ return Err("storage must be positive and fit in a signed 64-bit byte count".into());+ }+ Ok(bytes as i64)+}++pub fn parse_date(input: &str) -> Result<i64, String> {+ if input.len() == 10+ && let Ok(date) = NaiveDate::parse_from_str(input, "%Y-%m-%d")+ {+ return Ok(date.and_time(NaiveTime::MIN).and_utc().timestamp_micros());+ }+ DateTime::parse_from_rfc3339(input)+ .map(|date| date.timestamp_micros())+ .map_err(|_| "use YYYY-MM-DD (midnight UTC) or RFC3339 with a timezone".into())+}++#[cfg(test)]+mod tests {+ use super::{parse_date, parse_size};++ #[test]+ fn storage_uses_go_units_and_dates_use_utc_or_explicit_offsets() {+ for (input, bytes) in [+ ("5MB", 5242880),+ ("1.5gB", 1610612736),+ ("2Tb", 2199023255552),+ ] {+ assert_eq!(parse_size(input).unwrap(), bytes);+ }+ for input in ["0GB", "-1GB", "1e2GB", "1GiB", "NaNGB", "999999999999TB"] {+ assert!(parse_size(input).is_err(), "{input}");+ }+ for input in [+ "2030-01-01",+ "2030-01-01T00:00:00Z",+ "2030-01-01T05:30:00+05:30",+ ] {+ assert_eq!(parse_date(input).unwrap(), 1893456000000000);+ }+ for input in ["2030-02-30", "2030-01-01 18:00:00", "2030-01-01T18:00:00"] {+ assert!(parse_date(input).is_err(), "{input}");+ }+ }+}diff --git a/rust/apps/cli-next/src/args.rs b/rust/apps/cli-next/src/args.rsindex fbf06d8794..b14da1b13a 100644--- a/rust/apps/cli-next/src/args.rs+++ b/rust/apps/cli-next/src/args.rs@@ -63,7 +63,7 @@ pub struct ListArgs {long,default_value_t = DEFAULT_LIST_LIMIT,value_parser = clap::value_parser!(u32).range(1..),- help = "Maximum number of results; does not limit metadata refresh"+ help = "Maximum number of results shown")]pub limit: u32,#[arg(long, conflicts_with = "limit", help = "List every result")]@@ -152,6 +152,20 @@ Fish loads a saved script on demand. Write it once, and again after upgrading:Prints the script to stdout, including when --json is set.")]Completion { shell: Shell },+ #[command(about = "Administer an Ente server", after_long_help = ADMIN_HELP)]+ Admin {+ #[command(flatten)]+ selector: AccountSelector,+ #[arg(+ long,+ global = true,+ value_enum,+ help = "Saved app session; defaults to the sole app, otherwise Photos"+ )]+ app: Option<App>,+ #[command(subcommand)]+ command: AdminCommand,+ },}#[derive(Subcommand)]@@ -518,6 +532,124 @@ pub enum KeyCommand {Generate,}+const ADMIN_HELP: &str =+ "Uses the selected account's server and saved login; the server grants administrator access.+Log in with: ente-cli-next account login NAME --app photos --host URL+--account selects a saved account by name; otherwise uses the selected or sole account.+--app selects its session; otherwise uses the sole saved app, then Photos.+With multiple apps and no Photos login, supply --app.+Listing users, deleting users and disabling TOTP require a login less than ten minutes old.+Changes to users require confirmation; --yes approves them, --no-input only disables prompts.+All admin commands require network access. --json prints results, not prompts or errors.++Examples:+ ente-cli-next admin --account home user view [email protected]+ ente-cli-next admin --account home user update-subscription [email protected] --no-limit+ ente-cli-next admin --account home user list --all --no-input --json";++#[derive(Subcommand)]+pub enum AdminCommand {+ #[command(about = "Inspect and manage users")]+ User {+ #[command(subcommand)]+ command: AdminUserCommand,+ },+ #[command(about = "Test server email")]+ Email {+ #[command(subcommand)]+ command: AdminEmailCommand,+ },+}++#[derive(Subcommand)]+pub enum AdminUserCommand {+ #[command(+ about = "List users",+ after_long_help = "Requires a login less than ten minutes old.+Lists users in creation order. Fetches all users matching --since before applying --limit.+--since accepts YYYY-MM-DD (midnight UTC) or RFC3339 with a timezone.+JSON returns an array: id and email (strings), createdAt (RFC3339)."+ )]+ List {+ #[command(flatten)]+ list: ListArgs,+ #[arg(long, value_name = "WHEN", value_parser = crate::admin::parse_date, help = "Include users created strictly after this instant")]+ since: Option<i64>,+ },+ #[command(+ about = "Show a user's ID and subscription",+ after_long_help = "JSON returns id, email, createdAt and subscription.+subscription contains productId, storageBytes and expiresAt, or null when unavailable.+IDs are strings; timestamps are RFC3339; storageBytes is an integer."+ )]+ View {+ #[arg(value_name = "USER", help = "User email or numeric ID")]+ user: String,+ },+ #[command(+ about = "Delete an account and schedule its data cleanup",+ after_long_help = "Requires a login less than ten minutes old. Reports whether the server cancelled the subscription."+ )]+ Delete(AdminTarget),+ #[command(+ about = "Disable authenticator-code two-factor authentication",+ after_long_help = "Requires a login less than ten minutes old. Leaves passkeys enabled."+ )]+ DisableTotp(AdminTarget),+ #[command(+ about = "Remove all of a user's passkeys",+ after_long_help = "Leaves authenticator-code two-factor authentication enabled."+ )]+ RemovePasskeys(AdminTarget),+ #[command(+ about = "Change a free plan's storage or expiry",+ after_long_help = "--no-limit sets 100 TB (100 Γ 1024^4 bytes) and expiry 100 calendar years from now.+Alternatively supply --storage, --expires or both; omitted values stay unchanged.+With neither, prompts to choose the preset or custom values. --no-input requires explicit values and --yes.+Sizes accept MB, GB and TB, case-insensitive, using powers of 1024; fractions are allowed.+Dates accept YYYY-MM-DD (00:00 UTC at the start of that date) or RFC3339 with a timezone.+Shows old and new limits before confirmation. Paid plans are rejected."+ )]+ UpdateSubscription(SubscriptionArgs),+}++#[derive(Args)]+pub struct AdminTarget {+ #[arg(value_name = "USER", help = "User email or numeric ID")]+ pub user: String,+ #[arg(long, help = "Approve this change without prompting")]+ pub yes: bool,+}++#[derive(Args)]+pub struct SubscriptionArgs {+ #[command(flatten)]+ pub target: AdminTarget,+ #[arg(long, conflicts_with_all = ["storage", "expires"], help = "Set 100 TB of storage and another 100 years")]+ pub no_limit: bool,+ #[arg(long, value_name = "SIZE", value_parser = crate::admin::parse_size, help = "Storage limit, e.g. 500GB or 1.5TB")]+ pub storage: Option<i64>,+ #[arg(long, value_name = "WHEN", value_parser = crate::admin::parse_date, help = "Expiry date or timestamp")]+ pub expires: Option<i64>,+}++#[derive(Subcommand)]+pub enum AdminEmailCommand {+ #[command(+ about = "Send a fixed test message through the server",+ after_long_help = "Reports request acceptance, not delivery. An unconfigured server may accept without sending.+The server's SMTP configuration can override the supplied sender."+ )]+ Test {+ #[arg(long, help = "Recipient email")]+ to: String,+ #[arg(long, help = "Sender email")]+ from: String,+ #[arg(long, default_value = "Ente", help = "Sender name")]+ from_name: String,+ },+}+#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, ValueEnum)]#[serde(rename_all = "lowercase")]pub enum App {diff --git a/rust/apps/cli-next/src/main.rs b/rust/apps/cli-next/src/main.rsindex 8110492cb1..d75f2c6d94 100644--- a/rust/apps/cli-next/src/main.rs+++ b/rust/apps/cli-next/src/main.rs@@ -1,3 +1,4 @@+mod admin;mod api;mod args;mod auth;@@ -92,6 +93,11 @@ async fn run(cli: Cli) -> Result<()> {clap_complete::generate(shell, &mut command, name, &mut std::io::stdout().lock());Ok(())}+ Command::Admin {+ selector,+ app,+ command,+ } => admin::run(command, selector.account.as_deref(), app, &options).await,}}diff --git a/rust/apps/cli-next/src/output.rs b/rust/apps/cli-next/src/output.rsindex 246dcb0a58..80762cac6f 100644--- a/rust/apps/cli-next/src/output.rs+++ b/rust/apps/cli-next/src/output.rs@@ -3,11 +3,13 @@ use std::io::Write;use anyhow::{Context, Result};use chrono::{DateTime, SecondsFormat};use dialoguer::console::{Alignment, measure_text_width, pad_str};+use ente_accounts::admin::{Subscription, User, UserDetails};use ente_photos::{collections::{Collection, Visibility},files::File,};use serde::Serialize;+use serde_json::json as json_value;use uuid::Uuid;use crate::{@@ -122,6 +124,42 @@ impl FileView {}}+#[derive(Serialize)]+#[serde(rename_all = "camelCase")]+pub struct AdminUserView {+ id: String,+ email: String,+ created_at: String,+}++impl AdminUserView {+ pub fn new(user: &User) -> Result<Self> {+ Ok(Self {+ id: user.id.to_string(),+ email: user.email.clone(),+ created_at: timestamp(user.created_at)?,+ })+ }+}++#[derive(Serialize)]+#[serde(rename_all = "camelCase")]+pub struct SubscriptionView {+ product_id: String,+ pub storage_bytes: i64,+ pub expires_at: String,+}++impl SubscriptionView {+ pub fn new(subscription: &Subscription) -> Result<Self> {+ Ok(Self {+ product_id: subscription.product_id.clone(),+ storage_bytes: subscription.storage,+ expires_at: timestamp(subscription.expiry_time)?,+ })+ }+}+pub fn json(value: &impl Serialize) -> Result<()> {let mut stdout = std::io::stdout().lock();serde_json::to_writer(&mut stdout, value)?;@@ -275,6 +313,62 @@ pub fn file(file: &FileView) -> Result<()> {write_fields(&fields)}+pub fn admin_users(users: Vec<User>, args: &ListArgs, as_json: bool) -> Result<()> {+ list(+ users.iter().map(AdminUserView::new),+ args,+ as_json,+ ["ID", "EMAIL", "CREATED"],+ "No users.",+ |user| [user.id, escape_controls(&user.email), user.created_at],+ )+}++pub fn admin_user(details: &UserDetails, as_json: bool) -> Result<()> {+ let user = AdminUserView::new(&details.user)?;+ let subscription = details+ .subscription+ .as_ref()+ .map(SubscriptionView::new)+ .transpose()?;+ if as_json {+ return json(&json_value!({"id": user.id, "email": user.email,+ "createdAt": user.created_at, "subscription": subscription}));+ }+ let mut fields = vec![+ ("Email", escape_controls(&user.email)),+ ("ID", user.id),+ ("Created", user.created_at),+ ];+ if let Some(subscription) = subscription {+ fields.extend([+ ("Plan", escape_controls(&subscription.product_id)),+ ("Storage", storage_size(subscription.storage_bytes)),+ ("Expires", subscription.expires_at),+ ]);+ } else {+ fields.push(("Subscription", "unavailable".into()));+ }+ write_fields(&fields)+}++pub fn storage_size(bytes: i64) -> String {+ for (unit, divisor) in [+ ("TB", 1024_i64.pow(4)),+ ("GB", 1024_i64.pow(3)),+ ("MB", 1024_i64.pow(2)),+ ] {+ if bytes >= divisor {+ let size = format!("{:.2}", bytes as f64 / divisor as f64);+ return format!(+ "{} {unit} ({bytes} bytes)",+ size.trim_end_matches('0').trim_end_matches('.')+ );+ }+ }+ format!("{bytes} bytes")+}+fn timestamp(micros: i64) -> Result<String> {Ok(DateTime::from_timestamp_micros(micros).context("timestamp is outside the supported range")?@@ -399,7 +493,7 @@ fn write_row<const N: usize>(Ok(())}-fn escape_controls(value: &str) -> String {+pub(crate) fn escape_controls(value: &str) -> String {let mut escaped = String::with_capacity(value.len());for c in value.chars() {if c.is_control() {diff --git a/rust/apps/cli-next/tests/cli.rs b/rust/apps/cli-next/tests/cli.rsindex 7874728f87..5101defc4e 100644--- a/rust/apps/cli-next/tests/cli.rs+++ b/rust/apps/cli-next/tests/cli.rs@@ -28,7 +28,15 @@ fn completion_prints_scripts_without_storage() {assert!(output.stderr.is_empty(), "{shell}");let script = String::from_utf8(output.stdout).unwrap();assert!(serde_json::from_str::<Value>(&script).is_err(), "{shell}");- for item in ["help-all", "account", "paste", "hardlinks", "completion"] {+ for item in [+ "help-all",+ "account",+ "paste",+ "hardlinks",+ "completion",+ "admin",+ "update-subscription",+ ] {assert!(script.contains(item), "{shell}: {item}");}assert!(!path.exists(), "{shell} created the CLI home");@@ -983,21 +991,28 @@ fn failure(output: &Output) -> String {String::from_utf8_lossy(&output.stderr).into_owned()}-#[path = "support/files.rs"]-mod files;+#[path = "support/admin.rs"]+mod admin;++#[cfg(feature = "museum")]+#[path = "support/admin_museum.rs"]+mod admin_museum;#[path = "support/auth.rs"]mod auth;-#[path = "support/paste.rs"]-mod paste;+#[path = "support/export_process.rs"]+mod export_process;++#[path = "support/export_sources.rs"]+mod export_sources;++#[path = "support/files.rs"]+mod files;#[cfg(feature = "museum")]#[path = "support/museum.rs"]mod museum;-#[path = "support/export_sources.rs"]-mod export_sources;--#[path = "support/export_process.rs"]-mod export_process;+#[path = "support/paste.rs"]+mod paste;diff --git a/rust/apps/cli-next/tests/support/admin.rs b/rust/apps/cli-next/tests/support/admin.rsnew file mode 100644index 0000000000..38c31dc16f--- /dev/null+++ b/rust/apps/cli-next/tests/support/admin.rs@@ -0,0 +1,343 @@+use chrono::Datelike;+use mockito::{Matcher, Server};++use super::*;++const USER_ID: i64 = 9007199254740993;+const EMAIL: &str = "[email protected]";++fn details() -> Value {+ json!({"user": {"ID": USER_ID, "email": EMAIL, "creationTime": 1},+ "subscription": {"productID": "free", "storage": 1024, "expiryTime": 1893456000000000i64,+ "originalTransactionID": "existing", "paymentProvider": "", "attributes": {"isCancelled": true}}})+}++fn lookup(server: &mut Server, response: Value) -> mockito::Mock {+ server+ .mock("GET", "/admin/user")+ .match_query(Matcher::UrlEncoded("email".into(), EMAIL.into()))+ .with_body(response.to_string())+ .expect(1)+ .create()+}++#[test]+fn admin_list_and_app_selection() {+ let mut server = Server::new();+ let home = TestHome::new();+ home.seed(&server.url());+ let mut users = vec![details()["user"].clone(); 101];+ users[0]["email"] = json!("alice\[email protected]");+ let list = server+ .mock("GET", "/admin/users")+ .match_query(Matcher::UrlEncoded("sinceTime".into(), "0".into()))+ .match_header("x-client-package", "io.ente.photos")+ .match_header("x-auth-token", b64::encode_url_safe(&[0xfb, 0xef]).as_str())+ .with_body(json!({"users": users}).to_string())+ .expect(3)+ .create();+ let output = success(home.run(&["admin", "user", "list", "--json"]));+ let rows: Value = serde_json::from_slice(&output.stdout).unwrap();+ assert_eq!(rows.as_array().unwrap().len(), 100);+ assert_eq!(rows[0]["id"], USER_ID.to_string());+ assert_eq!(rows[0]["createdAt"], "1970-01-01T00:00:00.000001Z");+ assert!(String::from_utf8(output.stderr).unwrap().contains("--all"));+ let human = success(home.run(&["admin", "user", "list", "--limit", "1"]));+ assert!(+ String::from_utf8(human.stdout)+ .unwrap()+ .contains("alice\\[email protected]")+ );+ assert_eq!(+ home.json(&["admin", "user", "list", "--all"])+ .as_array()+ .unwrap()+ .len(),+ 101+ );+ list.assert();+ let since = server+ .mock("GET", "/admin/users")+ .match_query(Matcher::UrlEncoded(+ "sinceTime".into(),+ "1893456000000000".into(),+ ))+ .with_body(r#"{"users":[]}"#)+ .create();+ assert_eq!(+ home.json(&["admin", "user", "list", "--since", "2030-01-01"]),+ json!([])+ );+ since.assert();+ for (apps, explicit, expected) in [+ (vec!["auth"], None, "auth"),+ (vec!["locker"], None, "locker"),+ (vec!["auth", "photos"], None, "photos"),+ (vec!["auth", "locker"], Some("auth"), "auth"),+ ] {+ let mut state = home.read_vault();+ state["accounts"][0]["sessions"] = apps+ .into_iter()+ .map(|app| (app.to_owned(), json!({"token": [251, 239]})))+ .collect();+ home.write_vault(&state);+ let request = server+ .mock("GET", "/admin/user")+ .match_query(Matcher::UrlEncoded("email".into(), EMAIL.into()))+ .match_header("x-client-package", format!("io.ente.{expected}").as_str())+ .with_body(details().to_string())+ .create();+ let mut args = vec!["admin", "--account", "fixture", "user", "view", EMAIL];+ if let Some(app) = explicit {+ args.extend(["--app", app]);+ }+ assert_eq!(home.json(&args)["id"], USER_ID.to_string());+ request.assert();+ }+ assert!(+ failure(&home.run(&["admin", "user", "view", EMAIL])).contains("select one with --app")+ );+ assert!(+ failure(&home.run(&["admin", "--account", "missing", "user", "list"]))+ .contains("no account named")+ );+}++#[test]+fn admin_mutations_require_confirmation_and_use_resolved_target() {+ for (command, method, path, response, field) in [+ (+ "delete",+ "DELETE",+ "/admin/user/delete",+ json!({"isSubscriptionCancelled": false}),+ "deleted",+ ),+ (+ "disable-totp",+ "POST",+ "/admin/user/disable-2fa",+ json!({}),+ "totpEnabled",+ ),+ (+ "remove-passkeys",+ "POST",+ "/admin/user/disable-passkeys",+ json!({}),+ "passkeysRemoved",+ ),+ ] {+ let mut server = Server::new();+ let home = TestHome::new();+ home.seed(&server.url());+ let id = USER_ID.to_string();+ let get = server+ .mock("GET", "/admin/user")+ .match_query(Matcher::UrlEncoded("id".into(), id.clone()))+ .with_body(details().to_string())+ .expect(1)+ .create();+ let mut change = server+ .mock(method, path)+ .with_body(response.to_string())+ .expect(1);+ change = if method == "DELETE" {+ change.match_query(Matcher::UrlEncoded("email".into(), EMAIL.into()))+ } else {+ change.match_body(Matcher::Json(json!({"userID": USER_ID})))+ };+ let change = change.create();+ let mut args = vec!["admin", "user", command, &id, "--no-input"];+ assert!(failure(&home.run(&args)).contains("supply --yes"));+ args.push("--yes");+ let result = home.json(&args);+ assert_eq!(result["user"]["email"], EMAIL);+ assert_eq!(result[field], command != "disable-totp");+ if command == "delete" {+ assert_eq!(result["cleanupScheduled"], true);+ assert_eq!(result["subscriptionCancelled"], false);+ }+ get.assert();+ change.assert();+ }+}++#[test]+fn admin_subscription_uses_one_snapshot_and_preserves_other_fields() {+ for flags in [+ vec!["--storage", "1.5gb"],+ vec!["--expires", "2031-01-01"],+ vec!["--no-limit"],+ ] {+ let mut server = Server::new();+ let home = TestHome::new();+ home.seed(&server.url());+ let get = lookup(&mut server, details());+ let storage = if flags[0] == "--storage" {+ 1610612736i64+ } else if flags[0] == "--no-limit" {+ 100 * 1024_i64.pow(4)+ } else {+ 1024+ };+ let expiry = (flags[0] != "--no-limit").then_some(if flags[0] == "--expires" {+ 1924992000000000i64+ } else {+ 1893456000000000i64+ });+ let preset_year = chrono::Utc::now().year() + 100;+ let change = server.mock("PUT", "/admin/user/subscription")+ .match_body(Matcher::PartialJson(json!({"userID": USER_ID, "storage": storage,+ "productID": "free", "transactionID": "existing", "paymentProvider": "", "attributes": {"isCancelled": true}})))+ .match_request(move |request| {+ let body: Value = serde_json::from_str(&request.utf8_lossy_body().unwrap()).unwrap();+ let actual = body["expiryTime"].as_i64().unwrap();+ expiry.map_or_else(|| chrono::DateTime::from_timestamp_micros(actual).unwrap().year() == preset_year, |value| actual == value)+ }).with_body("{}").expect(1).create();+ let mut args = vec![+ "admin",+ "user",+ "update-subscription",+ EMAIL,+ "--yes",+ "--no-input",+ ];+ args.extend(flags);+ let result = home.json(&args);+ assert_eq!(result["subscription"]["storageBytes"], storage);+ if let Some(expiry) = expiry {+ assert_eq!(+ result["subscription"]["expiresAt"],+ chrono::DateTime::from_timestamp_micros(expiry)+ .unwrap()+ .to_rfc3339_opts(chrono::SecondsFormat::Micros, true)+ );+ }+ get.assert();+ change.assert();+ }+ let mut server = Server::new();+ let home = TestHome::new();+ home.seed(&server.url());+ let put = server+ .mock("PUT", "/admin/user/subscription")+ .expect(0)+ .create();+ let mut paid = details()["subscription"].clone();+ paid["productID"] = json!("paid");+ for (subscription, expected) in [+ (paid, "not on a free plan"),+ (Value::Null, "did not return a subscription"),+ ] {+ let mut user = details();+ user["subscription"] = subscription;+ let get = lookup(&mut server, user);+ let error = failure(&home.run(&[+ "admin",+ "user",+ "update-subscription",+ EMAIL,+ "--no-limit",+ "--yes",+ ]));+ assert!(error.contains(expected));+ get.assert();+ get.remove();+ }+ put.assert();+}++#[test]+fn admin_failures_do_not_all_recommend_login() {+ for (status, body, stale) in [+ (+ 400,+ json!({"code": "BAD_REQUEST", "message": "Token is too old"}),+ true,+ ),+ (401, json!({"error": "insufficient permissions"}), false),+ (404, json!({}), false),+ (+ 400,+ json!({"code": "BAD_REQUEST", "message": "Other failure"}),+ false,+ ),+ ] {+ let mut server = Server::new();+ let home = TestHome::new();+ home.seed(&server.url());+ let request = server+ .mock("GET", "/admin/users")+ .match_query(Matcher::UrlEncoded("sinceTime".into(), "0".into()))+ .with_status(status)+ .with_body(body.to_string())+ .expect(1)+ .create();+ let output = home.run(&["admin", "user", "list", "--json", "--no-input"]);+ let error = failure(&output);+ assert_eq!(+ error.contains("account login \"fixture\" --app photos"),+ stale,+ "{error}"+ );+ assert!(error.contains(&format!("HTTP {status}")));+ assert!(output.stdout.is_empty());+ request.assert();+ }+}++#[test]+fn admin_email_and_input_contract() {+ let mut server = Server::new();+ let home = TestHome::new();+ home.seed(&server.url());+ let email = server+ .mock("POST", "/admin/mail")+ .match_body(Matcher::Json(+ json!({"to": [EMAIL], "fromEmail": "[email protected]", "fromName": "Ente",+ "subject": "Test mail from Ente", "body": "This is a test mail from Ente"}),+ ))+ .with_body("{}")+ .create();+ assert_eq!(+ home.json(&[+ "admin",+ "email",+ "test",+ "--to",+ EMAIL,+ "--from",+ "[email protected]",+ "--no-input"+ ]),+ json!({"to": EMAIL, "accepted": true})+ );+ email.assert();+ for (flags, expected) in [+ (+ vec!["--yes", "--no-input"],+ "supply --no-limit, --storage or --expires",+ ),+ (+ vec!["--no-limit", "--storage", "2GB"],+ "cannot be used with '--storage <SIZE>'",+ ),+ (+ vec!["--no-limit", "--expires", "2030-01-01"],+ "cannot be used with '--expires <WHEN>'",+ ),+ (vec!["--storage", "2GiB"], "use a size such as"),+ (vec!["--expires", "2030-01-01 12:00:00"], "use YYYY-MM-DD"),+ ] {+ let mut args = vec!["admin", "user", "update-subscription", EMAIL];+ args.extend(flags);+ let output = home.run(&args);+ let error = failure(&output);+ assert!(error.contains(expected), "{error}");+ assert!(output.stdout.is_empty());+ }+ assert!(+ failure(&home.run(&["admin", "user", "list", "--offline"])).contains("require network")+ );+}diff --git a/rust/apps/cli-next/tests/support/admin_museum.rs b/rust/apps/cli-next/tests/support/admin_museum.rsnew file mode 100644index 0000000000..3e51749d04--- /dev/null+++ b/rust/apps/cli-next/tests/support/admin_museum.rs@@ -0,0 +1,639 @@+use std::{+ io::{BufRead, BufReader},+ net::{TcpListener, TcpStream},+ sync::{+ Arc,+ atomic::{AtomicBool, Ordering},+ mpsc,+ },+ thread,+ time::Duration,+};++use chrono::{Months, Utc};+use ente_accounts::{AccountsClient, AccountsClientConfig, TwoFactorSetup};+use ente_test_support::{Museum, TestResult, account_fixture::current_totp};+use sha2::{Digest, Sha256};+use uuid::Uuid;++use super::{+ museum::{create_account, login},+ *,+};++#[test]+fn admin_user_lifecycle() -> TestResult {+ Museum::run(|museum| {+ tokio::runtime::Runtime::new()?.block_on(async {+ let origin = museum.endpoint();+ let admin_email = format!("admin-{}@example.org", Uuid::new_v4());+ let email = format!("managed-{}@example.org", Uuid::new_v4());+ let admin = create_account(origin, &admin_email).await;+ let user = create_account(origin, &email).await;+ let home = TestHome::new();+ login(+ &home,+ "photos",+ &admin_email,+ &["--host", origin, "--no-input"],+ );+ let id = user.user_id.to_string();+ let users = home.json(&["admin", "user", "list", "--all", "--no-input"]);+ assert!(+ users+ .as_array()+ .unwrap()+ .iter()+ .any(|item| item["id"] == id)+ );+ let view = ["admin", "user", "view", id.as_str()];+ let before = home.json(&view);+ assert_eq!(before["email"], email);+ let admin_token = b64::encode_url_safe(&admin.secrets.token);+ let original = api_json(origin, &admin_token, "GET", &format!("/admin/user?id={id}"), None).await;+ api_json(origin, &admin_token, "PUT", "/admin/user/subscription", Some(json!({+ "userID": user.user_id, "productID": "free", "storage": original["subscription"]["storage"],+ "expiryTime": original["subscription"]["expiryTime"], "transactionID": "fixture",+ "paymentProvider": "", "attributes": {"isCancelled": true}+ }))).await;+ assert_eq!(home.json(&["admin", "user", "view", &email]), before);+ assert_eq!(+ home.json(&["admin", "user", "list", "--limit", "1"])+ .as_array()+ .unwrap()+ .len(),+ 1+ );+ assert_eq!(+ home.json(&[+ "admin",+ "user",+ "list",+ "--since",+ before["createdAt"].as_str().unwrap()+ ]),+ json!([])+ );+ let update = [+ "admin",+ "user",+ "update-subscription",+ email.as_str(),+ "--yes",+ "--no-input",+ ];+ let sized = home.json(&[update.as_slice(), &["--storage", "500GB"]].concat());+ assert_eq!(sized["subscription"]["storageBytes"], 500 * 1024_i64.pow(3));+ assert_eq!(+ sized["subscription"]["expiresAt"],+ before["subscription"]["expiresAt"]+ );+ assert_eq!(home.json(&view), sized);+ let dated = home.json(&[update.as_slice(), &["--expires", "2035-01-01"]].concat());+ assert_eq!(+ dated["subscription"]["storageBytes"],+ sized["subscription"]["storageBytes"]+ );+ assert_eq!(+ dated["subscription"]["expiresAt"],+ "2035-01-01T00:00:00.000000Z"+ );+ assert_eq!(home.json(&view), dated);+ let persisted = api_json(origin, &admin_token, "GET", &format!("/admin/user?id={id}"), None).await;+ assert_eq!(persisted["subscription"]["originalTransactionID"], "fixture");+ assert_eq!(persisted["subscription"]["paymentProvider"], "");+ assert_eq!(persisted["subscription"]["attributes"]["isCancelled"], true);+ let lower = Utc::now().checked_add_months(Months::new(1200)).unwrap();+ let unlimited = home.json(&[update.as_slice(), &["--no-limit"]].concat());+ let upper = Utc::now().checked_add_months(Months::new(1200)).unwrap();+ let expires = chrono::DateTime::parse_from_rfc3339(+ unlimited["subscription"]["expiresAt"].as_str().unwrap(),+ )+ .unwrap();+ assert!(+ (lower.timestamp_micros()..=upper.timestamp_micros())+ .contains(&expires.timestamp_micros())+ );+ assert_eq!(+ unlimited["subscription"]["storageBytes"],+ 100 * 1024_i64.pow(4)+ );+ assert_eq!(home.json(&view), unlimited);+ let changed = success(+ home.run(+ &[+ update.as_slice(),+ &[+ "--storage",+ "1.5GB",+ "--expires",+ "2035-01-01T05:30:00+05:30",+ ],+ ]+ .concat(),+ ),+ );+ let preview = String::from_utf8(changed.stderr).unwrap();+ assert!(+ preview.contains("100 TB (109951162777600 bytes) -> 1.5 GB (1610612736 bytes)"),+ "{preview}"+ );+ let rendered = String::from_utf8(changed.stdout).unwrap();+ assert!(rendered.contains("1.5 GB (1610612736 bytes)"), "{rendered}");+ assert_eq!(+ home.json(&view)["subscription"]["expiresAt"],+ "2035-01-01T00:00:00.000000Z"+ );++ for command in ["delete", "disable-totp", "remove-passkeys"] {+ let output = home.run(&["admin", "user", command, &id, "--no-input"]);+ assert!(failure(&output).contains("supply --yes"));+ assert!(output.stdout.is_empty());+ }+ assert!(+ failure(&home.run(&["admin", "user", "list", "--offline"]))+ .contains("require network")+ );++ let client = AccountsClient::new(+ AccountsClientConfig::new("io.ente.photos").with_origin(origin),+ )?;+ client.set_auth_token(Some(b64::encode_url_safe(&admin.secrets.token)));+ let mut subscription = client.admin_user(&id).await?.subscription.unwrap();+ subscription.product_id = "paid".into();+ client+ .admin_update_subscription(user.user_id, &subscription)+ .await?;+ assert!(+ failure(&home.run(&[update.as_slice(), &["--storage", "2GB"]].concat()))+ .contains("not on a free plan")+ );+ assert_eq!(home.json(&view)["subscription"]["productId"], "paid");+ subscription.product_id = "free".into();+ client+ .admin_update_subscription(user.user_id, &subscription)+ .await?;++ let missing = create_account(origin, &format!("missing-{}@example.org", Uuid::new_v4())).await;+ let missing_id = missing.user_id.to_string();+ museum.execute_sql(&format!("DELETE FROM subscriptions WHERE user_id = {missing_id}"))?;+ assert_eq!(home.json(&["admin", "user", "view", &missing_id])["subscription"], Value::Null);+ assert!(failure(&home.run(&["admin", "user", "update-subscription", &missing_id, "--no-limit", "--yes", "--no-input"])).contains("did not return a subscription"));++ for app in ["auth", "locker"] {+ login(&home, app, &admin_email, &["--host", origin, "--no-input"]);+ }+ let saved = home.read_vault();+ for (apps, expected, explicit) in [+ (vec!["auth"], "auth", None),+ (vec!["locker"], "locker", None),+ (vec!["photos", "auth", "locker"], "photos", None),+ (vec!["auth", "locker"], "auth", Some("auth")),+ ] {+ let mut state = saved.clone();+ let sessions = state["accounts"][0]["sessions"].as_object_mut().unwrap();+ sessions.retain(|app, _| apps.contains(&app.as_str()));+ for (app, session) in sessions {+ if app != expected {+ session["token"] = json!(vec![0; 32]);+ }+ }+ home.write_vault(&state);+ let mut args = vec!["admin", "user", "view", &id];+ if let Some(app) = explicit {+ args.extend(["--app", app]);+ }+ assert_eq!(home.json(&args)["id"], id);+ }+ assert!(failure(&home.run(&view)).contains("select one with --app"));+ home.write_vault(&saved);+ let ordinary = TestHome::new();+ login(+ &ordinary,+ "photos",+ &email,+ &["--host", origin, "--no-input"],+ );+ let error = failure(&ordinary.run(&["admin", "user", "list", "--no-input"]));+ assert!(error.contains("HTTP 401"));+ assert!(!error.contains("account login"));+ museum.execute_sql(&format!(+ "UPDATE tokens SET creation_time = creation_time - 660000000 WHERE user_id = {}",+ admin.user_id+ ))?;+ for args in [+ vec!["list"],+ vec!["delete", &id, "--yes"],+ vec!["disable-totp", &id, "--yes"],+ ] {+ let output =+ home.run(&[&["admin", "user"][..], args.as_slice(), &["--no-input"]].concat());+ let error = failure(&output);+ assert!(+ error.contains("HTTP 400") && error.contains("account login"),+ "{error}"+ );+ assert!(output.stdout.is_empty());+ }+ assert_eq!(+ home.json(&["admin", "user", "view", &admin_email])["id"],+ admin.user_id.to_string()+ );+ login(&home, "photos", &admin_email, &["--host", origin, "--no-input"]);+ let deleted = home.json(&["admin", "user", "delete", &id, "--yes", "--no-input"]);+ assert_eq!(deleted["deleted"], true);+ assert_eq!(deleted["cleanupScheduled"], true);+ assert_eq!(deleted["subscriptionCancelled"], true);+ assert!(failure(&home.run(&view)).contains("HTTP 404"));+ assert!(+ !home+ .json(&["admin", "user", "list", "--all"])+ .as_array()+ .unwrap()+ .iter()+ .any(|item| item["id"] == id)+ );+ Ok(())+ })+ })+}++#[cfg(unix)]+#[test]+fn admin_interactive_subscription() -> TestResult {+ Museum::run_async(|origin| async move {+ let admin_email = format!("admin-{}@example.org", Uuid::new_v4());+ let email = format!("interactive-{}@example.org", Uuid::new_v4());+ create_account(&origin, &admin_email).await;+ create_account(&origin, &email).await;+ let home = TestHome::new();+ login(+ &home,+ "photos",+ &admin_email,+ &["--host", &origin, "--no-input"],+ );+ for mode in ["decline", "preset", "storage", "expiry", "empty"] {+ let before = home.json(&["admin", "user", "view", &email]);+ let output = Command::new("python3")+ .env_clear()+ .args([+ concat!(env!("CARGO_MANIFEST_DIR"), "/tests/support/admin_prompt.py"),+ env!("CARGO_BIN_EXE_ente-cli-next"),+ &email,+ mode,+ ])+ .env("ENTE_CLI_HOME", home.dir.path())+ .env("ENTE_CLI_VAULT_KEY", b64::encode(home.key.as_bytes()))+ .output()?;+ assert!(+ output.status.success(),+ "{mode}: {}",+ String::from_utf8_lossy(&output.stderr)+ );+ let after = home.json(&["admin", "user", "view", &email]);+ match mode {+ "decline" | "empty" => assert_eq!(after, before),+ "preset" => {+ assert_eq!(after["subscription"]["storageBytes"], 100 * 1024_i64.pow(4))+ }+ "storage" => {+ assert_eq!(after["subscription"]["storageBytes"], 1572864);+ assert_eq!(+ after["subscription"]["expiresAt"],+ before["subscription"]["expiresAt"]+ );+ }+ "expiry" => {+ assert_eq!(+ after["subscription"]["storageBytes"],+ before["subscription"]["storageBytes"]+ );+ assert_eq!(+ after["subscription"]["expiresAt"],+ "2035-01-01T00:00:00.000000Z"+ );+ }+ _ => unreachable!(),+ }+ }+ Ok(())+ })+}++#[test]+fn admin_security_resets() -> TestResult {+ Museum::run_async(|origin| async move {+ let admin_email = format!("admin-{}@example.org", Uuid::new_v4());+ let email = format!("security-{}@example.org", Uuid::new_v4());+ create_account(&origin, &admin_email).await;+ let user = create_account(&origin, &email).await;+ let home = TestHome::new();+ login(+ &home,+ "photos",+ &admin_email,+ &["--host", &origin, "--no-input"],+ );+ let client =+ AccountsClient::new(AccountsClientConfig::new("io.ente.photos").with_origin(&origin))?;+ client.set_auth_token(Some(b64::encode_url_safe(&user.secrets.token)));+ let setup =+ TwoFactorSetup::start(&client, &user.secrets.master_key, &user.key_attributes).await?;+ setup+ .enable(&client, ¤t_totp(&setup.secret_code)?)+ .await?;+ assert!(client.get_two_factor_status().await?);+ let token = api_json(+ &origin,+ &b64::encode_url_safe(&user.secrets.token),+ "GET",+ "/users/accounts-token",+ None,+ )+ .await;+ let token = token["accountsToken"].as_str().unwrap();+ for _ in 0..2 {+ register_passkey(&origin, token).await;+ }+ assert_eq!(+ api_json(&origin, token, "GET", "/passkeys", None).await["passkeys"]+ .as_array()+ .unwrap()+ .len(),+ 2+ );+ assert_eq!(+ home.json(&[+ "admin",+ "user",+ "remove-passkeys",+ &email,+ "--yes",+ "--no-input"+ ])["passkeysRemoved"],+ true+ );+ assert!(api_json(&origin, token, "GET", "/passkeys", None).await["passkeys"].is_null());+ assert!(client.get_two_factor_status().await?);+ register_passkey(&origin, token).await;+ assert_eq!(+ home.json(&[+ "admin",+ "user",+ "disable-totp",+ &email,+ "--yes",+ "--no-input"+ ])["totpEnabled"],+ false+ );+ assert!(!client.get_two_factor_status().await?);+ assert_eq!(+ api_json(&origin, token, "GET", "/passkeys", None).await["passkeys"]+ .as_array()+ .unwrap()+ .len(),+ 1+ );+ home.json(&[+ "admin",+ "user",+ "remove-passkeys",+ &email,+ "--yes",+ "--no-input",+ ]);+ let cleared = TestHome::new();+ login(+ &cleared,+ "photos",+ &email,+ &["--host", &origin, "--no-input"],+ );+ Ok(())+ })+}++#[test]+fn admin_email_reaches_smtp() -> TestResult {+ for override_sender in [false, true] {+ let inbox = MailInbox::new();+ let port = inbox.port.to_string();+ Museum::run_with_env(+ &[+ ("ENTE_SMTP_HOST", "127.0.0.1"),+ ("ENTE_SMTP_PORT", &port),+ ("ENTE_SMTP_USERNAME", ""),+ ("ENTE_SMTP_PASSWORD", ""),+ ("ENTE_SMTP_ENCRYPTION", ""),+ (+ "ENTE_SMTP_EMAIL",+ if override_sender {+ } else {+ ""+ },+ ),+ (+ "ENTE_SMTP_SENDER_NAME",+ if override_sender { "Server sender" } else { "" },+ ),+ ],+ |museum| {+ tokio::runtime::Runtime::new()?.block_on(async {+ let email = format!("admin-{}@example.org", Uuid::new_v4());+ create_account(museum.endpoint(), &email).await;+ let home = TestHome::new();+ login(+ &home,+ "photos",+ &email,+ &["--host", museum.endpoint(), "--no-input"],+ );+ for from_name in [None, Some("CLI sender")] {+ let mut args = vec![+ "admin",+ "email",+ "test",+ "--to",+ "[email protected]",+ "--from",+ "[email protected]",+ "--no-input",+ ];+ if let Some(name) = from_name {+ args.extend(["--from-name", name]);+ }+ assert_eq!(+ home.json(&args),+ json!({"to": "[email protected]", "accepted": true})+ );+ let message = inbox.receive_test();+ let sender = if override_sender {+ } else {+ };+ let name = if override_sender {+ "Server sender"+ } else {+ from_name.unwrap_or("Ente")+ };+ assert!(+ message.contains(&format!("MAIL FROM:<{sender}>")),+ "{message}"+ );+ assert!(+ message.contains(&format!("From: \"{name}\" <{sender}>")),+ "{message}"+ );+ assert!(+ message.contains("RCPT TO:<[email protected]>"),+ "{message}"+ );+ assert_eq!(message.matches("RCPT TO:").count(), 1);+ assert!(+ message.contains("This is a test mail from Ente"),+ "{message}"+ );+ }+ Ok(())+ })+ },+ )?;+ }+ Ok(())+}++async fn api_json(+ origin: &str,+ token: &str,+ method: &str,+ path: &str,+ body: Option<Value>,+) -> Value {+ let request = reqwest::Client::new()+ .request(method.parse().unwrap(), format!("{origin}{path}"))+ .header("x-auth-token", token)+ .header("x-client-package", "io.ente.photos");+ let response = if let Some(body) = body {+ request.json(&body)+ } else {+ request+ }+ .send()+ .await+ .unwrap();+ let status = response.status();+ let text = response.text().await.unwrap();+ assert!(status.is_success(), "{method} {path}: {status}: {text}");+ serde_json::from_str(&text).unwrap()+}++async fn register_passkey(origin: &str, token: &str) {+ let begin = api_json(origin, token, "POST", "/passkeys/registration/begin", None).await;+ let id = Uuid::new_v4();+ let id_string = b64::encode_url_safe_no_padding(id.as_bytes());+ let options = &begin["options"]["publicKey"];+ let data = json!({"type": "webauthn.create", "challenge": options["challenge"], "origin": "http://localhost:3001"});+ let mut auth_data = Sha256::digest(options["rp"]["id"].as_str().unwrap().as_bytes()).to_vec();+ auth_data.extend_from_slice(&[0x45, 0, 0, 0, 0]);+ auth_data.extend_from_slice(&[0; 16]);+ auth_data.extend_from_slice(&[0, 16]);+ auth_data.extend_from_slice(id.as_bytes());+ // COSE P-256 public key; registration uses no attestation signature.+ auth_data.extend(b64::decode("pQECAyYgASFYIGsX0fLhLEJH+Lzm5WOkQPJ3A32BLeszoPShOUXYmMKWIlggT+NC4v4af5uO5+tKfA+eFivOM1drMV7Oy7ZAaDe/UfU=").unwrap());+ let mut attestation = b"\xa3\x63fmt\x64none\x67attStmt\xa0\x68authData\x58".to_vec();+ attestation.push(u8::try_from(auth_data.len()).unwrap());+ attestation.extend(auth_data);+ api_json(origin, token, "POST", &format!("/passkeys/registration/finish?sessionID={}&friendlyName=Test", begin["sessionID"].as_str().unwrap()), Some(json!({+ "id": id_string, "rawId": id_string, "type": "public-key",+ "response": {"clientDataJSON": b64::encode_url_safe_no_padding(&serde_json::to_vec(&data).unwrap()),+ "attestationObject": b64::encode_url_safe_no_padding(&attestation), "transports": ["internal"]}+ }))).await;+}++struct MailInbox {+ port: u16,+ messages: mpsc::Receiver<String>,+ stop: Arc<AtomicBool>,+ worker: Option<thread::JoinHandle<()>>,+}++impl MailInbox {+ fn new() -> Self {+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();+ let port = listener.local_addr().unwrap().port();+ listener.set_nonblocking(true).unwrap();+ let (send, messages) = mpsc::channel();+ let stop = Arc::new(AtomicBool::new(false));+ let done = stop.clone();+ let worker = thread::spawn(move || {+ while !done.load(Ordering::Relaxed) {+ match listener.accept() {+ Ok((stream, _)) => receive_mail(stream, &send),+ Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {+ thread::sleep(Duration::from_millis(10))+ }+ Err(error) => panic!("SMTP listener: {error}"),+ }+ }+ });+ Self {+ port,+ messages,+ stop,+ worker: Some(worker),+ }+ }++ fn receive_test(&self) -> String {+ loop {+ let message = self.messages.recv_timeout(Duration::from_secs(5)).unwrap();+ if message.contains("Subject: Test mail from Ente") {+ return message;+ }+ }+ }+}++impl Drop for MailInbox {+ fn drop(&mut self) {+ self.stop.store(true, Ordering::Relaxed);+ let _ = self.worker.take().unwrap().join();+ }+}++fn receive_mail(mut stream: TcpStream, messages: &mpsc::Sender<String>) {+ stream.set_nonblocking(false).unwrap();+ stream+ .set_read_timeout(Some(Duration::from_secs(5)))+ .unwrap();+ stream.write_all(b"220 localhost\r\n").unwrap();+ let mut reader = BufReader::new(stream.try_clone().unwrap());+ let mut message = String::new();+ loop {+ let mut line = String::new();+ if reader.read_line(&mut line).unwrap() == 0 {+ break;+ }+ message.push_str(&line);+ if line == "DATA\r\n" {+ stream.write_all(b"354 Send data\r\n").unwrap();+ loop {+ line.clear();+ assert_ne!(reader.read_line(&mut line).unwrap(), 0);+ if line == ".\r\n" {+ break;+ }+ message.push_str(&line);+ }+ messages.send(std::mem::take(&mut message)).unwrap();+ } else if line == "QUIT\r\n" {+ stream.write_all(b"221 Bye\r\n").unwrap();+ break;+ }+ stream.write_all(b"250 OK\r\n").unwrap();+ }+}diff --git a/rust/apps/cli-next/tests/support/admin_prompt.py b/rust/apps/cli-next/tests/support/admin_prompt.pynew file mode 100644index 0000000000..b2f6236656--- /dev/null+++ b/rust/apps/cli-next/tests/support/admin_prompt.py@@ -0,0 +1,69 @@+import os+import pty+import select+import signal+import sys+import time++binary, email, mode = sys.argv[1:]+args = [binary, "admin", "user", "update-subscription", email]+if mode == "decline":+ args += ["--storage", "2GB"]+pid, fd = pty.fork()+if pid == 0:+ os.execv(binary, args)+output = b""+cursor = 0+deadline = time.monotonic() + 20+++def read_output():+ global output+ if select.select([fd], [], [], 0.1)[0]:+ try:+ output += os.read(fd, 65536)+ except OSError:+ pass+++def expect(text):+ global cursor+ expected = text.encode()+ while expected not in output[cursor:]:+ assert time.monotonic() < deadline, output.decode(errors="replace")+ read_output()+ cursor = output.index(expected, cursor) + len(expected)+++try:+ if mode != "decline":+ expect("Subscription limits")+ os.write(fd, b"\x1b[B\r" if mode == "preset" else b"\x1b[B\x1b[B\r")+ if mode in ("storage", "expiry", "empty"):+ expect("Storage (")+ os.write(fd, b"1.5MB\r" if mode == "storage" else b"\r")+ expect("Expiry (")+ os.write(fd, b"2035-01-01\r" if mode == "expiry" else b"\r")+ if mode == "empty":+ expect("no subscription changes supplied")+ else:+ expect("Update subscription for")+ assert b"Storage:" in output and b"Expires:" in output+ os.write(fd, b"\r" if mode == "decline" else b"y\r")+ while time.monotonic() < deadline:+ done, status = os.waitpid(pid, os.WNOHANG)+ if done:+ assert os.waitstatus_to_exitcode(status) == (+ 1 if mode in ("decline", "empty") else 0+ ), output.decode(errors="replace")+ break+ read_output()+ else:+ raise AssertionError(output.decode(errors="replace"))+finally:+ os.close(fd)+ try:+ os.kill(pid, signal.SIGKILL)+ os.waitpid(pid, 0)+ except ProcessLookupError:+ passdiff --git a/rust/apps/cli-next/tests/support/museum.rs b/rust/apps/cli-next/tests/support/museum.rsindex b5239ad601..67d10592fa 100644--- a/rust/apps/cli-next/tests/support/museum.rs+++ b/rust/apps/cli-next/tests/support/museum.rs@@ -679,7 +679,7 @@ async fn exercise(origin: String) -> TestResult {Ok(())}-fn login(home: &TestHome, app: &str, email: &str, options: &[&str]) {+pub(super) fn login(home: &TestHome, app: &str, email: &str, options: &[&str]) {let mut args = vec!["account", "login", "--app", app, "--input", "-", "--json"];args.extend_from_slice(options);let output = success(home.with_input(&args, &credentials(email)));@@ -700,7 +700,7 @@ fn credentials(email: &str) -> Vec<u8> {.unwrap()}-async fn create_account(origin: &str, email: &str) -> AuthenticatedAccount {+pub(super) async fn create_account(origin: &str, email: &str) -> AuthenticatedAccount {let client =AccountsClient::new(AccountsClientConfig::new("io.ente.photos").with_origin(origin)).unwrap();diff --git a/rust/crates/accounts/Cargo.toml b/rust/crates/accounts/Cargo.tomlindex 6255f982a9..8f8ed8613b 100644--- a/rust/crates/accounts/Cargo.toml+++ b/rust/crates/accounts/Cargo.toml@@ -22,9 +22,7 @@ zeroize.workspace = true[dev-dependencies]ente-test-support.workspace = true-hmac.workspace = truemockito.workspace = true-sha1.workspace = truetokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] }[lints]diff --git a/rust/crates/accounts/src/admin.rs b/rust/crates/accounts/src/admin.rsnew file mode 100644index 0000000000..848f4a6c0e--- /dev/null+++ b/rust/crates/accounts/src/admin.rs@@ -0,0 +1,148 @@+use serde::Deserialize;+use serde_json::{Value, json};++use crate::{AccountsClient, Result};++#[derive(Deserialize)]+pub struct User {+ #[serde(rename = "ID")]+ pub id: i64,+ pub email: String,+ #[serde(rename = "creationTime")]+ pub created_at: i64,+}++#[derive(Deserialize)]+pub struct UserDetails {+ pub user: User,+ pub subscription: Option<Subscription>,+}++#[derive(Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct Subscription {+ #[serde(rename = "productID")]+ pub product_id: String,+ pub storage: i64,+ pub expiry_time: i64,+ #[serde(rename = "originalTransactionID")]+ transaction_id: String,+ payment_provider: String,+ attributes: Value,+}++#[derive(Deserialize)]+#[serde(rename_all = "camelCase")]+pub struct Deletion {+ pub is_subscription_cancelled: bool,+}++impl AccountsClient {+ pub async fn admin_user(&self, user: &str) -> Result<UserDetails> {+ let key = if user.parse::<i64>().is_ok() {+ "id"+ } else {+ "email"+ };+ Ok(self+ .api+ .get("/admin/user")+ .query(&[(key, user)])+ .send()+ .await?+ .error_for_code()+ .await?+ .json()+ .await?)+ }++ pub async fn admin_users(&self, since: i64) -> Result<Vec<User>> {+ #[derive(Deserialize)]+ struct Response {+ users: Vec<User>,+ }+ let response: Response = self+ .api+ .get("/admin/users")+ .query(&[("sinceTime", since)])+ .send()+ .await?+ .error_for_code()+ .await?+ .json()+ .await?;+ Ok(response.users)+ }++ pub async fn admin_delete_user(&self, email: &str) -> Result<Deletion> {+ Ok(self+ .api+ .delete("/admin/user/delete")+ .query(&[("email", email)])+ .send()+ .await?+ .error_for_code()+ .await?+ .json()+ .await?)+ }++ pub async fn admin_disable_totp(&self, user_id: i64) -> Result<()> {+ self.api+ .post("/admin/user/disable-2fa")+ .json(&json!({"userID": user_id}))+ .send()+ .await?+ .error_for_code()+ .await?;+ Ok(())+ }++ pub async fn admin_remove_passkeys(&self, user_id: i64) -> Result<()> {+ self.api+ .post("/admin/user/disable-passkeys")+ .json(&json!({"userID": user_id}))+ .send()+ .await?+ .error_for_code()+ .await?;+ Ok(())+ }++ pub async fn admin_update_subscription(+ &self,+ user_id: i64,+ subscription: &Subscription,+ ) -> Result<()> {+ self.api+ .put("/admin/user/subscription")+ .json(&json!({+ "userID": user_id,+ "storage": subscription.storage,+ "expiryTime": subscription.expiry_time,+ "productID": subscription.product_id,+ "transactionID": subscription.transaction_id,+ "paymentProvider": subscription.payment_provider,+ "attributes": subscription.attributes,+ }))+ .send()+ .await?+ .error_for_code()+ .await?;+ Ok(())+ }++ pub async fn admin_test_email(&self, to: &str, from: &str, from_name: &str) -> Result<()> {+ self.api+ .post("/admin/mail")+ .json(&json!({+ "to": [to], "fromEmail": from, "fromName": from_name,+ "subject": "Test mail from Ente", "body": "This is a test mail from Ente",+ }))+ .send()+ .await?+ .error_for_code()+ .await?;+ Ok(())+ }+}diff --git a/rust/crates/accounts/src/client.rs b/rust/crates/accounts/src/client.rsindex 653d5202b5..2eda4e19e2 100644--- a/rust/crates/accounts/src/client.rs+++ b/rust/crates/accounts/src/client.rs@@ -29,7 +29,7 @@ fn require_srp_m2(auth_response: &AuthResponse) -> Result<&str> {}pub struct AccountsClient {- api: Api,+ pub(crate) api: Api,client_package: String,}diff --git a/rust/crates/accounts/src/lib.rs b/rust/crates/accounts/src/lib.rsindex 347686321e..9519e41269 100644--- a/rust/crates/accounts/src/lib.rs+++ b/rust/crates/accounts/src/lib.rs@@ -1,3 +1,4 @@+pub mod admin;pub mod auth;pub mod client;pub mod error;diff --git a/rust/crates/accounts/tests/accounts.rs b/rust/crates/accounts/tests/accounts.rsindex 6449b05a39..3df0856883 100644--- a/rust/crates/accounts/tests/accounts.rs+++ b/rust/crates/accounts/tests/accounts.rs@@ -1,20 +1,15 @@#![cfg(test)]#![cfg(feature = "museum")]-use std::time::{SystemTime, UNIX_EPOCH};-use ente_accounts::{AccountsClient, AccountsClientConfig, TwoFactorSetup,login::{LoginFlow, LoginStep},signup::Signup,};+use ente_test_support::account_fixture::current_totp;use ente_test_support::{HARDCODED_OTT, HARDCODED_OTT_EMAIL_SUFFIX, Museum, TestResult};-use hmac::{Hmac, KeyInit, Mac};-use sha1::Sha1;use uuid::Uuid;-type HmacSha1 = Hmac<Sha1>;-#[test]fn accounts() -> TestResult {Museum::run_async(run)@@ -44,7 +39,7 @@ async fn run(endpoint: String) -> TestResult {).await?;setup- .enable(&client, ¤t_totp(&setup.secret_code))+ .enable(&client, ¤t_totp(&setup.secret_code)?).await?;let login_client = accounts_client(endpoint);@@ -54,7 +49,7 @@ async fn run(endpoint: String) -> TestResult {LoginStep::EmailCode => flow.submit_code(&login_client, HARDCODED_OTT).await?,LoginStep::Password => flow.submit_password(&login_client, &password).await?,LoginStep::SecondFactor { totp: true, .. } => {- flow.submit_code(&login_client, ¤t_totp(&setup.secret_code))+ flow.submit_code(&login_client, ¤t_totp(&setup.secret_code)?).await?}LoginStep::SecondFactor { .. } => panic!("expected authenticator verification"),@@ -76,41 +71,3 @@ fn accounts_client(endpoint: &str) -> AccountsClient {).unwrap()}--fn current_totp(secret: &str) -> String {- let key = decode_base32(secret);- let counter = SystemTime::now()- .duration_since(UNIX_EPOCH)- .unwrap()- .as_secs()- / 30;- let mut mac = HmacSha1::new_from_slice(&key).unwrap();- mac.update(&counter.to_be_bytes());- let digest = mac.finalize().into_bytes();- let offset = (digest[19] & 0x0f) as usize;- let binary = ((digest[offset] as u32 & 0x7f) << 24)- | ((digest[offset + 1] as u32) << 16)- | ((digest[offset + 2] as u32) << 8)- | digest[offset + 3] as u32;- format!("{:06}", binary % 1_000_000)-}--fn decode_base32(secret: &str) -> Vec<u8> {- let mut output = Vec::new();- let mut buffer = 0u32;- let mut bits = 0u8;- for ch in secret.chars().filter(|ch| *ch != '=') {- let value = match ch {- 'A'..='Z' => ch as u8 - b'A',- '2'..='7' => ch as u8 - b'2' + 26,- _ => panic!("invalid base32 character: {ch}"),- } as u32;- buffer = (buffer << 5) | value;- bits += 5;- while bits >= 8 {- bits -= 8;- output.push(((buffer >> bits) & 0xff) as u8);- }- }- output-}diff --git a/rust/crates/core/src/http.rs b/rust/crates/core/src/http.rsindex b18f68aa58..bbc7c64bc3 100644--- a/rust/crates/core/src/http.rs+++ b/rust/crates/core/src/http.rs@@ -38,6 +38,7 @@ pub enum Error {status: u16,path: String,code: String,+ message: Option<String>,},#[error(transparent)]@@ -351,6 +352,7 @@ impl Response {status,path,code: envelope.code,+ message: envelope.message,}),Err(_) => Err(Error::Http { status, path }),}@@ -411,6 +413,7 @@ mod body_stream {#[derive(Deserialize)]struct ApiErrorEnvelope {code: String,+ message: Option<String>,}#[derive(Deserialize, Debug)]@@ -702,8 +705,8 @@ mod tests {assert!(matches!(&err,- Error::Api { status: 401, path, code }- if path == "/x" && code == "SESSION_EXPIRED"+ Error::Api { status: 401, path, code, message }+ if path == "/x" && code == "SESSION_EXPIRED" && message.as_deref() == Some("session expired")));assert_eq!(err.to_string(), "HTTP 401 SESSION_EXPIRED at /x");assert_eq!(err.status_code(), Some(401));diff --git a/rust/crates/test-support/Cargo.toml b/rust/crates/test-support/Cargo.tomlindex cd571b2262..86c3defcc3 100644--- a/rust/crates/test-support/Cargo.toml+++ b/rust/crates/test-support/Cargo.toml@@ -8,7 +8,9 @@ publish = falsedirs.workspace = trueente-accounts.workspace = trueente-core.workspace = true+hmac.workspace = truepostgresql_embedded = { version = "0.20.4", default-features = false, features = ["blocking", "rustls", "theseus"] }+sha1.workspace = truetokio = { workspace = true, features = ["rt-multi-thread"] }uuid = { workspace = true, features = ["v4"] }diff --git a/rust/crates/test-support/src/account_fixture.rs b/rust/crates/test-support/src/account_fixture.rsindex 9263d6e84f..5615602359 100644--- a/rust/crates/test-support/src/account_fixture.rs+++ b/rust/crates/test-support/src/account_fixture.rs@@ -1,7 +1,11 @@+use std::time::{SystemTime, UNIX_EPOCH};+use ente_accounts::auth::{SrpSession, generate_srp_setup_with_login_key};use ente_accounts::{AccountsClient, AccountsClientConfig, KeyAttributes, models::SetupSrpRequest};use ente_core::b64;use ente_core::crypto::{Key, SecretKey, kdf, secretbox};+use hmac::{Hmac, KeyInit, Mac};+use sha1::Sha1;use uuid::Uuid;use crate::{HARDCODED_OTT, HARDCODED_OTT_EMAIL_SUFFIX, TestResult};@@ -111,6 +115,40 @@ pub async fn create_account(endpoint: &str, email_prefix: &str) -> TestResult<Te})}+pub fn current_totp(secret: &str) -> TestResult<String> {+ let key = decode_base32(secret)?;+ let counter = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs() / 30;+ let mut mac = Hmac::<Sha1>::new_from_slice(&key)?;+ mac.update(&counter.to_be_bytes());+ let digest = mac.finalize().into_bytes();+ let offset = (digest[19] & 0x0f) as usize;+ let binary = ((digest[offset] as u32 & 0x7f) << 24)+ | ((digest[offset + 1] as u32) << 16)+ | ((digest[offset + 2] as u32) << 8)+ | digest[offset + 3] as u32;+ Ok(format!("{:06}", binary % 1_000_000))+}++fn decode_base32(secret: &str) -> TestResult<Vec<u8>> {+ let mut output = Vec::new();+ let mut buffer = 0u32;+ let mut bits = 0u8;+ for ch in secret.chars().filter(|ch| *ch != '=') {+ let value = match ch {+ 'A'..='Z' => ch as u8 - b'A',+ '2'..='7' => ch as u8 - b'2' + 26,+ _ => return Err(format!("invalid base32 character: {ch}").into()),+ } as u32;+ buffer = (buffer << 5) | value;+ bits += 5;+ while bits >= 8 {+ bits -= 8;+ output.push(((buffer >> bits) & 0xff) as u8);+ }+ }+ Ok(output)+}+fn pad_left(data: &[u8], len: usize) -> Vec<u8> {let mut padded = vec![0; len.saturating_sub(data.len())];padded.extend_from_slice(data);diff --git a/rust/crates/test-support/src/museum.rs b/rust/crates/test-support/src/museum.rsindex 3983c1283e..96fe4ea307 100644--- a/rust/crates/test-support/src/museum.rs+++ b/rust/crates/test-support/src/museum.rs@@ -25,7 +25,14 @@ pub struct Museum {impl Museum {pub fn run(test: impl FnOnce(&Self) -> TestResult) -> TestResult {- let mut museum = Self::start()?;+ Self::run_with_env(&[], test)+ }++ pub fn run_with_env(+ env: &[(&str, &str)],+ test: impl FnOnce(&Self) -> TestResult,+ ) -> TestResult {+ let mut museum = Self::start(env)?;match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| test(&museum))) {Ok(Ok(())) => Ok(()),Ok(Err(error)) => {@@ -47,10 +54,9 @@ impl Museum {Self::run(|museum| tokio::runtime::Runtime::new()?.block_on(test(museum.endpoint.clone())))}- fn start() -> TestResult<Self> {+ fn start(env: &[(&str, &str)]) -> TestResult<Self> {let server_dir = server_dir()?;let mut temp_dir = TempDir::new("ente-test")?;- let log_dir = temp_dir.path().join("logs");let museum_port = free_port()?;let endpoint = format!("http://{LOCAL_HOST}:{museum_port}");let museum_config_file = temp_dir.path().join("museum.yaml");@@ -62,7 +68,7 @@ impl Museum {let postgres = postgres::start()?;let server = server::start(&server_dir,- &log_dir,+ env,&museum_config_file,museum_port,&museum_bin,@@ -100,6 +106,10 @@ impl Museum {pub fn object_store(&self) -> std::sync::Arc<crate::ObjectStoreControl> {self._object_store.control.clone()}++ pub fn execute_sql(&self, sql: &str) -> TestResult {+ self._postgres.execute_sql(sql)+ }}fn server_dir() -> TestResult<PathBuf> {diff --git a/rust/crates/test-support/src/postgres.rs b/rust/crates/test-support/src/postgres.rsindex 552e1ef954..b43771b359 100644--- a/rust/crates/test-support/src/postgres.rs+++ b/rust/crates/test-support/src/postgres.rs@@ -1,4 +1,4 @@-use std::path::PathBuf;+use std::{path::PathBuf, process::Command};use postgresql_embedded::Settings;use postgresql_embedded::blocking::PostgreSQL;@@ -34,6 +34,36 @@ impl Postgres {pub fn database(&self) -> &str {DATABASE}++ pub fn execute_sql(&self, sql: &str) -> TestResult {+ let output = Command::new(+ self.inner+ .settings()+ .binary_dir()+ .join(format!("psql{}", std::env::consts::EXE_SUFFIX)),+ )+ .env("PGPASSWORD", self.password())+ .args([+ "-X",+ "-v",+ "ON_ERROR_STOP=1",+ "-h",+ self.host(),+ "-p",+ &self.port().to_string(),+ "-U",+ self.username(),+ "-d",+ self.database(),+ "-c",+ sql,+ ])+ .output()?;+ if !output.status.success() {+ return Err(String::from_utf8_lossy(&output.stderr).into_owned().into());+ }+ Ok(())+ }}pub fn start() -> TestResult<Postgres> {diff --git a/rust/crates/test-support/src/server.rs b/rust/crates/test-support/src/server.rsindex adb130255f..aae0e60458 100644--- a/rust/crates/test-support/src/server.rs+++ b/rust/crates/test-support/src/server.rs@@ -14,7 +14,7 @@ use crate::{pub fn start(server_dir: &Path,- log_dir: &Path,+ env: &[(&str, &str)],config_file: &Path,museum_port: u16,museum_bin: &Path,@@ -50,9 +50,11 @@ pub fn start("ENTE_INTERNAL_HARDCODED_OTT_LOCAL_DOMAIN_VALUE",HARDCODED_OTT,)- .env("ENTE_JOBS_CRON_SKIP", "true");+ .env("ENTE_JOBS_CRON_SKIP", "true")+ .envs(env.iter().copied());- let mut museum = ChildProcess::spawn("museum", &mut command, log_dir)?;+ let mut museum =+ ChildProcess::spawn("museum", &mut command, &config_file.with_file_name("logs"))?;wait_for_museum(&mut museum, museum_port)?;Ok(museum)}