PLT-5521: Mandatory version in line 1 of bulk import. (#5400)

Этот коммит содержится в:
George Goldberg
2017-02-16 14:49:01 +00:00
коммит произвёл Harrison Healey
родитель 821939e5da
Коммит d60d05f2d8
3 изменённых файлов: 66 добавлений и 3 удалений

Просмотреть файл

@@ -25,6 +25,7 @@ type LineImportData struct {
Team *TeamImportData `json:"team"`
Channel *ChannelImportData `json:"channel"`
User *UserImportData `json:"user"`
Version *int `json:"version"`
}
type TeamImportData struct {
@@ -101,7 +102,16 @@ func BulkImport(fileReader io.Reader, dryRun bool) (*model.AppError, int) {
if err := decoder.Decode(&line); err != nil {
return model.NewLocAppError("BulkImport", "app.import.bulk_import.json_decode.error", nil, err.Error()), lineNumber
} else {
if err := ImportLine(line, dryRun); err != nil {
if lineNumber == 1 {
importDataFileVersion, apperr := processImportDataFileVersionLine(line)
if apperr != nil {
return apperr, lineNumber
}
if importDataFileVersion != 1 {
return model.NewAppError("BulkImport", "app.import.bulk_import.unsupported_version.error", nil, "", http.StatusBadRequest), lineNumber
}
} else if err := ImportLine(line, dryRun); err != nil {
return err, lineNumber
}
}
@@ -114,6 +124,14 @@ func BulkImport(fileReader io.Reader, dryRun bool) (*model.AppError, int) {
return nil, 0
}
func processImportDataFileVersionLine(line LineImportData) (int, *model.AppError) {
if line.Type != "version" || line.Version == nil {
return -1, model.NewAppError("BulkImport", "app.import.process_import_data_file_version_line.invalid_version.error", nil, "", http.StatusBadRequest)
}
return *line.Version, nil
}
func ImportLine(line LineImportData, dryRun bool) *model.AppError {
switch {
case line.Type == "team":

Просмотреть файл

@@ -18,6 +18,10 @@ func ptrInt64(i int64) *int64 {
return &i
}
func ptrInt(i int) *int {
return &i
}
func ptrBool(b bool) *bool {
return &b
}
@@ -1367,7 +1371,8 @@ func TestImportBulkImport(t *testing.T) {
channelName := model.NewId()
// Run bulk import with a valid 1 of everything.
data1 := `{"type": "team", "team": {"type": "O", "display_name": "lskmw2d7a5ao7ppwqh5ljchvr4", "name": "` + teamName + `"}}
data1 := `{"type": "version", "version": 1}
{"type": "team", "team": {"type": "O", "display_name": "lskmw2d7a5ao7ppwqh5ljchvr4", "name": "` + teamName + `"}}
{"type": "channel", "channel": {"type": "O", "display_name": "xr6m6udffngark2uekvr3hoeny", "team": "` + teamName + `", "name": "` + channelName + `"}}
{"type": "user", "user": {"username": "kufjgnkxkrhhfgbrip6qxkfsaa", "email": "kufjgnkxkrhhfgbrip6qxkfsaa@example.com"}}
{"type": "user", "user": {"username": "bwshaim6qnc2ne7oqkd5b2s2rq", "email": "bwshaim6qnc2ne7oqkd5b2s2rq@example.com", "teams": [{"name": "` + teamName + `", "channels": [{"name": "` + channelName + `"}]}]}}`
@@ -1377,8 +1382,40 @@ func TestImportBulkImport(t *testing.T) {
}
// Run bulk import using a string that contains a line with invalid json.
data2 := `{"type": "team", "team": {"type": "O", "display_name": "lskmw2d7a5ao7ppwqh5ljchvr4", "name": "vinewy665jam3n6oxzhsdgajly"}`
data2 := `{"type": "version", "version": 1`
if err, line := BulkImport(strings.NewReader(data2), false); err == nil || line != 1 {
t.Fatalf("Should have failed due to invalid JSON on line 1.")
}
// Run bulk import using valid JSON but missing version line at the start.
data3:= `{"type": "team", "team": {"type": "O", "display_name": "lskmw2d7a5ao7ppwqh5ljchvr4", "name": "` + teamName + `"}}
{"type": "channel", "channel": {"type": "O", "display_name": "xr6m6udffngark2uekvr3hoeny", "team": "` + teamName + `", "name": "` + channelName + `"}}
{"type": "user", "user": {"username": "kufjgnkxkrhhfgbrip6qxkfsaa", "email": "kufjgnkxkrhhfgbrip6qxkfsaa@example.com"}}
{"type": "user", "user": {"username": "bwshaim6qnc2ne7oqkd5b2s2rq", "email": "bwshaim6qnc2ne7oqkd5b2s2rq@example.com", "teams": [{"name": "` + teamName + `", "channels": [{"name": "` + channelName + `"}]}]}}`
if err, line := BulkImport(strings.NewReader(data3), false); err == nil || line != 1 {
t.Fatalf("Should have failed due to missing version line on line 1.")
}
}
func TestImportProcessImportDataFileVersionLine(t *testing.T) {
_ = Setup()
data := LineImportData{
Type: "version",
Version: ptrInt(1),
}
if version, err := processImportDataFileVersionLine(data); err != nil || version != 1 {
t.Fatalf("Expected no error and version 1.")
}
data.Type = "NotVersion"
if _, err := processImportDataFileVersionLine(data); err == nil {
t.Fatalf("Expected error on invalid version line.")
}
data.Type = "version"
data.Version = nil
if _, err := processImportDataFileVersionLine(data); err == nil {
t.Fatalf("Expected error on invalid version line.")
}
}

Просмотреть файл

@@ -2979,6 +2979,14 @@
"id": "app.import.import_user.save_preferences.error",
"translation": "Failed to save the provided preferences for the user."
},
{
"id": "app.import.process_import_data_file_version_line.invalid_version.error",
"translation": "Did not find a valid version type object in the first line of the import data file."
},
{
"id": "app.import.bulk_import.unsupported_version.error",
"translation": "The version of the import data file format of the provided file is not supported by this version of Mattermost."
},
{
"id": "authentication.permissions.create_team_roles.description",
"translation": "Ability to create new teams"