github.com/aeternity/aepp-sdk-go/v6@v6.0.0/cmd/account_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/aeternity/aepp-sdk-go/v6/naet"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  func TestAccountCreate(t *testing.T) {
    12  	password = "password"
    13  	emptyCmd := cobra.Command{}
    14  
    15  	dir, closer := testTempdir(t)
    16  	defer closer()
    17  	os.Chdir(dir)
    18  
    19  	err := createFunc(&emptyCmd, []string{"test.json"})
    20  	if err != nil {
    21  		t.Error(err)
    22  	}
    23  }
    24  
    25  func TestAccountAddress(t *testing.T) {
    26  	password = "password"
    27  	emptyCmd := cobra.Command{}
    28  
    29  	dir, closer := testTempdir(t)
    30  	defer closer()
    31  	os.Chdir(dir)
    32  
    33  	err := createFunc(&emptyCmd, []string{"test.json"})
    34  	if err != nil {
    35  		t.Error(err)
    36  	}
    37  
    38  	err = addressFunc(&emptyCmd, []string{"test.json"})
    39  	if err != nil {
    40  		t.Error(err)
    41  	}
    42  }
    43  
    44  func TestAccountSave(t *testing.T) {
    45  	password = "password"
    46  	emptyCmd := cobra.Command{}
    47  	privateKey := "025528252ec5db7d77cd57e14ae7819b9205c84abe5eef8353f88330467048f458019537ef2e809fefe1f2513cda8c8aacc74fb30f8c1f8b32d99a16b7f539b8"
    48  	dir, closer := testTempdir(t)
    49  	defer closer()
    50  	os.Chdir(dir)
    51  
    52  	err := saveFunc(&emptyCmd, []string{"test.json", privateKey})
    53  	if err != nil {
    54  		t.Error(err)
    55  	}
    56  }
    57  func TestAccountSign(t *testing.T) {
    58  	password = "password"
    59  	emptyCmd := cobra.Command{}
    60  	dir, closer := testTempdir(t)
    61  	defer closer()
    62  	os.Chdir(dir)
    63  
    64  	err := createFunc(&emptyCmd, []string{"test.json"})
    65  	if err != nil {
    66  		t.Error(err)
    67  	}
    68  
    69  	err = signFunc(&emptyCmd, []string{"test.json", "tx_+E8MAaEBzqet5HDJ+Z2dTkAIgKhvHUm7REti8Rqeu2S7z+tz/vOhAR8To7CL8AFABmKmi2nYdfeAPOxMCGR/btXYTHiXvVCjCoJOIIKC5wGAkBVBMQ=="})
    70  	if err != nil {
    71  		t.Error(err)
    72  	}
    73  }
    74  
    75  func Test_balanceFunc(t *testing.T) {
    76  	setPrivateNetParams()
    77  	type args struct {
    78  		conn              naet.GetAccounter
    79  		args              []string
    80  		accountPrivateKey string
    81  		password          string
    82  	}
    83  	tests := []struct {
    84  		name    string
    85  		args    args
    86  		wantErr bool
    87  		online  bool
    88  	}{
    89  		{
    90  			name: "Balance exists",
    91  			args: args{
    92  				conn:              &mockGetAccounter{account: `{"balance":1600000000000000077131306000000000000000,"id":"ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi","kind":"basic","nonce":0}`},
    93  				args:              []string{"test.json"},
    94  				accountPrivateKey: "e6a91d633c77cf5771329d3354b3bcef1bc5e032c43d70b6d35af923ce1eb74dcea7ade470c9f99d9d4e400880a86f1d49bb444b62f11a9ebb64bbcfeb73fef3",
    95  				password:          "password",
    96  			},
    97  			wantErr: false,
    98  			online:  false,
    99  		},
   100  		{
   101  			name: "Online: Balance exists",
   102  			args: args{
   103  				conn:              newAeNode(),
   104  				args:              []string{"test.json"},
   105  				accountPrivateKey: "e6a91d633c77cf5771329d3354b3bcef1bc5e032c43d70b6d35af923ce1eb74dcea7ade470c9f99d9d4e400880a86f1d49bb444b62f11a9ebb64bbcfeb73fef3",
   106  				password:          "password",
   107  			},
   108  			wantErr: false,
   109  			online:  true,
   110  		},
   111  		{
   112  			name: "Online: Balance does not exist",
   113  			args: args{
   114  				conn:              newAeNode(),
   115  				args:              []string{"test.json"},
   116  				accountPrivateKey: "f8f7a742bf75497bc5541b3bb487d1c3b822d73fb182229baf123ecafe926fe5d5ab4dd50536b3889f9b467b6076a9cabcd6d523756f15297967a40e6af3142b",
   117  				password:          "password",
   118  			},
   119  			wantErr: true,
   120  			online:  true,
   121  		},
   122  	}
   123  	for _, tt := range tests {
   124  		t.Run(tt.name, func(t *testing.T) {
   125  			if !online && tt.online {
   126  				t.Skip("Skipping online test")
   127  			}
   128  
   129  			// Create the account keystore
   130  			dir, closer := testTempdir(t)
   131  			defer closer()
   132  			os.Chdir(dir)
   133  			password = tt.args.password
   134  			err := saveFunc(&cobra.Command{}, []string{"test.json", tt.args.accountPrivateKey})
   135  			if err != nil {
   136  				t.Fatal(err)
   137  			}
   138  			// Query the balance using the account keystore
   139  			if err := balanceFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
   140  				t.Errorf("balanceFunc() error = %v, wantErr %v", err, tt.wantErr)
   141  			}
   142  		})
   143  	}
   144  }