added onlyVerified auth collection option

This commit is contained in:
Gani Georgiev
2023-12-06 11:57:04 +02:00
parent 865865fdeb
commit 31317df21c
43 changed files with 591 additions and 446 deletions
+1
View File
@@ -179,6 +179,7 @@ type CollectionAuthOptions struct {
AllowEmailAuth bool `form:"allowEmailAuth" json:"allowEmailAuth"`
RequireEmail bool `form:"requireEmail" json:"requireEmail"`
ExceptEmailDomains []string `form:"exceptEmailDomains" json:"exceptEmailDomains"`
OnlyVerified bool `form:"onlyVerified" json:"onlyVerified"`
OnlyEmailDomains []string `form:"onlyEmailDomains" json:"onlyEmailDomains"`
MinPasswordLength int `form:"minPasswordLength" json:"minPasswordLength"`
}
+103 -93
View File
@@ -89,20 +89,22 @@ func TestCollectionMarshalJSON(t *testing.T) {
},
{
"auth type + non empty options",
models.Collection{BaseModel: models.BaseModel{Id: "test"}, Type: models.CollectionTypeAuth, Options: types.JsonMap{"test": 123, "allowOAuth2Auth": true, "minPasswordLength": 4}},
`{"id":"test","created":"","updated":"","name":"","type":"auth","system":false,"schema":[],"indexes":[],"listRule":null,"viewRule":null,"createRule":null,"updateRule":null,"deleteRule":null,"options":{"allowEmailAuth":false,"allowOAuth2Auth":true,"allowUsernameAuth":false,"exceptEmailDomains":null,"manageRule":null,"minPasswordLength":4,"onlyEmailDomains":null,"requireEmail":false}}`,
models.Collection{BaseModel: models.BaseModel{Id: "test"}, Type: models.CollectionTypeAuth, Options: types.JsonMap{"test": 123, "allowOAuth2Auth": true, "minPasswordLength": 4, "onlyVerified": true}},
`{"id":"test","created":"","updated":"","name":"","type":"auth","system":false,"schema":[],"indexes":[],"listRule":null,"viewRule":null,"createRule":null,"updateRule":null,"deleteRule":null,"options":{"allowEmailAuth":false,"allowOAuth2Auth":true,"allowUsernameAuth":false,"exceptEmailDomains":null,"manageRule":null,"minPasswordLength":4,"onlyEmailDomains":null,"onlyVerified":true,"requireEmail":false}}`,
},
}
for _, s := range scenarios {
result, err := s.collection.MarshalJSON()
if err != nil {
t.Errorf("[%s] Unexpected error %v", s.name, err)
continue
}
if string(result) != s.expected {
t.Errorf("[%s] Expected\n%v \ngot \n%v", s.name, s.expected, string(result))
}
t.Run(s.name, func(t *testing.T) {
result, err := s.collection.MarshalJSON()
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
if string(result) != s.expected {
t.Fatalf("Expected\n%v\ngot\n%v", s.expected, string(result))
}
})
}
}
@@ -135,22 +137,24 @@ func TestCollectionBaseOptions(t *testing.T) {
}
for _, s := range scenarios {
result := s.collection.BaseOptions()
t.Run(s.name, func(t *testing.T) {
result := s.collection.BaseOptions()
encoded, err := json.Marshal(result)
if err != nil {
t.Fatal(err)
}
encoded, err := json.Marshal(result)
if err != nil {
t.Fatal(err)
}
if strEncoded := string(encoded); strEncoded != s.expected {
t.Errorf("[%s] Expected \n%v \ngot \n%v", s.name, s.expected, strEncoded)
}
if strEncoded := string(encoded); strEncoded != s.expected {
t.Fatalf("Expected \n%v \ngot \n%v", s.expected, strEncoded)
}
})
}
}
func TestCollectionAuthOptions(t *testing.T) {
options := types.JsonMap{"test": 123, "minPasswordLength": 4}
expectedSerialization := `{"manageRule":null,"allowOAuth2Auth":false,"allowUsernameAuth":false,"allowEmailAuth":false,"requireEmail":false,"exceptEmailDomains":null,"onlyEmailDomains":null,"minPasswordLength":4}`
expectedSerialization := `{"manageRule":null,"allowOAuth2Auth":false,"allowUsernameAuth":false,"allowEmailAuth":false,"requireEmail":false,"exceptEmailDomains":null,"onlyVerified":false,"onlyEmailDomains":null,"minPasswordLength":4}`
scenarios := []struct {
name string
@@ -180,16 +184,18 @@ func TestCollectionAuthOptions(t *testing.T) {
}
for _, s := range scenarios {
result := s.collection.AuthOptions()
t.Run(s.name, func(t *testing.T) {
result := s.collection.AuthOptions()
encoded, err := json.Marshal(result)
if err != nil {
t.Fatal(err)
}
encoded, err := json.Marshal(result)
if err != nil {
t.Fatal(err)
}
if strEncoded := string(encoded); strEncoded != s.expected {
t.Errorf("[%s] Expected \n%v \ngot \n%v", s.name, s.expected, strEncoded)
}
if strEncoded := string(encoded); strEncoded != s.expected {
t.Fatalf("Expected \n%v \ngot \n%v", s.expected, strEncoded)
}
})
}
}
@@ -225,16 +231,18 @@ func TestCollectionViewOptions(t *testing.T) {
}
for _, s := range scenarios {
result := s.collection.ViewOptions()
t.Run(s.name, func(t *testing.T) {
result := s.collection.ViewOptions()
encoded, err := json.Marshal(result)
if err != nil {
t.Fatal(err)
}
encoded, err := json.Marshal(result)
if err != nil {
t.Fatal(err)
}
if strEncoded := string(encoded); strEncoded != s.expected {
t.Errorf("[%s] Expected \n%v \ngot \n%v", s.name, s.expected, strEncoded)
}
if strEncoded := string(encoded); strEncoded != s.expected {
t.Fatalf("Expected \n%v \ngot \n%v", s.expected, strEncoded)
}
})
}
}
@@ -257,24 +265,25 @@ func TestNormalizeOptions(t *testing.T) {
{
"auth type",
models.Collection{Type: models.CollectionTypeAuth, Options: types.JsonMap{"test": 123, "minPasswordLength": 4}},
`{"allowEmailAuth":false,"allowOAuth2Auth":false,"allowUsernameAuth":false,"exceptEmailDomains":null,"manageRule":null,"minPasswordLength":4,"onlyEmailDomains":null,"requireEmail":false}`,
`{"allowEmailAuth":false,"allowOAuth2Auth":false,"allowUsernameAuth":false,"exceptEmailDomains":null,"manageRule":null,"minPasswordLength":4,"onlyEmailDomains":null,"onlyVerified":false,"requireEmail":false}`,
},
}
for _, s := range scenarios {
if err := s.collection.NormalizeOptions(); err != nil {
t.Errorf("[%s] Unexpected error %v", s.name, err)
continue
}
t.Run(s.name, func(t *testing.T) {
if err := s.collection.NormalizeOptions(); err != nil {
t.Fatalf("Unexpected error %v", err)
}
encoded, err := json.Marshal(s.collection.Options)
if err != nil {
t.Fatal(err)
}
encoded, err := json.Marshal(s.collection.Options)
if err != nil {
t.Fatal(err)
}
if strEncoded := string(encoded); strEncoded != s.expected {
t.Errorf("[%s] Expected \n%v \ngot \n%v", s.name, s.expected, strEncoded)
}
if strEncoded := string(encoded); strEncoded != s.expected {
t.Fatalf("Expected \n%v \ngot \n%v", s.expected, strEncoded)
}
})
}
}
@@ -325,24 +334,25 @@ func TestSetOptions(t *testing.T) {
"auth type",
models.Collection{Type: models.CollectionTypeAuth, Options: types.JsonMap{"test": 123}},
map[string]any{"test": 456, "minPasswordLength": 4},
`{"allowEmailAuth":false,"allowOAuth2Auth":false,"allowUsernameAuth":false,"exceptEmailDomains":null,"manageRule":null,"minPasswordLength":4,"onlyEmailDomains":null,"requireEmail":false}`,
`{"allowEmailAuth":false,"allowOAuth2Auth":false,"allowUsernameAuth":false,"exceptEmailDomains":null,"manageRule":null,"minPasswordLength":4,"onlyEmailDomains":null,"onlyVerified":false,"requireEmail":false}`,
},
}
for _, s := range scenarios {
if err := s.collection.SetOptions(s.options); err != nil {
t.Errorf("[%s] Unexpected error %v", s.name, err)
continue
}
t.Run(s.name, func(t *testing.T) {
if err := s.collection.SetOptions(s.options); err != nil {
t.Fatalf("Unexpected error %v", err)
}
encoded, err := json.Marshal(s.collection.Options)
if err != nil {
t.Fatal(err)
}
encoded, err := json.Marshal(s.collection.Options)
if err != nil {
t.Fatal(err)
}
if strEncoded := string(encoded); strEncoded != s.expected {
t.Errorf("[%s] Expected \n%v \ngot \n%v", s.name, s.expected, strEncoded)
}
if strEncoded := string(encoded); strEncoded != s.expected {
t.Fatalf("Expected\n%v\ngot\n%v", s.expected, strEncoded)
}
})
}
}
@@ -418,25 +428,25 @@ func TestCollectionAuthOptionsValidate(t *testing.T) {
}
for _, s := range scenarios {
result := s.options.Validate()
t.Run(s.name, func(t *testing.T) {
result := s.options.Validate()
// parse errors
errs, ok := result.(validation.Errors)
if !ok && result != nil {
t.Errorf("[%s] Failed to parse errors %v", s.name, result)
continue
}
if len(errs) != len(s.expectedErrors) {
t.Errorf("[%s] Expected error keys %v, got errors \n%v", s.name, s.expectedErrors, result)
continue
}
for key := range errs {
if !list.ExistInSlice(key, s.expectedErrors) {
t.Errorf("[%s] Unexpected error key %q in \n%v", s.name, key, errs)
// parse errors
errs, ok := result.(validation.Errors)
if !ok && result != nil {
t.Fatalf("Failed to parse errors %v", result)
}
}
if len(errs) != len(s.expectedErrors) {
t.Fatalf("Expected error keys %v, got errors \n%v", s.expectedErrors, result)
}
for key := range errs {
if !list.ExistInSlice(key, s.expectedErrors) {
t.Fatalf("Unexpected error key %q in \n%v", key, errs)
}
}
})
}
}
@@ -461,24 +471,24 @@ func TestCollectionViewOptionsValidate(t *testing.T) {
}
for _, s := range scenarios {
result := s.options.Validate()
t.Run(s.name, func(t *testing.T) {
result := s.options.Validate()
// parse errors
errs, ok := result.(validation.Errors)
if !ok && result != nil {
t.Errorf("[%s] Failed to parse errors %v", s.name, result)
continue
}
if len(errs) != len(s.expectedErrors) {
t.Errorf("[%s] Expected error keys %v, got errors \n%v", s.name, s.expectedErrors, result)
continue
}
for key := range errs {
if !list.ExistInSlice(key, s.expectedErrors) {
t.Errorf("[%s] Unexpected error key %q in \n%v", s.name, key, errs)
// parse errors
errs, ok := result.(validation.Errors)
if !ok && result != nil {
t.Fatalf("Failed to parse errors %v", result)
}
}
if len(errs) != len(s.expectedErrors) {
t.Fatalf("Expected error keys %v, got errors \n%v", s.expectedErrors, result)
}
for key := range errs {
if !list.ExistInSlice(key, s.expectedErrors) {
t.Fatalf("Unexpected error key %q in \n%v", key, errs)
}
}
})
}
}