add more tests around token parsing
Brian Kassouf
7 years ago
8 | 8 | ) |
9 | 9 | |
10 | 10 | 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" | |
15 | 17 | ) |
16 | 18 | |
17 | 19 | func TestSigner(t *testing.T) { |
38 | 40 | e := func(ctx context.Context, i interface{}) (interface{}, error) { return ctx, nil } |
39 | 41 | |
40 | 42 | keyfunc := func(token *jwt.Token) (interface{}, error) { return []byte(key), nil } |
43 | badKeyfunc := func(token *jwt.Token) (interface{}, error) { return []byte("bad"), nil } | |
41 | 44 | |
42 | 45 | 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) | |
44 | 78 | ctx1, err := parser(ctx, struct{}{}) |
45 | 79 | if err != nil { |
46 | 80 | t.Fatalf("Parser returned error: %s", err) |