Exemple #1
0
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)
}
Exemple #2
0
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)
}
Exemple #3
0
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)
}
Exemple #4
0
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)
}
Exemple #5
0
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)
}
Exemple #6
0
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 TestLastChar(t *testing.T) {
	var c uint8

	c = lastChar("minion")
	tst.AssertEqual(t, uint8('n'), c)

	c = lastChar("/users/")
	tst.AssertEqual(t, uint8('/'), c)

	c = lastChar("")
	tst.AssertEqual(t, uint8(0), c)
}
Exemple #8
0
func TestGetResource(t *testing.T) {
	user := struct {
		ID    string `json:"id"`
		Name  string `json:"name"`
		Email string `json:"email"`
	}{
		"1",
		"John Doe",
		"*****@*****.**",
	}

	body := bytes.NewBufferString(`{"id":"1","name":"John Doe","email":"*****@*****.**"}`)

	m := minion.New(minion.Options{})
	m.Post("/users", nil)

	ts := httptest.NewServer(m)
	defer ts.Close()

	req, _ := http.NewRequest("POST", ts.URL+"/users", body)

	var resource struct {
		ID    string `json:"id"`
		Name  string `json:"name"`
		Email string `json:"email"`
	}

	ctx := &minion.Context{Req: req}
	err := ctx.GetResource(&resource)

	tst.AssertNoError(t, err)
	tst.AssertEqual(t, user, resource)
}
func TestCalculateAbsolutePath(t *testing.T) {
	var router *Router
	var path string

	router = &Router{namespace: "/"}
	path = router.calculateAbsolutePath("/users")
	tst.AssertEqual(t, "/users", path)

	router = &Router{namespace: "/api"}
	path = router.calculateAbsolutePath("/users")
	tst.AssertEqual(t, "/api/users", path)

	router = &Router{namespace: "/api"}
	path = router.calculateAbsolutePath("")
	tst.AssertEqual(t, "/api", path)

	router = &Router{namespace: "/api"}
	path = router.calculateAbsolutePath("/users/")
	tst.AssertEqual(t, "/api/users/", path)
}
Exemple #10
0
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)
}
Exemple #11
0
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)
}
Exemple #12
0
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)
}
Exemple #13
0
func TestVersion(t *testing.T) {
	tst.AssertEqual(t, "0.0.1", minion.Version())
}
Exemple #14
0
func TestDefaultEnv(t *testing.T) {
	tst.AssertEqual(t, minion.DEV, minion.MinionEnv)
}