github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/names/output_test.go (about)

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package namesPkg
     6  
     7  import (
     8  	"fmt"
     9  	"net/http"
    10  	"net/http/httptest"
    11  	"net/url"
    12  	"os"
    13  	"reflect"
    14  	"strings"
    15  	"testing"
    16  
    17  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    18  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/crud"
    19  )
    20  
    21  func TestNamesOptions_getCrudDataHttp(t *testing.T) {
    22  	type fields struct {
    23  		Autoname string
    24  	}
    25  	type args struct {
    26  		r *http.Request
    27  	}
    28  	tests := []struct {
    29  		name     string
    30  		fields   fields
    31  		args     args
    32  		wantData *crud.NameCrud
    33  		wantErr  bool
    34  	}{
    35  		{
    36  			name: "read from valid HTTP form",
    37  			args: args{
    38  				r: (func() (r *http.Request) {
    39  					data := url.Values{}
    40  					data.Set("address", "0x199d5ed7f45f4ee35960cf22eade2076e95b253f")
    41  					data.Set("name", "some name 1")
    42  					fmt.Println("encoded", data.Encode())
    43  					reader := strings.NewReader(data.Encode())
    44  					r = httptest.NewRequest("POST", "/names", reader)
    45  					r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    46  					return
    47  				})(),
    48  			},
    49  			wantData: &crud.NameCrud{
    50  				Address: crud.Field[base.Address]{
    51  					Value:   base.HexToAddress("0x199d5ed7f45f4ee35960cf22eade2076e95b253f"),
    52  					Updated: true,
    53  				},
    54  				Name: crud.Field[string]{
    55  					Value:   "some name 1",
    56  					Updated: true,
    57  				},
    58  				// Tags:         "",
    59  				// Source:      "",
    60  				// Symbol:      "",
    61  				// Decimals:    "",
    62  				// Description: "",
    63  			},
    64  		},
    65  		{
    66  			name: "read from invalid HTTP form",
    67  			args: args{
    68  				r: (func() (r *http.Request) {
    69  					data := url.Values{}
    70  
    71  					data.Set("name", "some name 2")
    72  					fmt.Println("encoded", data.Encode())
    73  					reader := strings.NewReader(data.Encode())
    74  					r = httptest.NewRequest("POST", "/names", reader)
    75  					r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    76  					return
    77  				})(),
    78  			},
    79  			wantErr: true,
    80  		},
    81  	}
    82  	for _, tt := range tests {
    83  		t.Run(tt.name, func(t *testing.T) {
    84  			opts := &NamesOptions{
    85  				Autoname: tt.fields.Autoname,
    86  			}
    87  			gotData, err := crud.NewNameCrud(len(opts.Autoname) > 0, tt.args.r)
    88  			if (err != nil) != tt.wantErr {
    89  				t.Errorf("NamesOptions.getCrudDataHttp() error = %v, wantErr %v", err, tt.wantErr)
    90  				return
    91  			}
    92  			if !reflect.DeepEqual(gotData, tt.wantData) {
    93  				t.Errorf("NamesOptions.getCrudDataHttp() = %v, want %v", gotData, tt.wantData)
    94  			}
    95  		})
    96  	}
    97  }
    98  
    99  func TestNamesOptions_getCrudDataEnv(t *testing.T) {
   100  	setEnvs := func(data *crud.NameCrud) {
   101  		if data.Address.Value.Hex() != "" {
   102  			os.Setenv("TB_NAME_ADDRESS", data.Address.Value.Hex())
   103  		}
   104  		if data.Name.Value != "" {
   105  			os.Setenv("TB_NAME_NAME", data.Name.Value)
   106  		}
   107  		if data.Tags.Value != "" {
   108  			os.Setenv("TB_NAME_TAGS", data.Tags.Value)
   109  		}
   110  		if data.Source.Value != "" {
   111  			os.Setenv("TB_NAME_SOURCE", data.Source.Value)
   112  		}
   113  		if data.Symbol.Value != "" {
   114  			os.Setenv("TB_NAME_SYMBOL", data.Symbol.Value)
   115  		}
   116  		if data.Decimals.Value != "" {
   117  			os.Setenv("TB_NAME_DECIMALS", data.Decimals.Value)
   118  		}
   119  	}
   120  	var expected *crud.NameCrud
   121  	var result *crud.NameCrud
   122  	var err error
   123  
   124  	// valid envs
   125  	expected = &crud.NameCrud{
   126  		Address: crud.Field[base.Address]{
   127  			Value:   base.HexToAddress("0x199d5ed7f45f4ee35960cf22eade2076e95b253f"),
   128  			Updated: true,
   129  		},
   130  		Name: crud.Field[string]{
   131  			Value:   "some name",
   132  			Updated: true,
   133  		},
   134  	}
   135  	setEnvs(expected)
   136  	result, err = crud.NewNameCrud(true /* nameRequired */, nil /* request */)
   137  	if err != nil {
   138  		t.Fatal(err)
   139  	}
   140  	if !reflect.DeepEqual(result, expected) {
   141  		t.Fatal("wrong value:", fmt.Sprintf("%+v", result))
   142  	}
   143  	os.Clearenv()
   144  
   145  	// invalid envs
   146  	expected = &crud.NameCrud{
   147  		Address: crud.Field[base.Address]{
   148  			Value:   base.ZeroAddr,
   149  			Updated: true,
   150  		},
   151  		Name: crud.Field[string]{
   152  			Value:   "some name",
   153  			Updated: true,
   154  		},
   155  	}
   156  	setEnvs(expected)
   157  	_, err = crud.NewNameCrud(true /* nameRequired */, nil /* request */)
   158  	if err == nil {
   159  		t.Fatal("error expected")
   160  	}
   161  	os.Clearenv()
   162  }