func TestJWTAuthenticationUnauthenticatedPath(t *testing.T) { m := minion.Classic(minion.Options{ JWTToken: "123", UnauthenticatedRoutes: []string{"/unauthenticated"}, }) usersHandler := func(ctx *minion.Context) { j := struct { Message string `json:"message"` }{ "ok", } ctx.JSON(200, j) } m.Get("/unauthenticated", usersHandler) ts := httptest.NewServer(m) defer ts.Close() var j, body string var status int status, body = tst.Request(t, ts, "GET", "/unauthenticated", nil, nil) tst.AssertEqual(t, 200, status) j = `{"message":"ok"}` tst.AssertEqual(t, j, body) }
func TestJWTAuthentication(t *testing.T) { m := minion.Classic(minion.Options{JWTToken: "123"}) usersHandler := func(ctx *minion.Context) { j := struct { Message string `json:"message"` }{ "ok", } ctx.JSON(200, j) } m.Get("/users", usersHandler) ts := httptest.NewServer(m) defer ts.Close() var j, body string var status int tokenString, _ := minion.CreateJWTToken(nil) h := http.Header{} h.Set("Authorization", "BEARER "+tokenString) status, body = tst.Request(t, ts, "GET", "/users", h, nil) tst.AssertEqual(t, 200, status) j = `{"message":"ok"}` tst.AssertEqual(t, j, body) }
func TestJWTAuthenticationWrongToken(t *testing.T) { m := minion.Classic(minion.Options{JWTToken: "123"}) usersHandler := func(ctx *minion.Context) { j := struct { Message string `json:"message"` }{ "ok", } ctx.JSON(200, j) } m.Get("/users", usersHandler) ts := httptest.NewServer(m) defer ts.Close() var j, body string var status int tokenAuth := jwtauth.New("HS256", []byte("wrong"), nil) _, tokenString, _ := tokenAuth.Encode(nil) h := http.Header{} h.Set("Authorization", "BEARER "+tokenString) status, body = tst.Request(t, ts, "GET", "/users", h, nil) tst.AssertEqual(t, 401, status) j = `{"status":401,"message":"Unauthorized"}` tst.AssertEqual(t, j, body) }
func TestClassic(t *testing.T) { m := minion.Classic(minion.Options{}) tst.AssertNotNil(t, m) tst.AssertNotNil(t, m.Router) }