2016-09-29 02:44:05 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-09-29 02:44:05 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeCreateInviteRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
2021-02-10 20:13:11 +00:00
|
|
|
router.HandleFunc("/api/v1/fleet/invites", func(writer http.ResponseWriter, request *http.Request) {
|
2021-04-05 20:28:43 +00:00
|
|
|
_, err := decodeCreateInviteRequest(context.Background(), request)
|
2016-09-29 02:44:05 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
}).Methods("POST")
|
|
|
|
|
2016-10-12 16:35:34 +00:00
|
|
|
t.Run("lowercase email", func(t *testing.T) {
|
|
|
|
var body bytes.Buffer
|
|
|
|
body.Write([]byte(`{
|
2016-09-29 02:44:05 +00:00
|
|
|
"name": "foo",
|
2021-06-06 22:07:29 +00:00
|
|
|
"email": "foo@fleet.co"
|
2016-09-29 02:44:05 +00:00
|
|
|
}`))
|
|
|
|
|
2016-10-12 16:35:34 +00:00
|
|
|
router.ServeHTTP(
|
|
|
|
httptest.NewRecorder(),
|
2021-02-10 20:13:11 +00:00
|
|
|
httptest.NewRequest("POST", "/api/v1/fleet/invites", &body),
|
2016-10-12 16:35:34 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("uppercase email", func(t *testing.T) {
|
|
|
|
// email string should be lowerased after decode.
|
|
|
|
var body bytes.Buffer
|
|
|
|
body.Write([]byte(`{
|
|
|
|
"name": "foo",
|
2021-06-06 22:07:29 +00:00
|
|
|
"email": "Foo@fleet.co"
|
2016-10-12 16:35:34 +00:00
|
|
|
}`))
|
|
|
|
|
|
|
|
router.ServeHTTP(
|
|
|
|
httptest.NewRecorder(),
|
2021-02-10 20:13:11 +00:00
|
|
|
httptest.NewRequest("POST", "/api/v1/fleet/invites", &body),
|
2016-10-12 16:35:34 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
2016-12-30 01:58:12 +00:00
|
|
|
|
|
|
|
func TestDecodeVerifyInviteRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
2021-02-10 20:13:11 +00:00
|
|
|
router.HandleFunc("/api/v1/fleet/invites/{token}", func(writer http.ResponseWriter, request *http.Request) {
|
2016-12-30 01:58:12 +00:00
|
|
|
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(),
|
2021-02-10 20:13:11 +00:00
|
|
|
httptest.NewRequest("GET", "/api/v1/fleet/tokens/test_token", nil),
|
2016-12-30 01:58:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
}
|