2016-10-03 03:14:35 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2018-06-18 17:09:08 +00:00
|
|
|
"bytes"
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-10-03 03:14:35 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeDeleteLabelRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
2018-05-04 18:05:55 +00:00
|
|
|
router.HandleFunc("/api/v1/kolide/labels/{name}", func(writer http.ResponseWriter, request *http.Request) {
|
2016-10-03 03:14:35 +00:00
|
|
|
r, err := decodeDeleteLabelRequest(context.Background(), request)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
params := r.(deleteLabelRequest)
|
2018-05-04 18:05:55 +00:00
|
|
|
assert.Equal(t, "foo", params.Name)
|
2016-10-03 03:14:35 +00:00
|
|
|
}).Methods("DELETE")
|
|
|
|
|
|
|
|
router.ServeHTTP(
|
|
|
|
httptest.NewRecorder(),
|
2018-05-04 18:05:55 +00:00
|
|
|
httptest.NewRequest("DELETE", "/api/v1/kolide/labels/foo", nil),
|
2016-10-03 03:14:35 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
)
|
|
|
|
}
|
2018-06-18 17:09:08 +00:00
|
|
|
|
|
|
|
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),
|
|
|
|
)
|
|
|
|
}
|