fleet/server/service/transport_sessions_test.go
Zachary Wasserman 715d908613 Update go-kit to 0.4.0 (#1411)
Notable refactoring:
- Use stdlib "context" in place of "golang.org/x/net/context"
- Go-kit no longer wraps errors, so we remove the unwrap in transport_error.go
- Use MakeHandler when setting up endpoint tests (fixes test bug caught during
  this refactoring)

Closes #1411.
2017-03-15 08:55:30 -07:00

114 lines
3.1 KiB
Go

package service
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)
func TestDecodeGetInfoAboutSessionRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/sessions/{id}", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeGetInfoAboutSessionRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(getInfoAboutSessionRequest)
assert.Equal(t, uint(1), params.ID)
}).Methods("GET")
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("GET", "/api/v1/kolide/sessions/1", nil),
)
}
func TestDecodeGetInfoAboutSessionsForUserRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/user/{id}/sessions", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeGetInfoAboutSessionsForUserRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(getInfoAboutSessionsForUserRequest)
assert.Equal(t, uint(1), params.ID)
}).Methods("GET")
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("GET", "/api/v1/kolide/users/1/sessions", nil),
)
}
func TestDecodeDeleteSessionRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/sessions/{id}", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeDeleteSessionRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(deleteSessionRequest)
assert.Equal(t, uint(1), params.ID)
}).Methods("DELETE")
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("DELETE", "/api/v1/kolide/sessions/1", nil),
)
}
func TestDecodeDeleteSessionsForUserRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/user/{id}/sessions", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeDeleteSessionsForUserRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(deleteSessionsForUserRequest)
assert.Equal(t, uint(1), params.ID)
}).Methods("DELETE")
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("DELETE", "/api/v1/kolide/users/1/sessions", nil),
)
}
func TestDecodeLoginRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/login", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeLoginRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(loginRequest)
assert.Equal(t, "foo", params.Username)
assert.Equal(t, "bar", params.Password)
}).Methods("POST")
t.Run("lowercase username", func(t *testing.T) {
var body bytes.Buffer
body.Write([]byte(`{
"username": "foo",
"password": "bar"
}`))
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("POST", "/api/v1/kolide/login", &body),
)
})
t.Run("uppercase username", func(t *testing.T) {
var body bytes.Buffer
body.Write([]byte(`{
"username": "Foo",
"password": "bar"
}`))
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("POST", "/api/v1/kolide/login", &body),
)
})
}