fleet/server/service/transport_invites_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

71 lines
1.7 KiB
Go

package service
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)
func TestDecodeCreateInviteRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/invites", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeCreateInviteRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(createInviteRequest)
assert.Equal(t, uint(1), *params.payload.InvitedBy)
}).Methods("POST")
t.Run("lowercase email", func(t *testing.T) {
var body bytes.Buffer
body.Write([]byte(`{
"name": "foo",
"email": "foo@kolide.co",
"invited_by": 1
}`))
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("POST", "/api/v1/kolide/invites", &body),
)
})
t.Run("uppercase email", func(t *testing.T) {
// email string should be lowerased after decode.
var body bytes.Buffer
body.Write([]byte(`{
"name": "foo",
"email": "Foo@Kolide.co",
"invited_by": 1
}`))
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("POST", "/api/v1/kolide/invites", &body),
)
})
}
func TestDecodeVerifyInviteRequest(t *testing.T) {
router := mux.NewRouter()
router.HandleFunc("/api/v1/kolide/invites/{token}", func(writer http.ResponseWriter, request *http.Request) {
r, err := decodeCreateInviteRequest(context.Background(), request)
assert.Nil(t, err)
params := r.(verifyInviteRequest)
assert.Equal(t, "test_token", params.Token)
}).Methods("GET")
router.ServeHTTP(
httptest.NewRecorder(),
httptest.NewRequest("GET", "/api/v1/kolide/tokens/test_token", nil),
)
}