summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorGeorge Goldberg <george@gberg.me>2017-02-16 14:49:01 +0000
committerHarrison Healey <harrisonmhealey@gmail.com>2017-02-16 09:49:01 -0500
commitd60d05f2d86400faf3303e655c46c5b8d77879c4 (patch)
tree8907bdf20c38ab06c066c0946126157462b34981 /app
parent821939e5dab144ac14b4f288cb8d0c9e3bb570d0 (diff)
downloadchat-d60d05f2d86400faf3303e655c46c5b8d77879c4.tar.gz
chat-d60d05f2d86400faf3303e655c46c5b8d77879c4.tar.bz2
chat-d60d05f2d86400faf3303e655c46c5b8d77879c4.zip
PLT-5521: Mandatory version in line 1 of bulk import. (#5400)
Diffstat (limited to 'app')
-rw-r--r--app/import.go20
-rw-r--r--app/import_test.go41
2 files changed, 58 insertions, 3 deletions
diff --git a/app/import.go b/app/import.go
index 1241048d3..10a915b2a 100644
--- a/app/import.go
+++ b/app/import.go
@@ -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":
diff --git a/app/import_test.go b/app/import_test.go
index 008267942..ea71d0e0c 100644
--- a/app/import_test.go
+++ b/app/import_test.go
@@ -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.")
+ }
}