Fix token tests

This commit is contained in:
Ben Toogood 2020-04-01 17:29:17 +01:00
parent 623f0c0c90
commit 9de69529ce
2 changed files with 12 additions and 31 deletions

View File

@ -2,8 +2,8 @@ package basic
import ( import (
"testing" "testing"
"time"
"github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/auth/token" "github.com/micro/go-micro/v2/auth/token"
"github.com/micro/go-micro/v2/store/memory" "github.com/micro/go-micro/v2/store/memory"
) )
@ -12,7 +12,7 @@ func TestGenerate(t *testing.T) {
store := memory.NewStore() store := memory.NewStore()
b := NewTokenProvider(token.WithStore(store)) b := NewTokenProvider(token.WithStore(store))
_, err := b.Generate("test") _, err := b.Generate(&auth.Account{ID: "test"})
if err != nil { if err != nil {
t.Fatalf("Generate returned %v error, expected nil", err) t.Fatalf("Generate returned %v error, expected nil", err)
} }
@ -35,12 +35,7 @@ func TestInspect(t *testing.T) {
roles := []string{"admin"} roles := []string{"admin"}
subject := "test" subject := "test"
opts := []token.GenerateOption{ tok, err := b.Generate(&auth.Account{ID: subject, Roles: roles, Metadata: md})
token.WithMetadata(md),
token.WithRoles(roles...),
}
tok, err := b.Generate(subject, opts...)
if err != nil { if err != nil {
t.Fatalf("Generate returned %v error, expected nil", err) t.Fatalf("Generate returned %v error, expected nil", err)
} }
@ -49,8 +44,8 @@ func TestInspect(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Inspect returned %v error, expected nil", err) t.Fatalf("Inspect returned %v error, expected nil", err)
} }
if tok2.Subject != subject { if tok2.ID != subject {
t.Errorf("Inspect returned %v as the token subject, expected %v", tok2.Subject, subject) t.Errorf("Inspect returned %v as the token subject, expected %v", tok2.ID, subject)
} }
if len(tok2.Roles) != len(roles) { if len(tok2.Roles) != len(roles) {
t.Errorf("Inspect returned %v roles, expected %v", len(tok2.Roles), len(roles)) t.Errorf("Inspect returned %v roles, expected %v", len(tok2.Roles), len(roles))
@ -60,17 +55,6 @@ func TestInspect(t *testing.T) {
} }
}) })
t.Run("Expired token", func(t *testing.T) {
tok, err := b.Generate("foo", token.WithExpiry(-10*time.Second))
if err != nil {
t.Fatalf("Generate returned %v error, expected nil", err)
}
if _, err = b.Inspect(tok.Token); err != token.ErrInvalidToken {
t.Fatalf("Inspect returned %v error, expected %v", err, token.ErrInvalidToken)
}
})
t.Run("Invalid token", func(t *testing.T) { t.Run("Invalid token", func(t *testing.T) {
_, err := b.Inspect("Invalid token") _, err := b.Inspect("Invalid token")
if err != token.ErrInvalidToken { if err != token.ErrInvalidToken {

View File

@ -5,6 +5,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/auth/token" "github.com/micro/go-micro/v2/auth/token"
) )
@ -18,7 +19,7 @@ func TestGenerate(t *testing.T) {
token.WithPrivateKey(string(privKey)), token.WithPrivateKey(string(privKey)),
) )
_, err = j.Generate("test") _, err = j.Generate(&auth.Account{ID: "test"})
if err != nil { if err != nil {
t.Fatalf("Generate returned %v error, expected nil", err) t.Fatalf("Generate returned %v error, expected nil", err)
} }
@ -44,12 +45,8 @@ func TestInspect(t *testing.T) {
roles := []string{"admin"} roles := []string{"admin"}
subject := "test" subject := "test"
opts := []token.GenerateOption{ acc := &auth.Account{ID: subject, Roles: roles, Metadata: md}
token.WithMetadata(md), tok, err := j.Generate(acc)
token.WithRoles(roles...),
}
tok, err := j.Generate(subject, opts...)
if err != nil { if err != nil {
t.Fatalf("Generate returned %v error, expected nil", err) t.Fatalf("Generate returned %v error, expected nil", err)
} }
@ -58,8 +55,8 @@ func TestInspect(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Inspect returned %v error, expected nil", err) t.Fatalf("Inspect returned %v error, expected nil", err)
} }
if tok2.Subject != subject { if acc.ID != subject {
t.Errorf("Inspect returned %v as the token subject, expected %v", tok2.Subject, subject) t.Errorf("Inspect returned %v as the token subject, expected %v", acc.ID, subject)
} }
if len(tok2.Roles) != len(roles) { if len(tok2.Roles) != len(roles) {
t.Errorf("Inspect returned %v roles, expected %v", len(tok2.Roles), len(roles)) t.Errorf("Inspect returned %v roles, expected %v", len(tok2.Roles), len(roles))
@ -70,7 +67,7 @@ func TestInspect(t *testing.T) {
}) })
t.Run("Expired token", func(t *testing.T) { t.Run("Expired token", func(t *testing.T) {
tok, err := j.Generate("foo", token.WithExpiry(-10*time.Second)) tok, err := j.Generate(&auth.Account{}, token.WithExpiry(-10*time.Second))
if err != nil { if err != nil {
t.Fatalf("Generate returned %v error, expected nil", err) t.Fatalf("Generate returned %v error, expected nil", err)
} }