mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 08:55:24 +00:00
6424a029ee
Replaces the UI endpoints for creating and modifying labels. These were removed in #1686 because we thought we were killing the UI. Now labels can be created and edited in the UI again.
70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestDecodeDeleteLabelRequest(t *testing.T) {
|
|
router := mux.NewRouter()
|
|
router.HandleFunc("/api/v1/kolide/labels/{name}", func(writer http.ResponseWriter, request *http.Request) {
|
|
r, err := decodeDeleteLabelRequest(context.Background(), request)
|
|
assert.Nil(t, err)
|
|
|
|
params := r.(deleteLabelRequest)
|
|
assert.Equal(t, "foo", params.Name)
|
|
}).Methods("DELETE")
|
|
|
|
router.ServeHTTP(
|
|
httptest.NewRecorder(),
|
|
httptest.NewRequest("DELETE", "/api/v1/kolide/labels/foo", nil),
|
|
)
|
|
}
|
|
|
|
func TestDecodeGetLabelRequest(t *testing.T) {
|
|
router := mux.NewRouter()
|
|
router.HandleFunc("/api/v1/kolide/labels/{id}", func(writer http.ResponseWriter, request *http.Request) {
|
|
r, err := decodeGetLabelRequest(context.Background(), request)
|
|
assert.Nil(t, err)
|
|
|
|
params := r.(getLabelRequest)
|
|
assert.Equal(t, uint(1), params.ID)
|
|
}).Methods("GET")
|
|
|
|
router.ServeHTTP(
|
|
httptest.NewRecorder(),
|
|
httptest.NewRequest("GET", "/api/v1/kolide/labels/1", nil),
|
|
)
|
|
}
|
|
|
|
func TestDecodeCreateLabelRequest(t *testing.T) {
|
|
router := mux.NewRouter()
|
|
router.HandleFunc("/api/v1/kolide/labels", func(writer http.ResponseWriter, request *http.Request) {
|
|
r, err := decodeCreateLabelRequest(context.Background(), request)
|
|
assert.Nil(t, err)
|
|
|
|
params := r.(createLabelRequest)
|
|
assert.Equal(t, "foo", *params.payload.Name)
|
|
assert.Equal(t, "select * from foo;", *params.payload.Query)
|
|
assert.Equal(t, "darwin", *params.payload.Platform)
|
|
}).Methods("POST")
|
|
|
|
var body bytes.Buffer
|
|
body.Write([]byte(`{
|
|
"name": "foo",
|
|
"query": "select * from foo;",
|
|
"platform": "darwin"
|
|
}`))
|
|
|
|
router.ServeHTTP(
|
|
httptest.NewRecorder(),
|
|
httptest.NewRequest("POST", "/api/v1/kolide/labels", &body),
|
|
)
|
|
}
|