From 00778cc9bae7f7a7e512f9de20ecea8fedc7f3ce Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 8 Jun 2021 14:39:17 +0300 Subject: [PATCH] [#293] pkg/token: Implement and use generator of BearerToken Signed-off-by: Leonard Lyubich --- pkg/token/bearer_test.go | 4 ++-- pkg/token/test/generate.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 pkg/token/test/generate.go diff --git a/pkg/token/bearer_test.go b/pkg/token/bearer_test.go index afcde5a..38bf47b 100644 --- a/pkg/token/bearer_test.go +++ b/pkg/token/bearer_test.go @@ -6,6 +6,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl" "github.com/nspcc-dev/neofs-api-go/pkg/owner" "github.com/nspcc-dev/neofs-api-go/pkg/token" + tokentest "github.com/nspcc-dev/neofs-api-go/pkg/token/test" "github.com/nspcc-dev/neofs-crypto/test" "github.com/stretchr/testify/require" ) @@ -32,8 +33,7 @@ func TestBearerToken_Issuer(t *testing.T) { } func TestFilterEncoding(t *testing.T) { - f := token.NewBearerToken() - f.SetLifetime(1, 2, 3) + f := tokentest.Generate() t.Run("binary", func(t *testing.T) { data, err := f.Marshal() diff --git a/pkg/token/test/generate.go b/pkg/token/test/generate.go new file mode 100644 index 0000000..f7ae59d --- /dev/null +++ b/pkg/token/test/generate.go @@ -0,0 +1,35 @@ +package tokentest + +import ( + eacltest "github.com/nspcc-dev/neofs-api-go/pkg/acl/eacl/test" + ownertest "github.com/nspcc-dev/neofs-api-go/pkg/owner/test" + "github.com/nspcc-dev/neofs-api-go/pkg/token" + "github.com/nspcc-dev/neofs-crypto/test" +) + +// Generate returns random token.BearerToken. +// +// Resulting token is unsigned. +func Generate() *token.BearerToken { + x := token.NewBearerToken() + + x.SetLifetime(3, 2, 1) + x.SetOwner(ownertest.Generate()) + x.SetEACLTable(eacltest.Table()) + + return x +} + +// GenerateSigned returns signed random token.BearerToken. +// +// Panics if token could not be signed (actually unexpected). +func GenerateSigned() *token.BearerToken { + tok := Generate() + + err := tok.SignToken(test.DecodeKey(0)) + if err != nil { + panic(err) + } + + return tok +}