[#525] event/container: Parse session token from Delete notification

The 3rd item of `Delete` container notification event is a byte array of
serialized session token.  Parse session token in `ParseDelete` function.
Provide `Delete.SessionToken` method.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-05-26 13:55:58 +03:00 committed by Leonard Lyubich
parent c12ea210da
commit 166e5440ab
2 changed files with 33 additions and 3 deletions

View file

@ -12,6 +12,7 @@ import (
type Delete struct {
containerID []byte
signature []byte
token []byte
}
// MorphEvent implements Neo:Morph Event interface.
@ -23,15 +24,25 @@ func (d Delete) ContainerID() []byte { return d.containerID }
// Signature of marshalled container by container owner.
func (d Delete) Signature() []byte { return d.signature }
// SessionToken returns binary token of the session
// within which the eACL was set.
func (d Delete) SessionToken() []byte {
return d.token
}
// ParseDelete from notification into container event structure.
//
// Expects 3 stack items.
func ParseDelete(params []stackitem.Item) (event.Event, error) {
var (
ev Delete
err error
)
if ln := len(params); ln != 2 {
return nil, event.WrongNumberOfParameters(2, ln)
const expectedItemNumEACL = 3
if ln := len(params); ln != expectedItemNumEACL {
return nil, event.WrongNumberOfParameters(expectedItemNumEACL, ln)
}
// parse container
@ -46,5 +57,11 @@ func ParseDelete(params []stackitem.Item) (event.Event, error) {
return nil, fmt.Errorf("could not get signature: %w", err)
}
// parse session token
ev.token, err = client.BytesFromStackItem(params[2])
if err != nil {
return nil, fmt.Errorf("could not get session token: %w", err)
}
return ev, nil
}

View file

@ -12,6 +12,7 @@ func TestParseDelete(t *testing.T) {
var (
containerID = []byte("containreID")
signature = []byte("signature")
token = []byte("token")
)
t.Run("wrong number of parameters", func(t *testing.T) {
@ -20,7 +21,7 @@ func TestParseDelete(t *testing.T) {
}
_, err := ParseDelete(prms)
require.EqualError(t, err, event.WrongNumberOfParameters(2, len(prms)).Error())
require.EqualError(t, err, event.WrongNumberOfParameters(3, len(prms)).Error())
})
t.Run("wrong container parameter", func(t *testing.T) {
@ -40,10 +41,21 @@ func TestParseDelete(t *testing.T) {
require.Error(t, err)
})
t.Run("wrong session token parameter", func(t *testing.T) {
_, err := ParseDelete([]stackitem.Item{
stackitem.NewByteArray(containerID),
stackitem.NewByteArray(signature),
stackitem.NewMap(),
})
require.Error(t, err)
})
t.Run("correct behavior", func(t *testing.T) {
ev, err := ParseDelete([]stackitem.Item{
stackitem.NewByteArray(containerID),
stackitem.NewByteArray(signature),
stackitem.NewByteArray(token),
})
require.NoError(t, err)
@ -51,6 +63,7 @@ func TestParseDelete(t *testing.T) {
require.Equal(t, Delete{
containerID: containerID,
signature: signature,
token: token,
}, ev)
})
}