mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-08 05:57:03 +09:00
Merge pull request #422 from anyproto/go-5297-push-service-1st-iteration
GO-5297: add FirstMetadataKey function to AclState
This commit is contained in:
commit
acda7ef392
2 changed files with 50 additions and 0 deletions
|
@ -165,6 +165,13 @@ func (st *AclState) CurrentMetadataKey() (crypto.PubKey, error) {
|
||||||
return curKeys.MetadataPubKey, nil
|
return curKeys.MetadataPubKey, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (st *AclState) FirstMetadataKey() (crypto.PrivKey, error) {
|
||||||
|
if firstKey, ok := st.keys[st.id]; ok && firstKey.MetadataPrivKey != nil {
|
||||||
|
return firstKey.MetadataPrivKey, nil
|
||||||
|
}
|
||||||
|
return nil, ErrNoMetadataKey
|
||||||
|
}
|
||||||
|
|
||||||
func (st *AclState) Keys() map[string]AclKeys {
|
func (st *AclState) Keys() map[string]AclKeys {
|
||||||
return st.keys
|
return st.keys
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
package list
|
package list
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/anyproto/any-sync/util/crypto"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -68,3 +71,43 @@ func TestAclStateIsEmpty(t *testing.T) {
|
||||||
require.True(t, st.IsEmpty())
|
require.True(t, st.IsEmpty())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAclState_FirstMetadataKey(t *testing.T) {
|
||||||
|
t.Run("returns first metadata key successfully", func(t *testing.T) {
|
||||||
|
privKey, _, err := crypto.GenerateEd25519Key(rand.Reader)
|
||||||
|
require.NoError(t, err)
|
||||||
|
pubKey := privKey.GetPublic()
|
||||||
|
readKey := crypto.NewAES()
|
||||||
|
state := &AclState{
|
||||||
|
id: "recordId",
|
||||||
|
keys: map[string]AclKeys{
|
||||||
|
"recordId": {
|
||||||
|
ReadKey: readKey,
|
||||||
|
MetadataPrivKey: privKey,
|
||||||
|
MetadataPubKey: pubKey,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
key, err := state.FirstMetadataKey()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, privKey, key)
|
||||||
|
})
|
||||||
|
t.Run("first metadata is nil", func(t *testing.T) {
|
||||||
|
state := &AclState{
|
||||||
|
id: "recordId",
|
||||||
|
keys: map[string]AclKeys{
|
||||||
|
"recordId": {
|
||||||
|
ReadKey: crypto.NewAES(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
key, err := state.FirstMetadataKey()
|
||||||
|
require.ErrorIs(t, err, ErrNoMetadataKey)
|
||||||
|
require.Nil(t, key)
|
||||||
|
})
|
||||||
|
t.Run("returns error when no read key changes", func(t *testing.T) {
|
||||||
|
state := &AclState{}
|
||||||
|
_, err := state.FirstMetadataKey()
|
||||||
|
require.ErrorIs(t, err, ErrNoMetadataKey)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue