summaryrefslogtreecommitdiffstats
path: root/api/file_test.go
diff options
context:
space:
mode:
authorJoramWilander <jwawilander@gmail.com>2015-07-16 12:50:38 -0400
committerJoramWilander <jwawilander@gmail.com>2015-07-21 19:22:04 -0400
commita6fc129a01bf760aa163c8f842a3f2b67b375e3e (patch)
treeff73611404b577ba3658f1836cd202ab686d8ed4 /api/file_test.go
parentada84835eec1d69a962769afb590088d2f5a7d0a (diff)
downloadchat-a6fc129a01bf760aa163c8f842a3f2b67b375e3e.tar.gz
chat-a6fc129a01bf760aa163c8f842a3f2b67b375e3e.tar.bz2
chat-a6fc129a01bf760aa163c8f842a3f2b67b375e3e.zip
update file unit tests
Diffstat (limited to 'api/file_test.go')
-rw-r--r--api/file_test.go173
1 files changed, 122 insertions, 51 deletions
diff --git a/api/file_test.go b/api/file_test.go
index 79ee03c77..d5817234d 100644
--- a/api/file_test.go
+++ b/api/file_test.go
@@ -68,12 +68,14 @@ func TestUploadFile(t *testing.T) {
}
resp, appErr := Client.UploadFile("/files/upload", body.Bytes(), writer.FormDataContentType())
- if utils.IsS3Configured() {
+ if utils.IsS3Configured() && !utils.Cfg.ServiceSettings.UseLocalStorage {
if appErr != nil {
t.Fatal(appErr)
}
- filenames := resp.Data.(*model.FileUploadResponse).Filenames
+ filenames := strings.Split(resp.Data.(*model.FileUploadResponse).Filenames[0], "/")
+ filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
+ fileId := strings.Split(filename, ".")[0]
var auth aws.Auth
auth.AccessKey = utils.Cfg.AWSSettings.S3AccessKeyId
@@ -82,12 +84,10 @@ func TestUploadFile(t *testing.T) {
s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region])
bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
- fileId := strings.Split(filenames[0], ".")[0]
-
// wait a bit for files to ready
time.Sleep(5 * time.Second)
- err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + filenames[0])
+ err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + filename)
if err != nil {
t.Fatal(err)
}
@@ -97,13 +97,35 @@ func TestUploadFile(t *testing.T) {
t.Fatal(err)
}
- err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_preview.png")
+ err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_preview.jpg")
if err != nil {
t.Fatal(err)
}
+ } else if utils.Cfg.ServiceSettings.UseLocalStorage && len(utils.Cfg.ServiceSettings.StorageDirectory) > 0 {
+ filenames := strings.Split(resp.Data.(*model.FileUploadResponse).Filenames[0], "/")
+ filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
+ fileId := strings.Split(filename, ".")[0]
+
+ // wait a bit for files to ready
+ time.Sleep(5 * time.Second)
+
+ path := utils.Cfg.ServiceSettings.StorageDirectory + "teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + filename
+ if err := os.Remove(path); err != nil {
+ t.Fatal("Couldn't remove file at " + path)
+ }
+
+ path = utils.Cfg.ServiceSettings.StorageDirectory + "teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_thumb.jpg"
+ if err := os.Remove(path); err != nil {
+ t.Fatal("Couldn't remove file at " + path)
+ }
+
+ path = utils.Cfg.ServiceSettings.StorageDirectory + "teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_preview.jpg"
+ if err := os.Remove(path); err != nil {
+ t.Fatal("Couldn't remove file at " + path)
+ }
} else {
if appErr == nil {
- t.Fatal("S3 not configured, should have failed")
+ t.Fatal("S3 and local storage not configured, should have failed")
}
}
}
@@ -123,7 +145,7 @@ func TestGetFile(t *testing.T) {
channel1 := &model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
- if utils.IsS3Configured() {
+ if utils.IsS3Configured() || utils.Cfg.ServiceSettings.UseLocalStorage {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
@@ -225,28 +247,51 @@ func TestGetFile(t *testing.T) {
t.Fatal("Should have errored - user not logged in and link not public")
}
- var auth aws.Auth
- auth.AccessKey = utils.Cfg.AWSSettings.S3AccessKeyId
- auth.SecretKey = utils.Cfg.AWSSettings.S3SecretAccessKey
-
- s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region])
- bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
-
- fileId := strings.Split(filenames[0], ".")[0]
-
- err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + filenames[0])
- if err != nil {
- t.Fatal(err)
- }
-
- err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_thumb.jpg")
- if err != nil {
- t.Fatal(err)
- }
-
- err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_preview.png")
- if err != nil {
- t.Fatal(err)
+ if utils.IsS3Configured() && !utils.Cfg.ServiceSettings.UseLocalStorage {
+ var auth aws.Auth
+ auth.AccessKey = utils.Cfg.AWSSettings.S3AccessKeyId
+ auth.SecretKey = utils.Cfg.AWSSettings.S3SecretAccessKey
+
+ s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region])
+ bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
+
+ filenames := strings.Split(resp.Data.(*model.FileUploadResponse).Filenames[0], "/")
+ filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
+ fileId := strings.Split(filename, ".")[0]
+
+ err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + filename)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_thumb.jpg")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_preview.jpg")
+ if err != nil {
+ t.Fatal(err)
+ }
+ } else {
+ filenames := strings.Split(resp.Data.(*model.FileUploadResponse).Filenames[0], "/")
+ filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
+ fileId := strings.Split(filename, ".")[0]
+
+ path := utils.Cfg.ServiceSettings.StorageDirectory + "teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + filename
+ if err := os.Remove(path); err != nil {
+ t.Fatal("Couldn't remove file at " + path)
+ }
+
+ path = utils.Cfg.ServiceSettings.StorageDirectory + "teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_thumb.jpg"
+ if err := os.Remove(path); err != nil {
+ t.Fatal("Couldn't remove file at " + path)
+ }
+
+ path = utils.Cfg.ServiceSettings.StorageDirectory + "teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_preview.jpg"
+ if err := os.Remove(path); err != nil {
+ t.Fatal("Couldn't remove file at " + path)
+ }
}
} else {
if _, downErr := Client.GetFile("/files/get/yxebdmbz5pgupx7q6ez88rw11a/n3btzxu9hbnapqk36iwaxkjxhc/junk.jpg", false); downErr.StatusCode != http.StatusNotImplemented {
@@ -274,7 +319,7 @@ func TestGetPublicLink(t *testing.T) {
channel1 := &model.Channel{DisplayName: "Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
- if utils.IsS3Configured() {
+ if utils.IsS3Configured() || utils.Cfg.ServiceSettings.UseLocalStorage {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
@@ -350,26 +395,52 @@ func TestGetPublicLink(t *testing.T) {
t.Fatal("should have errored, user not member of channel")
}
- // perform clean-up on s3
- var auth aws.Auth
- auth.AccessKey = utils.Cfg.AWSSettings.S3AccessKeyId
- auth.SecretKey = utils.Cfg.AWSSettings.S3SecretAccessKey
-
- s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region])
- bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
-
- fileId := strings.Split(filenames[0], ".")[0]
-
- if err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + rpost1.Data.(*model.Post).UserId + "/" + filenames[0]); err != nil {
- t.Fatal(err)
- }
-
- if err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + rpost1.Data.(*model.Post).UserId + "/" + fileId + "_thumb.jpg"); err != nil {
- t.Fatal(err)
- }
-
- if err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + rpost1.Data.(*model.Post).UserId + "/" + fileId + "_preview.png"); err != nil {
- t.Fatal(err)
+ if utils.IsS3Configured() && !utils.Cfg.ServiceSettings.UseLocalStorage {
+ // perform clean-up on s3
+ var auth aws.Auth
+ auth.AccessKey = utils.Cfg.AWSSettings.S3AccessKeyId
+ auth.SecretKey = utils.Cfg.AWSSettings.S3SecretAccessKey
+
+ s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region])
+ bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
+
+ filenames := strings.Split(resp.Data.(*model.FileUploadResponse).Filenames[0], "/")
+ filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
+ fileId := strings.Split(filename, ".")[0]
+
+ err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + filename)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_thumb.jpg")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_preview.jpg")
+ if err != nil {
+ t.Fatal(err)
+ }
+ } else {
+ filenames := strings.Split(resp.Data.(*model.FileUploadResponse).Filenames[0], "/")
+ filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
+ fileId := strings.Split(filename, ".")[0]
+
+ path := utils.Cfg.ServiceSettings.StorageDirectory + "teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + filename
+ if err := os.Remove(path); err != nil {
+ t.Fatal("Couldn't remove file at " + path)
+ }
+
+ path = utils.Cfg.ServiceSettings.StorageDirectory + "teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_thumb.jpg"
+ if err := os.Remove(path); err != nil {
+ t.Fatal("Couldn't remove file at " + path)
+ }
+
+ path = utils.Cfg.ServiceSettings.StorageDirectory + "teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_preview.jpg"
+ if err := os.Remove(path); err != nil {
+ t.Fatal("Couldn't remove file at " + path)
+ }
}
} else {
data := make(map[string]string)