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) {
|
func (st *AclState) FirstMetadataKey() (crypto.PrivKey, error) {
|
||||||
if len(st.readKeyChanges) == 0 {
|
if firstKey, ok := st.keys[st.id]; ok && firstKey.MetadataPrivKey != nil {
|
||||||
return nil, ErrNoMetadataKey
|
return firstKey.MetadataPrivKey, nil
|
||||||
}
|
|
||||||
for _, change := range st.readKeyChanges {
|
|
||||||
key, exists := st.keys[change]
|
|
||||||
if !exists {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if key.MetadataPrivKey != nil {
|
|
||||||
return key.MetadataPrivKey, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil, ErrNoMetadataKey
|
return nil, ErrNoMetadataKey
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,10 @@ package list
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"github.com/anyproto/any-sync/util/crypto"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/anyproto/any-sync/util/crypto"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -73,14 +74,12 @@ func TestAclStateIsEmpty(t *testing.T) {
|
||||||
|
|
||||||
func TestAclState_FirstMetadataKey(t *testing.T) {
|
func TestAclState_FirstMetadataKey(t *testing.T) {
|
||||||
t.Run("returns first metadata key successfully", func(t *testing.T) {
|
t.Run("returns first metadata key successfully", func(t *testing.T) {
|
||||||
// given
|
|
||||||
privKey, _, err := crypto.GenerateEd25519Key(rand.Reader)
|
privKey, _, err := crypto.GenerateEd25519Key(rand.Reader)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
pubKey := privKey.GetPublic()
|
pubKey := privKey.GetPublic()
|
||||||
readKey := crypto.NewAES()
|
readKey := crypto.NewAES()
|
||||||
|
|
||||||
state := &AclState{
|
state := &AclState{
|
||||||
readKeyChanges: []string{"recordId"},
|
id: "recordId",
|
||||||
keys: map[string]AclKeys{
|
keys: map[string]AclKeys{
|
||||||
"recordId": {
|
"recordId": {
|
||||||
ReadKey: readKey,
|
ReadKey: readKey,
|
||||||
|
@ -89,53 +88,26 @@ func TestAclState_FirstMetadataKey(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// when
|
|
||||||
key, err := state.FirstMetadataKey()
|
key, err := state.FirstMetadataKey()
|
||||||
|
|
||||||
// then
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, privKey, key)
|
require.Equal(t, privKey, key)
|
||||||
})
|
})
|
||||||
t.Run("first metadata is nil", func(t *testing.T) {
|
t.Run("first metadata is nil", func(t *testing.T) {
|
||||||
// given
|
|
||||||
state := &AclState{
|
state := &AclState{
|
||||||
readKeyChanges: []string{"recordId"},
|
id: "recordId",
|
||||||
keys: map[string]AclKeys{
|
keys: map[string]AclKeys{
|
||||||
"recordId": {
|
"recordId": {
|
||||||
ReadKey: crypto.NewAES(),
|
ReadKey: crypto.NewAES(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// when
|
|
||||||
key, err := state.FirstMetadataKey()
|
key, err := state.FirstMetadataKey()
|
||||||
|
|
||||||
// then
|
|
||||||
require.ErrorIs(t, err, ErrNoMetadataKey)
|
require.ErrorIs(t, err, ErrNoMetadataKey)
|
||||||
require.Nil(t, key)
|
require.Nil(t, key)
|
||||||
})
|
})
|
||||||
t.Run("returns error when no read key changes", func(t *testing.T) {
|
t.Run("returns error when no read key changes", func(t *testing.T) {
|
||||||
// given
|
state := &AclState{}
|
||||||
state := &AclState{readKeyChanges: []string{}}
|
|
||||||
|
|
||||||
// when
|
|
||||||
_, err := state.FirstMetadataKey()
|
_, 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)
|
require.ErrorIs(t, err, ErrNoMetadataKey)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue