[#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 { type Delete struct {
containerID []byte containerID []byte
signature []byte signature []byte
token []byte
} }
// MorphEvent implements Neo:Morph Event interface. // 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. // Signature of marshalled container by container owner.
func (d Delete) Signature() []byte { return d.signature } 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. // ParseDelete from notification into container event structure.
//
// Expects 3 stack items.
func ParseDelete(params []stackitem.Item) (event.Event, error) { func ParseDelete(params []stackitem.Item) (event.Event, error) {
var ( var (
ev Delete ev Delete
err error err error
) )
if ln := len(params); ln != 2 { const expectedItemNumEACL = 3
return nil, event.WrongNumberOfParameters(2, ln)
if ln := len(params); ln != expectedItemNumEACL {
return nil, event.WrongNumberOfParameters(expectedItemNumEACL, ln)
} }
// parse container // parse container
@ -46,5 +57,11 @@ func ParseDelete(params []stackitem.Item) (event.Event, error) {
return nil, fmt.Errorf("could not get signature: %w", err) 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 return ev, nil
} }

View file

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