1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +09:00

Change method

This commit is contained in:
Mikhail Rakhmanov 2025-04-24 16:46:00 +02:00
parent 120f03409e
commit 02ee0d327b
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
2 changed files with 7 additions and 44 deletions

View file

@ -152,17 +152,8 @@ func (st *AclState) CurrentMetadataKey() (crypto.PubKey, error) {
}
func (st *AclState) FirstMetadataKey() (crypto.PrivKey, error) {
if len(st.readKeyChanges) == 0 {
return nil, ErrNoMetadataKey
}
for _, change := range st.readKeyChanges {
key, exists := st.keys[change]
if !exists {
continue
}
if key.MetadataPrivKey != nil {
return key.MetadataPrivKey, nil
}
if firstKey, ok := st.keys[st.id]; ok && firstKey.MetadataPrivKey != nil {
return firstKey.MetadataPrivKey, nil
}
return nil, ErrNoMetadataKey
}

View file

@ -2,9 +2,10 @@ package list
import (
"crypto/rand"
"github.com/anyproto/any-sync/util/crypto"
"testing"
"github.com/anyproto/any-sync/util/crypto"
"github.com/stretchr/testify/require"
)
@ -73,14 +74,12 @@ func TestAclStateIsEmpty(t *testing.T) {
func TestAclState_FirstMetadataKey(t *testing.T) {
t.Run("returns first metadata key successfully", func(t *testing.T) {
// given
privKey, _, err := crypto.GenerateEd25519Key(rand.Reader)
require.NoError(t, err)
pubKey := privKey.GetPublic()
readKey := crypto.NewAES()
state := &AclState{
readKeyChanges: []string{"recordId"},
id: "recordId",
keys: map[string]AclKeys{
"recordId": {
ReadKey: readKey,
@ -89,53 +88,26 @@ func TestAclState_FirstMetadataKey(t *testing.T) {
},
},
}
// when
key, err := state.FirstMetadataKey()
// then
require.NoError(t, err)
require.Equal(t, privKey, key)
})
t.Run("first metadata is nil", func(t *testing.T) {
// given
state := &AclState{
readKeyChanges: []string{"recordId"},
id: "recordId",
keys: map[string]AclKeys{
"recordId": {
ReadKey: crypto.NewAES(),
},
},
}
// when
key, err := state.FirstMetadataKey()
// then
require.ErrorIs(t, err, ErrNoMetadataKey)
require.Nil(t, key)
})
t.Run("returns error when no read key changes", func(t *testing.T) {
// given
state := &AclState{readKeyChanges: []string{}}
// when
state := &AclState{}
_, err := state.FirstMetadataKey()
// then
require.ErrorIs(t, err, ErrNoMetadataKey)
})
t.Run("returns error when first read key change is missing", func(t *testing.T) {
// given
state := &AclState{
readKeyChanges: []string{"missingRecord"},
keys: make(map[string]AclKeys)}
// when
_, err := state.FirstMetadataKey()
// then
require.ErrorIs(t, err, ErrNoMetadataKey)
})
}