fleet/server/service/transport_scheduled_queries_test.go
Zachary Wasserman 0b7747bef0
Fix pack and query UI issues in Fleet 2.0 (#1829)
Replaces (and appropriately refactors) a number of endpoints that were removed long ago when we decided to kill the UI with the fleetctl release. We turned out not to do this, and now need to restore these missing endpoints.

This is not a straight up replacement of the existing code because of refactoring to the DB schemas that was also done in the migration.

Most of the replaced code was removed in #1670 and #1686.

Fixes #1811, fixes #1810
2018-06-15 10:13:11 -04:00

119 lines
3.3 KiB
Go

package service
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDecodeGetScheduledQueriesInPackRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/packs/{id}/scheduled", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeGetScheduledQueriesInPackRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(getScheduledQueriesInPackRequest)
assert.Equal(t, uint(1), params.ID)
}).Methods("GET")
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("GET", "/api/v1/kolide/packs/1/scheduled", nil),
)
}
func TestDecodeScheduleQueryRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/schedule", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeScheduleQueryRequest(context.Background(), request)
require.Nil(t, err)
params := r.(scheduleQueryRequest)
assert.Equal(t, uint(5), params.PackID)
assert.Equal(t, uint(1), params.QueryID)
assert.Equal(t, uint(60), params.Interval)
assert.Equal(t, true, *params.Snapshot)
}).Methods("POST")
var body bytes.Buffer
body.Write([]byte(`{
"pack_id": 5,
"query_id": 1,
"interval": 60,
"snapshot": true
}`))
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("POST", "/api/v1/kolide/schedule", &body),
)
}
func TestDecodeModifyScheduledQueryRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/scheduled/{id}", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeModifyScheduledQueryRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(modifyScheduledQueryRequest)
assert.Equal(t, uint(1), params.ID)
assert.Equal(t, uint(5), *params.payload.PackID)
assert.Equal(t, uint(6), *params.payload.QueryID)
assert.Equal(t, true, *params.payload.Removed)
assert.Equal(t, uint(60), *params.payload.Interval)
assert.Equal(t, uint(1), *params.payload.Shard)
}).Methods("PATCH")
var body bytes.Buffer
body.Write([]byte(`{
"pack_id": 5,
"query_id": 6,
"removed": true,
"interval": 60,
"shard": 1
}`))
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("PATCH", "/api/v1/kolide/scheduled/1", &body),
)
}
func TestDecodeDeleteScheduledQueryRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/scheduled/{id}", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeDeleteScheduledQueryRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(deleteScheduledQueryRequest)
assert.Equal(t, uint(1), params.ID)
}).Methods("DELETE")
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("DELETE", "/api/v1/kolide/scheduled/1", nil),
)
}
func TestDecodeGetScheduledQueryRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/scheduled/{id}", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeGetScheduledQueryRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(getScheduledQueryRequest)
assert.Equal(t, uint(1), params.ID)
}).Methods("GET")
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("GET", "/api/v1/kolide/scheduled/1", nil),
)
}