github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/conf/jwt/zz_jwt_test.go (about) 1 package jwt_test 2 3 import ( 4 "context" 5 "reflect" 6 "runtime" 7 "testing" 8 "time" 9 10 "github.com/agiledragon/gomonkey/v2" 11 . "github.com/onsi/gomega" 12 "github.com/pkg/errors" 13 14 "github.com/machinefi/w3bstream/pkg/depends/base/types" 15 . "github.com/machinefi/w3bstream/pkg/depends/conf/jwt" 16 "github.com/machinefi/w3bstream/pkg/depends/kit/statusx" 17 ) 18 19 func TestJwt(t *testing.T) { 20 conf := &Jwt{ 21 Issuer: "jwt_unit_test", 22 ExpIn: types.Duration(time.Second), 23 SignKey: "any", 24 } 25 26 t.Run("#JwtConf", func(t *testing.T) { 27 t.Run("#Init", func(t *testing.T) { 28 c := *conf 29 c.SignKey = "" 30 c.Init() 31 NewWithT(t).Expect(c.SignKey).To(Equal("xxxx")) 32 33 c.ExpIn = 0 34 c.Init() 35 NewWithT(t).Expect(c.ExpIn).To(Equal(types.Duration(time.Hour))) 36 }) 37 38 t.Run("#ExpiresAt", func(t *testing.T) { 39 c := *conf 40 NewWithT(t).Expect(c.ExpiresAt()).NotTo(BeNil()) 41 42 c.ExpIn = 0 43 NewWithT(t).Expect(c.ExpiresAt()).To(BeNil()) 44 }) 45 46 t.Run("#GeneratingAndParsing", func(t *testing.T) { 47 conf.ExpIn = types.Duration(time.Second * 2) 48 payload := interface{}("any") 49 tok, err := conf.GenerateTokenByPayload(payload) 50 NewWithT(t).Expect(err).To(BeNil()) 51 NewWithT(t).Expect(tok).NotTo(BeEmpty()) 52 53 t.Run("#Success", func(t *testing.T) { 54 claim, err := conf.ParseToken(tok) 55 NewWithT(t).Expect(err).To(BeNil()) 56 57 NewWithT(t).Expect(claim.Payload).To(Equal(payload)) 58 }) 59 60 t.Run("#Failed", func(t *testing.T) { 61 t.Run("#TokenExpired", func(t *testing.T) { 62 time.Sleep(2 * time.Second) 63 64 _, err = conf.ParseToken(tok) 65 NewWithT(t).Expect(err).NotTo(BeNil()) 66 67 ve, ok := err.(*statusx.StatusErr) 68 NewWithT(t).Expect(ok).To(BeTrue()) 69 NewWithT(t).Expect(ve.Key).To(Equal(InvalidToken.Key())) 70 }) 71 72 t.Run("#ParseWithClaimFailed", func(t *testing.T) { 73 _, err = conf.ParseToken("not equal token gen before") 74 NewWithT(t).Expect(err).NotTo(BeNil()) 75 }) 76 }) 77 }) 78 }) 79 80 auth := &Auth{ 81 AuthInQuery: "", 82 AuthInHeader: "", 83 } 84 ctx := WithConfContext(conf)(context.Background()) 85 86 fromCtx := MustConfFromContext(ctx) 87 NewWithT(t).Expect(fromCtx).To(Equal(conf)) 88 89 fromCtx, _ = ConfFromContext(ctx) 90 NewWithT(t).Expect(fromCtx).To(Equal(conf)) 91 92 t.Run("#AuthMiddleware", func(t *testing.T) { 93 payload := interface{}("any") 94 t.Run("#Output", func(t *testing.T) { 95 tok, _ := conf.GenerateTokenWithoutExpByPayload(payload) 96 t.Run("#InQuery", func(t *testing.T) { 97 auth.AuthInHeader = "" 98 auth.AuthInQuery = tok 99 pl, err := auth.Output(ctx) 100 NewWithT(t).Expect(err).To(BeNil()) 101 NewWithT(t).Expect(pl).To(Equal(payload)) 102 }) 103 t.Run("#InHeader", func(t *testing.T) { 104 auth.AuthInHeader = tok 105 auth.AuthInQuery = "" 106 pl, err := auth.Output(ctx) 107 NewWithT(t).Expect(err).To(BeNil()) 108 NewWithT(t).Expect(pl).To(Equal(payload)) 109 }) 110 111 t.Run("#WithBuiltinValidator", func(t *testing.T) { 112 anyError := errors.New("any") 113 cases := []*struct { 114 name string 115 builtin func(_ context.Context, _ string) (interface{}, error, bool) 116 expectPayload interface{} 117 expectError error 118 }{ 119 { 120 name: "$WithoutBuiltinFn", 121 builtin: nil, 122 expectPayload: payload, 123 }, 124 { 125 name: "$CannotBeValidated", 126 builtin: func(_ context.Context, _ string) (interface{}, error, bool) { 127 return nil, nil, false 128 }, 129 expectPayload: payload, 130 }, 131 { 132 name: "$ValidatorReturnsError", 133 builtin: func(_ context.Context, _ string) (interface{}, error, bool) { 134 return nil, anyError, true 135 }, 136 expectError: anyError, 137 }, 138 { 139 name: "$ValidatorSuccessValidating", 140 builtin: func(_ context.Context, _ string) (interface{}, error, bool) { 141 return payload.(string), nil, true 142 }, 143 expectPayload: payload, 144 }, 145 } 146 for _, c := range cases { 147 t.Run(c.name, func(t *testing.T) { 148 SetBuiltInTokenFn(c.builtin) 149 150 pl, err := auth.Output(ctx) 151 152 if c.expectError != nil { 153 NewWithT(t).Expect(err).To(Equal(c.expectError)) 154 } else { 155 NewWithT(t).Expect(err).To(BeNil()) 156 NewWithT(t).Expect(pl).To(Equal(c.expectPayload)) 157 } 158 }) 159 } 160 SetBuiltInTokenFn(nil) 161 }) 162 163 t.Run("#ParseTokenFailed", func(t *testing.T) { 164 SetBuiltInTokenFn(nil) 165 if runtime.GOOS == `darwin` { 166 return 167 } 168 patch := gomonkey.ApplyMethod( 169 reflect.TypeOf(&Jwt{}), 170 "ParseToken", 171 func(*Jwt, string) (*Claims, error) { 172 return nil, errors.New("any") 173 }, 174 ) 175 defer patch.Reset() 176 _, err := auth.Output(ctx) 177 NewWithT(t).Expect(err).NotTo(BeNil()) 178 }) 179 180 t.Run("#WithPermissionFn", func(t *testing.T) { 181 cases := []*struct { 182 name string 183 fn func(interface{}) bool 184 expectError error 185 expectPayload interface{} 186 }{ 187 { 188 name: "$WithoutPermissionFn", 189 fn: nil, 190 expectPayload: payload, 191 }, 192 { 193 name: "$PermissionOK", 194 fn: func(interface{}) bool { return true }, 195 expectPayload: payload, 196 }, 197 { 198 name: "$PermissionDenied", 199 fn: func(interface{}) bool { return false }, 200 expectError: ErrNoPermission, 201 }, 202 } 203 204 for _, c := range cases { 205 t.Run(c.name, func(t *testing.T) { 206 SetWithPermissionFn(c.fn) 207 208 pl, err := auth.Output(ctx) 209 210 if c.expectError != nil { 211 NewWithT(t).Expect(err).To(Equal(c.expectError)) 212 } else { 213 NewWithT(t).Expect(err).To(BeNil()) 214 NewWithT(t).Expect(pl).To(Equal(c.expectPayload)) 215 } 216 }) 217 } 218 SetWithPermissionFn(nil) 219 }) 220 }) 221 }) 222 }