Add mocks for tree
This commit is contained in:
parent
94c95b44ec
commit
cde907e15a
@ -9,7 +9,10 @@ import (
|
|||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/symmetric"
|
"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 {
|
type ChangeContent struct {
|
||||||
ChangesData proto.Marshaler
|
ChangesData proto.Marshaler
|
||||||
|
|||||||
@ -60,12 +60,19 @@ func (c *changeBuilder) ConvertFromRawAndVerify(rawChange *aclpb.RawChange) (ch
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// verifying signature
|
||||||
res, err := identityKey.Verify(rawChange.Payload, rawChange.Signature)
|
res, err := identityKey.Verify(rawChange.Payload, rawChange.Signature)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !res {
|
if !res {
|
||||||
err = ErrIncorrectSignature
|
err = ErrIncorrectSignature
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// verifying ID
|
||||||
|
if !cid.VerifyCID(rawChange.Payload, rawChange.Id) {
|
||||||
|
err = ErrIncorrectCID
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|||||||
@ -1,21 +1,96 @@
|
|||||||
package tree
|
package tree
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/aclpb"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/app"
|
"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/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"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestObjectTree(t *testing.T) {
|
type mockChangeCreator struct{}
|
||||||
a := &app.App{}
|
|
||||||
inmemory := storage.NewInMemoryTreeStorage(...)
|
|
||||||
app.RegisterWithType[storage.TreeStorage](a, inmemory)
|
|
||||||
app.RegisterWithType[]()
|
|
||||||
|
|
||||||
a.Start(context.Background())
|
|
||||||
objectTree := app.MustComponentWithType[ObjectTree](a).(ObjectTree)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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{
|
return &Change{
|
||||||
PreviousIds: prevIds,
|
PreviousIds: prevIds,
|
||||||
Id: id,
|
Id: id,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user