fleet/server/service/transport_queries_test.go
Zachary Wasserman 003ad9b5fa
Delete spec entities by name instead of ID (#1755)
With the UI, deleting by ID made sense. With fleetctl, we now want to delete
by name. Transition only the methods used for spec related entities, as others
will be removed soon.
2018-05-04 11:05:55 -07:00

90 lines
2.4 KiB
Go

package service
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)
func TestDecodeCreateQueryRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/queries", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeCreateQueryRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(createQueryRequest)
assert.Equal(t, "foo", *params.payload.Name)
assert.Equal(t, "select * from time;", *params.payload.Query)
}).Methods("POST")
var body bytes.Buffer
body.Write([]byte(`{
"name": "foo",
"query": "select * from time;"
}`))
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("POST", "/api/v1/kolide/queries", &body),
)
}
func TestDecodeModifyQueryRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/queries/{id}", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeModifyQueryRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(modifyQueryRequest)
assert.Equal(t, "foo", *params.payload.Name)
assert.Equal(t, uint(1), params.ID)
}).Methods("PATCH")
var body bytes.Buffer
body.Write([]byte(`{
"name": "foo"
}`))
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("PATCH", "/api/v1/kolide/queries/1", &body),
)
}
func TestDecodeDeleteQueryRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/queries/{name}", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeDeleteQueryRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(deleteQueryRequest)
assert.Equal(t, "qwerty", params.Name)
}).Methods("DELETE")
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("DELETE", "/api/v1/kolide/queries/qwerty", nil),
)
}
func TestDecodeGetQueryRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/queries/{id}", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeGetQueryRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(getQueryRequest)
assert.Equal(t, uint(1), params.ID)
}).Methods("GET")
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("GET", "/api/v1/kolide/queries/1", nil),
)
}