|
| 1 | +package token |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + |
| 11 | + "github.com/shaj13/go-guardian/auth" |
| 12 | + "github.com/shaj13/go-guardian/store" |
| 13 | +) |
| 14 | + |
| 15 | +func TestNewCahced(t *testing.T) { |
| 16 | + table := []struct { |
| 17 | + name string |
| 18 | + panic bool |
| 19 | + expectedErr bool |
| 20 | + cache store.Cache |
| 21 | + authFunc AuthenticateFunc |
| 22 | + info interface{} |
| 23 | + token string |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "it return error when cache load return error", |
| 27 | + expectedErr: true, |
| 28 | + panic: false, |
| 29 | + cache: make(mockCache), |
| 30 | + token: "error", |
| 31 | + authFunc: NoOpAuthenticate, |
| 32 | + info: nil, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "it return error when user authenticate func return error", |
| 36 | + expectedErr: true, |
| 37 | + cache: make(mockCache), |
| 38 | + authFunc: NoOpAuthenticate, |
| 39 | + panic: false, |
| 40 | + info: nil, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "it return error when cache store return error", |
| 44 | + expectedErr: true, |
| 45 | + cache: make(mockCache), |
| 46 | + authFunc: func(_ context.Context, _ *http.Request, _ string) (auth.Info, error) { return nil, nil }, |
| 47 | + token: "store-error", |
| 48 | + panic: false, |
| 49 | + info: nil, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "it return error when cache return invalid type", |
| 53 | + expectedErr: true, |
| 54 | + cache: make(mockCache), |
| 55 | + authFunc: func(_ context.Context, _ *http.Request, _ string) (auth.Info, error) { return nil, nil }, |
| 56 | + panic: false, |
| 57 | + info: "sample-data", |
| 58 | + token: "valid", |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "it return user when token cached", |
| 62 | + expectedErr: false, |
| 63 | + cache: make(mockCache), |
| 64 | + authFunc: NoOpAuthenticate, |
| 65 | + panic: false, |
| 66 | + info: auth.NewDefaultUser("1", "1", nil, nil), |
| 67 | + token: "valid-user", |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "it panic when Authenticate func nil", |
| 71 | + expectedErr: false, |
| 72 | + panic: true, |
| 73 | + info: nil, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "it panic when Cache nil", |
| 77 | + expectedErr: false, |
| 78 | + authFunc: NoOpAuthenticate, |
| 79 | + panic: true, |
| 80 | + info: nil, |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + for _, tt := range table { |
| 85 | + t.Run(tt.name, func(t *testing.T) { |
| 86 | + if tt.panic { |
| 87 | + assert.Panics(t, func() { |
| 88 | + New(tt.authFunc, tt.cache) |
| 89 | + }) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + strategy := New(tt.authFunc, tt.cache) |
| 94 | + r, _ := http.NewRequest("GET", "/", nil) |
| 95 | + r.Header.Set("Authorization", "Bearer "+tt.token) |
| 96 | + _ = tt.cache.Store(tt.token, tt.info, r) |
| 97 | + info, err := strategy.Authenticate(r.Context(), r) |
| 98 | + if tt.expectedErr { |
| 99 | + assert.Error(t, err) |
| 100 | + return |
| 101 | + } |
| 102 | + assert.Equal(t, tt.info, info) |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestCahcedTokenAppend(t *testing.T) { |
| 108 | + cache := make(mockCache) |
| 109 | + strategy := &cachedToken{cache: cache} |
| 110 | + info := auth.NewDefaultUser("1", "2", nil, nil) |
| 111 | + strategy.Append("test-append", info, nil) |
| 112 | + cachedInfo, ok, _ := cache.Load("test-append", nil) |
| 113 | + assert.True(t, ok) |
| 114 | + assert.Equal(t, info, cachedInfo) |
| 115 | +} |
| 116 | + |
| 117 | +func TestCahcedTokenChallenge(t *testing.T) { |
| 118 | + strategy := &cachedToken{ |
| 119 | + typ: Bearer, |
| 120 | + } |
| 121 | + |
| 122 | + got := strategy.Challenge("Test Realm") |
| 123 | + expected := `Bearer realm="Test Realm", title="Bearer Token Based Authentication Scheme"` |
| 124 | + |
| 125 | + assert.Equal(t, expected, got) |
| 126 | +} |
| 127 | + |
| 128 | +func BenchmarkCachedToken(b *testing.B) { |
| 129 | + r, _ := http.NewRequest("GET", "/", nil) |
| 130 | + r.Header.Set("Authorization", "Bearer token") |
| 131 | + |
| 132 | + cache := make(mockCache) |
| 133 | + cache.Store("token", auth.NewDefaultUser("benchmark", "1", nil, nil), r) |
| 134 | + |
| 135 | + strategy := New(NoOpAuthenticate, cache) |
| 136 | + |
| 137 | + b.ResetTimer() |
| 138 | + b.RunParallel(func(pb *testing.PB) { |
| 139 | + for pb.Next() { |
| 140 | + _, err := strategy.Authenticate(r.Context(), r) |
| 141 | + if err != nil { |
| 142 | + b.Error(err) |
| 143 | + } |
| 144 | + } |
| 145 | + }) |
| 146 | +} |
| 147 | + |
| 148 | +type mockCache map[string]interface{} |
| 149 | + |
| 150 | +func (m mockCache) Load(key string, _ *http.Request) (interface{}, bool, error) { |
| 151 | + if key == "error" { |
| 152 | + return nil, false, fmt.Errorf("Load Error") |
| 153 | + } |
| 154 | + v, ok := m[key] |
| 155 | + return v, ok, nil |
| 156 | +} |
| 157 | + |
| 158 | +func (m mockCache) Store(key string, value interface{}, _ *http.Request) error { |
| 159 | + if key == "store-error" { |
| 160 | + return fmt.Errorf("Store Error") |
| 161 | + } |
| 162 | + m[key] = value |
| 163 | + return nil |
| 164 | +} |
| 165 | +func (m mockCache) Keys() []string { return nil } |
| 166 | + |
| 167 | +func (m mockCache) Delete(key string, _ *http.Request) error { |
| 168 | + return nil |
| 169 | +} |
0 commit comments