2021-12-16 15:26:13 +00:00
|
|
|
package container_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/container"
|
2022-05-12 16:37:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-12-16 15:26:13 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
|
|
|
containerCore "github.com/nspcc-dev/neofs-node/pkg/core/container"
|
|
|
|
containerSvc "github.com/nspcc-dev/neofs-node/pkg/services/container"
|
|
|
|
containerSvcMorph "github.com/nspcc-dev/neofs-node/pkg/services/container/morph"
|
|
|
|
containerSDK "github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-05-12 16:37:46 +00:00
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
2021-12-16 15:26:13 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/eacl"
|
2022-05-18 15:20:08 +00:00
|
|
|
sessiontest "github.com/nspcc-dev/neofs-sdk-go/session/test"
|
2021-12-16 15:26:13 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
type mock struct {
|
|
|
|
containerSvcMorph.Reader
|
|
|
|
}
|
2021-12-16 15:26:13 +00:00
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (m mock) Put(_ *containerSDK.Container) (*cid.ID, error) {
|
2021-12-16 15:26:13 +00:00
|
|
|
return new(cid.ID), nil
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (m mock) Delete(_ containerCore.RemovalWitness) error {
|
2021-12-16 15:26:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (m mock) PutEACL(_ *eacl.Table) error {
|
2021-12-16 15:26:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidToken(t *testing.T) {
|
|
|
|
m := mock{}
|
|
|
|
e := containerSvcMorph.NewExecutor(m, m)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr := cidtest.ID()
|
|
|
|
|
|
|
|
var cnrV2 refs.ContainerID
|
|
|
|
cnr.WriteToV2(&cnrV2)
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
var tokV2 session.Token
|
|
|
|
sessiontest.ContainerSigned().WriteToV2(&tokV2)
|
|
|
|
|
2021-12-16 15:26:13 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
op func(e containerSvc.ServiceExecutor, ctx containerSvc.ContextWithToken) error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "put",
|
|
|
|
op: func(e containerSvc.ServiceExecutor, ctx containerSvc.ContextWithToken) (err error) {
|
2022-05-16 13:15:31 +00:00
|
|
|
var reqBody container.PutRequestBody
|
|
|
|
reqBody.SetSignature(new(refs.Signature))
|
|
|
|
|
|
|
|
_, err = e.Put(ctx, &reqBody)
|
2021-12-16 15:26:13 +00:00
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "delete",
|
|
|
|
op: func(e containerSvc.ServiceExecutor, ctx containerSvc.ContextWithToken) (err error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
var reqBody container.DeleteRequestBody
|
|
|
|
reqBody.SetContainerID(&cnrV2)
|
|
|
|
|
|
|
|
_, err = e.Delete(ctx, &reqBody)
|
2021-12-16 15:26:13 +00:00
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "setEACL",
|
|
|
|
op: func(e containerSvc.ServiceExecutor, ctx containerSvc.ContextWithToken) (err error) {
|
2022-05-16 13:15:31 +00:00
|
|
|
var reqBody container.SetExtendedACLRequestBody
|
|
|
|
reqBody.SetSignature(new(refs.Signature))
|
|
|
|
|
|
|
|
_, err = e.SetExtendedACL(ctx, &reqBody)
|
2021-12-16 15:26:13 +00:00
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
ctx := containerSvc.ContextWithToken{
|
|
|
|
Context: context.Background(),
|
|
|
|
SessionToken: generateToken(new(session.ObjectSessionContext)),
|
|
|
|
}
|
2022-05-18 15:20:08 +00:00
|
|
|
require.Error(t, test.op(e, ctx))
|
2021-12-16 15:26:13 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
ctx.SessionToken = &tokV2
|
2021-12-16 15:26:13 +00:00
|
|
|
require.NoError(t, test.op(e, ctx))
|
|
|
|
|
|
|
|
ctx.SessionToken = nil
|
|
|
|
require.NoError(t, test.op(e, ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func generateToken(ctx session.TokenContext) *session.Token {
|
|
|
|
body := new(session.TokenBody)
|
2021-12-16 15:26:13 +00:00
|
|
|
body.SetContext(ctx)
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
tok := new(session.Token)
|
2021-12-16 15:26:13 +00:00
|
|
|
tok.SetBody(body)
|
|
|
|
|
|
|
|
return tok
|
|
|
|
}
|