Basic validation checks in error tests (just to make sure validation functions are called)

This commit is contained in:
Daniel Krol 2022-06-21 19:46:10 -04:00
parent 39015963e6
commit e0f847a381
3 changed files with 34 additions and 3 deletions

View file

@ -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()

View file

@ -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()

View file

@ -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",