|
| 1 | +import pytest |
| 2 | + |
| 3 | +from hiero_sdk_python.crypto.private_key import PrivateKey |
| 4 | +from hiero_sdk_python.exceptions import PrecheckError |
| 5 | +from hiero_sdk_python.hbar import Hbar |
| 6 | +from hiero_sdk_python.query.account_balance_query import CryptoGetAccountBalanceQuery |
| 7 | +from hiero_sdk_python.tokens.token_create_transaction import TokenCreateTransaction |
| 8 | +from hiero_sdk_python.tokens.token_associate_transaction import TokenAssociateTransaction |
| 9 | +from hiero_sdk_python.account.account_create_transaction import AccountCreateTransaction |
| 10 | +from hiero_sdk_python.response_code import ResponseCode |
| 11 | +from hiero_sdk_python.tokens.token_pause_transaction import TokenPauseTransaction |
| 12 | +from hiero_sdk_python.transaction.transfer_transaction import TransferTransaction |
| 13 | +from tests.integration.utils_for_test import IntegrationTestEnv, create_fungible_token |
| 14 | +from hiero_sdk_python.tokens.token_id import TokenId |
| 15 | +from hiero_sdk_python.tokens.token_type import TokenType |
| 16 | +from hiero_sdk_python.tokens.supply_type import SupplyType |
| 17 | +from hiero_sdk_python.tokens.token_info_query import TokenInfoQuery |
| 18 | + |
| 19 | +@pytest.mark.integration |
| 20 | +def test_pause_without_setting_token_id_raises_client_error(): |
| 21 | + """ |
| 22 | + Building a TokenPauseTransaction without ever |
| 23 | + calling set_token_id(), should error. |
| 24 | + """ |
| 25 | + env = IntegrationTestEnv() |
| 26 | + try: |
| 27 | + tx = TokenPauseTransaction() # no set_token_id() |
| 28 | + with pytest.raises(ValueError, match="token_id must be set"): |
| 29 | + tx.freeze_with(env.client) |
| 30 | + finally: |
| 31 | + env.close() |
| 32 | + |
| 33 | +@pytest.mark.integration |
| 34 | +def test_pause_nonexistent_token_raises_precheck(): |
| 35 | + """ |
| 36 | + Trying to pause a token that doesn’t exist should fail fast |
| 37 | + with INVALID_TOKEN_ID. |
| 38 | + """ |
| 39 | + env = IntegrationTestEnv() |
| 40 | + try: |
| 41 | + bogus = TokenPauseTransaction().set_token_id(TokenId(0, 0, 99999999)) |
| 42 | + bogus.freeze_with(env.client) |
| 43 | + |
| 44 | + with pytest.raises( |
| 45 | + PrecheckError, |
| 46 | + match="failed precheck with status: INVALID_TOKEN_ID" |
| 47 | + ) as excinfo: |
| 48 | + bogus.execute(env.client) |
| 49 | + |
| 50 | + assert "INVALID_TOKEN_ID" in str(excinfo.value) |
| 51 | + finally: |
| 52 | + env.close() |
| 53 | + |
| 54 | +@pytest.mark.integration |
| 55 | +def test_pause_without_pause_key_fails(): |
| 56 | + """ |
| 57 | + A token WITHOUT a pause key, attempting to pause it |
| 58 | + should hit TOKEN_HAS_NO_PAUSE_KEY error. |
| 59 | + """ |
| 60 | + env = IntegrationTestEnv() |
| 61 | + try: |
| 62 | + # 1) Create a token with no pause_key |
| 63 | + token = ( |
| 64 | + TokenCreateTransaction() |
| 65 | + .set_token_name("NoPause") |
| 66 | + .set_token_symbol("NOP") |
| 67 | + .set_decimals(0) |
| 68 | + .set_initial_supply(1) |
| 69 | + .set_treasury_account_id(env.operator_id) |
| 70 | + .set_token_type(TokenType.FUNGIBLE_COMMON) |
| 71 | + .set_supply_type(SupplyType.FINITE) |
| 72 | + .set_max_supply(1) |
| 73 | + .freeze_with(env.client) |
| 74 | + ).execute(env.client).tokenId |
| 75 | + |
| 76 | + # 2) Attempt to pause it |
| 77 | + tx = TokenPauseTransaction().set_token_id(token) |
| 78 | + tx.freeze_with(env.client) |
| 79 | + |
| 80 | + with pytest.raises( |
| 81 | + PrecheckError, |
| 82 | + match="failed precheck with status: TOKEN_HAS_NO_PAUSE_KEY" |
| 83 | + ): |
| 84 | + tx.execute(env.client) |
| 85 | + finally: |
| 86 | + env.close() |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.integration |
| 90 | +def test_pause_prevents_transfers_and_reflects_in_info(): |
| 91 | + """ |
| 92 | + Create a token with a pause key, make a transfer, |
| 93 | + pause the token, verify transfers now fail, |
| 94 | + and confirm TokenInfoQuery reports PAUSED status. |
| 95 | + """ |
| 96 | + env = IntegrationTestEnv() |
| 97 | + try: |
| 98 | + # 1) create a test account to receive tokens |
| 99 | + recv_key = PrivateKey.generate() |
| 100 | + recv_tx = ( |
| 101 | + AccountCreateTransaction() |
| 102 | + .set_key(recv_key.public_key()) |
| 103 | + .set_initial_balance(Hbar(1)) |
| 104 | + .freeze_with(env.client) |
| 105 | + ).execute(env.client) |
| 106 | + recv = recv_tx.accountId |
| 107 | + |
| 108 | + # 2) create a token WITH a pause key |
| 109 | + token = env.create_fungible_token(pause_key=env.operator_key) |
| 110 | + |
| 111 | + # 3) associate and transfer some |
| 112 | + ( |
| 113 | + TokenAssociateTransaction() |
| 114 | + .set_account_id(recv) |
| 115 | + .add_token_id(token) |
| 116 | + .freeze_with(env.client) |
| 117 | + .sign(recv_key) |
| 118 | + ).execute(env.client) |
| 119 | + ( |
| 120 | + TransferTransaction() |
| 121 | + .add_token_transfer(token, env.operator_id, -10) |
| 122 | + .add_token_transfer(token, recv, 10) |
| 123 | + .freeze_with(env.client) |
| 124 | + ).execute(env.client) |
| 125 | + |
| 126 | + # balance check before pause |
| 127 | + bal = CryptoGetAccountBalanceQuery(recv).execute(env.client) |
| 128 | + assert bal.token_balances[token] == 10 |
| 129 | + |
| 130 | + # 4) pause the token |
| 131 | + ( |
| 132 | + TokenPauseTransaction() |
| 133 | + .set_token_id(token) |
| 134 | + .freeze_with(env.client) |
| 135 | + .sign(env.operator_key) |
| 136 | + ).execute(env.client) |
| 137 | + |
| 138 | + # info query should show PAUSED |
| 139 | + status = TokenInfoQuery().set_token_id(token).execute(env.client).token_status |
| 140 | + assert status.name == "PAUSED" |
| 141 | + |
| 142 | + # 5) any further transfer should now throw ReceiptStatusException TOKEN_IS_PAUSED |
| 143 | + with pytest.raises(ReceiptStatusException) as exc: |
| 144 | + ( |
| 145 | + TransferTransaction() |
| 146 | + .add_token_transfer(token, env.operator_id, -1) |
| 147 | + .add_token_transfer(token, recv, 1) |
| 148 | + .freeze_with(env.client) |
| 149 | + .sign(env.operator_key) |
| 150 | + .execute(env.client) |
| 151 | + ) |
| 152 | + assert "TOKEN_IS_PAUSED" in str(exc.value) |
| 153 | + |
| 154 | + finally: |
| 155 | + env.close() |
| 156 | + |
| 157 | +@pytest.mark.integration |
| 158 | +def test_double_pause_raises_token_already_paused(): |
| 159 | + """ |
| 160 | + Once a token is already paused, attempting to pause it again |
| 161 | + should hit the TOKEN_ALREADY_PAUSED precheck error. |
| 162 | + """ |
| 163 | + env = IntegrationTestEnv() |
| 164 | + try: |
| 165 | + # Create a token with a pause key |
| 166 | + token = env.create_fungible_token(pause_key=env.operator_key) |
| 167 | + |
| 168 | + # First pause should succeed |
| 169 | + ( |
| 170 | + TokenPauseTransaction() |
| 171 | + .set_token_id(token) |
| 172 | + .freeze_with(env.client) |
| 173 | + .sign(env.operator_key) |
| 174 | + .execute(env.client) |
| 175 | + ) |
| 176 | + |
| 177 | + # Confirm status is PAUSED |
| 178 | + status = ( |
| 179 | + TokenInfoQuery() |
| 180 | + .set_token_id(token) |
| 181 | + .execute(env.client) |
| 182 | + .token_status |
| 183 | + ) |
| 184 | + assert status.name == "PAUSED" |
| 185 | + |
| 186 | + # Second pause should fail with TOKEN_ALREADY_PAUSED |
| 187 | + tx = TokenPauseTransaction().set_token_id(token) |
| 188 | + tx.freeze_with(env.client) |
| 189 | + tx.sign(env.operator_key) |
| 190 | + with pytest.raises( |
| 191 | + PrecheckError, |
| 192 | + match="failed precheck with status: TOKEN_ALREADY_PAUSED" |
| 193 | + ): |
| 194 | + tx.execute(env.client) |
| 195 | + |
| 196 | + finally: |
| 197 | + env.close() |
| 198 | + |
| 199 | + |
| 200 | +@pytest.mark.integration |
| 201 | +def test_pause_with_wrong_key_raises_sig_mismatch(): |
| 202 | + """ |
| 203 | + Signing a pause transaction with a key that is NOT the token's pause key |
| 204 | + should fail the precheck with SIG_MISMATCH. |
| 205 | + """ |
| 206 | + env = IntegrationTestEnv() |
| 207 | + try: |
| 208 | + # Create a token whose pause key is the operator |
| 209 | + token = env.create_fungible_token(pause_key=env.operator_key) |
| 210 | + |
| 211 | + # Build & freeze the pause transaction |
| 212 | + tx = TokenPauseTransaction().set_token_id(token) |
| 213 | + tx.freeze_with(env.client) |
| 214 | + |
| 215 | + # Sign with a completely unrelated key |
| 216 | + wrong_key = PrivateKey.generate() |
| 217 | + tx.sign(wrong_key) |
| 218 | + |
| 219 | + # Expect signature mismatch at precheck |
| 220 | + with pytest.raises( |
| 221 | + PrecheckError, |
| 222 | + match="failed precheck with status: SIG_MISMATCH" |
| 223 | + ): |
| 224 | + tx.execute(env.client) |
| 225 | + |
| 226 | + finally: |
| 227 | + env.close() |
0 commit comments