2021-01-25 14:26:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func makeTestCookie(value []byte) *fasthttp.RequestHeader {
|
|
|
|
header := new(fasthttp.RequestHeader)
|
2021-02-16 15:20:15 +00:00
|
|
|
header.SetCookie(bearerTokenHdr, string(value))
|
2021-01-25 14:26:08 +00:00
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeTestHeader(value []byte) *fasthttp.RequestHeader {
|
|
|
|
header := new(fasthttp.RequestHeader)
|
|
|
|
if value != nil {
|
2021-02-16 15:20:15 +00:00
|
|
|
header.Set(fasthttp.HeaderAuthorization, bearerTokenHdr+" "+string(value))
|
2021-01-25 14:26:08 +00:00
|
|
|
}
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_fromCookie(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
actual []byte
|
|
|
|
expect []byte
|
|
|
|
}{
|
|
|
|
{name: "empty"},
|
|
|
|
{name: "normal", actual: []byte("TOKEN"), expect: []byte("TOKEN")},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range cases {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
require.Equal(t, tt.expect, fromCookie(makeTestCookie(tt.actual)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_fromHeader(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
actual []byte
|
|
|
|
expect []byte
|
|
|
|
}{
|
|
|
|
{name: "empty"},
|
|
|
|
{name: "normal", actual: []byte("TOKEN"), expect: []byte("TOKEN")},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range cases {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
require.Equal(t, tt.expect, fromHeader(makeTestHeader(tt.actual)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 15:20:15 +00:00
|
|
|
func Test_fetchBearerToken(t *testing.T) {
|
2021-01-25 14:26:08 +00:00
|
|
|
uid := owner.NewID()
|
|
|
|
|
|
|
|
tkn := new(token.BearerToken)
|
|
|
|
tkn.SetOwner(uid)
|
|
|
|
|
|
|
|
data, err := tkn.Marshal()
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t64 := base64.StdEncoding.EncodeToString(data)
|
|
|
|
require.NotEmpty(t, t64)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
cookie string
|
|
|
|
header string
|
|
|
|
|
|
|
|
error string
|
|
|
|
expect *token.BearerToken
|
|
|
|
}{
|
|
|
|
{name: "empty"},
|
|
|
|
|
|
|
|
{name: "bad base64 header", header: "WRONG BASE64", error: "could not fetch marshaled from base64"},
|
|
|
|
{name: "bad base64 cookie", cookie: "WRONG BASE64", error: "could not fetch marshaled from base64"},
|
|
|
|
|
|
|
|
{name: "header token unmarshal error", header: "dGVzdAo=", error: "could not unmarshal bearer token"},
|
|
|
|
{name: "cookie token unmarshal error", cookie: "dGVzdAo=", error: "could not unmarshal bearer token"},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "bad header and cookie",
|
|
|
|
header: "WRONG BASE64",
|
|
|
|
cookie: "dGVzdAo=",
|
|
|
|
error: "could not unmarshal bearer token",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "bad header, but good cookie",
|
|
|
|
header: "dGVzdAo=",
|
|
|
|
cookie: t64,
|
|
|
|
expect: tkn,
|
|
|
|
},
|
|
|
|
|
|
|
|
{name: "ok for header", header: t64, expect: tkn},
|
|
|
|
{name: "ok for cookie", cookie: t64, expect: tkn},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range cases {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := makeTestRequest(tt.cookie, tt.header)
|
2021-02-16 15:20:15 +00:00
|
|
|
actual, err := fetchBearerToken(ctx)
|
2021-01-25 14:26:08 +00:00
|
|
|
|
|
|
|
if tt.error == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tt.expect, actual)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Contains(t, err.Error(), tt.error)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeTestRequest(cookie, header string) *fasthttp.RequestCtx {
|
|
|
|
ctx := new(fasthttp.RequestCtx)
|
|
|
|
|
|
|
|
if cookie != "" {
|
2021-02-16 15:20:15 +00:00
|
|
|
ctx.Request.Header.SetCookie(bearerTokenHdr, cookie)
|
2021-01-25 14:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if header != "" {
|
2021-02-16 15:20:15 +00:00
|
|
|
ctx.Request.Header.Set(fasthttp.HeaderAuthorization, bearerTokenHdr+" "+header)
|
2021-01-25 14:26:08 +00:00
|
|
|
}
|
|
|
|
return ctx
|
|
|
|
}
|
2021-02-16 15:20:15 +00:00
|
|
|
|
|
|
|
func Test_checkAndPropagateBearerToken(t *testing.T) {
|
|
|
|
uid := owner.NewID()
|
|
|
|
|
|
|
|
tkn := new(token.BearerToken)
|
|
|
|
tkn.SetOwner(uid)
|
|
|
|
|
|
|
|
data, err := tkn.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t64 := base64.StdEncoding.EncodeToString(data)
|
|
|
|
require.NotEmpty(t, t64)
|
|
|
|
|
|
|
|
ctx := makeTestRequest(t64, "")
|
|
|
|
|
|
|
|
// Expect to see the token within the context.
|
2021-03-30 22:46:33 +00:00
|
|
|
require.NoError(t, storeBearerToken(ctx))
|
2021-02-16 15:20:15 +00:00
|
|
|
|
|
|
|
// Expect to see the same token without errors.
|
2021-03-30 22:46:33 +00:00
|
|
|
actual, err := loadBearerToken(ctx)
|
2021-02-16 15:20:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tkn, actual)
|
|
|
|
}
|