github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/auth/token/basic/basic_test.go (about) 1 // Copyright 2020 Asim Aslam 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Original source: github.com/micro/go-micro/v3/util/token/basic/basic_test.go 16 17 package basic 18 19 import ( 20 "testing" 21 22 "github.com/tickoalcantara12/micro/v3/service/auth" 23 "github.com/tickoalcantara12/micro/v3/service/store/memory" 24 "github.com/tickoalcantara12/micro/v3/util/auth/token" 25 ) 26 27 func TestGenerate(t *testing.T) { 28 store := memory.NewStore() 29 b := NewTokenProvider(token.WithStore(store)) 30 31 _, err := b.Generate(&auth.Account{ID: "test"}) 32 if err != nil { 33 t.Fatalf("Generate returned %v error, expected nil", err) 34 } 35 36 recs, err := store.List() 37 if err != nil { 38 t.Fatalf("Unable to read from store: %v", err) 39 } 40 if len(recs) != 1 { 41 t.Errorf("Generate didn't write to the store, expected 1 record, got %v", len(recs)) 42 } 43 } 44 45 func TestInspect(t *testing.T) { 46 store := memory.NewStore() 47 b := NewTokenProvider(token.WithStore(store)) 48 49 t.Run("Valid token", func(t *testing.T) { 50 md := map[string]string{"foo": "bar"} 51 scopes := []string{"admin"} 52 subject := "test" 53 54 tok, err := b.Generate(&auth.Account{ID: subject, Scopes: scopes, Metadata: md}) 55 if err != nil { 56 t.Fatalf("Generate returned %v error, expected nil", err) 57 } 58 59 tok2, err := b.Inspect(tok.Token) 60 if err != nil { 61 t.Fatalf("Inspect returned %v error, expected nil", err) 62 } 63 if tok2.ID != subject { 64 t.Errorf("Inspect returned %v as the token subject, expected %v", tok2.ID, subject) 65 } 66 if len(tok2.Scopes) != len(scopes) { 67 t.Errorf("Inspect returned %v scopes, expected %v", len(tok2.Scopes), len(scopes)) 68 } 69 if len(tok2.Metadata) != len(md) { 70 t.Errorf("Inspect returned %v as the token metadata, expected %v", tok2.Metadata, md) 71 } 72 }) 73 74 t.Run("Invalid token", func(t *testing.T) { 75 _, err := b.Inspect("Invalid token") 76 if err != token.ErrInvalidToken { 77 t.Fatalf("Inspect returned %v error, expected %v", err, token.ErrInvalidToken) 78 } 79 }) 80 }