Files
services/streams/handler/token_test.go
Dominic Wong a711e10961 fix topic validation (#74)
* fix topic validation

* fix tests
2021-03-19 13:29:53 +00:00

39 lines
1.1 KiB
Go

package handler_test
import (
"context"
"testing"
"github.com/micro/micro/v3/service/auth"
"github.com/micro/services/streams/handler"
pb "github.com/micro/services/streams/proto"
"github.com/stretchr/testify/assert"
)
func TestToken(t *testing.T) {
h := testHandler(t)
t.Run("WithoutTopic", func(t *testing.T) {
var rsp pb.TokenResponse
ctx := auth.ContextWithAccount(context.TODO(), &auth.Account{Issuer: "foo"})
err := h.Token(ctx, &pb.TokenRequest{}, &rsp)
assert.NoError(t, err)
assert.NotEmpty(t, rsp.Token)
})
t.Run("WithTopic", func(t *testing.T) {
var rsp pb.TokenResponse
ctx := auth.ContextWithAccount(context.TODO(), &auth.Account{Issuer: "foo"})
err := h.Token(ctx, &pb.TokenRequest{Topic: "helloworld"}, &rsp)
assert.NoError(t, err)
assert.NotEmpty(t, rsp.Token)
})
t.Run("WithBadTopic", func(t *testing.T) {
var rsp pb.TokenResponse
ctx := auth.ContextWithAccount(context.TODO(), &auth.Account{Issuer: "foo"})
err := h.Token(ctx, &pb.TokenRequest{Topic: "helloworld/1"}, &rsp)
assert.Equal(t, handler.ErrInvalidTopic, err)
})
}