code.vegaprotocol.io/vega@v0.79.0/commands/update_referral_set_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  	"code.vegaprotocol.io/vega/libs/ptr"
    24  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    25  	vgtest "code.vegaprotocol.io/vega/libs/test"
    26  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestUpdateReferralSet(t *testing.T) {
    33  	t.Run("Updating referral set succeeds", testUpdatingTeamSucceeds)
    34  	t.Run("Updating referral set with team ID fails", testUpdateReferralSetWithoutTeamIDFails)
    35  	t.Run("Updating referral set fails", testUpdateReferralSetFails)
    36  }
    37  
    38  func testUpdatingTeamSucceeds(t *testing.T) {
    39  	tcs := []struct {
    40  		name string
    41  		cmd  *commandspb.UpdateReferralSet
    42  	}{
    43  		{
    44  			name: "with empty values",
    45  			cmd: &commandspb.UpdateReferralSet{
    46  				Id: vgtest.RandomVegaID(),
    47  			},
    48  		}, {
    49  			name: "with just enabled rewards",
    50  			cmd: &commandspb.UpdateReferralSet{
    51  				Id:     vgtest.RandomVegaID(),
    52  				IsTeam: false,
    53  			},
    54  		}, {
    55  			name: "with just name",
    56  			cmd: &commandspb.UpdateReferralSet{
    57  				Id:     vgtest.RandomVegaID(),
    58  				IsTeam: true,
    59  				Team: &commandspb.UpdateReferralSet_Team{
    60  					Name:      ptr.From(vgrand.RandomStr(5)),
    61  					TeamUrl:   nil,
    62  					AvatarUrl: nil,
    63  					Closed:    nil,
    64  				},
    65  			},
    66  		}, {
    67  			name: "with just team URL",
    68  			cmd: &commandspb.UpdateReferralSet{
    69  				Id:     vgtest.RandomVegaID(),
    70  				IsTeam: true,
    71  				Team: &commandspb.UpdateReferralSet_Team{
    72  					Name:      nil,
    73  					TeamUrl:   ptr.From(vgrand.RandomStr(5)),
    74  					AvatarUrl: nil,
    75  					Closed:    nil,
    76  				},
    77  			},
    78  		}, {
    79  			name: "with just avatar URL",
    80  			cmd: &commandspb.UpdateReferralSet{
    81  				Id:     vgtest.RandomVegaID(),
    82  				IsTeam: true,
    83  				Team: &commandspb.UpdateReferralSet_Team{
    84  					Name:      nil,
    85  					TeamUrl:   nil,
    86  					AvatarUrl: ptr.From(vgrand.RandomStr(5)),
    87  					Closed:    nil,
    88  				},
    89  			},
    90  		}, {
    91  			name: "with all at once",
    92  			cmd: &commandspb.UpdateReferralSet{
    93  				Id:     vgtest.RandomVegaID(),
    94  				IsTeam: true,
    95  				Team: &commandspb.UpdateReferralSet_Team{
    96  					Name:      ptr.From(vgrand.RandomStr(5)),
    97  					TeamUrl:   ptr.From(vgrand.RandomStr(5)),
    98  					AvatarUrl: ptr.From(vgrand.RandomStr(5)),
    99  					Closed:    ptr.From(true),
   100  				},
   101  			},
   102  		},
   103  	}
   104  
   105  	for _, tc := range tcs {
   106  		t.Run(tc.name, func(tt *testing.T) {
   107  			require.Empty(tt, checkUpdateReferralSet(tt, tc.cmd))
   108  		})
   109  	}
   110  }
   111  
   112  func testUpdateReferralSetWithoutTeamIDFails(t *testing.T) {
   113  	err := checkUpdateReferralSet(t, &commandspb.UpdateReferralSet{
   114  		Id: "",
   115  	})
   116  
   117  	assert.Contains(t, err.Get("update_referral_set.id"), commands.ErrShouldBeAValidVegaID)
   118  }
   119  
   120  func testUpdateReferralSetFails(t *testing.T) {
   121  	err := checkUpdateReferralSet(t, &commandspb.UpdateReferralSet{
   122  		Id:     "someid",
   123  		IsTeam: true,
   124  		Team:   nil,
   125  	})
   126  
   127  	assert.Contains(t, err.Get("update_referral_set.team"), commands.ErrIsRequired)
   128  
   129  	err = checkUpdateReferralSet(t, &commandspb.UpdateReferralSet{
   130  		Id:     "someid",
   131  		IsTeam: true,
   132  		Team: &commandspb.UpdateReferralSet_Team{
   133  			Name: ptr.From(""),
   134  		},
   135  	})
   136  
   137  	assert.Contains(t, err.Get("update_referral_set.team.name"), commands.ErrIsRequired)
   138  
   139  	err = checkUpdateReferralSet(t, &commandspb.UpdateReferralSet{
   140  		IsTeam: true,
   141  		Team: &commandspb.UpdateReferralSet_Team{
   142  			Name: ptr.From(vgrand.RandomStr(101)),
   143  		},
   144  	})
   145  
   146  	assert.Contains(t, err.Get("update_referral_set.team.name"), commands.ErrMustBeLessThan100Chars)
   147  
   148  	err = checkUpdateReferralSet(t, &commandspb.UpdateReferralSet{
   149  		IsTeam: true,
   150  		Team: &commandspb.UpdateReferralSet_Team{
   151  			AvatarUrl: ptr.From(vgrand.RandomStr(201)),
   152  		},
   153  	})
   154  
   155  	assert.Contains(t, err.Get("update_referral_set.team.avatar_url"), commands.ErrMustBeLessThan200Chars)
   156  
   157  	err = checkUpdateReferralSet(t, &commandspb.UpdateReferralSet{
   158  		IsTeam: true,
   159  		Team: &commandspb.UpdateReferralSet_Team{
   160  			TeamUrl: ptr.From(vgrand.RandomStr(201)),
   161  		},
   162  	})
   163  
   164  	assert.Contains(t, err.Get("update_referral_set.team.team_url"), commands.ErrMustBeLessThan200Chars)
   165  
   166  	err = checkUpdateReferralSet(t, &commandspb.UpdateReferralSet{
   167  		IsTeam: true,
   168  		Team: &commandspb.UpdateReferralSet_Team{
   169  			AllowList: []string{vgrand.RandomStr(5)},
   170  		},
   171  	})
   172  
   173  	assert.Contains(t, err.Get("update_referral_set.team.allow_list"), commands.ErrSettingAllowListRequireSettingClosedState)
   174  
   175  	err = checkUpdateReferralSet(t, &commandspb.UpdateReferralSet{
   176  		IsTeam: true,
   177  		Team: &commandspb.UpdateReferralSet_Team{
   178  			Closed:    ptr.From(false),
   179  			AllowList: []string{vgrand.RandomStr(5)},
   180  		},
   181  	})
   182  
   183  	assert.Contains(t, err.Get("update_referral_set.team.allow_list"), commands.ErrCannotSetAllowListWhenTeamIsOpened)
   184  }
   185  
   186  func checkUpdateReferralSet(t *testing.T, cmd *commandspb.UpdateReferralSet) commands.Errors {
   187  	t.Helper()
   188  
   189  	err := commands.CheckUpdateReferralSet(cmd)
   190  
   191  	var e commands.Errors
   192  	if ok := errors.As(err, &e); !ok {
   193  		return commands.NewErrors()
   194  	}
   195  
   196  	return e
   197  }