From 4b6811e0cfdb673f66179d5797468886a9a422e5 Mon Sep 17 00:00:00 2001 From: Przemek Komosa Date: Sat, 2 Jul 2016 00:07:40 +0200 Subject: [PATCH] Refactor JWT tests to table based Now state isn't shared. --- middleware/jwt_test.go | 104 ++++++++++++++++++++++++++++------------- 1 file changed, 71 insertions(+), 33 deletions(-) diff --git a/middleware/jwt_test.go b/middleware/jwt_test.go index e35e6d3e..07eeaa31 100644 --- a/middleware/jwt_test.go +++ b/middleware/jwt_test.go @@ -12,46 +12,84 @@ import ( func TestJWT(t *testing.T) { e := echo.New() - req := test.NewRequest(echo.GET, "/", nil) - res := test.NewResponseRecorder() - c := e.NewContext(req, res) handler := func(c echo.Context) error { return c.String(http.StatusOK, "test") } - config := JWTConfig{} token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ" + validKey := []byte("secret") + invalidKey := []byte("invalid-key") + validAuth := bearer + " " + token - // No signing key provided - assert.Panics(t, func() { - JWTWithConfig(config) - }) + for _, tc := range []struct { + expPanic bool + expErrCode int // 0 for Success + config JWTConfig + reqURL string // "/" if empty + hdrAuth string + info string + }{ + {expPanic: true, info: "No signing key provided"}, + { + expErrCode: http.StatusBadRequest, + config: JWTConfig{ + SigningKey: validKey, + SigningMethod: "RS256", + }, + info: "Unexpected signing method", + }, + { + expErrCode: http.StatusUnauthorized, + hdrAuth: validAuth, + config: JWTConfig{SigningKey: invalidKey}, + info: "Invalid key", + }, + { + hdrAuth: validAuth, + config: JWTConfig{SigningKey: validKey}, + info: "Valid JWT", + }, + { + hdrAuth: "invalid-auth", + expErrCode: http.StatusBadRequest, + config: JWTConfig{SigningKey: validKey}, + info: "Invalid Authorization header", + }, + { + config: JWTConfig{SigningKey: validKey}, + hdrAuth: "", + expErrCode: http.StatusBadRequest, + info: "Empty header auth field", + }, + } { - // Unexpected signing method - config.SigningKey = []byte("secret") - config.SigningMethod = "RS256" - h := JWTWithConfig(config)(handler) - he := h(c).(*echo.HTTPError) - assert.Equal(t, http.StatusBadRequest, he.Code) + if tc.reqURL == "" { + tc.reqURL = "/" + } - // Invalid key - auth := bearer + " " + token - req.Header().Set(echo.HeaderAuthorization, auth) - config.SigningKey = []byte("invalid-key") - h = JWTWithConfig(config)(handler) - he = h(c).(*echo.HTTPError) - assert.Equal(t, http.StatusUnauthorized, he.Code) + req := test.NewRequest(echo.GET, tc.reqURL, nil) + res := test.NewResponseRecorder() + req.Header().Set(echo.HeaderAuthorization, tc.hdrAuth) + c := e.NewContext(req, res) - // Valid JWT - h = JWT([]byte("secret"))(handler) - if assert.NoError(t, h(c)) { - user := c.Get("user").(*jwt.Token) - claims := user.Claims.(jwt.MapClaims) - assert.Equal(t, claims["name"], "John Doe") + if tc.expPanic { + assert.Panics(t, func() { + JWTWithConfig(tc.config) + }, tc.info) + continue + } + + if tc.expErrCode != 0 { + h := JWTWithConfig(tc.config)(handler) + he := h(c).(*echo.HTTPError) + assert.Equal(t, tc.expErrCode, he.Code, tc.info) + continue + } + + h := JWTWithConfig(tc.config)(handler) + if assert.NoError(t, h(c), tc.info) { + user := c.Get("user").(*jwt.Token) + claims := user.Claims.(jwt.MapClaims) + assert.Equal(t, claims["name"], "John Doe", tc.info) + } } - - // Invalid Authorization header - req.Header().Set(echo.HeaderAuthorization, "invalid-auth") - h = JWT([]byte("secret"))(handler) - he = h(c).(*echo.HTTPError) - assert.Equal(t, http.StatusBadRequest, he.Code) }