From e0f847a381d887123213ad6f74570153d155eec5 Mon Sep 17 00:00:00 2001 From: Daniel Krol Date: Tue, 21 Jun 2022 19:46:10 -0400 Subject: [PATCH] Basic validation checks in error tests (just to make sure validation functions are called) --- server/auth_test.go | 16 +++++++++++++++- server/register_test.go | 15 ++++++++++++++- server/wallet_test.go | 6 +++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/server/auth_test.go b/server/auth_test.go index 0fe96c8..91b72fc 100644 --- a/server/auth_test.go +++ b/server/auth_test.go @@ -42,14 +42,26 @@ func TestServerAuthHandlerSuccess(t *testing.T) { func TestServerAuthHandlerErrors(t *testing.T) { tt := []struct { name string + email string expectedStatusCode int expectedErrorString string storeErrors TestStoreFunctionsErrors authFailGenToken bool }{ + { + name: "validation error", // missing email address + email: "", + expectedStatusCode: http.StatusBadRequest, + expectedErrorString: http.StatusText(http.StatusBadRequest) + ": Request failed validation", + + // 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. + }, { name: "login fail", + email: "abc@example.com", expectedStatusCode: http.StatusUnauthorized, expectedErrorString: http.StatusText(http.StatusUnauthorized) + ": No match for email and password", @@ -57,6 +69,7 @@ func TestServerAuthHandlerErrors(t *testing.T) { }, { name: "generate token fail", + email: "abc@example.com", expectedStatusCode: http.StatusInternalServerError, expectedErrorString: http.StatusText(http.StatusInternalServerError), @@ -64,6 +77,7 @@ func TestServerAuthHandlerErrors(t *testing.T) { }, { name: "save token fail", + email: "abc@example.com", expectedStatusCode: http.StatusInternalServerError, expectedErrorString: http.StatusText(http.StatusInternalServerError), @@ -83,7 +97,7 @@ func TestServerAuthHandlerErrors(t *testing.T) { // Make request // So long as the JSON is well-formed, the content doesn't matter here since the password check will be stubbed out - requestBody := `{"deviceId": "dev-1", "email": "abc@example.com", "password": "123"}` + requestBody := fmt.Sprintf(`{"deviceId": "dev-1", "email": "%s", "password": "123"}`, tc.email) req := httptest.NewRequest(http.MethodPost, PathAuthToken, bytes.NewBuffer([]byte(requestBody))) w := httptest.NewRecorder() diff --git a/server/register_test.go b/server/register_test.go index 9d9c402..2f7ea64 100644 --- a/server/register_test.go +++ b/server/register_test.go @@ -38,14 +38,26 @@ func TestServerRegisterSuccess(t *testing.T) { func TestServerRegisterErrors(t *testing.T) { tt := []struct { name string + email string requestBody string expectedStatusCode int expectedErrorString string storeErrors TestStoreFunctionsErrors }{ + { + name: "validation error", // missing email address + email: "", + expectedStatusCode: http.StatusBadRequest, + expectedErrorString: http.StatusText(http.StatusBadRequest) + ": Request failed validation", + + // 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. + }, { name: "existing account", + email: "abc@example.com", expectedStatusCode: http.StatusConflict, expectedErrorString: http.StatusText(http.StatusConflict) + ": Error registering", @@ -53,6 +65,7 @@ func TestServerRegisterErrors(t *testing.T) { }, { name: "unspecified account creation failure", + email: "abc@example.com", expectedStatusCode: http.StatusInternalServerError, expectedErrorString: http.StatusText(http.StatusInternalServerError), @@ -68,7 +81,7 @@ func TestServerRegisterErrors(t *testing.T) { server := Server{&testAuth, &testStore} // Make request - requestBody := `{ "email": "abc@example.com", "password": "123"}` + requestBody := fmt.Sprintf(`{"email": "%s", "password": "123"}`, tc.email) req := httptest.NewRequest(http.MethodPost, PathAuthToken, bytes.NewBuffer([]byte(requestBody))) w := httptest.NewRecorder() diff --git a/server/wallet_test.go b/server/wallet_test.go index 7de1037..6293e5f 100644 --- a/server/wallet_test.go +++ b/server/wallet_test.go @@ -30,10 +30,14 @@ func TestServerGetWallet(t *testing.T) { expectedStatusCode: http.StatusOK, }, { - name: "validation error", // mising auth token + name: "validation error", // missing auth token tokenString: auth.TokenString(""), expectedStatusCode: http.StatusBadRequest, expectedErrorString: http.StatusText(http.StatusBadRequest) + ": Missing token parameter", + + // Just check one validation error (missing auth token) to make sure the + // validate function is called. We'll check the rest of the validation + // errors in the other test below. }, { name: "auth error",