2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-04 05:13:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeCreatePackRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/api/v1/kolide/packs", func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
r, err := decodeCreatePackRequest(context.Background(), request)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
params := r.(createPackRequest)
|
|
|
|
assert.Equal(t, "foo", *params.payload.Name)
|
2016-11-21 19:49:36 +00:00
|
|
|
assert.Equal(t, "bar", *params.payload.Description)
|
2016-09-04 05:13:42 +00:00
|
|
|
}).Methods("POST")
|
|
|
|
|
|
|
|
var body bytes.Buffer
|
|
|
|
body.Write([]byte(`{
|
2016-11-21 19:49:36 +00:00
|
|
|
"name": "foo",
|
|
|
|
"description": "bar"
|
2016-09-04 05:13:42 +00:00
|
|
|
}`))
|
|
|
|
|
|
|
|
router.ServeHTTP(
|
|
|
|
httptest.NewRecorder(),
|
|
|
|
httptest.NewRequest("POST", "/api/v1/kolide/packs", &body),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeModifyPackRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/api/v1/kolide/packs/{id}", func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
r, err := decodeModifyPackRequest(context.Background(), request)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
params := r.(modifyPackRequest)
|
|
|
|
assert.Equal(t, "foo", *params.payload.Name)
|
2016-11-21 19:49:36 +00:00
|
|
|
assert.Equal(t, "bar", *params.payload.Description)
|
2016-09-04 05:13:42 +00:00
|
|
|
assert.Equal(t, uint(1), params.ID)
|
|
|
|
}).Methods("PATCH")
|
|
|
|
|
|
|
|
var body bytes.Buffer
|
|
|
|
body.Write([]byte(`{
|
2016-11-21 19:49:36 +00:00
|
|
|
"name": "foo",
|
|
|
|
"description": "bar"
|
2016-09-04 05:13:42 +00:00
|
|
|
}`))
|
|
|
|
|
|
|
|
router.ServeHTTP(
|
|
|
|
httptest.NewRecorder(),
|
|
|
|
httptest.NewRequest("PATCH", "/api/v1/kolide/packs/1", &body),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeDeletePackRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/api/v1/kolide/packs/{id}", func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
r, err := decodeDeletePackRequest(context.Background(), request)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
params := r.(deletePackRequest)
|
|
|
|
assert.Equal(t, uint(1), params.ID)
|
|
|
|
}).Methods("DELETE")
|
|
|
|
|
|
|
|
router.ServeHTTP(
|
|
|
|
httptest.NewRecorder(),
|
|
|
|
httptest.NewRequest("DELETE", "/api/v1/kolide/packs/1", nil),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeGetPackRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
|
|
|
router.HandleFunc("/api/v1/kolide/packs/{id}", func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
r, err := decodeGetPackRequest(context.Background(), request)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
params := r.(getPackRequest)
|
|
|
|
assert.Equal(t, uint(1), params.ID)
|
|
|
|
}).Methods("GET")
|
|
|
|
|
|
|
|
router.ServeHTTP(
|
|
|
|
httptest.NewRecorder(),
|
|
|
|
httptest.NewRequest("GET", "/api/v1/kolide/packs/1", nil),
|
|
|
|
)
|
|
|
|
}
|