code.vegaprotocol.io/vega@v0.79.0/commands/create_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  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestCreateReferralSet(t *testing.T) {
    32  	t.Run("Creating referral set succeeds", testCreatingTeamSucceeds)
    33  	t.Run("Creating referral set fails", testCreateReferralSetFails)
    34  }
    35  
    36  func testCreatingTeamSucceeds(t *testing.T) {
    37  	tcs := []struct {
    38  		name string
    39  		cmd  *commandspb.CreateReferralSet
    40  	}{
    41  		{
    42  			name: "with empty values",
    43  			cmd:  &commandspb.CreateReferralSet{},
    44  		}, {
    45  			name: "with just enabled rewards",
    46  			cmd: &commandspb.CreateReferralSet{
    47  				IsTeam: false,
    48  			},
    49  		}, {
    50  			name: "with just name",
    51  			cmd: &commandspb.CreateReferralSet{
    52  				IsTeam: true,
    53  				Team: &commandspb.CreateReferralSet_Team{
    54  					Name:      vgrand.RandomStr(5),
    55  					TeamUrl:   nil,
    56  					AvatarUrl: nil,
    57  				},
    58  			},
    59  		}, {
    60  			name: "with just team URL",
    61  			cmd: &commandspb.CreateReferralSet{
    62  				IsTeam: true,
    63  				Team: &commandspb.CreateReferralSet_Team{
    64  					Name:      "some team",
    65  					TeamUrl:   ptr.From(vgrand.RandomStr(5)),
    66  					AvatarUrl: nil,
    67  				},
    68  			},
    69  		}, {
    70  			name: "with just avatar URL",
    71  			cmd: &commandspb.CreateReferralSet{
    72  				IsTeam: true,
    73  				Team: &commandspb.CreateReferralSet_Team{
    74  					Name:      "some team",
    75  					TeamUrl:   nil,
    76  					AvatarUrl: ptr.From(vgrand.RandomStr(5)),
    77  				},
    78  			},
    79  		}, {
    80  			name: "with all at once",
    81  			cmd: &commandspb.CreateReferralSet{
    82  				IsTeam: true,
    83  				Team: &commandspb.CreateReferralSet_Team{
    84  					Name:      vgrand.RandomStr(5),
    85  					TeamUrl:   ptr.From(vgrand.RandomStr(5)),
    86  					AvatarUrl: ptr.From(vgrand.RandomStr(5)),
    87  					Closed:    true,
    88  					AllowList: []string{vgrand.RandomStr(5), vgrand.RandomStr(5)},
    89  				},
    90  			},
    91  		},
    92  	}
    93  
    94  	for _, tc := range tcs {
    95  		t.Run(tc.name, func(tt *testing.T) {
    96  			require.Empty(tt, checkCreateReferralSet(tt, tc.cmd), tc.name)
    97  		})
    98  	}
    99  }
   100  
   101  func testCreateReferralSetFails(t *testing.T) {
   102  	err := checkCreateReferralSet(t, &commandspb.CreateReferralSet{
   103  		IsTeam: true,
   104  		Team:   nil,
   105  	})
   106  
   107  	assert.Contains(t, err.Get("create_referral_set.team"), commands.ErrIsRequired)
   108  
   109  	err = checkCreateReferralSet(t, &commandspb.CreateReferralSet{
   110  		IsTeam: true,
   111  		Team:   &commandspb.CreateReferralSet_Team{},
   112  	})
   113  
   114  	assert.Contains(t, err.Get("create_referral_set.team.name"), commands.ErrIsRequired)
   115  
   116  	err = checkCreateReferralSet(t, &commandspb.CreateReferralSet{
   117  		IsTeam: true,
   118  		Team: &commandspb.CreateReferralSet_Team{
   119  			Name: vgrand.RandomStr(101),
   120  		},
   121  	})
   122  
   123  	assert.Contains(t, err.Get("create_referral_set.team.name"), commands.ErrMustBeLessThan100Chars)
   124  
   125  	err = checkCreateReferralSet(t, &commandspb.CreateReferralSet{
   126  		IsTeam: true,
   127  		Team: &commandspb.CreateReferralSet_Team{
   128  			AvatarUrl: ptr.From(vgrand.RandomStr(201)),
   129  		},
   130  	})
   131  
   132  	assert.Contains(t, err.Get("create_referral_set.team.avatar_url"), commands.ErrMustBeLessThan200Chars)
   133  
   134  	err = checkCreateReferralSet(t, &commandspb.CreateReferralSet{
   135  		IsTeam: true,
   136  		Team: &commandspb.CreateReferralSet_Team{
   137  			TeamUrl: ptr.From(vgrand.RandomStr(201)),
   138  		},
   139  	})
   140  
   141  	assert.Contains(t, err.Get("create_referral_set.team.team_url"), commands.ErrMustBeLessThan200Chars)
   142  
   143  	err = checkCreateReferralSet(t, &commandspb.CreateReferralSet{
   144  		IsTeam: true,
   145  		Team: &commandspb.CreateReferralSet_Team{
   146  			Closed:    false,
   147  			AllowList: []string{vgrand.RandomStr(5)},
   148  		},
   149  	})
   150  
   151  	assert.Contains(t, err.Get("create_referral_set.team.allow_list"), commands.ErrCannotSetAllowListWhenTeamIsOpened)
   152  }
   153  
   154  func checkCreateReferralSet(t *testing.T, cmd *commandspb.CreateReferralSet) commands.Errors {
   155  	t.Helper()
   156  
   157  	err := commands.CheckCreateReferralSet(cmd)
   158  
   159  	var e commands.Errors
   160  	if ok := errors.As(err, &e); !ok {
   161  		return commands.NewErrors()
   162  	}
   163  
   164  	return e
   165  }