code.vegaprotocol.io/vega@v0.79.0/core/parties/engine_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 parties_test
    17  
    18  import (
    19  	"testing"
    20  
    21  	"code.vegaprotocol.io/vega/core/types"
    22  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    23  	vgtest "code.vegaprotocol.io/vega/libs/test"
    24  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    25  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    26  
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestUpdatingProfiles(t *testing.T) {
    31  	ctx := vgtest.VegaContext(vgrand.RandomStr(5), vgtest.RandomI64())
    32  
    33  	te := newEngine(t)
    34  
    35  	party1 := newPartyID(t)
    36  
    37  	expectPartyProfileUpdatedEvent(t, te)
    38  	require.NoError(t, te.engine.UpdateProfile(ctx, party1, &commandspb.UpdatePartyProfile{
    39  		Alias: "test1",
    40  		Metadata: []*vegapb.Metadata{
    41  			{
    42  				Key:   "key1",
    43  				Value: "value1",
    44  			},
    45  		},
    46  	}))
    47  
    48  	party2 := newPartyID(t)
    49  
    50  	expectPartyProfileUpdatedEvent(t, te)
    51  	require.NoError(t, te.engine.UpdateProfile(ctx, party2, &commandspb.UpdatePartyProfile{
    52  		Alias: "test2",
    53  		Metadata: []*vegapb.Metadata{
    54  			{
    55  				Key:   "key1",
    56  				Value: "value1",
    57  			},
    58  		},
    59  	}))
    60  
    61  	expectPartyProfileUpdatedEvent(t, te)
    62  	require.NoError(t, te.engine.UpdateProfile(ctx, party1, &commandspb.UpdatePartyProfile{
    63  		Alias: "test1",
    64  		Metadata: []*vegapb.Metadata{
    65  			{
    66  				Key:   "key2",
    67  				Value: "value2",
    68  			},
    69  			{
    70  				Key:   "key3",
    71  				Value: "value3",
    72  			},
    73  		},
    74  	}))
    75  
    76  	// Attempt using alias from party 2.
    77  	require.Error(t, te.engine.UpdateProfile(ctx, party1, &commandspb.UpdatePartyProfile{
    78  		Alias: "test2",
    79  	}))
    80  
    81  	assertEqualProfiles(t, []types.PartyProfile{
    82  		{
    83  			PartyID: party1,
    84  			Alias:   "test1",
    85  			Metadata: map[string]string{
    86  				"key2": "value2",
    87  				"key3": "value3",
    88  			},
    89  			DerivedKeys: map[string]struct{}{},
    90  		},
    91  		{
    92  			PartyID: party2,
    93  			Alias:   "test2",
    94  			Metadata: map[string]string{
    95  				"key1": "value1",
    96  			},
    97  			DerivedKeys: map[string]struct{}{},
    98  		},
    99  	}, te.engine.ListProfiles())
   100  
   101  	require.Error(t, te.engine.UpdateProfile(ctx, party1, &commandspb.UpdatePartyProfile{
   102  		Alias: "network",
   103  	}))
   104  }
   105  
   106  func TestAssigningDerivedKeys(t *testing.T) {
   107  	ctx := vgtest.VegaContext(vgrand.RandomStr(5), vgtest.RandomI64())
   108  
   109  	te := newEngine(t)
   110  
   111  	party1 := newPartyID(t)
   112  
   113  	// Non-existing party
   114  	require.False(t, te.engine.CheckDerivedKeyOwnership(party1, "derivedKey1"))
   115  
   116  	// Assigning derived keys create profile if it doesn't exist
   117  	te.engine.AssignDeriveKey(ctx, party1, "derivedKey1")
   118  	te.engine.AssignDeriveKey(ctx, party1, "derivedKey2")
   119  
   120  	require.True(t, te.engine.CheckDerivedKeyOwnership(party1, "derivedKey1"))
   121  	require.True(t, te.engine.CheckDerivedKeyOwnership(party1, "derivedKey2"))
   122  	require.False(t, te.engine.CheckDerivedKeyOwnership(party1, "derivedKey3"))
   123  
   124  	// Updating profile doesn't remove derived keys and still works
   125  	expectPartyProfileUpdatedEvent(t, te)
   126  	require.NoError(t, te.engine.UpdateProfile(ctx, party1, &commandspb.UpdatePartyProfile{
   127  		Alias: "test1",
   128  		Metadata: []*vegapb.Metadata{
   129  			{
   130  				Key:   "key1",
   131  				Value: "value1",
   132  			},
   133  		},
   134  	}))
   135  
   136  	party2 := newPartyID(t)
   137  
   138  	expectPartyProfileUpdatedEvent(t, te)
   139  	require.NoError(t, te.engine.UpdateProfile(ctx, party2, &commandspb.UpdatePartyProfile{
   140  		Alias: "test2",
   141  		Metadata: []*vegapb.Metadata{
   142  			{
   143  				Key:   "key1",
   144  				Value: "value1",
   145  			},
   146  		},
   147  	}))
   148  
   149  	expectPartyProfileUpdatedEvent(t, te)
   150  	require.NoError(t, te.engine.UpdateProfile(ctx, party1, &commandspb.UpdatePartyProfile{
   151  		Alias: "test1",
   152  		Metadata: []*vegapb.Metadata{
   153  			{
   154  				Key:   "key2",
   155  				Value: "value2",
   156  			},
   157  			{
   158  				Key:   "key3",
   159  				Value: "value3",
   160  			},
   161  		},
   162  	}))
   163  
   164  	// Assign key for party 2
   165  	te.engine.AssignDeriveKey(ctx, party2, "derivedKey3")
   166  
   167  	// Attempt using alias from party 2.
   168  	require.Error(t, te.engine.UpdateProfile(ctx, party1, &commandspb.UpdatePartyProfile{
   169  		Alias: "test2",
   170  	}))
   171  
   172  	assertEqualProfiles(t, []types.PartyProfile{
   173  		{
   174  			PartyID: party1,
   175  			Alias:   "test1",
   176  			Metadata: map[string]string{
   177  				"key2": "value2",
   178  				"key3": "value3",
   179  			},
   180  			DerivedKeys: map[string]struct{}{"derivedKey1": {}, "derivedKey2": {}},
   181  		},
   182  		{
   183  			PartyID: party2,
   184  			Alias:   "test2",
   185  			Metadata: map[string]string{
   186  				"key1": "value1",
   187  			},
   188  			DerivedKeys: map[string]struct{}{"derivedKey3": {}},
   189  		},
   190  	}, te.engine.ListProfiles())
   191  }