mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
Refactor JWT tests to table based
Now state isn't shared.
This commit is contained in:
parent
c00d017178
commit
4b6811e0cf
@ -12,46 +12,84 @@ import (
|
|||||||
|
|
||||||
func TestJWT(t *testing.T) {
|
func TestJWT(t *testing.T) {
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
req := test.NewRequest(echo.GET, "/", nil)
|
|
||||||
res := test.NewResponseRecorder()
|
|
||||||
c := e.NewContext(req, res)
|
|
||||||
handler := func(c echo.Context) error {
|
handler := func(c echo.Context) error {
|
||||||
return c.String(http.StatusOK, "test")
|
return c.String(http.StatusOK, "test")
|
||||||
}
|
}
|
||||||
config := JWTConfig{}
|
|
||||||
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"
|
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"
|
||||||
|
validKey := []byte("secret")
|
||||||
|
invalidKey := []byte("invalid-key")
|
||||||
|
validAuth := bearer + " " + token
|
||||||
|
|
||||||
// No signing key provided
|
for _, tc := range []struct {
|
||||||
assert.Panics(t, func() {
|
expPanic bool
|
||||||
JWTWithConfig(config)
|
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
|
if tc.reqURL == "" {
|
||||||
config.SigningKey = []byte("secret")
|
tc.reqURL = "/"
|
||||||
config.SigningMethod = "RS256"
|
}
|
||||||
h := JWTWithConfig(config)(handler)
|
|
||||||
he := h(c).(*echo.HTTPError)
|
|
||||||
assert.Equal(t, http.StatusBadRequest, he.Code)
|
|
||||||
|
|
||||||
// Invalid key
|
req := test.NewRequest(echo.GET, tc.reqURL, nil)
|
||||||
auth := bearer + " " + token
|
res := test.NewResponseRecorder()
|
||||||
req.Header().Set(echo.HeaderAuthorization, auth)
|
req.Header().Set(echo.HeaderAuthorization, tc.hdrAuth)
|
||||||
config.SigningKey = []byte("invalid-key")
|
c := e.NewContext(req, res)
|
||||||
h = JWTWithConfig(config)(handler)
|
|
||||||
he = h(c).(*echo.HTTPError)
|
|
||||||
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
|
||||||
|
|
||||||
// Valid JWT
|
if tc.expPanic {
|
||||||
h = JWT([]byte("secret"))(handler)
|
assert.Panics(t, func() {
|
||||||
if assert.NoError(t, h(c)) {
|
JWTWithConfig(tc.config)
|
||||||
user := c.Get("user").(*jwt.Token)
|
}, tc.info)
|
||||||
claims := user.Claims.(jwt.MapClaims)
|
continue
|
||||||
assert.Equal(t, claims["name"], "John Doe")
|
}
|
||||||
|
|
||||||
|
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)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user