wallet-sync-server/server/register_test.go

112 lines
3.3 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"
"testing"
2022-06-17 21:39:12 +02:00
2022-06-19 22:41:20 +02:00
"orblivion/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
requestBody string
expectedStatusCode int
expectedErrorString string
storeErrors TestStoreFunctionsErrors
}{
{
name: "existing account",
expectedStatusCode: http.StatusConflict,
expectedErrorString: http.StatusText(http.StatusConflict) + ": Error registering",
storeErrors: TestStoreFunctionsErrors{CreateAccount: store.ErrDuplicateEmail},
},
{
name: "unspecified account creation failure",
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 := `{ "email": "abc@example.com", "password": "123"}`
req := httptest.NewRequest(http.MethodPost, PathAuthToken, bytes.NewBuffer([]byte(requestBody)))
w := httptest.NewRecorder()
server.register(w, req)
2022-06-21 17:52:03 +02:00
expectStatusCode(t, w, tc.expectedStatusCode)
expectErrorString(t, w, 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() {
t.Fatalf("Expected valid RegisterRequest to successfully validate")
}
registerRequest = RegisterRequest{Email: "joe-example.com", Password: "aoeu"}
if registerRequest.validate() {
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"}
if registerRequest.validate() {
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"}
if registerRequest.validate() {
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"}
if registerRequest.validate() {
t.Fatalf("Expected RegisterRequest with missing password to not successfully validate")
}
}