diff --git a/pkg/webtests/integrations.go b/pkg/webtests/integrations.go index a733358ac..9187db6f1 100644 --- a/pkg/webtests/integrations.go +++ b/pkg/webtests/integrations.go @@ -217,6 +217,11 @@ func assertHandlerErrorCode(t *testing.T, err error, expectedErrorCode int) { t.FailNow() } +// httpCodeGetter is an interface for errors that can provide their HTTP status code. +type httpCodeGetter interface { + GetHTTPCode() int +} + // getHTTPErrorCode extracts the HTTP status code from various error types func getHTTPErrorCode(err error) int { // First, try domain errors that implement HTTPErrorProcessor @@ -224,6 +229,11 @@ func getHTTPErrorCode(err error) int { return httpErr.HTTPError().HTTPCode } + // Try errors that implement httpCodeGetter (like ValidationHTTPError) + if codeGetter, ok := err.(httpCodeGetter); ok { + return codeGetter.GetHTTPCode() + } + // Fall back to echo.HTTPError var httperr *echo.HTTPError if errors.As(err, &httperr) { diff --git a/pkg/webtests/user_change_password_test.go b/pkg/webtests/user_change_password_test.go index 6571f9ece..345e7758d 100644 --- a/pkg/webtests/user_change_password_test.go +++ b/pkg/webtests/user_change_password_test.go @@ -38,7 +38,7 @@ func TestUserChangePassword(t *testing.T) { }) t.Run("Wrong old password", func(t *testing.T) { _, err := newTestRequestWithUser(t, http.MethodPost, apiv1.UserChangePassword, &testuser1, `{ - "new_password": "12345", + "new_password": "12345678", "old_password": "invalid" }`, nil, nil) require.Error(t, err) @@ -46,7 +46,7 @@ func TestUserChangePassword(t *testing.T) { }) t.Run("Empty old password", func(t *testing.T) { _, err := newTestRequestWithUser(t, http.MethodPost, apiv1.UserChangePassword, &testuser1, `{ - "new_password": "12345", + "new_password": "12345678", "old_password": "" }`, nil, nil) require.Error(t, err) @@ -60,4 +60,12 @@ func TestUserChangePassword(t *testing.T) { require.Error(t, err) assertHandlerErrorCode(t, err, user.ErrCodeEmptyNewPassword) }) + t.Run("New password too short", func(t *testing.T) { + _, err := newTestRequestWithUser(t, http.MethodPost, apiv1.UserChangePassword, &testuser1, `{ + "new_password": "1234567", + "old_password": "12345678" +}`, nil, nil) + require.Error(t, err) + assert.Equal(t, http.StatusPreconditionFailed, getHTTPErrorCode(err)) + }) } diff --git a/pkg/webtests/user_password_reset_test.go b/pkg/webtests/user_password_reset_test.go index 68fcd6c72..3c4b413fb 100644 --- a/pkg/webtests/user_password_reset_test.go +++ b/pkg/webtests/user_password_reset_test.go @@ -57,4 +57,12 @@ func TestUserPasswordReset(t *testing.T) { require.Error(t, err) assertHandlerErrorCode(t, err, user.ErrCodeInvalidPasswordResetToken) }) + t.Run("Password too short", func(t *testing.T) { + _, err := newTestRequest(t, http.MethodPost, apiv1.UserResetPassword, `{ + "new_password": "1234567", + "token": "passwordresettesttoken" +}`, nil, nil) + require.Error(t, err) + assert.Equal(t, http.StatusPreconditionFailed, getHTTPErrorCode(err)) + }) }