mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 08:55:24 +00:00
715d908613
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.
123 lines
3.3 KiB
Go
123 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"
|
|
)
|
|
|
|
func TestDecodeCreateUserRequest(t *testing.T) {
|
|
router := mux.NewRouter()
|
|
router.HandleFunc("/api/v1/kolide/users", func(writer http.ResponseWriter, request *http.Request) {
|
|
r, err := decodeCreateUserRequest(context.Background(), request)
|
|
assert.Nil(t, err)
|
|
|
|
params := r.(createUserRequest)
|
|
assert.Equal(t, "foo", *params.payload.Name)
|
|
assert.Equal(t, "foo@kolide.co", *params.payload.Email)
|
|
}).Methods("POST")
|
|
|
|
var body bytes.Buffer
|
|
body.Write([]byte(`{
|
|
"name": "foo",
|
|
"email": "foo@kolide.co"
|
|
}`))
|
|
|
|
router.ServeHTTP(
|
|
httptest.NewRecorder(),
|
|
httptest.NewRequest("POST", "/api/v1/kolide/users", &body),
|
|
)
|
|
}
|
|
|
|
func TestDecodeGetUserRequest(t *testing.T) {
|
|
router := mux.NewRouter()
|
|
router.HandleFunc("/api/v1/kolide/users/{id}", func(writer http.ResponseWriter, request *http.Request) {
|
|
r, err := decodeGetUserRequest(context.Background(), request)
|
|
assert.Nil(t, err)
|
|
|
|
params := r.(getUserRequest)
|
|
assert.Equal(t, uint(1), params.ID)
|
|
}).Methods("GET")
|
|
|
|
router.ServeHTTP(
|
|
httptest.NewRecorder(),
|
|
httptest.NewRequest("GET", "/api/v1/kolide/users/1", nil),
|
|
)
|
|
}
|
|
|
|
func TestDecodeChangePasswordRequest(t *testing.T) {
|
|
router := mux.NewRouter()
|
|
router.HandleFunc("/api/v1/kolide/change_password", func(writer http.ResponseWriter, request *http.Request) {
|
|
r, err := decodeChangePasswordRequest(context.Background(), request)
|
|
assert.Nil(t, err)
|
|
|
|
params := r.(changePasswordRequest)
|
|
assert.Equal(t, "foo", params.OldPassword)
|
|
assert.Equal(t, "bar", params.NewPassword)
|
|
}).Methods("POST")
|
|
|
|
var body bytes.Buffer
|
|
body.Write([]byte(`{
|
|
"old_password": "foo",
|
|
"new_password": "bar"
|
|
}`))
|
|
|
|
router.ServeHTTP(
|
|
httptest.NewRecorder(),
|
|
httptest.NewRequest("POST", "/api/v1/kolide/change_password", &body),
|
|
)
|
|
}
|
|
|
|
func TestDecodeResetPasswordRequest(t *testing.T) {
|
|
router := mux.NewRouter()
|
|
router.HandleFunc("/api/v1/kolide/users/{id}/password", func(writer http.ResponseWriter, request *http.Request) {
|
|
r, err := decodeResetPasswordRequest(context.Background(), request)
|
|
assert.Nil(t, err)
|
|
|
|
params := r.(resetPasswordRequest)
|
|
assert.Equal(t, "bar", params.NewPassword)
|
|
assert.Equal(t, "baz", params.PasswordResetToken)
|
|
}).Methods("POST")
|
|
|
|
var body bytes.Buffer
|
|
body.Write([]byte(`{
|
|
"new_password": "bar",
|
|
"password_reset_token": "baz"
|
|
}`))
|
|
|
|
router.ServeHTTP(
|
|
httptest.NewRecorder(),
|
|
httptest.NewRequest("POST", "/api/v1/kolide/users/1/password", &body),
|
|
)
|
|
}
|
|
|
|
func TestDecodeModifyUserRequest(t *testing.T) {
|
|
router := mux.NewRouter()
|
|
router.HandleFunc("/api/v1/kolide/users/{id}", func(writer http.ResponseWriter, request *http.Request) {
|
|
r, err := decodeModifyUserRequest(context.Background(), request)
|
|
assert.Nil(t, err)
|
|
|
|
params := r.(modifyUserRequest)
|
|
assert.Equal(t, "foo", *params.payload.Name)
|
|
assert.Equal(t, "foo@kolide.co", *params.payload.Email)
|
|
assert.Equal(t, uint(1), params.ID)
|
|
}).Methods("PATCH")
|
|
|
|
var body bytes.Buffer
|
|
body.Write([]byte(`{
|
|
"name": "foo",
|
|
"email": "foo@kolide.co"
|
|
}`))
|
|
|
|
request := httptest.NewRequest("PATCH", "/api/v1/kolide/users/1", &body)
|
|
router.ServeHTTP(
|
|
httptest.NewRecorder(),
|
|
request,
|
|
)
|
|
}
|