diff options
author | George Goldberg <george@gberg.me> | 2017-08-25 15:38:13 +0100 |
---|---|---|
committer | Harrison Healey <harrisonmhealey@gmail.com> | 2017-08-25 10:38:13 -0400 |
commit | 50fc6e1e9e8d286fd6a406cef75e48a28f9427ad (patch) | |
tree | 0689de640729194b9a123ec8bfef36cbecd8cefd /utils/file_test.go | |
parent | 99acf6106833a2186c0f7e07985feac5d9c25de1 (diff) | |
download | chat-50fc6e1e9e8d286fd6a406cef75e48a28f9427ad.tar.gz chat-50fc6e1e9e8d286fd6a406cef75e48a28f9427ad.tar.bz2 chat-50fc6e1e9e8d286fd6a406cef75e48a28f9427ad.zip |
PLT-???? Prepare file upload infrastructure for Data Retention. (#7266)
* Prepare file upload infrastructure for Data Retention.
This commit prepares the file upload infrastructure for the data
retention feature that is under construction. Changes are:
* Move file management code to utils to allow access to it from jobs.
* From now on, store all file uploads in a top level folder which is the
date of the day on which they were uploaded.
This commit is based on Harrison Healey's branch, but updated to work
with the latest master.
* Use NewAppError
Diffstat (limited to 'utils/file_test.go')
-rw-r--r-- | utils/file_test.go | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/utils/file_test.go b/utils/file_test.go new file mode 100644 index 000000000..202a5354e --- /dev/null +++ b/utils/file_test.go @@ -0,0 +1,122 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package utils + +import ( + "testing" + + "github.com/mattermost/platform/model" +) + +func TestReadWriteFile(t *testing.T) { + TranslationsPreInit() + LoadConfig("config.json") + InitTranslations(Cfg.LocalizationSettings) + + b := []byte("test") + path := "tests/" + model.NewId() + + if err := WriteFile(b, path); err != nil { + t.Fatal(err) + } + defer RemoveFile(path) + + if read, err := ReadFile(path); err != nil { + t.Fatal(err) + } else if readString := string(read); readString != "test" { + t.Fatal("should've read back contents of file") + } +} + +func TestMoveFile(t *testing.T) { + TranslationsPreInit() + LoadConfig("config.json") + InitTranslations(Cfg.LocalizationSettings) + + b := []byte("test") + path1 := "tests/" + model.NewId() + path2 := "tests/" + model.NewId() + + if err := WriteFile(b, path1); err != nil { + t.Fatal(err) + } + defer RemoveFile(path1) + + if err := MoveFile(path1, path2); err != nil { + t.Fatal(err) + } + defer RemoveFile(path2) + + if _, err := ReadFile(path1); err == nil { + t.Fatal("file should no longer exist at old path") + } + + if _, err := ReadFile(path2); err != nil { + t.Fatal("file should exist at new path", err) + } +} + +func TestRemoveFile(t *testing.T) { + TranslationsPreInit() + LoadConfig("config.json") + InitTranslations(Cfg.LocalizationSettings) + + b := []byte("test") + path := "tests/" + model.NewId() + + if err := WriteFile(b, path); err != nil { + t.Fatal(err) + } + + if err := RemoveFile(path); err != nil { + t.Fatal(err) + } + + if _, err := ReadFile(path); err == nil { + t.Fatal("should've removed file") + } + + if err := WriteFile(b, "tests2/foo"); err != nil { + t.Fatal(err) + } + if err := WriteFile(b, "tests2/bar"); err != nil { + t.Fatal(err) + } + if err := WriteFile(b, "tests2/asdf"); err != nil { + t.Fatal(err) + } + if err := RemoveDirectory("tests2"); err != nil { + t.Fatal(err) + } +} + +func TestRemoveDirectory(t *testing.T) { + TranslationsPreInit() + LoadConfig("config.json") + InitTranslations(Cfg.LocalizationSettings) + + b := []byte("test") + + if err := WriteFile(b, "tests2/foo"); err != nil { + t.Fatal(err) + } + if err := WriteFile(b, "tests2/bar"); err != nil { + t.Fatal(err) + } + if err := WriteFile(b, "tests2/aaa"); err != nil { + t.Fatal(err) + } + + if err := RemoveDirectory("tests2"); err != nil { + t.Fatal(err) + } + + if _, err := ReadFile("tests2/foo"); err == nil { + t.Fatal("should've removed file") + } else if _, err := ReadFile("tests2/bar"); err == nil { + t.Fatal("should've removed file") + } else if _, err := ReadFile("tests2/asdf"); err == nil { + t.Fatal("should've removed file") + } +} |