github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/auth/token/jwt/jwt_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/jwt/jwt_test.go
    16  
    17  package jwt
    18  
    19  import (
    20  	"io/ioutil"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/tickoalcantara12/micro/v3/service/auth"
    25  	"github.com/tickoalcantara12/micro/v3/util/auth/token"
    26  )
    27  
    28  func TestGenerate(t *testing.T) {
    29  	privKey, err := ioutil.ReadFile("test/sample_key")
    30  	if err != nil {
    31  		t.Fatalf("Unable to read private key: %v", err)
    32  	}
    33  
    34  	j := NewTokenProvider(
    35  		token.WithPrivateKey(string(privKey)),
    36  	)
    37  
    38  	_, err = j.Generate(&auth.Account{ID: "test"})
    39  	if err != nil {
    40  		t.Fatalf("Generate returned %v error, expected nil", err)
    41  	}
    42  }
    43  
    44  func TestInspect(t *testing.T) {
    45  	pubKey, err := ioutil.ReadFile("test/sample_key.pub")
    46  	if err != nil {
    47  		t.Fatalf("Unable to read public key: %v", err)
    48  	}
    49  	privKey, err := ioutil.ReadFile("test/sample_key")
    50  	if err != nil {
    51  		t.Fatalf("Unable to read private key: %v", err)
    52  	}
    53  
    54  	j := NewTokenProvider(
    55  		token.WithPublicKey(string(pubKey)),
    56  		token.WithPrivateKey(string(privKey)),
    57  	)
    58  
    59  	t.Run("Valid token", func(t *testing.T) {
    60  		md := map[string]string{"foo": "bar"}
    61  		scopes := []string{"admin"}
    62  		subject := "test"
    63  		name := "testname"
    64  
    65  		acc := &auth.Account{ID: subject, Scopes: scopes, Metadata: md, Name: name}
    66  		tok, err := j.Generate(acc)
    67  		if err != nil {
    68  			t.Fatalf("Generate returned %v error, expected nil", err)
    69  		}
    70  
    71  		tok2, err := j.Inspect(tok.Token)
    72  		if err != nil {
    73  			t.Fatalf("Inspect returned %v error, expected nil", err)
    74  		}
    75  		if acc.ID != subject {
    76  			t.Errorf("Inspect returned %v as the token subject, expected %v", acc.ID, subject)
    77  		}
    78  		if len(tok2.Scopes) != len(scopes) {
    79  			t.Errorf("Inspect returned %v scopes, expected %v", len(tok2.Scopes), len(scopes))
    80  		}
    81  		if len(tok2.Metadata) != len(md) {
    82  			t.Errorf("Inspect returned %v as the token metadata, expected %v", tok2.Metadata, md)
    83  		}
    84  		if tok2.Name != name {
    85  			t.Errorf("Inspect returned %v as the token name, expected %v", tok2.Name, name)
    86  		}
    87  	})
    88  
    89  	t.Run("Expired token", func(t *testing.T) {
    90  		tok, err := j.Generate(&auth.Account{}, token.WithExpiry(-10*time.Second))
    91  		if err != nil {
    92  			t.Fatalf("Generate returned %v error, expected nil", err)
    93  		}
    94  
    95  		if _, err = j.Inspect(tok.Token); err != token.ErrInvalidToken {
    96  			t.Fatalf("Inspect returned %v error, expected %v", err, token.ErrInvalidToken)
    97  		}
    98  	})
    99  
   100  	t.Run("Invalid token", func(t *testing.T) {
   101  		_, err := j.Inspect("Invalid token")
   102  		if err != token.ErrInvalidToken {
   103  			t.Fatalf("Inspect returned %v error, expected %v", err, token.ErrInvalidToken)
   104  		}
   105  	})
   106  
   107  	t.Run("Default name", func(t *testing.T) {
   108  		tok, err := j.Generate(&auth.Account{ID: "test"})
   109  		if err != nil {
   110  			t.Fatalf("Generate returned %v error, expected nil", err)
   111  		}
   112  
   113  		tok2, err := j.Inspect(tok.Token)
   114  		if err != nil {
   115  			t.Fatalf("Inspect returned %v error, expected nil", err)
   116  		}
   117  		if tok2.Name != "test" {
   118  			t.Fatalf("Inspect returned %v as the token name, expected test", tok2.Name)
   119  		}
   120  	})
   121  
   122  }