mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-09 09:35:03 +09:00
Add mocks for tree
This commit is contained in:
parent
266fd9436c
commit
b0f2f875aa
4 changed files with 99 additions and 14 deletions
|
@ -9,7 +9,10 @@ import (
|
|||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/symmetric"
|
||||
)
|
||||
|
||||
var ErrIncorrectSignature = errors.New("change has incorrect signature")
|
||||
var (
|
||||
ErrIncorrectSignature = errors.New("change has incorrect signature")
|
||||
ErrIncorrectCID = errors.New("change has incorrect CID")
|
||||
)
|
||||
|
||||
type ChangeContent struct {
|
||||
ChangesData proto.Marshaler
|
||||
|
|
|
@ -60,12 +60,19 @@ func (c *changeBuilder) ConvertFromRawAndVerify(rawChange *aclpb.RawChange) (ch
|
|||
return
|
||||
}
|
||||
|
||||
// verifying signature
|
||||
res, err := identityKey.Verify(rawChange.Payload, rawChange.Signature)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !res {
|
||||
err = ErrIncorrectSignature
|
||||
return
|
||||
}
|
||||
|
||||
// verifying ID
|
||||
if !cid.VerifyCID(rawChange.Payload, rawChange.Id) {
|
||||
err = ErrIncorrectCID
|
||||
}
|
||||
|
||||
return
|
||||
|
|
|
@ -1,21 +1,96 @@
|
|||
package tree
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/app"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/aclpb"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/list"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/acllistbuilder"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestObjectTree(t *testing.T) {
|
||||
a := &app.App{}
|
||||
inmemory := storage.NewInMemoryTreeStorage(...)
|
||||
app.RegisterWithType[storage.TreeStorage](a, inmemory)
|
||||
app.RegisterWithType[]()
|
||||
|
||||
a.Start(context.Background())
|
||||
objectTree := app.MustComponentWithType[ObjectTree](a).(ObjectTree)
|
||||
|
||||
|
||||
type mockChangeCreator struct{}
|
||||
|
||||
func (c *mockChangeCreator) createRaw(id, aclId, snapshotId string, isSnapshot bool, prevIds ...string) *aclpb.RawChange {
|
||||
aclChange := &aclpb.Change{
|
||||
TreeHeadIds: prevIds,
|
||||
AclHeadId: aclId,
|
||||
SnapshotBaseId: snapshotId,
|
||||
ChangesData: nil,
|
||||
IsSnapshot: isSnapshot,
|
||||
}
|
||||
res, _ := aclChange.Marshal()
|
||||
return &aclpb.RawChange{
|
||||
Payload: res,
|
||||
Signature: nil,
|
||||
Id: id,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *mockChangeCreator) createNewTreeStorage(treeId, aclListId, aclHeadId, firstChangeId string) storage.TreeStorage {
|
||||
firstChange := c.createRaw(firstChangeId, aclHeadId, "", true)
|
||||
header := &aclpb.Header{
|
||||
FirstId: firstChangeId,
|
||||
AclListId: aclListId,
|
||||
WorkspaceId: "",
|
||||
DocType: aclpb.Header_DocTree,
|
||||
}
|
||||
treeStorage, _ := storage.NewInMemoryTreeStorage(treeId, header, []*aclpb.RawChange{firstChange})
|
||||
return treeStorage
|
||||
}
|
||||
|
||||
type mockChangeBuilder struct{}
|
||||
|
||||
func (c *mockChangeBuilder) ConvertFromRaw(rawChange *aclpb.RawChange) (ch *Change, err error) {
|
||||
unmarshalled := &aclpb.Change{}
|
||||
err = proto.Unmarshal(rawChange.Payload, unmarshalled)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ch = NewChange(rawChange.Id, unmarshalled, rawChange.Signature)
|
||||
return
|
||||
}
|
||||
func (c *mockChangeBuilder) ConvertFromRawAndVerify(rawChange *aclpb.RawChange) (ch *Change, err error) {
|
||||
return c.ConvertFromRaw(rawChange)
|
||||
}
|
||||
|
||||
func (c *mockChangeBuilder) BuildContent(payload BuilderContent) (ch *Change, raw *aclpb.RawChange, err error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
type mockChangeValidator struct{}
|
||||
|
||||
func (m *mockChangeValidator) ValidateTree(tree *Tree, aclList list.ACLList) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func prepareACLList(t *testing.T) list.ACLList {
|
||||
st, err := acllistbuilder.NewListStorageWithTestName("userjoinexample.yml")
|
||||
require.NoError(t, err, "building storage should not result in error")
|
||||
|
||||
aclList, err := list.BuildACLList(signingkey.NewEDPubKeyDecoder(), st)
|
||||
require.NoError(t, err, "building acl list should be without error")
|
||||
|
||||
return aclList
|
||||
}
|
||||
|
||||
func TestObjectTree_Build(t *testing.T) {
|
||||
aclList := prepareACLList(t)
|
||||
changeCreator := &mockChangeCreator{}
|
||||
treeStorage := changeCreator.createNewTreeStorage("treeId", aclList.ID(), aclList.Head().Id, "0")
|
||||
changeBuilder := &mockChangeBuilder{}
|
||||
deps := objectTreeDeps{
|
||||
changeBuilder: changeBuilder,
|
||||
treeBuilder: newTreeBuilder(treeStorage, changeBuilder),
|
||||
treeStorage: treeStorage,
|
||||
updateListener: nil,
|
||||
validator: &mockChangeValidator{},
|
||||
aclList: aclList,
|
||||
}
|
||||
|
||||
_, err := buildObjectTree(deps)
|
||||
require.NoError(t, err, "building tree should be without error")
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ func newChange(id string, snapshotId string, prevIds ...string) *Change {
|
|||
}
|
||||
}
|
||||
|
||||
func newSnapshot(id string, snapshotId string, prevIds ...string) *Change {
|
||||
func newSnapshot(id, snapshotId string, prevIds ...string) *Change {
|
||||
return &Change{
|
||||
PreviousIds: prevIds,
|
||||
Id: id,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue