From a9ea3587bb040f4af8422b7c89508bf16e4a316a Mon Sep 17 00:00:00 2001 From: Martin Kraft Date: Thu, 9 Apr 2020 18:51:27 -0400 Subject: [PATCH] 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 --- app/import_functions_test.go | 1 + app/import_validators.go | 11 ++++++++++ app/import_validators_test.go | 38 +++++++++++++++++++++++++++++++++++ i18n/en.json | 4 ++++ 4 files changed, 54 insertions(+) diff --git a/app/import_functions_test.go b/app/import_functions_test.go index 60f02eaeb4..13ce82a55e 100644 --- a/app/import_functions_test.go +++ b/app/import_functions_test.go @@ -847,6 +847,7 @@ func TestImportImportUser(t *testing.T) { require.NotNil(t, err, "Should have failed to import invalid user.") data.AuthData = nil + data.AuthService = nil err = th.App.importUser(&data, false) require.Nil(t, err, "Should have succeeded to update valid user %v", err) diff --git a/app/import_validators.go b/app/import_validators.go index 2cbceb17d5..8d9c0c255b 100644 --- a/app/import_validators.go +++ b/app/import_validators.go @@ -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) } + 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 { return model.NewAppError("BulkImport", "app.import.validate_user_import_data.password_length.error", nil, "", http.StatusBadRequest) } diff --git a/app/import_validators_test.go b/app/import_validators_test.go index a79c9cdaf0..929a01875f 100644 --- a/app/import_validators_test.go +++ b/app/import_validators_test.go @@ -4,6 +4,7 @@ package app import ( + "fmt" "path/filepath" "strings" "testing" @@ -632,6 +633,43 @@ func TestImportValidateUserImportData(t *testing.T) { 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) { // Invalid Name. diff --git a/i18n/en.json b/i18n/en.json index 8c30fdc356..59ab9f59f9 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -3398,6 +3398,10 @@ "id": "app.import.validate_user_import_data.auth_data_and_password.error", "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", "translation": "User AuthData is too long."