code.vegaprotocol.io/vega@v0.79.0/commands/update_party_profile_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package commands_test
    17  
    18  import (
    19  	"errors"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/commands"
    23  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    24  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestUpdatePartyProfile(t *testing.T) {
    30  	t.Run("Updating party's profile succeeds", testUpdatePartyProfileSucceeds)
    31  	t.Run("Updating party's profile with invalid alias fails", testUpdatePartyProfileWithInvalidAliasFails)
    32  	t.Run("Updating party's profile with invalid metadata fails", testUpdatePartyProfileWithInvalidMetadataFails)
    33  }
    34  
    35  func testUpdatePartyProfileSucceeds(t *testing.T) {
    36  	tcs := []struct {
    37  		name string
    38  		cmd  *commandspb.UpdatePartyProfile
    39  	}{
    40  		{
    41  			name: "when empty",
    42  			cmd:  &commandspb.UpdatePartyProfile{},
    43  		}, {
    44  			name: "with an alias",
    45  			cmd: &commandspb.UpdatePartyProfile{
    46  				Alias: "test",
    47  			},
    48  		}, {
    49  			name: "with metadata",
    50  			cmd: &commandspb.UpdatePartyProfile{
    51  				Metadata: []*vegapb.Metadata{
    52  					{
    53  						Key:   "key",
    54  						Value: "value",
    55  					},
    56  				},
    57  			},
    58  		}, {
    59  			name: "with both",
    60  			cmd: &commandspb.UpdatePartyProfile{
    61  				Alias: "test",
    62  				Metadata: []*vegapb.Metadata{
    63  					{
    64  						Key:   "key",
    65  						Value: "value",
    66  					},
    67  				},
    68  			},
    69  		},
    70  	}
    71  
    72  	for _, tc := range tcs {
    73  		t.Run(tc.name, func(t *testing.T) {
    74  			err := checkUpdatePartyProfile(t, tc.cmd)
    75  
    76  			assert.Empty(t, err)
    77  		})
    78  	}
    79  }
    80  
    81  // 0088-PPRF-001.
    82  func testUpdatePartyProfileWithInvalidAliasFails(t *testing.T) {
    83  	tcs := []struct {
    84  		name  string
    85  		alias string
    86  		err   error
    87  	}{
    88  		{
    89  			name:  "with more than 32 characters",
    90  			alias: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    91  			err:   commands.ErrIsLimitedTo32Characters,
    92  		},
    93  	}
    94  
    95  	for _, tc := range tcs {
    96  		t.Run(tc.name, func(t *testing.T) {
    97  			err := checkUpdatePartyProfile(t, &commandspb.UpdatePartyProfile{
    98  				Alias: tc.alias,
    99  			})
   100  
   101  			assert.Contains(t, err.Get("update_party_profile.alias"), tc.err)
   102  		})
   103  	}
   104  }
   105  
   106  // 0088-PPRF-003, 0088-PPRF-004, 0088-PPRF-005.
   107  func testUpdatePartyProfileWithInvalidMetadataFails(t *testing.T) {
   108  	tcs := []struct {
   109  		name     string
   110  		metadata []*vegapb.Metadata
   111  		field    string
   112  		err      error
   113  	}{
   114  		{
   115  			name:     "with more than 10 entries",
   116  			metadata: []*vegapb.Metadata{{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}},
   117  			field:    "update_party_profile.metadata",
   118  			err:      commands.ErrIsLimitedTo10Entries,
   119  		}, {
   120  			name: "with empty key",
   121  			metadata: []*vegapb.Metadata{
   122  				{
   123  					Key:   "",
   124  					Value: "",
   125  				},
   126  			},
   127  			field: "update_party_profile.metadata.0.key",
   128  			err:   commands.ErrCannotBeBlank,
   129  		}, {
   130  			name: "with key more than 32 characters",
   131  			metadata: []*vegapb.Metadata{
   132  				{
   133  					Key:   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
   134  					Value: "",
   135  				},
   136  			},
   137  			field: "update_party_profile.metadata.0.key",
   138  			err:   commands.ErrIsLimitedTo32Characters,
   139  		}, {
   140  			name: "with duplicated key",
   141  			metadata: []*vegapb.Metadata{
   142  				{
   143  					Key:   "hello",
   144  					Value: "value1",
   145  				}, {
   146  					Key:   "hello",
   147  					Value: "value2",
   148  				},
   149  			},
   150  			field: "update_party_profile.metadata.1.key",
   151  			err:   commands.ErrIsDuplicated,
   152  		}, {
   153  			name: "with value more than 255 characters",
   154  			metadata: []*vegapb.Metadata{
   155  				{
   156  					Key: "test",
   157  					Value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
   158  						"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
   159  						"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
   160  				},
   161  			},
   162  			field: "update_party_profile.metadata.0.value",
   163  			err:   commands.ErrIsLimitedTo255Characters,
   164  		},
   165  	}
   166  
   167  	for _, tc := range tcs {
   168  		t.Run(tc.name, func(t *testing.T) {
   169  			err := checkUpdatePartyProfile(t, &commandspb.UpdatePartyProfile{
   170  				Metadata: tc.metadata,
   171  			})
   172  
   173  			assert.Contains(t, err.Get(tc.field), tc.err)
   174  		})
   175  	}
   176  }
   177  
   178  func checkUpdatePartyProfile(t *testing.T, cmd *commandspb.UpdatePartyProfile) commands.Errors {
   179  	t.Helper()
   180  
   181  	err := commands.CheckUpdatePartyProfile(cmd)
   182  
   183  	var e commands.Errors
   184  	if ok := errors.As(err, &e); !ok {
   185  		return commands.NewErrors()
   186  	}
   187  
   188  	return e
   189  }