forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backported some of the v0.23.0 form validators, fixes and tests
- Loading branch information
1 parent
e92d3cc
commit cbc88d7
Showing
5 changed files
with
98 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,86 @@ | ||
package forms_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
validation "github.com/go-ozzo/ozzo-validation/v4" | ||
"github.com/pocketbase/pocketbase/forms" | ||
) | ||
|
||
func TestRealtimeSubscribeValidate(t *testing.T) { | ||
t.Parallel() | ||
|
||
validSubscriptionsLimit := make([]string, 1000) | ||
for i := 0; i < len(validSubscriptionsLimit); i++ { | ||
validSubscriptionsLimit[i] = fmt.Sprintf(`"%d"`, i) | ||
} | ||
invalidSubscriptionsLimit := make([]string, 1001) | ||
for i := 0; i < len(invalidSubscriptionsLimit); i++ { | ||
invalidSubscriptionsLimit[i] = fmt.Sprintf(`"%d"`, i) | ||
} | ||
|
||
scenarios := []struct { | ||
clientId string | ||
expectError bool | ||
name string | ||
data string | ||
expectedErrors []string | ||
}{ | ||
{"", true}, | ||
{strings.Repeat("a", 256), true}, | ||
{"test", false}, | ||
{ | ||
"empty data", | ||
`{}`, | ||
[]string{"clientId"}, | ||
}, | ||
{ | ||
"clientId > max chars limit", | ||
`{"clientId":"` + strings.Repeat("a", 256) + `"}`, | ||
[]string{"clientId"}, | ||
}, | ||
{ | ||
"clientId <= max chars limit", | ||
`{"clientId":"` + strings.Repeat("a", 255) + `"}`, | ||
[]string{}, | ||
}, | ||
{ | ||
"subscriptions > max limit", | ||
`{"clientId":"test", "subscriptions":[` + strings.Join(invalidSubscriptionsLimit, ",") + `]}`, | ||
[]string{"subscriptions"}, | ||
}, | ||
{ | ||
"subscriptions <= max limit", | ||
`{"clientId":"test", "subscriptions":[` + strings.Join(validSubscriptionsLimit, ",") + `]}`, | ||
[]string{}, | ||
}, | ||
} | ||
|
||
for i, s := range scenarios { | ||
form := forms.NewRealtimeSubscribe() | ||
form.ClientId = s.clientId | ||
for _, s := range scenarios { | ||
t.Run(s.name, func(t *testing.T) { | ||
form := forms.NewRealtimeSubscribe() | ||
|
||
err := json.Unmarshal([]byte(s.data), &form) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
result := form.Validate() | ||
|
||
err := form.Validate() | ||
// parse errors | ||
errs, ok := result.(validation.Errors) | ||
if !ok && result != nil { | ||
t.Fatalf("Failed to parse errors %v", result) | ||
return | ||
} | ||
|
||
hasErr := err != nil | ||
if hasErr != s.expectError { | ||
t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, s.expectError, hasErr, err) | ||
} | ||
// check errors | ||
if len(errs) > len(s.expectedErrors) { | ||
t.Fatalf("Expected error keys %v, got %v", s.expectedErrors, errs) | ||
} | ||
for _, k := range s.expectedErrors { | ||
if _, ok := errs[k]; !ok { | ||
t.Fatalf("Missing expected error key %q in %v", k, errs) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters