|
| 1 | +use serde_json::Value; |
| 2 | +use soroban_test::{AssertExt, TestEnv}; |
| 3 | + |
| 4 | +use crate::integration::{ |
| 5 | + token::{add_trustline, deploy_sac, sac_id}, |
| 6 | + util::{new_account, test_address}, |
| 7 | +}; |
| 8 | + |
| 9 | +/// Enable the revocable flag on `issuer`, required to deauthorize an existing |
| 10 | +/// trustline. |
| 11 | +fn enable_revocable(sandbox: &TestEnv, issuer: &str) { |
| 12 | + sandbox |
| 13 | + .new_assert_cmd("tx") |
| 14 | + .args(["new", "set-options", "--set-revocable", "--source", issuer]) |
| 15 | + .assert() |
| 16 | + .success(); |
| 17 | +} |
| 18 | + |
| 19 | +/// Read whether `account` is authorized on the token through its SAC. |
| 20 | +fn sac_authorized(sandbox: &TestEnv, contract_id: &str, account: &str) -> bool { |
| 21 | + let stdout = sandbox |
| 22 | + .new_assert_cmd("contract") |
| 23 | + .args([ |
| 24 | + "invoke", |
| 25 | + "--id", |
| 26 | + contract_id, |
| 27 | + "--source-account", |
| 28 | + "test", |
| 29 | + "--", |
| 30 | + "authorized", |
| 31 | + "--id", |
| 32 | + account, |
| 33 | + ]) |
| 34 | + .assert() |
| 35 | + .success() |
| 36 | + .stdout_as_str(); |
| 37 | + stdout.trim().parse().unwrap() |
| 38 | +} |
| 39 | + |
| 40 | +#[tokio::test] |
| 41 | +async fn set_authorized_toggles_authorization_and_returns_receipt() { |
| 42 | + let sandbox = &TestEnv::new(); |
| 43 | + let test = test_address(sandbox); |
| 44 | + let issuer = new_account(sandbox, "issuer"); |
| 45 | + let asset = format!("USDC:{issuer}"); |
| 46 | + |
| 47 | + // Deauthorizing an existing trustline requires the issuer to be revocable. |
| 48 | + enable_revocable(sandbox, "issuer"); |
| 49 | + add_trustline(sandbox, "test", &asset); |
| 50 | + deploy_sac(sandbox, &asset, "issuer"); |
| 51 | + let sac = sac_id(sandbox, &asset); |
| 52 | + |
| 53 | + // A fresh trustline starts authorized. |
| 54 | + assert!( |
| 55 | + sac_authorized(sandbox, &sac, &test), |
| 56 | + "trustline should start authorized" |
| 57 | + ); |
| 58 | + |
| 59 | + let stdout = sandbox |
| 60 | + .new_assert_cmd("token") |
| 61 | + .args([ |
| 62 | + "set-authorized", |
| 63 | + "--id", |
| 64 | + &asset, |
| 65 | + "--admin", |
| 66 | + "issuer", |
| 67 | + "--account", |
| 68 | + &test, |
| 69 | + "--authorize", |
| 70 | + "false", |
| 71 | + "--output", |
| 72 | + "json", |
| 73 | + ]) |
| 74 | + .assert() |
| 75 | + .success() |
| 76 | + .stdout_as_str(); |
| 77 | + let receipt: Value = serde_json::from_str(&stdout).unwrap(); |
| 78 | + assert!( |
| 79 | + receipt["tx_hash"].as_str().is_some(), |
| 80 | + "expected a tx hash, got: {receipt}" |
| 81 | + ); |
| 82 | + |
| 83 | + // The account is now deauthorized on-chain. |
| 84 | + assert!( |
| 85 | + !sac_authorized(sandbox, &sac, &test), |
| 86 | + "account should be deauthorized after set-authorized false" |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +#[tokio::test] |
| 91 | +async fn set_authorized_fails_when_sac_not_deployed() { |
| 92 | + let sandbox = &TestEnv::new(); |
| 93 | + let test = test_address(sandbox); |
| 94 | + let issuer = new_account(sandbox, "issuer"); |
| 95 | + let asset = format!("USDC:{issuer}"); |
| 96 | + |
| 97 | + // No SAC deployed → structured deploy-pointer error with a typed discriminator. |
| 98 | + let stdout = sandbox |
| 99 | + .new_assert_cmd("token") |
| 100 | + .args([ |
| 101 | + "set-authorized", |
| 102 | + "--id", |
| 103 | + &asset, |
| 104 | + "--admin", |
| 105 | + "issuer", |
| 106 | + "--account", |
| 107 | + &test, |
| 108 | + "--authorize", |
| 109 | + "true", |
| 110 | + "--output", |
| 111 | + "json", |
| 112 | + ]) |
| 113 | + .assert() |
| 114 | + .failure() |
| 115 | + .stdout_as_str(); |
| 116 | + let value: Value = serde_json::from_str(&stdout).unwrap(); |
| 117 | + assert_eq!( |
| 118 | + value["error"]["type"], "sac_not_deployed", |
| 119 | + "expected a typed error, got: {stdout}" |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +#[tokio::test] |
| 124 | +async fn set_authorized_rejects_muxed_source_with_clear_error() { |
| 125 | + let sandbox = &TestEnv::new(); |
| 126 | + let test = test_address(sandbox); |
| 127 | + |
| 128 | + // Muxed (M…) source accounts aren't supported by the invoke pipeline yet |
| 129 | + // (see #2645). Until then the command must reject them up front with a clear |
| 130 | + // message rather than a raw strkey decode error deep in the pipeline. |
| 131 | + let muxed = "MA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAAAAAAAAAPCICBKU"; |
| 132 | + sandbox |
| 133 | + .new_assert_cmd("token") |
| 134 | + .args([ |
| 135 | + "set-authorized", |
| 136 | + "--id", |
| 137 | + "native", |
| 138 | + "--admin", |
| 139 | + muxed, |
| 140 | + "--account", |
| 141 | + &test, |
| 142 | + "--authorize", |
| 143 | + "true", |
| 144 | + ]) |
| 145 | + .assert() |
| 146 | + .failure() |
| 147 | + .stderr(predicates::str::contains( |
| 148 | + "muxed (M…) source accounts are not yet supported", |
| 149 | + )); |
| 150 | +} |
0 commit comments