2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-04 18:31:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-09-04 18:31:17 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeCreateUserRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
2021-02-10 20:13:11 +00:00
|
|
|
router.HandleFunc("/api/v1/fleet/users", func(writer http.ResponseWriter, request *http.Request) {
|
2016-09-04 18:31:17 +00:00
|
|
|
r, err := decodeCreateUserRequest(context.Background(), request)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
params := r.(createUserRequest)
|
|
|
|
assert.Equal(t, "foo", *params.payload.Name)
|
2021-06-06 22:07:29 +00:00
|
|
|
assert.Equal(t, "foo@fleet.co", *params.payload.Email)
|
2016-09-04 18:31:17 +00:00
|
|
|
}).Methods("POST")
|
|
|
|
|
|
|
|
var body bytes.Buffer
|
|
|
|
body.Write([]byte(`{
|
|
|
|
"name": "foo",
|
2021-06-06 22:07:29 +00:00
|
|
|
"email": "foo@fleet.co"
|
2016-09-04 18:31:17 +00:00
|
|
|
}`))
|
|
|
|
|
|
|
|
router.ServeHTTP(
|
|
|
|
httptest.NewRecorder(),
|
2021-02-10 20:13:11 +00:00
|
|
|
httptest.NewRequest("POST", "/api/v1/fleet/users", &body),
|
2016-09-04 18:31:17 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeGetUserRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
2021-02-10 20:13:11 +00:00
|
|
|
router.HandleFunc("/api/v1/fleet/users/{id}", func(writer http.ResponseWriter, request *http.Request) {
|
2016-09-04 18:31:17 +00:00
|
|
|
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(),
|
2021-02-10 20:13:11 +00:00
|
|
|
httptest.NewRequest("GET", "/api/v1/fleet/users/1", nil),
|
2016-09-04 18:31:17 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeChangePasswordRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
2021-02-10 20:13:11 +00:00
|
|
|
router.HandleFunc("/api/v1/fleet/change_password", func(writer http.ResponseWriter, request *http.Request) {
|
2016-09-04 18:31:17 +00:00
|
|
|
r, err := decodeChangePasswordRequest(context.Background(), request)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2016-12-14 18:11:43 +00:00
|
|
|
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(),
|
2021-02-10 20:13:11 +00:00
|
|
|
httptest.NewRequest("POST", "/api/v1/fleet/change_password", &body),
|
2016-12-14 18:11:43 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeResetPasswordRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
2021-02-10 20:13:11 +00:00
|
|
|
router.HandleFunc("/api/v1/fleet/users/{id}/password", func(writer http.ResponseWriter, request *http.Request) {
|
2016-12-14 18:11:43 +00:00
|
|
|
r, err := decodeResetPasswordRequest(context.Background(), request)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
params := r.(resetPasswordRequest)
|
2016-09-04 18:31:17 +00:00
|
|
|
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(),
|
2021-02-10 20:13:11 +00:00
|
|
|
httptest.NewRequest("POST", "/api/v1/fleet/users/1/password", &body),
|
2016-09-04 18:31:17 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeModifyUserRequest(t *testing.T) {
|
|
|
|
router := mux.NewRouter()
|
2021-02-10 20:13:11 +00:00
|
|
|
router.HandleFunc("/api/v1/fleet/users/{id}", func(writer http.ResponseWriter, request *http.Request) {
|
2016-09-04 18:31:17 +00:00
|
|
|
r, err := decodeModifyUserRequest(context.Background(), request)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
params := r.(modifyUserRequest)
|
|
|
|
assert.Equal(t, "foo", *params.payload.Name)
|
2021-06-06 22:07:29 +00:00
|
|
|
assert.Equal(t, "foo@fleet.co", *params.payload.Email)
|
2016-09-04 18:31:17 +00:00
|
|
|
assert.Equal(t, uint(1), params.ID)
|
|
|
|
}).Methods("PATCH")
|
|
|
|
|
|
|
|
var body bytes.Buffer
|
|
|
|
body.Write([]byte(`{
|
|
|
|
"name": "foo",
|
2021-06-06 22:07:29 +00:00
|
|
|
"email": "foo@fleet.co"
|
2016-09-04 18:31:17 +00:00
|
|
|
}`))
|
|
|
|
|
2021-02-10 20:13:11 +00:00
|
|
|
request := httptest.NewRequest("PATCH", "/api/v1/fleet/users/1", &body)
|
2016-09-04 18:31:17 +00:00
|
|
|
router.ServeHTTP(
|
|
|
|
httptest.NewRecorder(),
|
2016-09-15 14:52:17 +00:00
|
|
|
request,
|
2016-09-04 18:31:17 +00:00
|
|
|
)
|
|
|
|
}
|