MM-23244: Validate that AuthData and AuthService fields are mutually inclusive. (#14175)
* MM-23244: Validate that either both or neither AuthData and AuthService fields are set. * MM-23244: Readability improvement. * MM-23244: Adds translation. Tests for error id. * MM-23244: Fix test. Co-authored-by: mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
bfb380630c
Коммит
a9ea3587bb
@@ -847,6 +847,7 @@ func TestImportImportUser(t *testing.T) {
|
|||||||
require.NotNil(t, err, "Should have failed to import invalid user.")
|
require.NotNil(t, err, "Should have failed to import invalid user.")
|
||||||
|
|
||||||
data.AuthData = nil
|
data.AuthData = nil
|
||||||
|
data.AuthService = nil
|
||||||
err = th.App.importUser(&data, false)
|
err = th.App.importUser(&data, false)
|
||||||
require.Nil(t, err, "Should have succeeded to update valid user %v", err)
|
require.Nil(t, err, "Should have succeeded to update valid user %v", err)
|
||||||
|
|
||||||
|
|||||||
@@ -219,6 +219,17 @@ func validateUserImportData(data *UserImportData) *model.AppError {
|
|||||||
return model.NewAppError("BulkImport", "app.import.validate_user_import_data.auth_data_length.error", nil, "", http.StatusBadRequest)
|
return model.NewAppError("BulkImport", "app.import.validate_user_import_data.auth_data_length.error", nil, "", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
blank := func(str *string) bool {
|
||||||
|
if str == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return len(*str) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!blank(data.AuthService) && blank(data.AuthData)) || (blank(data.AuthService) && !blank(data.AuthData)) {
|
||||||
|
return model.NewAppError("BulkImport", "app.import.validate_user_import_data.auth_data_and_service_dependency.error", nil, "", http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
if data.Password != nil && len(*data.Password) == 0 {
|
if data.Password != nil && len(*data.Password) == 0 {
|
||||||
return model.NewAppError("BulkImport", "app.import.validate_user_import_data.password_length.error", nil, "", http.StatusBadRequest)
|
return model.NewAppError("BulkImport", "app.import.validate_user_import_data.password_length.error", nil, "", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -632,6 +633,43 @@ func TestImportValidateUserImportData(t *testing.T) {
|
|||||||
checkError(t, validateUserImportData(&data))
|
checkError(t, validateUserImportData(&data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestImportValidateUserAuth(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
authService *string
|
||||||
|
authData *string
|
||||||
|
isValid bool
|
||||||
|
}{
|
||||||
|
{nil, nil, true},
|
||||||
|
{ptrStr(""), ptrStr(""), true},
|
||||||
|
{ptrStr("foo"), ptrStr("foo"), true},
|
||||||
|
{nil, ptrStr(""), true},
|
||||||
|
{ptrStr(""), nil, true},
|
||||||
|
|
||||||
|
{ptrStr("foo"), nil, false},
|
||||||
|
{ptrStr("foo"), ptrStr(""), false},
|
||||||
|
{nil, ptrStr("foo"), false},
|
||||||
|
{ptrStr(""), ptrStr("foo"), false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
data := UserImportData{
|
||||||
|
Username: ptrStr("bob"),
|
||||||
|
Email: ptrStr("bob@example.com"),
|
||||||
|
AuthService: test.authService,
|
||||||
|
AuthData: test.authData,
|
||||||
|
}
|
||||||
|
err := validateUserImportData(&data)
|
||||||
|
|
||||||
|
if test.isValid {
|
||||||
|
require.Nil(t, err, fmt.Sprintf("authService: %v, authData: %v", test.authService, test.authData))
|
||||||
|
} else {
|
||||||
|
require.NotNil(t, err, fmt.Sprintf("authService: %v, authData: %v", test.authService, test.authData))
|
||||||
|
require.Equal(t, "app.import.validate_user_import_data.auth_data_and_service_dependency.error", err.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestImportValidateUserTeamsImportData(t *testing.T) {
|
func TestImportValidateUserTeamsImportData(t *testing.T) {
|
||||||
|
|
||||||
// Invalid Name.
|
// Invalid Name.
|
||||||
|
|||||||
@@ -3398,6 +3398,10 @@
|
|||||||
"id": "app.import.validate_user_import_data.auth_data_and_password.error",
|
"id": "app.import.validate_user_import_data.auth_data_and_password.error",
|
||||||
"translation": "User AuthData and Password are mutually exclusive."
|
"translation": "User AuthData and Password are mutually exclusive."
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"id": "app.import.validate_user_import_data.auth_data_and_service_dependency.error",
|
||||||
|
"translation": "User AuthService and AuthData are mutually inclusive."
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": "app.import.validate_user_import_data.auth_data_length.error",
|
"id": "app.import.validate_user_import_data.auth_data_length.error",
|
||||||
"translation": "User AuthData is too long."
|
"translation": "User AuthData is too long."
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user