github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_delete_transaction_unit_test.go (about)

     1  //go:build all || unit
     2  // +build all unit
     3  
     4  package hedera
     5  
     6  /*-
     7   *
     8   * Hedera Go SDK
     9   *
    10   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    11   *
    12   * Licensed under the Apache License, Version 2.0 (the "License");
    13   * you may not use this file except in compliance with the License.
    14   * You may obtain a copy of the License at
    15   *
    16   *      http://www.apache.org/licenses/LICENSE-2.0
    17   *
    18   * Unless required by applicable law or agreed to in writing, software
    19   * distributed under the License is distributed on an "AS IS" BASIS,
    20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21   * See the License for the specific language governing permissions and
    22   * limitations under the License.
    23   *
    24   */
    25  
    26  import (
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/hashgraph/hedera-protobufs-go/services"
    31  	protobuf "google.golang.org/protobuf/proto"
    32  
    33  	"github.com/stretchr/testify/assert"
    34  
    35  	"github.com/stretchr/testify/require"
    36  )
    37  
    38  func TestUnitTokenDeleteTransactionValidate(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	client, err := _NewMockClient()
    42  	client.SetLedgerID(*NewLedgerIDTestnet())
    43  	require.NoError(t, err)
    44  	client.SetAutoValidateChecksums(true)
    45  	tokenID, err := TokenIDFromString("0.0.123-esxsf")
    46  	require.NoError(t, err)
    47  
    48  	tokenDelete := NewTokenDeleteTransaction().
    49  		SetTokenID(tokenID)
    50  
    51  	err = tokenDelete.validateNetworkOnIDs(client)
    52  	require.NoError(t, err)
    53  }
    54  
    55  func TestUnitTokenDeleteTransactionValidateWrong(t *testing.T) {
    56  	t.Parallel()
    57  
    58  	client, err := _NewMockClient()
    59  	client.SetLedgerID(*NewLedgerIDTestnet())
    60  	require.NoError(t, err)
    61  	client.SetAutoValidateChecksums(true)
    62  	tokenID, err := TokenIDFromString("0.0.123-rmkykd")
    63  	require.NoError(t, err)
    64  
    65  	tokenDelete := NewTokenDeleteTransaction().
    66  		SetTokenID(tokenID)
    67  
    68  	err = tokenDelete.validateNetworkOnIDs(client)
    69  	assert.Error(t, err)
    70  	if err != nil {
    71  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    72  	}
    73  }
    74  
    75  func TestUnitTokenDeleteTransactionGet(t *testing.T) {
    76  	t.Parallel()
    77  
    78  	tokenID := TokenID{Token: 7}
    79  
    80  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
    81  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
    82  
    83  	transaction, err := NewTokenDeleteTransaction().
    84  		SetTransactionID(transactionID).
    85  		SetNodeAccountIDs(nodeAccountID).
    86  		SetTokenID(tokenID).
    87  		SetMaxTransactionFee(NewHbar(10)).
    88  		SetTransactionMemo("").
    89  		SetTransactionValidDuration(60 * time.Second).
    90  		SetRegenerateTransactionID(false).
    91  		Freeze()
    92  	require.NoError(t, err)
    93  
    94  	transaction.GetTransactionID()
    95  	transaction.GetNodeAccountIDs()
    96  
    97  	_, err = transaction.GetTransactionHash()
    98  	require.NoError(t, err)
    99  
   100  	transaction.GetTokenID()
   101  	transaction.GetMaxTransactionFee()
   102  	transaction.GetTransactionMemo()
   103  	transaction.GetRegenerateTransactionID()
   104  	_, err = transaction.GetSignatures()
   105  	require.NoError(t, err)
   106  	transaction.GetRegenerateTransactionID()
   107  	transaction.GetMaxTransactionFee()
   108  	transaction.GetRegenerateTransactionID()
   109  }
   110  
   111  func TestUnitTokenDeleteTransactionNothingSet(t *testing.T) {
   112  	t.Parallel()
   113  
   114  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   115  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   116  
   117  	transaction, err := NewTokenDeleteTransaction().
   118  		SetTransactionID(transactionID).
   119  		SetNodeAccountIDs(nodeAccountID).
   120  		Freeze()
   121  	require.NoError(t, err)
   122  
   123  	transaction.GetTransactionID()
   124  	transaction.GetNodeAccountIDs()
   125  
   126  	_, err = transaction.GetTransactionHash()
   127  	require.NoError(t, err)
   128  
   129  	transaction.GetTokenID()
   130  	transaction.GetMaxTransactionFee()
   131  	transaction.GetTransactionMemo()
   132  	transaction.GetRegenerateTransactionID()
   133  	_, err = transaction.GetSignatures()
   134  	require.NoError(t, err)
   135  	transaction.GetRegenerateTransactionID()
   136  	transaction.GetMaxTransactionFee()
   137  	transaction.GetRegenerateTransactionID()
   138  }
   139  
   140  func TestUnitTokenDeleteTransactionCoverage(t *testing.T) {
   141  	t.Parallel()
   142  
   143  	checksum := "dmqui"
   144  	grpc := time.Second * 30
   145  	token := TokenID{Token: 3, checksum: &checksum}
   146  	nodeAccountID := []AccountID{{Account: 10}}
   147  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   148  
   149  	newKey, err := PrivateKeyGenerateEd25519()
   150  	require.NoError(t, err)
   151  
   152  	client, err := _NewMockClient()
   153  	client.SetLedgerID(*NewLedgerIDTestnet())
   154  	require.NoError(t, err)
   155  	client.SetAutoValidateChecksums(true)
   156  
   157  	transaction, err := NewTokenDeleteTransaction().
   158  		SetTransactionID(transactionID).
   159  		SetNodeAccountIDs(nodeAccountID).
   160  		SetTokenID(token).
   161  		SetGrpcDeadline(&grpc).
   162  		SetMaxTransactionFee(NewHbar(3)).
   163  		SetMaxRetry(3).
   164  		SetMaxBackoff(time.Second * 30).
   165  		SetMinBackoff(time.Second * 10).
   166  		SetTransactionMemo("no").
   167  		SetTransactionValidDuration(time.Second * 30).
   168  		SetRegenerateTransactionID(false).
   169  		Freeze()
   170  	require.NoError(t, err)
   171  
   172  	err = transaction.validateNetworkOnIDs(client)
   173  	require.NoError(t, err)
   174  	_, err = transaction.Schedule()
   175  	require.NoError(t, err)
   176  	transaction.GetTransactionID()
   177  	transaction.GetNodeAccountIDs()
   178  	transaction.GetMaxRetry()
   179  	transaction.GetMaxTransactionFee()
   180  	transaction.GetMaxBackoff()
   181  	transaction.GetMinBackoff()
   182  	transaction.GetRegenerateTransactionID()
   183  	byt, err := transaction.ToBytes()
   184  	require.NoError(t, err)
   185  	txFromBytes, err := TransactionFromBytes(byt)
   186  	require.NoError(t, err)
   187  	sig, err := newKey.SignTransaction(&transaction.Transaction)
   188  	require.NoError(t, err)
   189  
   190  	_, err = transaction.GetTransactionHash()
   191  	require.NoError(t, err)
   192  	transaction.GetMaxTransactionFee()
   193  	transaction.GetTransactionMemo()
   194  	transaction.GetRegenerateTransactionID()
   195  	transaction.GetTokenID()
   196  	_, err = transaction.GetSignatures()
   197  	require.NoError(t, err)
   198  	transaction.getName()
   199  	switch b := txFromBytes.(type) {
   200  	case TokenDeleteTransaction:
   201  		b.AddSignature(newKey.PublicKey(), sig)
   202  	}
   203  }
   204  
   205  func TestUnitTokenDeleteTransactionMock(t *testing.T) {
   206  	t.Parallel()
   207  
   208  	newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98")
   209  	require.NoError(t, err)
   210  
   211  	call := func(request *services.Transaction) *services.TransactionResponse {
   212  		require.NotEmpty(t, request.SignedTransactionBytes)
   213  		signedTransaction := services.SignedTransaction{}
   214  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
   215  
   216  		require.NotEmpty(t, signedTransaction.BodyBytes)
   217  		transactionBody := services.TransactionBody{}
   218  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
   219  
   220  		require.NotNil(t, transactionBody.TransactionID)
   221  		transactionId := transactionBody.TransactionID.String()
   222  		require.NotEqual(t, "", transactionId)
   223  
   224  		sigMap := signedTransaction.GetSigMap()
   225  		require.NotNil(t, sigMap)
   226  
   227  		return &services.TransactionResponse{
   228  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   229  		}
   230  	}
   231  	responses := [][]interface{}{{
   232  		call,
   233  	}}
   234  
   235  	client, server := NewMockClientAndServer(responses)
   236  	defer server.Close()
   237  
   238  	checksum := "dmqui"
   239  	token := TokenID{Token: 3, checksum: &checksum}
   240  
   241  	freez, err := NewTokenDeleteTransaction().
   242  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   243  		SetTokenID(token).
   244  		FreezeWith(client)
   245  	require.NoError(t, err)
   246  
   247  	_, err = freez.Sign(newKey).Execute(client)
   248  	require.NoError(t, err)
   249  }