github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_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  	"github.com/stretchr/testify/assert"
    32  	protobuf "google.golang.org/protobuf/proto"
    33  
    34  	"github.com/stretchr/testify/require"
    35  )
    36  
    37  func TestUnitAccountDeleteTransactionValidate(t *testing.T) {
    38  	t.Parallel()
    39  
    40  	client, err := _NewMockClient()
    41  	client.SetLedgerID(*NewLedgerIDTestnet())
    42  	require.NoError(t, err)
    43  	client.SetAutoValidateChecksums(true)
    44  	accountID, err := AccountIDFromString("0.0.123-esxsf")
    45  	require.NoError(t, err)
    46  
    47  	deleteAccount := NewAccountDeleteTransaction().
    48  		SetAccountID(accountID).
    49  		SetTransferAccountID(accountID)
    50  
    51  	err = deleteAccount.validateNetworkOnIDs(client)
    52  	require.NoError(t, err)
    53  }
    54  
    55  func TestUnitAccountDeleteTransactionValidateWrong(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  	accountID, err := AccountIDFromString("0.0.123-rmkykd")
    63  	require.NoError(t, err)
    64  
    65  	deleteAccount := NewAccountDeleteTransaction().
    66  		SetAccountID(accountID).
    67  		SetTransferAccountID(accountID)
    68  
    69  	err = deleteAccount.validateNetworkOnIDs(client)
    70  	assert.Error(t, err)
    71  	if err != nil {
    72  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    73  	}
    74  }
    75  
    76  func TestUnitAccountDeleteTransactionGet(t *testing.T) {
    77  	t.Parallel()
    78  
    79  	spenderAccountID1 := AccountID{Account: 7}
    80  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
    81  
    82  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
    83  
    84  	transaction, err := NewAccountDeleteTransaction().
    85  		SetTransactionID(transactionID).
    86  		SetNodeAccountIDs(nodeAccountID).
    87  		SetTransferAccountID(spenderAccountID1).
    88  		SetAccountID(spenderAccountID1).
    89  		Freeze()
    90  	require.NoError(t, err)
    91  
    92  	transaction.GetTransactionID()
    93  	transaction.GetNodeAccountIDs()
    94  
    95  	_, err = transaction.GetTransactionHash()
    96  	require.NoError(t, err)
    97  
    98  	transaction.GetMaxTransactionFee()
    99  	transaction.GetTransactionMemo()
   100  	transaction.GetRegenerateTransactionID()
   101  	_, err = transaction.GetSignatures()
   102  	require.NoError(t, err)
   103  	transaction.GetRegenerateTransactionID()
   104  	transaction.GetMaxTransactionFee()
   105  	transaction.GetRegenerateTransactionID()
   106  	transaction.GetTransferAccountID()
   107  	transaction.GetAccountID()
   108  }
   109  
   110  func TestUnitAccountDeleteTransactionSetNothing(t *testing.T) {
   111  	t.Parallel()
   112  
   113  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   114  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   115  
   116  	transaction, err := NewAccountDeleteTransaction().
   117  		SetTransactionID(transactionID).
   118  		SetNodeAccountIDs(nodeAccountID).
   119  		Freeze()
   120  	require.NoError(t, err)
   121  
   122  	transaction.GetTransactionID()
   123  	transaction.GetNodeAccountIDs()
   124  
   125  	_, err = transaction.GetTransactionHash()
   126  	require.NoError(t, err)
   127  
   128  	transaction.GetMaxTransactionFee()
   129  	transaction.GetTransactionMemo()
   130  	transaction.GetRegenerateTransactionID()
   131  	_, err = transaction.GetSignatures()
   132  	require.NoError(t, err)
   133  	transaction.GetRegenerateTransactionID()
   134  	transaction.GetMaxTransactionFee()
   135  	transaction.GetRegenerateTransactionID()
   136  	transaction.GetTransferAccountID()
   137  	transaction.GetAccountID()
   138  }
   139  
   140  func TestUnitAccountDeleteTransactionProtoCheck(t *testing.T) {
   141  	t.Parallel()
   142  
   143  	spenderAccountID1 := AccountID{Account: 7}
   144  	transferAccountID := AccountID{Account: 8}
   145  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   146  
   147  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   148  
   149  	transaction, err := NewAccountDeleteTransaction().
   150  		SetTransactionID(transactionID).
   151  		SetNodeAccountIDs(nodeAccountID).
   152  		SetTransferAccountID(transferAccountID).
   153  		SetAccountID(spenderAccountID1).
   154  		Freeze()
   155  	require.NoError(t, err)
   156  
   157  	transaction.GetTransactionID()
   158  	transaction.GetNodeAccountIDs()
   159  
   160  	proto := transaction.build().GetCryptoDelete()
   161  	require.Equal(t, proto.TransferAccountID.String(), transferAccountID._ToProtobuf().String())
   162  	require.Equal(t, proto.DeleteAccountID.String(), spenderAccountID1._ToProtobuf().String())
   163  }
   164  
   165  func TestUnitAccountDeleteTransactionTransactionCoverage(t *testing.T) {
   166  	t.Parallel()
   167  
   168  	checksum := "dmqui"
   169  	grpc := time.Second * 3
   170  	account := AccountID{Account: 3, checksum: &checksum}
   171  	nodeAccountID := []AccountID{{Account: 10}}
   172  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   173  
   174  	newKey, err := PrivateKeyGenerateEd25519()
   175  	require.NoError(t, err)
   176  
   177  	client, err := _NewMockClient()
   178  	client.SetLedgerID(*NewLedgerIDTestnet())
   179  	require.NoError(t, err)
   180  	client.SetAutoValidateChecksums(true)
   181  
   182  	transaction, err := NewAccountDeleteTransaction().
   183  		SetTransactionID(transactionID).
   184  		SetNodeAccountIDs(nodeAccountID).
   185  		SetAccountID(account).
   186  		SetTransferAccountID(account).
   187  		SetGrpcDeadline(&grpc).
   188  		SetMaxTransactionFee(NewHbar(3)).
   189  		SetMaxRetry(3).
   190  		SetMaxBackoff(time.Second * 30).
   191  		SetMinBackoff(time.Second * 10).
   192  		SetTransactionMemo("no").
   193  		SetTransactionValidDuration(time.Second * 30).
   194  		SetRegenerateTransactionID(false).
   195  		Freeze()
   196  	require.NoError(t, err)
   197  
   198  	transaction.validateNetworkOnIDs(client)
   199  
   200  	_, err = transaction.Schedule()
   201  	require.NoError(t, err)
   202  	transaction.GetTransactionID()
   203  	transaction.GetNodeAccountIDs()
   204  	transaction.GetMaxRetry()
   205  	transaction.GetMaxTransactionFee()
   206  	transaction.GetMaxBackoff()
   207  	transaction.GetMinBackoff()
   208  	transaction.GetRegenerateTransactionID()
   209  	byt, err := transaction.ToBytes()
   210  	require.NoError(t, err)
   211  	txFromBytes, err := TransactionFromBytes(byt)
   212  	require.NoError(t, err)
   213  	sig, err := newKey.SignTransaction(&transaction.Transaction)
   214  	require.NoError(t, err)
   215  
   216  	_, err = transaction.GetTransactionHash()
   217  	require.NoError(t, err)
   218  	transaction.GetAccountID()
   219  	transaction.GetMaxTransactionFee()
   220  	transaction.GetTransactionMemo()
   221  	transaction.GetRegenerateTransactionID()
   222  	_, err = transaction.GetSignatures()
   223  	require.NoError(t, err)
   224  	transaction.GetTransferAccountID()
   225  	transaction.getName()
   226  	switch b := txFromBytes.(type) {
   227  	case AccountDeleteTransaction:
   228  		b.AddSignature(newKey.PublicKey(), sig)
   229  	}
   230  }
   231  
   232  func TestUnitAccountDeleteTransactionTransactionMock(t *testing.T) {
   233  	t.Parallel()
   234  
   235  	nodeAccountID := []AccountID{{Account: 3}}
   236  	transactionID := TransactionIDGenerate(AccountID{Account: 3})
   237  
   238  	call := func(request *services.Transaction) *services.TransactionResponse {
   239  		require.NotEmpty(t, request.SignedTransactionBytes)
   240  		signedTransaction := services.SignedTransaction{}
   241  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
   242  
   243  		require.NotEmpty(t, signedTransaction.BodyBytes)
   244  		transactionBody := services.TransactionBody{}
   245  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
   246  
   247  		require.NotNil(t, transactionBody.TransactionID)
   248  		transactionId := transactionBody.TransactionID.String()
   249  		require.NotEqual(t, "", transactionId)
   250  
   251  		sigMap := signedTransaction.GetSigMap()
   252  		require.NotNil(t, sigMap)
   253  
   254  		return &services.TransactionResponse{
   255  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   256  		}
   257  	}
   258  	responses := [][]interface{}{{
   259  		call,
   260  	}}
   261  
   262  	client, server := NewMockClientAndServer(responses)
   263  	defer server.Close()
   264  
   265  	_, err := NewAccountDeleteTransaction().
   266  		SetTransactionID(transactionID).
   267  		SetNodeAccountIDs(nodeAccountID).
   268  		SetAccountID(AccountID{Account: 3}).
   269  		Execute(client)
   270  	require.NoError(t, err)
   271  }