github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_update_nfts_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  	"github.com/stretchr/testify/require"
    33  	protobuf "google.golang.org/protobuf/proto"
    34  )
    35  
    36  func TestUnitTokenUpdateNftsTransactionValidate(t *testing.T) {
    37  	t.Parallel()
    38  
    39  	client, err := _NewMockClient()
    40  	client.SetLedgerID(*NewLedgerIDTestnet())
    41  	require.NoError(t, err)
    42  	client.SetAutoValidateChecksums(true)
    43  	require.NoError(t, err)
    44  	tokenID, err := TokenIDFromString("0.0.123-esxsf")
    45  	require.NoError(t, err)
    46  
    47  	tokenUpdate := NewTokenUpdateNftsTransaction().
    48  		SetTokenID(tokenID).
    49  		SetSerialNumbers([]int64{1, 2, 3}).
    50  		SetMetadata([]byte("metadata"))
    51  
    52  	err = tokenUpdate.validateNetworkOnIDs(client)
    53  	require.NoError(t, err)
    54  }
    55  
    56  func TestUnitTokenUpdateNftsTransactionValidateWrong(t *testing.T) {
    57  	t.Parallel()
    58  
    59  	client, err := _NewMockClient()
    60  	client.SetLedgerID(*NewLedgerIDTestnet())
    61  	require.NoError(t, err)
    62  	client.SetAutoValidateChecksums(true)
    63  	require.NoError(t, err)
    64  	tokenID, err := TokenIDFromString("0.0.123-rmkykd")
    65  	require.NoError(t, err)
    66  
    67  	tokenUpdate := NewTokenUpdateNftsTransaction().
    68  		SetTokenID(tokenID).
    69  		SetSerialNumbers([]int64{1, 2, 3}).
    70  		SetMetadata([]byte("metadata"))
    71  
    72  	err = tokenUpdate.validateNetworkOnIDs(client)
    73  	assert.Error(t, err)
    74  	if err != nil {
    75  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    76  	}
    77  }
    78  
    79  func TestUnitTokenUpdateNftsTransactionGet(t *testing.T) {
    80  	t.Parallel()
    81  
    82  	grpc := time.Second * 30
    83  	checksum := "dmqui"
    84  	tokenID := TokenID{Token: 3, checksum: &checksum}
    85  
    86  	newKey, err := PrivateKeyGenerateEd25519()
    87  	require.NoError(t, err)
    88  
    89  	nodeAccountID := []AccountID{{Account: 10}}
    90  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
    91  
    92  	client, err := _NewMockClient()
    93  	client.SetLedgerID(*NewLedgerIDTestnet())
    94  	require.NoError(t, err)
    95  	client.SetAutoValidateChecksums(true)
    96  
    97  	transaction, err := NewTokenUpdateNftsTransaction().
    98  		SetTokenID(tokenID).
    99  		SetSerialNumbers([]int64{1, 2, 3}).
   100  		SetTransactionID(transactionID).
   101  		SetNodeAccountIDs(nodeAccountID).
   102  		SetMetadata([]byte("metadata")).
   103  		SetTransactionMemo("").
   104  		SetTransactionValidDuration(60 * time.Second).
   105  		SetRegenerateTransactionID(false).
   106  		SetGrpcDeadline(&grpc).
   107  		SetMaxRetry(3).
   108  		SetMaxBackoff(time.Second * 30).
   109  		SetMinBackoff(time.Second * 10).
   110  		SetMaxTransactionFee(NewHbar(10)).
   111  		SetLogLevel(LoggerLevelInfo).
   112  		Freeze()
   113  
   114  	err = transaction.validateNetworkOnIDs(client)
   115  	require.NoError(t, err)
   116  	_, err = transaction.Schedule()
   117  	require.NoError(t, err)
   118  
   119  	require.NotNil(t, transaction.GetTokenID())
   120  	require.NotNil(t, transaction.GetSerialNumbers())
   121  	require.NotNil(t, transaction.GetMetadata())
   122  	require.NotNil(t, transaction.GetMaxTransactionFee())
   123  	require.NotNil(t, transaction.GetTransactionMemo())
   124  	require.NotNil(t, transaction.GetRegenerateTransactionID())
   125  	_, err = transaction.GetSignatures()
   126  	require.NoError(t, err)
   127  	require.NotNil(t, transaction.GetRegenerateTransactionID())
   128  	require.NotNil(t, transaction.GetMaxTransactionFee())
   129  	require.NotNil(t, transaction.GetRegenerateTransactionID())
   130  	byt, err := transaction.ToBytes()
   131  	require.NoError(t, err)
   132  	txFromBytes, err := TransactionFromBytes(byt)
   133  	require.NoError(t, err)
   134  	sig, err := newKey.SignTransaction(&transaction.Transaction)
   135  	require.NoError(t, err)
   136  	require.NotNil(t, transaction.getName())
   137  	require.NotNil(t, transaction.GetMaxRetry())
   138  	require.NotNil(t, transaction.GetMaxBackoff())
   139  	require.NotNil(t, transaction.GetMinBackoff())
   140  	switch b := txFromBytes.(type) {
   141  	case TokenUpdateNfts:
   142  		b.AddSignature(newKey.PublicKey(), sig)
   143  	}
   144  }
   145  
   146  func TestUnitTokenUpdateNftsTransactionNothingSet(t *testing.T) {
   147  	t.Parallel()
   148  	nodeAccountID := []AccountID{{Account: 10}}
   149  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   150  
   151  	transaction, err := NewTokenUpdateNftsTransaction().
   152  		SetTransactionID(transactionID).
   153  		SetNodeAccountIDs(nodeAccountID).Freeze()
   154  
   155  	require.NoError(t, err)
   156  	require.Nil(t, transaction.GetTokenID())
   157  	require.Nil(t, transaction.GetSerialNumbers())
   158  	require.Nil(t, transaction.GetMetadata())
   159  	require.NotNil(t, transaction.GetMaxTransactionFee())
   160  	require.NotNil(t, transaction.GetTransactionMemo())
   161  	require.NotNil(t, transaction.GetRegenerateTransactionID())
   162  	_, err = transaction.GetSignatures()
   163  	require.NoError(t, err)
   164  	require.NotNil(t, transaction.GetRegenerateTransactionID())
   165  	require.NotNil(t, transaction.GetMaxTransactionFee())
   166  	require.NotNil(t, transaction.GetRegenerateTransactionID())
   167  	require.NotNil(t, transaction.getName())
   168  	require.NotNil(t, transaction.GetMaxRetry())
   169  	require.NotNil(t, transaction.GetMaxBackoff())
   170  	require.NotNil(t, transaction.GetMinBackoff())
   171  	proto := transaction.build().GetTokenUpdateNfts()
   172  	require.Nil(t, proto.Metadata)
   173  	require.Nil(t, proto.SerialNumbers)
   174  	require.Nil(t, proto.Token)
   175  }
   176  
   177  func TestUnitTokenUpdateNftTransactionMock(t *testing.T) {
   178  	t.Parallel()
   179  
   180  	newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98")
   181  	require.NoError(t, err)
   182  
   183  	call := func(request *services.Transaction) *services.TransactionResponse {
   184  		require.NotEmpty(t, request.SignedTransactionBytes)
   185  		signedTransaction := services.SignedTransaction{}
   186  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
   187  
   188  		require.NotEmpty(t, signedTransaction.BodyBytes)
   189  		transactionBody := services.TransactionBody{}
   190  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
   191  
   192  		require.NotNil(t, transactionBody.TransactionID)
   193  		transactionId := transactionBody.TransactionID.String()
   194  		require.NotEqual(t, "", transactionId)
   195  
   196  		sigMap := signedTransaction.GetSigMap()
   197  		require.NotNil(t, sigMap)
   198  
   199  		return &services.TransactionResponse{
   200  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   201  		}
   202  	}
   203  	responses := [][]interface{}{{
   204  		call,
   205  	}}
   206  
   207  	client, server := NewMockClientAndServer(responses)
   208  	defer server.Close()
   209  
   210  	checksum := "dmqui"
   211  	token := TokenID{Token: 3, checksum: &checksum}
   212  
   213  	freeze, err := NewTokenUpdateNftsTransaction().
   214  		SetTokenID(token).
   215  		SetSerialNumbers([]int64{1, 2, 3}).
   216  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   217  		SetMetadata([]byte("metadata")).FreezeWith(client)
   218  	require.NoError(t, err)
   219  
   220  	_, err = freeze.Sign(newKey).Execute(client)
   221  	require.NoError(t, err)
   222  
   223  }
   224  
   225  func TestUnitTokenUpdateNftsTransactionSignWithOperator(t *testing.T) {
   226  	t.Parallel()
   227  
   228  	client, err := _NewMockClient()
   229  	require.NoError(t, err)
   230  
   231  	tokenID, err := TokenIDFromString("0.0.123")
   232  	require.NoError(t, err)
   233  
   234  	tokenUpdate := NewTokenUpdateNftsTransaction().
   235  		SetTokenID(tokenID).
   236  		SetSerialNumbers([]int64{1, 2, 3}).
   237  		SetMetadata([]byte("metadata"))
   238  
   239  	_, err = tokenUpdate.SignWithOperator(client)
   240  	require.NoError(t, err)
   241  }