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:
parent
120f03409e
commit
02ee0d327b
2 changed files with 7 additions and 44 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue