Codebase list golang-github-go-kit-kit / c6b9e0f
add more tests around token parsing Brian Kassouf 7 years ago
1 changed file(s) with 39 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
88 )
99
1010 var (
11 key = "test_signing_key"
12 method = jwt.SigningMethodHS256
13 claims = jwt.MapClaims{"user": "go-kit"}
14 signedKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZ28ta2l0In0.MMefQU5pwDeoWBSdyagqNlr1tDGddGUOMGiIWmMlFvk"
11 key = "test_signing_key"
12 method = jwt.SigningMethodHS256
13 invalidMethod = jwt.SigningMethodRS256
14 claims = jwt.MapClaims{"user": "go-kit"}
15 signedKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZ28ta2l0In0.MMefQU5pwDeoWBSdyagqNlr1tDGddGUOMGiIWmMlFvk"
16 invalidKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"
1517 )
1618
1719 func TestSigner(t *testing.T) {
3840 e := func(ctx context.Context, i interface{}) (interface{}, error) { return ctx, nil }
3941
4042 keyfunc := func(token *jwt.Token) (interface{}, error) { return []byte(key), nil }
43 badKeyfunc := func(token *jwt.Token) (interface{}, error) { return []byte("bad"), nil }
4144
4245 parser := NewParser(keyfunc, method)(e)
43 ctx := context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
46
47 // No Token is passed into the parser
48 _, err := parser(context.Background(), struct{}{})
49 if err == nil {
50 t.Error("Parser should have returned an error")
51 }
52
53 // Invalid Token is passed into the parser
54 ctx := context.WithValue(context.Background(), JWTTokenContextKey, invalidKey)
55 _, err = parser(ctx, struct{}{})
56 if err == nil {
57 t.Error("Parser should have returned an error")
58 }
59
60 // Invalid Method is used in the parser
61 badParser := NewParser(keyfunc, invalidMethod)(e)
62 ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
63 _, err = badParser(ctx, struct{}{})
64 if err == nil {
65 t.Error("Parser should have returned an error")
66 }
67
68 // Invalid key is used in the parser
69 badParser = NewParser(badKeyfunc, method)(e)
70 ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
71 _, err = badParser(ctx, struct{}{})
72 if err == nil {
73 t.Error("Parser should have returned an error")
74 }
75
76 // Correct token is passed into the parser
77 ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
4478 ctx1, err := parser(ctx, struct{}{})
4579 if err != nil {
4680 t.Fatalf("Parser returned error: %s", err)