1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +09:00

Add batch in test suite

This commit is contained in:
mcrakhman 2024-02-26 19:50:04 +01:00
parent 08b8e5dbc9
commit 47df1f5b86
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
3 changed files with 164 additions and 40 deletions

View file

@ -47,8 +47,127 @@ var (
errIncorrectParts = errors.New("incorrect parts")
)
func (a *AclTestExecutor) buildBatchRequest(args []string, acl AclList, getPerm func(perm string) AclPermissions, addRec func(rec *consensusproto.RawRecordWithId) error) (afterAll []func(), err error) {
// remove:a,b,c;add:d,rw,m1|e,r,m2;changes:f,rw|g,r;revoke:inv1id;decline:g,h;
batchPayload := BatchRequestPayload{}
for _, arg := range args {
parts := strings.Split(arg, ":")
if len(parts) != 2 {
return nil, errIncorrectParts
}
command := parts[0]
commandArgs := strings.Split(parts[1], "|")
switch command {
case "add":
var payloads []AccountAdd
for _, arg := range commandArgs {
argParts := strings.Split(arg, ",")
if len(argParts) != 3 {
return nil, errIncorrectParts
}
keys, err := accountdata.NewRandom()
if err != nil {
return nil, err
}
ownerAcl := a.actualAccounts[a.owner].Acl.(*aclList)
accountAcl, err := BuildAclListWithIdentity(keys, ownerAcl.storage, NoOpAcceptorVerifier{})
if err != nil {
return nil, err
}
state := &TestAclState{
Keys: keys,
Acl: accountAcl,
}
account := argParts[0]
a.actualAccounts[account] = state
a.expectedAccounts[account] = &accountExpectedState{
perms: getPerm(argParts[1]),
status: StatusActive,
metadata: []byte(argParts[2]),
pseudoId: account,
}
payloads = append(payloads, AccountAdd{
Identity: keys.SignKey.GetPublic(),
Permissions: getPerm(argParts[1]),
Metadata: []byte(argParts[2]),
})
}
defer func() {
if err != nil {
for _, arg := range args {
argParts := strings.Split(arg, ",")
account := argParts[0]
delete(a.expectedAccounts, account)
delete(a.actualAccounts, account)
}
}
}()
batchPayload.Additions = payloads
case "remove":
identities := strings.Split(commandArgs[0], ",")
var pubKeys []crypto.PubKey
for _, id := range identities {
pk := a.actualAccounts[id].Keys.SignKey.GetPublic()
pubKeys = append(pubKeys, pk)
}
priv, _, err := crypto.GenerateRandomEd25519KeyPair()
if err != nil {
return nil, err
}
sym := crypto.NewAES()
payload := AccountRemovePayload{
Identities: pubKeys,
Change: ReadKeyChangePayload{
MetadataKey: priv,
ReadKey: sym,
},
}
batchPayload.Removals = payload
case "changes":
var payloads []PermissionChangePayload
for _, arg := range commandArgs {
argParts := strings.Split(arg, ",")
if len(argParts) != 2 {
return nil, errIncorrectParts
}
changed := a.actualAccounts[argParts[0]].Keys.SignKey.GetPublic()
perms := getPerm(argParts[1])
payloads = append(payloads, PermissionChangePayload{
Identity: changed,
Permissions: perms,
})
afterAll = append(afterAll, func() {
a.expectedAccounts[argParts[0]].perms = perms
})
}
batchPayload.Changes = payloads
case "revoke":
invite := a.invites[commandArgs[0]]
invId, err := acl.AclState().GetInviteIdByPrivKey(invite)
if err != nil {
return nil, err
}
batchPayload.InviteRevokes = append(batchPayload.InviteRevokes, invId)
case "decline":
id := commandArgs[0]
pk := a.actualAccounts[id].Keys.SignKey.GetPublic()
rec, err := acl.AclState().JoinRecord(pk, false)
if err != nil {
return nil, err
}
batchPayload.Declines = append(batchPayload.Declines, rec.RecordId)
}
}
res, err := acl.RecordBuilder().BuildBatchRequest(batchPayload)
if err != nil {
return nil, err
}
return afterAll, addRec(WrapAclRecord(res))
}
func (a *AclTestExecutor) Execute(cmd string) (err error) {
parts := strings.Split(cmd, ":")
parts := strings.Split(cmd, "::")
if len(parts) != 2 {
return errIncorrectParts
}
@ -181,6 +300,11 @@ func (a *AclTestExecutor) Execute(cmd string) (err error) {
if err != nil {
return err
}
case "batch":
afterAll, err = a.buildBatchRequest(args, acl, getPerm, addRec)
if err != nil {
return err
}
case "approve":
recs, err := acl.AclState().JoinRecords(false)
if err != nil {

View file

@ -50,61 +50,61 @@ func TestAclExecutor(t *testing.T) {
err error
}
cmds := []cmdErr{
{"a.init:a", nil},
{"a.init::a", nil},
// creating an invite
{"a.invite:invId", nil},
{"a.invite::invId", nil},
// cannot self join
{"a.join:invId", ErrInsufficientPermissions},
{"a.join::invId", ErrInsufficientPermissions},
// now b can join
{"b.join:invId", nil},
{"b.join::invId", nil},
// a approves b, it can write now
{"a.approve:b,r", nil},
{"a.approve::b,r", nil},
// c joins with the same invite
{"c.join:invId", nil},
{"c.join::invId", nil},
// a approves c
{"a.approve:c,r", nil},
{"a.approve::c,r", nil},
// a removes c
{"a.remove:c", nil},
{"a.remove::c", nil},
// e also joins as an admin
{"e.join:invId", nil},
{"a.approve:e,adm", nil},
{"e.join::invId", nil},
{"a.approve::e,adm", nil},
// now e can remove other users
{"e.remove:b", nil},
{"e.revoke:invId", nil},
{"z.join:invId", ErrNoSuchInvite},
{"e.remove::b", nil},
{"e.revoke::invId", nil},
{"z.join::invId", ErrNoSuchInvite},
// e can't revoke the same id
{"e.revoke:invId", ErrNoSuchRecord},
{"e.revoke::invId", ErrNoSuchRecord},
// e can't remove a, because a is the owner
{"e.remove:a", ErrInsufficientPermissions},
{"e.remove::a", ErrInsufficientPermissions},
// e can add new users
{"e.add:x,r,m1;y,adm,m2", nil},
{"e.add::x,r,m1;y,adm,m2", nil},
// now y can also change permission as an admin
{"y.changes:x,rw", nil},
{"y.changes::x,rw", nil},
// e can generate another invite
{"e.invite:inv1Id", nil},
{"e.invite::inv1Id", nil},
// b tries to join again
{"b.join:inv1Id", nil},
{"b.join::inv1Id", nil},
// e approves b
{"e.approve:b,rw", nil},
{"g.join:inv1Id", nil},
{"g.cancel:g", nil},
{"e.approve::b,rw", nil},
{"g.join::inv1Id", nil},
{"g.cancel::g", nil},
// e cannot approve cancelled request
{"e.approve:g,rw", fmt.Errorf("no join records for approve")},
{"g.join:inv1Id", nil},
{"e.decline:g", nil},
{"e.approve::g,rw", fmt.Errorf("no join records for approve")},
{"g.join::inv1Id", nil},
{"e.decline::g", nil},
// g cannot cancel declined request
{"g.cancel:g", ErrNoSuchRecord},
{"g.join:inv1Id", nil},
{"e.approve:g,r", nil},
{"g.cancel::g", ErrNoSuchRecord},
{"g.join::inv1Id", nil},
{"e.approve::g,r", nil},
// g can request remove
{"g.request_remove:g", nil},
{"g.request_remove::g", nil},
// g can cancel request to remove
{"g.cancel:g", nil},
{"g.request_remove:g", nil},
{"g.request_remove:g", ErrPendingRequest},
{"a.remove:g", nil},
{"g.cancel::g", nil},
{"g.request_remove::g", nil},
{"g.request_remove::g", ErrPendingRequest},
{"a.remove::g", nil},
// g cannot cancel not existing request to remove
{"g.cancel:g", ErrNoSuchRecord},
{"g.cancel::g", ErrNoSuchRecord},
}
for _, cmd := range cmds {
err := a.Execute(cmd.cmd)

View file

@ -128,10 +128,10 @@ func TestObjectTree(t *testing.T) {
err error
}
cmds := []cmdErr{
{"a.init:a", nil},
{"a.invite:invId", nil},
{"b.join:invId", nil},
{"a.approve:b,r", nil},
{"a.init::a", nil},
{"a.invite::invId", nil},
{"b.join::invId", nil},
{"a.approve::b,r", nil},
}
for _, cmd := range cmds {
err := exec.Execute(cmd.cmd)
@ -161,7 +161,7 @@ func TestObjectTree(t *testing.T) {
bStore := aTree.Storage().(*treestorage.InMemoryTreeStorage).Copy()
bTree, err := BuildKeyFilterableObjectTree(bStore, bAccount.Acl)
require.NoError(t, err)
err = exec.Execute("a.remove:b")
err = exec.Execute("a.remove::b")
require.NoError(t, err)
res, err := aTree.AddContent(ctx, SignableChangeContent{
Data: []byte("some"),