-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10340 from vegaprotocol/feat/10307
Support updating profile for a party
- Loading branch information
Showing
41 changed files
with
7,703 additions
and
5,954 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (C) 2023 Gobalsky Labs Limited | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package commands | ||
|
||
import ( | ||
"fmt" | ||
|
||
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" | ||
) | ||
|
||
func CheckUpdatePartyProfile(cmd *commandspb.UpdatePartyProfile) error { | ||
return checkUpdatePartyProfile(cmd).ErrorOrNil() | ||
} | ||
|
||
func checkUpdatePartyProfile(cmd *commandspb.UpdatePartyProfile) Errors { | ||
errs := NewErrors() | ||
|
||
if cmd == nil { | ||
return errs.FinalAddForProperty("update_party_profile", ErrIsRequired) | ||
} | ||
|
||
if len(cmd.Alias) > 32 { | ||
errs.AddForProperty("update_party_profile.alias", ErrIsLimitedTo32Characters) | ||
} | ||
|
||
if len(cmd.Metadata) > 10 { | ||
errs.AddForProperty("update_party_profile.metadata", ErrIsLimitedTo10Entries) | ||
} else { | ||
seenKeys := map[string]interface{}{} | ||
for i, m := range cmd.Metadata { | ||
if len(m.Key) > 32 { | ||
errs.AddForProperty(fmt.Sprintf("update_party_profile.metadata.%d.key", i), ErrIsLimitedTo32Characters) | ||
} else if len(m.Key) == 0 { | ||
errs.AddForProperty(fmt.Sprintf("update_party_profile.metadata.%d.key", i), ErrCannotBeBlank) | ||
} | ||
|
||
_, alreadySeen := seenKeys[m.Key] | ||
if alreadySeen { | ||
errs.AddForProperty(fmt.Sprintf("update_party_profile.metadata.%d.key", i), ErrIsDuplicated) | ||
} | ||
seenKeys[m.Key] = nil | ||
|
||
if len(m.Value) > 255 { | ||
errs.AddForProperty(fmt.Sprintf("update_party_profile.metadata.%d.value", i), ErrIsLimitedTo255Characters) | ||
} | ||
} | ||
} | ||
|
||
return errs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
// Copyright (C) 2023 Gobalsky Labs Limited | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package commands_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"code.vegaprotocol.io/vega/commands" | ||
vegapb "code.vegaprotocol.io/vega/protos/vega" | ||
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUpdatePartyProfile(t *testing.T) { | ||
t.Run("Updating party's profile succeeds", testUpdatePartyProfileSucceeds) | ||
t.Run("Updating party's profile with invalid alias fails", testUpdatePartyProfileWithInvalidAliasFails) | ||
t.Run("Updating party's profile with invalid metadata fails", testUpdatePartyProfileWithInvalidMetadataFails) | ||
} | ||
|
||
func testUpdatePartyProfileSucceeds(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
cmd *commandspb.UpdatePartyProfile | ||
}{ | ||
{ | ||
name: "when empty", | ||
cmd: &commandspb.UpdatePartyProfile{}, | ||
}, { | ||
name: "with an alias", | ||
cmd: &commandspb.UpdatePartyProfile{ | ||
Alias: "test", | ||
}, | ||
}, { | ||
name: "with metadata", | ||
cmd: &commandspb.UpdatePartyProfile{ | ||
Metadata: []*vegapb.Metadata{ | ||
{ | ||
Key: "key", | ||
Value: "value", | ||
}, | ||
}, | ||
}, | ||
}, { | ||
name: "with both", | ||
cmd: &commandspb.UpdatePartyProfile{ | ||
Alias: "test", | ||
Metadata: []*vegapb.Metadata{ | ||
{ | ||
Key: "key", | ||
Value: "value", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := checkUpdatePartyProfile(t, tc.cmd) | ||
|
||
assert.Empty(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func testUpdatePartyProfileWithInvalidAliasFails(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
alias string | ||
err error | ||
}{ | ||
{ | ||
name: "with more than 32 characters", | ||
alias: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
err: commands.ErrIsLimitedTo32Characters, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := checkUpdatePartyProfile(t, &commandspb.UpdatePartyProfile{ | ||
Alias: tc.alias, | ||
}) | ||
|
||
assert.Contains(t, err.Get("update_party_profile.alias"), tc.err) | ||
}) | ||
} | ||
} | ||
|
||
func testUpdatePartyProfileWithInvalidMetadataFails(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
metadata []*vegapb.Metadata | ||
field string | ||
err error | ||
}{ | ||
{ | ||
name: "with more than 10 entries", | ||
metadata: []*vegapb.Metadata{{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}}, | ||
field: "update_party_profile.metadata", | ||
err: commands.ErrIsLimitedTo10Entries, | ||
}, { | ||
name: "with empty key", | ||
metadata: []*vegapb.Metadata{ | ||
{ | ||
Key: "", | ||
Value: "", | ||
}, | ||
}, | ||
field: "update_party_profile.metadata.0.key", | ||
err: commands.ErrCannotBeBlank, | ||
}, { | ||
name: "with key more than 32 characters", | ||
metadata: []*vegapb.Metadata{ | ||
{ | ||
Key: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
Value: "", | ||
}, | ||
}, | ||
field: "update_party_profile.metadata.0.key", | ||
err: commands.ErrIsLimitedTo32Characters, | ||
}, { | ||
name: "with duplicated key", | ||
metadata: []*vegapb.Metadata{ | ||
{ | ||
Key: "hello", | ||
Value: "value1", | ||
}, { | ||
Key: "hello", | ||
Value: "value2", | ||
}, | ||
}, | ||
field: "update_party_profile.metadata.1.key", | ||
err: commands.ErrIsDuplicated, | ||
}, { | ||
name: "with value more than 255 characters", | ||
metadata: []*vegapb.Metadata{ | ||
{ | ||
Key: "test", | ||
Value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + | ||
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + | ||
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
}, | ||
}, | ||
field: "update_party_profile.metadata.0.value", | ||
err: commands.ErrIsLimitedTo255Characters, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := checkUpdatePartyProfile(t, &commandspb.UpdatePartyProfile{ | ||
Metadata: tc.metadata, | ||
}) | ||
|
||
assert.Contains(t, err.Get(tc.field), tc.err) | ||
}) | ||
} | ||
} | ||
|
||
func checkUpdatePartyProfile(t *testing.T, cmd *commandspb.UpdatePartyProfile) commands.Errors { | ||
t.Helper() | ||
|
||
err := commands.CheckUpdatePartyProfile(cmd) | ||
|
||
var e commands.Errors | ||
if ok := errors.As(err, &e); !ok { | ||
return commands.NewErrors() | ||
} | ||
|
||
return e | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.