2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-05 19:50:57 +00:00
|
|
|
|
|
|
|
import (
|
2016-09-08 01:24:11 +00:00
|
|
|
"bytes"
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-09-05 19:50:57 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeGetInfoAboutSessionRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
2021-02-10 20:13:11 +00:00
|
|
|
router.HandleFunc("/api/v1/fleet/sessions/{id}", func(writer http.ResponseWriter, request *http.Request) {
|
2016-09-05 19:50:57 +00:00
|
|
|
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(),
|
2021-02-10 20:13:11 +00:00
|
|
|
httptest.NewRequest("GET", "/api/v1/fleet/sessions/1", nil),
|
2016-09-05 19:50:57 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeGetInfoAboutSessionsForUserRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
2021-02-10 20:13:11 +00:00
|
|
|
router.HandleFunc("/api/v1/fleet/user/{id}/sessions", func(writer http.ResponseWriter, request *http.Request) {
|
2016-09-05 19:50:57 +00:00
|
|
|
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(),
|
2021-02-10 20:13:11 +00:00
|
|
|
httptest.NewRequest("GET", "/api/v1/fleet/users/1/sessions", nil),
|
2016-09-05 19:50:57 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeDeleteSessionRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
2021-02-10 20:13:11 +00:00
|
|
|
router.HandleFunc("/api/v1/fleet/sessions/{id}", func(writer http.ResponseWriter, request *http.Request) {
|
2016-09-05 19:50:57 +00:00
|
|
|
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(),
|
2021-02-10 20:13:11 +00:00
|
|
|
httptest.NewRequest("DELETE", "/api/v1/fleet/sessions/1", nil),
|
2016-09-05 19:50:57 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeDeleteSessionsForUserRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
2021-02-10 20:13:11 +00:00
|
|
|
router.HandleFunc("/api/v1/fleet/user/{id}/sessions", func(writer http.ResponseWriter, request *http.Request) {
|
2016-09-05 19:50:57 +00:00
|
|
|
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(),
|
2021-02-10 20:13:11 +00:00
|
|
|
httptest.NewRequest("DELETE", "/api/v1/fleet/users/1/sessions", nil),
|
2016-09-05 19:50:57 +00:00
|
|
|
)
|
|
|
|
}
|
2016-09-08 01:24:11 +00:00
|
|
|
|
|
|
|
func TestDecodeLoginRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
2021-02-10 20:13:11 +00:00
|
|
|
router.HandleFunc("/api/v1/fleet/login", func(writer http.ResponseWriter, request *http.Request) {
|
2016-09-08 01:24:11 +00:00
|
|
|
r, err := decodeLoginRequest(context.Background(), request)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
params := r.(loginRequest)
|
2021-06-24 20:42:29 +00:00
|
|
|
assert.Equal(t, "foo", params.Email)
|
2016-09-08 01:24:11 +00:00
|
|
|
assert.Equal(t, "bar", params.Password)
|
|
|
|
}).Methods("POST")
|
2021-06-24 20:42:29 +00:00
|
|
|
t.Run("lowercase email", func(t *testing.T) {
|
2016-10-12 16:35:34 +00:00
|
|
|
var body bytes.Buffer
|
|
|
|
body.Write([]byte(`{
|
2021-06-24 20:42:29 +00:00
|
|
|
"email": "foo",
|
2016-09-08 01:24:11 +00:00
|
|
|
"password": "bar"
|
|
|
|
}`))
|
|
|
|
|
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/login", &body),
|
2016-10-12 16:35:34 +00:00
|
|
|
)
|
|
|
|
})
|
2021-06-24 20:42:29 +00:00
|
|
|
t.Run("uppercase email", func(t *testing.T) {
|
2016-10-12 16:35:34 +00:00
|
|
|
var body bytes.Buffer
|
|
|
|
body.Write([]byte(`{
|
2021-06-24 20:42:29 +00:00
|
|
|
"email": "Foo",
|
2016-10-12 16:35:34 +00:00
|
|
|
"password": "bar"
|
|
|
|
}`))
|
|
|
|
|
|
|
|
router.ServeHTTP(
|
|
|
|
httptest.NewRecorder(),
|
2021-02-10 20:13:11 +00:00
|
|
|
httptest.NewRequest("POST", "/api/v1/fleet/login", &body),
|
2016-10-12 16:35:34 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2016-09-08 01:24:11 +00:00
|
|
|
}
|