wallet-sync-server/server/account_test.go

131 lines
4.1 KiB
Go
Raw Normal View History

package server
import (
2022-06-17 21:39:12 +02:00
"bytes"
2022-06-19 22:41:20 +02:00
"fmt"
2022-06-17 21:39:12 +02:00
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
2022-06-17 21:39:12 +02:00
2022-07-12 04:10:19 +02:00
"lbryio/lbry-id/store"
)
func TestServerRegisterSuccess(t *testing.T) {
2022-06-21 17:52:03 +02:00
testAuth := TestAuth{}
2022-06-17 21:39:12 +02:00
testStore := TestStore{}
s := Server{&testAuth, &testStore}
requestBody := []byte(`{"email": "abc@example.com", "password": "123"}`)
req := httptest.NewRequest(http.MethodPost, PathRegister, bytes.NewBuffer(requestBody))
w := httptest.NewRecorder()
s.register(w, req)
body, _ := ioutil.ReadAll(w.Body)
2022-06-21 17:52:03 +02:00
expectStatusCode(t, w, http.StatusCreated)
2022-06-17 21:39:12 +02:00
if string(body) != "{}" {
t.Errorf("Expected register response to be \"{}\": result: %+v", string(body))
}
if !testStore.Called.CreateAccount {
t.Errorf("Expected Store.CreateAccount to be called")
}
}
func TestServerRegisterErrors(t *testing.T) {
2022-06-19 22:41:20 +02:00
tt := []struct {
name string
2022-06-22 17:06:05 +02:00
email string
2022-06-19 22:41:20 +02:00
expectedStatusCode int
expectedErrorString string
storeErrors TestStoreFunctionsErrors
}{
{
name: "validation error", // missing email address
email: "",
expectedStatusCode: http.StatusBadRequest,
expectedErrorString: http.StatusText(http.StatusBadRequest) + ": Request failed validation: Invalid 'email'",
// Just check one validation error (missing email address) to make sure the
// validate function is called. We'll check the rest of the validation
// errors in the other test below.
},
2022-06-19 22:41:20 +02:00
{
name: "existing account",
email: "abc@example.com",
2022-06-19 22:41:20 +02:00
expectedStatusCode: http.StatusConflict,
expectedErrorString: http.StatusText(http.StatusConflict) + ": Error registering",
storeErrors: TestStoreFunctionsErrors{CreateAccount: store.ErrDuplicateEmail},
},
{
name: "unspecified account creation failure",
email: "abc@example.com",
2022-06-19 22:41:20 +02:00
expectedStatusCode: http.StatusInternalServerError,
expectedErrorString: http.StatusText(http.StatusInternalServerError),
storeErrors: TestStoreFunctionsErrors{CreateAccount: fmt.Errorf("TestStore.CreateAccount fail")},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
// Set this up to fail according to specification
2022-06-21 17:52:03 +02:00
testAuth := TestAuth{}
2022-06-19 22:41:20 +02:00
testStore := TestStore{Errors: tc.storeErrors}
server := Server{&testAuth, &testStore}
// Make request
requestBody := fmt.Sprintf(`{"email": "%s", "password": "123"}`, tc.email)
2022-06-19 22:41:20 +02:00
req := httptest.NewRequest(http.MethodPost, PathAuthToken, bytes.NewBuffer([]byte(requestBody)))
w := httptest.NewRecorder()
server.register(w, req)
2022-06-22 17:37:03 +02:00
body, _ := ioutil.ReadAll(w.Body)
2022-06-21 17:52:03 +02:00
expectStatusCode(t, w, tc.expectedStatusCode)
2022-06-22 17:37:03 +02:00
expectErrorString(t, body, tc.expectedErrorString)
2022-06-19 22:41:20 +02:00
})
}
}
func TestServerValidateRegisterRequest(t *testing.T) {
2022-06-17 22:12:20 +02:00
registerRequest := RegisterRequest{Email: "joe@example.com", Password: "aoeu"}
if registerRequest.validate() != nil {
2022-06-17 22:12:20 +02:00
t.Fatalf("Expected valid RegisterRequest to successfully validate")
}
registerRequest = RegisterRequest{Email: "joe-example.com", Password: "aoeu"}
err := registerRequest.validate()
if !strings.Contains(err.Error(), "email") {
2022-06-17 22:12:20 +02:00
t.Fatalf("Expected RegisterRequest with invalid email to not successfully validate")
}
// Note that Golang's email address parser, which I use, will accept
// "Joe <joe@example.com>" so we need to make sure to avoid accepting it. See
// the implementation.
registerRequest = RegisterRequest{Email: "Joe <joe@example.com>", Password: "aoeu"}
err = registerRequest.validate()
if !strings.Contains(err.Error(), "email") {
2022-06-17 22:12:20 +02:00
t.Fatalf("Expected RegisterRequest with email with unexpected formatting to not successfully validate")
}
2022-06-17 22:15:27 +02:00
registerRequest = RegisterRequest{Password: "aoeu"}
err = registerRequest.validate()
if !strings.Contains(err.Error(), "email") {
2022-06-17 22:15:27 +02:00
t.Fatalf("Expected RegisterRequest with missing email to not successfully validate")
}
2022-06-17 22:12:20 +02:00
registerRequest = RegisterRequest{Email: "joe@example.com"}
err = registerRequest.validate()
if !strings.Contains(err.Error(), "password") {
2022-06-17 22:12:20 +02:00
t.Fatalf("Expected RegisterRequest with missing password to not successfully validate")
}
}