1+ import time
12import pytest
23from unittest .mock import MagicMock
34
5+ from hiero_sdk_python .hapi .services import (
6+ response_header_pb2 ,
7+ response_pb2 ,
8+ timestamp_pb2 ,
9+ transaction_get_receipt_pb2 ,
10+ )
411from hiero_sdk_python .hapi .services .transaction_receipt_pb2 import TransactionReceipt as TransactionReceiptProto
512from hiero_sdk_python .hapi .services .transaction_response_pb2 import TransactionResponse as TransactionResponseProto
13+
614from hiero_sdk_python .response_code import ResponseCode
715from hiero_sdk_python .tokens .token_pause_transaction import TokenPauseTransaction
8- from hiero_sdk_python .hapi .services import response_header_pb2 , response_pb2 , timestamp_pb2 , transaction_get_receipt_pb2
9- from hiero_sdk_python .transaction .transaction_id import TransactionId
1016from hiero_sdk_python .tokens .token_id import TokenId
17+ from hiero_sdk_python .transaction .transaction_id import TransactionId
1118
1219from tests .unit .mock_server import mock_hedera_servers
1320
1421pytestmark = pytest .mark .unit
1522
16- def generate_transaction_id (account_id_proto ):
17- """Generate a unique transaction ID based on the account ID and the current timestamp."""
18- import time
19- current_time = time .time ()
20- timestamp_seconds = int (current_time )
21- timestamp_nanos = int ((current_time - timestamp_seconds ) * 1e9 )
23+ @pytest .fixture
24+ def built_pause_tx (mock_account_ids , mock_client , generate_transaction_id ):
25+ """
26+ Factory fixture: given a pause_key, returns a TokenPauseTransaction
27+ that’s already set up, frozen, and signed with that key.
28+ """
29+ sender , _ , node_account , fungible_token , _ = mock_account_ids
2230
23- tx_timestamp = timestamp_pb2 .Timestamp (seconds = timestamp_seconds , nanos = timestamp_nanos )
31+ def _make (pause_key ):
32+ tx = TokenPauseTransaction ().set_token_id (fungible_token )
33+ tx .transaction_id = generate_transaction_id (sender )
34+ tx .node_account_id = node_account
35+ tx .freeze_with (mock_client )
36+ tx .sign (pause_key )
37+ return tx
38+
39+ return _make
2440
25- tx_id = TransactionId (
26- valid_start = tx_timestamp ,
27- account_id = account_id_proto
28- )
29- return tx_id
3041
31- def test_builds_token_pause_body_with_correct_token_id (mock_account_ids ):
42+ def test_builds_token_pause_body_with_correct_ids (mock_account_ids , generate_transaction_id ):
3243 """
33- Given a TokenPauseTransaction with a valid token_id set, when building the
34- transaction body, then the inner `tokenPause.token` fields should match.
44+ build_transaction_body() should embed:
45+ - tokenPause.token == token_id.to_proto()
46+ - transactionID == transaction_id.to_proto()
47+ - nodeAccountID == node_account_id.to_proto()
3548 """
36- account_id , node_account_id , token_id , _ = mock_account_ids
49+ sender , _ , node_account , token_id , _ = mock_account_ids
3750
38- pause_tx = (
39- TokenPauseTransaction ()
40- .set_token_id (token_id )
41- )
42-
43- pause_tx .transaction_id = generate_transaction_id (account_id )
44- pause_tx .node_account_id = node_account_id
51+ pause_tx = TokenPauseTransaction ().set_token_id (token_id )
52+ pause_tx .transaction_id = generate_transaction_id (sender )
53+ pause_tx .node_account_id = node_account
4554
46- transaction_body = pause_tx .build_transaction_body ()
55+ body = pause_tx .build_transaction_body ()
4756
48- expected = token_id .to_proto ()
49- assert transaction_body .tokenPause .token == expected
57+ assert body .tokenPause .token == token_id .to_proto ()
58+ assert body .transactionID == pause_tx .transaction_id .to_proto ()
59+ assert body .nodeAccountID == pause_tx .node_account_id .to_proto ()
5060
51- assert transaction_body .transactionID == pause_tx .transaction_id .to_proto ()
52- assert transaction_body .nodeAccountID == pause_tx .node_account_id .to_proto ()
5361
54- @pytest .mark .parametrize ("bad_token" , [None , TokenId (0 ,0 , 0 )])
62+ @pytest .mark .parametrize ("bad_token" , [None , TokenId (0 , 0 , 0 )])
5563def test_build_transaction_body_without_valid_token_id_raises (bad_token ):
56- """Building a transaction body without a valid token_id must raise ValueError."""
57- tx = TokenPauseTransaction ()
64+ """
65+ If token_id is missing or zero, build_transaction_body() must ValueError.
66+ """
67+ pause_tx = TokenPauseTransaction ()
5868 if bad_token is not None :
59- tx .token_id = bad_token
60- with pytest .raises (ValueError ):
61- tx .build_transaction_body ()
62-
63- # This test uses fixture (mock_account_ids, mock_client) as parameter
64- def test_signed_bytes_include_token_pause_transaction (mock_account_ids , mock_client ):
65- """Test converting the token pause transaction to protobuf format after signing."""
66- account_id , _ , token_id , _ = mock_account_ids
67-
68- pause_tx = (
69- TokenPauseTransaction ()
70- .set_token_id (token_id )
71- )
72-
73- pause_tx .transaction_id = generate_transaction_id (account_id )
69+ pause_tx .token_id = bad_token
7470
71+ with pytest .raises (ValueError , match = "token_id must be set" ):
72+ pause_tx .build_transaction_body ()
73+
74+ def test_signed_bytes_include_token_pause_transaction (built_pause_tx ):
75+ """
76+ After freeze() and sign(pause_key), to_proto() must embed a non-empty
77+ signedTransactionBytes blob.
78+ """
79+ # Arrange: stub pause key + public key
7580 pause_key = MagicMock ()
76- pause_key .sign .return_value = b'signature'
77- pause_key .public_key ().to_bytes_raw .return_value = b'public_key'
78-
79- pause_tx .freeze_with (mock_client )
81+ pause_key .sign .return_value = b'__FAKE_SIG__'
8082
81- pause_tx .sign (pause_key )
83+ fake_pub = pause_key .public_key ()
84+ fake_pub .to_bytes_raw .return_value = b'__FAKE_PUB__'
85+
86+ # Act
87+ pause_tx = built_pause_tx (pause_key )
8288 proto = pause_tx .to_proto ()
8389
90+ # Assert
8491 assert proto .signedTransactionBytes
8592 assert len (proto .signedTransactionBytes ) > 0
8693
87- # This test uses fixture mock_account_ids as parameter
8894def test_pause_transaction_can_execute (mock_account_ids ):
89- """Test that a pause transaction can be executed successfully."""
90- account_id , node_account_id , token_id , _ = mock_account_ids
91-
92- # Create test transaction responses
93- ok_response = TransactionResponseProto ()
94- ok_response .nodeTransactionPrecheckCode = ResponseCode .OK
95-
96- # Create a mock receipt for a successful token pause
97- mock_receipt_proto = TransactionReceiptProto (
98- status = ResponseCode .SUCCESS
99- )
100-
101- # Create a response for the receipt query
102- receipt_query_response = response_pb2 .Response (
95+ """
96+ A properly built & signed TokenPauseTransaction against a mock server
97+ should return a SUCCESS receipt.
98+ """
99+ sender , _ , node_account , fungible_token , _ = mock_account_ids
100+
101+ # 1) Precheck-ok response
102+ ok_resp = TransactionResponseProto (nodeTransactionPrecheckCode = ResponseCode .OK )
103+ # 2) Receipt SUCCESS response
104+ success_receipt = TransactionReceiptProto (status = ResponseCode .SUCCESS )
105+ receipt_query = response_pb2 .Response (
103106 transactionGetReceipt = transaction_get_receipt_pb2 .TransactionGetReceiptResponse (
104107 header = response_header_pb2 .ResponseHeader (
105108 nodeTransactionPrecheckCode = ResponseCode .OK
106109 ),
107- receipt = mock_receipt_proto
110+ receipt = success_receipt
108111 )
109112 )
110-
113+
111114 response_sequences = [
112- [ok_response , receipt_query_response ],
115+ [ok_resp , receipt_query ],
113116 ]
114-
115117 with mock_hedera_servers (response_sequences ) as client :
116- # Build the transaction
117- transaction = (
118- TokenPauseTransaction ()
119- .set_token_id (token_id )
120- )
121- # Set identifiers so freeze/sign can populate the payload correctly
122- transaction .transaction_id = generate_transaction_id (account_id )
123- transaction .node_account_id = node_account_id
124-
125- # Freeze and sign before execute
126- transaction .freeze_with (client )
127- dummy_key = MagicMock ()
128- dummy_key .sign .return_value = b'__SIG__'
129- dummy_key .public_key ().to_bytes_raw .return_value = b'PUB'
130- transaction .sign (dummy_key )
118+ pause_tx = TokenPauseTransaction ().set_token_id (fungible_token )
119+ pause_tx .transaction_id = TransactionId .generate (sender )
120+ pause_tx .node_account_id = node_account
121+
122+ pause_tx .freeze_with (client )
123+
124+ dummy_priv_key = MagicMock ()
125+ dummy_priv_key .sign .return_value = b'__SIG__'
131126
132- # Execute and assert
133- receipt = transaction .execute (client )
134- assert receipt .status == ResponseCode .SUCCESS , "Transaction should have succeeded"
127+ dummy_pub_key = dummy_priv_key .public_key ()
128+ dummy_pub_key .to_bytes_raw .return_value = b'PUB'
129+
130+ pause_tx .sign (dummy_priv_key )
131+
132+ receipt = pause_tx .execute (client )
133+ assert receipt .status == ResponseCode .SUCCESS
134+
135+
0 commit comments