fleet/server/service/transport_packs_test.go
Mike Arpaia 0122f6cb0a Add host_ids and label_ids fields to the packs API (#737)
This PR adds the `host_ids` and `label_ids` field to the packs HTTP API so that one can operate on the hosts/labels which a pack is scheduled to be executed on. This replaces (and deletes) the `/api/v1/kolide/packs/123/labels/456` API in favor of `PATCH /api/v1/packs/123` and specifying the `label_ids` field. This also allows for bulk operations.

Consider the following API examples:

## Creating a pack with a known set of hosts and labels

The key addition is the `host_ids` and `label_ids` field in both the request and the response.

### Request

```
POST /api/v1/kolide/packs
```

```json
{
	"name": "My new pack",
	"description": "The newest of the packs",
	"host_ids": [1, 2, 3],
	"label_ids": [1, 3, 5]
}
```

### Response

```json
{
	"pack": {
		"id": 123,
		"name": "My new pack",
		"description": "The newest of the packs",
		"platform": "",
		"created_by": 1,
		"disabled": false,
		"query_count": 0,
		"total_hosts_count": 5,
		"host_ids": [1, 2, 3],
		"label_ids": [1, 3, 5]
	}
}
```

## Modifying the hosts and/or labels that a pack is scheduled to execute on

### Request

```
PATCH /api/v1/kolide/packs/123
```

```json
{
	"host_ids": [1, 2, 3, 4, 5],
	"label_ids": [1, 3, 5, 7]
}
```

### Response

```json
{
	"pack": {
		"id": 123,
		"name": "My new pack",
		"description": "The newest of the packs",
		"platform": "",
		"created_by": 1,
		"disabled": false,
		"query_count": 0,
		"total_hosts_count": 5,
		"host_ids": [1, 2, 3, 4, 5],
		"label_ids": [1, 3, 5, 7]
	}
}
```

close #633
2017-01-03 10:32:06 -07:00

105 lines
2.9 KiB
Go

package service
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"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)
assert.Equal(t, "bar", *params.payload.Description)
require.NotNil(t, params.payload.HostIDs)
assert.Len(t, *params.payload.HostIDs, 3)
require.NotNil(t, params.payload.LabelIDs)
assert.Len(t, *params.payload.LabelIDs, 2)
}).Methods("POST")
var body bytes.Buffer
body.Write([]byte(`{
"name": "foo",
"description": "bar",
"host_ids": [1, 2, 3],
"label_ids": [1, 5]
}`))
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, uint(1), params.ID)
assert.Equal(t, "foo", *params.payload.Name)
assert.Equal(t, "bar", *params.payload.Description)
require.NotNil(t, params.payload.HostIDs)
assert.Len(t, *params.payload.HostIDs, 3)
require.NotNil(t, params.payload.LabelIDs)
assert.Len(t, *params.payload.LabelIDs, 2)
}).Methods("PATCH")
var body bytes.Buffer
body.Write([]byte(`{
"name": "foo",
"description": "bar",
"host_ids": [1, 2, 3],
"label_ids": [1, 5]
}`))
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),
)
}