github.com/greenpau/go-authcrunch@v1.0.50/pkg/authz/cache/cache_test.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 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 // http://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 package cache 16 17 import ( 18 "fmt" 19 "github.com/greenpau/go-authcrunch/internal/tests" 20 "github.com/greenpau/go-authcrunch/internal/testutils" 21 "github.com/greenpau/go-authcrunch/pkg/errors" 22 "testing" 23 "time" 24 ) 25 26 func TestTokenCache(t *testing.T) { 27 c := NewTokenCache(100) 28 d := NewTokenCache(0) 29 30 testcases := []struct { 31 name string 32 delay int 33 deletedByManager bool 34 emptyUser bool 35 emptyToken bool 36 emptyCache bool 37 emptyCacheEntries bool 38 err error 39 shouldErr bool 40 }{ 41 { 42 name: "valid token", 43 }, 44 { 45 name: "get expired token", 46 delay: -900, 47 shouldErr: true, 48 err: fmt.Errorf("token expired"), 49 }, 50 { 51 name: "expired token deleted by cache manager", 52 deletedByManager: true, 53 shouldErr: true, 54 err: fmt.Errorf("no user found"), 55 }, 56 { 57 name: "nil user", 58 emptyUser: true, 59 shouldErr: true, 60 err: errors.ErrCacheNilUser, 61 }, 62 { 63 name: "empty token", 64 emptyToken: true, 65 shouldErr: true, 66 err: errors.ErrCacheEmptyToken, 67 }, 68 { 69 name: "cache entries is nil", 70 emptyCacheEntries: true, 71 }, 72 } 73 74 for _, tc := range testcases { 75 t.Run(tc.name, func(t *testing.T) { 76 var msgs []string 77 msgs = append(msgs, fmt.Sprintf("test name: %s", tc.name)) 78 usr := testutils.NewTestUser() 79 ks := testutils.NewTestCryptoKeyStore() 80 err := ks.SignToken("access_token", "HS512", usr) 81 if tc.emptyToken { 82 usr.Token = "" 83 } 84 if tc.emptyUser { 85 usr = nil 86 } 87 err = c.Add(usr) 88 d.Add(usr) 89 if tc.delay == 0 && !tc.deletedByManager { 90 if tests.EvalErrWithLog(t, err, "signed token", tc.shouldErr, tc.err, msgs) { 91 return 92 } 93 } 94 95 if tc.deletedByManager { 96 usr.Claims.ExpiresAt = time.Now().Add(time.Duration(-1000) * time.Second).Unix() 97 } 98 99 if tc.emptyCacheEntries { 100 c.Entries = nil 101 } 102 103 time.Sleep(time.Millisecond * time.Duration(200)) 104 105 if tc.emptyCacheEntries { 106 return 107 } 108 109 switch { 110 case tc.delay < 0: 111 usr.Claims.ExpiresAt = time.Now().Add(time.Duration(tc.delay) * time.Second).Unix() 112 if c.Get(usr.Token) == nil { 113 err = fmt.Errorf("token expired") 114 } 115 case tc.deletedByManager: 116 if c.Get(usr.Token) == nil { 117 err = fmt.Errorf("no user found") 118 } 119 default: 120 if c.Get(usr.Token) == nil { 121 err = fmt.Errorf("token expired") 122 } 123 } 124 125 if c.Get("foobar") != nil { 126 err = fmt.Errorf("got user for invalid token") 127 } 128 129 if tests.EvalErrWithLog(t, err, "cache", tc.shouldErr, tc.err, msgs) { 130 return 131 } 132 }) 133 } 134 }