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 TestDelete(t *testing.T) { m := minion.New(minion.Options{ UnauthenticatedRoutes: []string{"*"}}, ) userHandler := func(ctx *minion.Context) { j := struct { Message string `json:"message"` }{ "ok", } ctx.JSON(200, j) } m.Delete("/users/:id", userHandler) ts := httptest.NewServer(m) defer ts.Close() var j, body string var status int status, body = tst.Request(t, ts, "DELETE", "/users/1", nil, nil) tst.AssertEqual(t, 200, status) j = `{"message":"ok"}` tst.AssertEqual(t, j, body) }
func TestPatch(t *testing.T) { m := minion.New(minion.Options{ UnauthenticatedRoutes: []string{"*"}}, ) type user struct { ID string `json:"id"` Name string `json:"name"` Email string `json:"email"` } userHandler := func(ctx *minion.Context) { dbUser := &user{"1", "John", "*****@*****.**"} user := new(user) ctx.GetResource(user) dbUser.Email = user.Email ctx.JSON(200, dbUser) } m.Patch("/users/:id", userHandler) ts := httptest.NewServer(m) defer ts.Close() var j, body string var status int payload := `{"email":"*****@*****.**"}` status, body = tst.Request(t, ts, "PATCH", "/users/1", nil, bytes.NewBuffer([]byte(payload))) tst.AssertEqual(t, 200, status) j = `{"id":"1","name":"John","email":"*****@*****.**"}` tst.AssertEqual(t, j, body) }
func TestPost(t *testing.T) { m := minion.New(minion.Options{ UnauthenticatedRoutes: []string{"*"}}, ) usersHandler := func(ctx *minion.Context) { user := &struct { ID string `json:"id"` Name string `json:"name"` Email string `json:"email"` }{} ctx.GetResource(user) user.ID = "1" ctx.JSON(200, user) } m.Post("/users", usersHandler) ts := httptest.NewServer(m) defer ts.Close() var j, body string var status int payload := `{"name":"John","email":"*****@*****.**"}` status, body = tst.Request(t, ts, "POST", "/users", nil, bytes.NewBuffer([]byte(payload))) tst.AssertEqual(t, 200, status) j = `{"id":"1","name":"John","email":"*****@*****.**"}` tst.AssertEqual(t, j, body) }
func TestNamespace(t *testing.T) { usersHandler := func(ctx *minion.Context) { ctx.JSON(200, nil) } m := minion.New(minion.Options{ UnauthenticatedRoutes: []string{"*"}, Namespace: "/v1"}, ) m.Get("/users", usersHandler) ts := httptest.NewServer(m) defer ts.Close() var status int status, _ = tst.Request(t, ts, "GET", "/v1/users", nil, nil) tst.AssertEqual(t, 200, status) status, _ = tst.Request(t, ts, "GET", "/users", nil, nil) tst.AssertEqual(t, 404, status) }
func TestHead(t *testing.T) { m := minion.New(minion.Options{ UnauthenticatedRoutes: []string{"*"}}, ) userHandler := func(ctx *minion.Context) { ctx.Text(200, "") } m.Head("/users", userHandler) ts := httptest.NewServer(m) defer ts.Close() var body string var status int status, body = tst.Request(t, ts, "HEAD", "/users", nil, nil) tst.AssertEqual(t, 200, status) tst.AssertEqual(t, "", body) }
func TestGet(t *testing.T) { john := struct { Name string `json:"name"` Email string `json:"email"` }{ "John Doe", "*****@*****.**", } foo := struct { Name string `json:"name"` Email string `json:"email"` }{ "Foo Bar", "*****@*****.**", } usersHandler := func(ctx *minion.Context) { users := struct { Users []interface{} `json:"users"` }{ []interface{}{john, foo}, } ctx.JSON(200, users) } userHandler := func(ctx *minion.Context) { id := ctx.ByGet("id") doe := struct { ID string `json:"id"` Name string `json:"name"` Email string `json:"email"` }{ id, "John Doe", "*****@*****.**", } ctx.JSON(200, doe) } m := minion.New(minion.Options{ UnauthenticatedRoutes: []string{"*"}}, ) m.Get("/users", usersHandler) m.Get("/user/:id", userHandler) ts := httptest.NewServer(m) defer ts.Close() var j, body string var status int status, body = tst.Request(t, ts, "GET", "/users", nil, nil) tst.AssertEqual(t, 200, status) j = `{"users":[{"name":"John Doe","email":"*****@*****.**"},{"name":"Foo Bar","email":"*****@*****.**"}]}` tst.AssertEqual(t, j, body) status, body = tst.Request(t, ts, "GET", "/user/1", nil, nil) tst.AssertEqual(t, 200, status) j = `{"id":"1","name":"John Doe","email":"*****@*****.**"}` tst.AssertEqual(t, j, body) }