update file unit tests
Этот коммит содержится в:
10
api/file.go
10
api/file.go
@@ -150,9 +150,8 @@ func fireAndForgetHandleImages(filenames []string, fileData [][]byte, teamId, ch
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = writeFile(buf.Bytes(), dest+name+"_thumb.jpg")
|
if err := writeFile(buf.Bytes(), dest+name+"_thumb.jpg"); err != nil {
|
||||||
if err != nil {
|
l4g.Error("Unable to upload thumbnail channelId=%v userId=%v filename=%v err=%v", channelId, userId, filename, err)
|
||||||
l4g.Error("Unable to upload thumbnail to S3 channelId=%v userId=%v filename=%v err=%v", channelId, userId, filename, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -174,9 +173,8 @@ func fireAndForgetHandleImages(filenames []string, fileData [][]byte, teamId, ch
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = writeFile(buf.Bytes(), dest+name+"_preview.jpg")
|
if err := writeFile(buf.Bytes(), dest+name+"_preview.jpg"); err != nil {
|
||||||
if err != nil {
|
l4g.Error("Unable to upload preview channelId=%v userId=%v filename=%v err=%v", channelId, userId, filename, err)
|
||||||
l4g.Error("Unable to upload preview to S3 channelId=%v userId=%v filename=%v err=%v", channelId, userId, filename, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
153
api/file_test.go
153
api/file_test.go
@@ -68,12 +68,14 @@ func TestUploadFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resp, appErr := Client.UploadFile("/files/upload", body.Bytes(), writer.FormDataContentType())
|
resp, appErr := Client.UploadFile("/files/upload", body.Bytes(), writer.FormDataContentType())
|
||||||
if utils.IsS3Configured() {
|
if utils.IsS3Configured() && !utils.Cfg.ServiceSettings.UseLocalStorage {
|
||||||
if appErr != nil {
|
if appErr != nil {
|
||||||
t.Fatal(appErr)
|
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
|
var auth aws.Auth
|
||||||
auth.AccessKey = utils.Cfg.AWSSettings.S3AccessKeyId
|
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])
|
s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region])
|
||||||
bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
|
bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
|
||||||
|
|
||||||
fileId := strings.Split(filenames[0], ".")[0]
|
|
||||||
|
|
||||||
// wait a bit for files to ready
|
// wait a bit for files to ready
|
||||||
time.Sleep(5 * time.Second)
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -97,13 +97,35 @@ func TestUploadFile(t *testing.T) {
|
|||||||
t.Fatal(err)
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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 {
|
} else {
|
||||||
if appErr == nil {
|
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 := &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)
|
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
|
||||||
|
|
||||||
if utils.IsS3Configured() {
|
if utils.IsS3Configured() || utils.Cfg.ServiceSettings.UseLocalStorage {
|
||||||
|
|
||||||
body := &bytes.Buffer{}
|
body := &bytes.Buffer{}
|
||||||
writer := multipart.NewWriter(body)
|
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")
|
t.Fatal("Should have errored - user not logged in and link not public")
|
||||||
}
|
}
|
||||||
|
|
||||||
var auth aws.Auth
|
if utils.IsS3Configured() && !utils.Cfg.ServiceSettings.UseLocalStorage {
|
||||||
auth.AccessKey = utils.Cfg.AWSSettings.S3AccessKeyId
|
var auth aws.Auth
|
||||||
auth.SecretKey = utils.Cfg.AWSSettings.S3SecretAccessKey
|
auth.AccessKey = utils.Cfg.AWSSettings.S3AccessKeyId
|
||||||
|
auth.SecretKey = utils.Cfg.AWSSettings.S3SecretAccessKey
|
||||||
|
|
||||||
s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region])
|
s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region])
|
||||||
bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
|
bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
|
||||||
|
|
||||||
fileId := strings.Split(filenames[0], ".")[0]
|
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 + "/" + filenames[0])
|
err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_thumb.jpg")
|
err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_thumb.jpg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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 {
|
} else {
|
||||||
if _, downErr := Client.GetFile("/files/get/yxebdmbz5pgupx7q6ez88rw11a/n3btzxu9hbnapqk36iwaxkjxhc/junk.jpg", false); downErr.StatusCode != http.StatusNotImplemented {
|
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 := &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)
|
channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
|
||||||
|
|
||||||
if utils.IsS3Configured() {
|
if utils.IsS3Configured() || utils.Cfg.ServiceSettings.UseLocalStorage {
|
||||||
|
|
||||||
body := &bytes.Buffer{}
|
body := &bytes.Buffer{}
|
||||||
writer := multipart.NewWriter(body)
|
writer := multipart.NewWriter(body)
|
||||||
@@ -350,26 +395,52 @@ func TestGetPublicLink(t *testing.T) {
|
|||||||
t.Fatal("should have errored, user not member of channel")
|
t.Fatal("should have errored, user not member of channel")
|
||||||
}
|
}
|
||||||
|
|
||||||
// perform clean-up on s3
|
if utils.IsS3Configured() && !utils.Cfg.ServiceSettings.UseLocalStorage {
|
||||||
var auth aws.Auth
|
// perform clean-up on s3
|
||||||
auth.AccessKey = utils.Cfg.AWSSettings.S3AccessKeyId
|
var auth aws.Auth
|
||||||
auth.SecretKey = utils.Cfg.AWSSettings.S3SecretAccessKey
|
auth.AccessKey = utils.Cfg.AWSSettings.S3AccessKeyId
|
||||||
|
auth.SecretKey = utils.Cfg.AWSSettings.S3SecretAccessKey
|
||||||
|
|
||||||
s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region])
|
s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region])
|
||||||
bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
|
bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
|
||||||
|
|
||||||
fileId := strings.Split(filenames[0], ".")[0]
|
filenames := strings.Split(resp.Data.(*model.FileUploadResponse).Filenames[0], "/")
|
||||||
|
filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
|
||||||
|
fileId := strings.Split(filename, ".")[0]
|
||||||
|
|
||||||
if err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + rpost1.Data.(*model.Post).UserId + "/" + filenames[0]); err != nil {
|
err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + filename)
|
||||||
t.Fatal(err)
|
if 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 {
|
err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_thumb.jpg")
|
||||||
t.Fatal(err)
|
if 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 {
|
err = bucket.Del("teams/" + team.Id + "/channels/" + channel1.Id + "/users/" + user1.Id + "/" + fileId + "_preview.jpg")
|
||||||
t.Fatal(err)
|
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 {
|
} else {
|
||||||
data := make(map[string]string)
|
data := make(map[string]string)
|
||||||
|
|||||||
@@ -368,7 +368,7 @@ func TestUserUploadProfileImage(t *testing.T) {
|
|||||||
user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
|
user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
|
||||||
store.Must(Srv.Store.User().VerifyEmail(user.Id))
|
store.Must(Srv.Store.User().VerifyEmail(user.Id))
|
||||||
|
|
||||||
if utils.IsS3Configured() {
|
if utils.IsS3Configured() || utils.Cfg.ServiceSettings.UseLocalStorage {
|
||||||
|
|
||||||
body := &bytes.Buffer{}
|
body := &bytes.Buffer{}
|
||||||
writer := multipart.NewWriter(body)
|
writer := multipart.NewWriter(body)
|
||||||
@@ -436,15 +436,22 @@ func TestUserUploadProfileImage(t *testing.T) {
|
|||||||
|
|
||||||
Client.DoGet("/users/"+user.Id+"/image", "", "")
|
Client.DoGet("/users/"+user.Id+"/image", "", "")
|
||||||
|
|
||||||
var auth aws.Auth
|
if utils.IsS3Configured() && !utils.Cfg.ServiceSettings.UseLocalStorage {
|
||||||
auth.AccessKey = utils.Cfg.AWSSettings.S3AccessKeyId
|
var auth aws.Auth
|
||||||
auth.SecretKey = utils.Cfg.AWSSettings.S3SecretAccessKey
|
auth.AccessKey = utils.Cfg.AWSSettings.S3AccessKeyId
|
||||||
|
auth.SecretKey = utils.Cfg.AWSSettings.S3SecretAccessKey
|
||||||
|
|
||||||
s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region])
|
s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region])
|
||||||
bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
|
bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
|
||||||
|
|
||||||
if err := bucket.Del("teams/" + user.TeamId + "/users/" + user.Id + "/profile.png"); err != nil {
|
if err := bucket.Del("teams/" + user.TeamId + "/users/" + user.Id + "/profile.png"); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
path := utils.Cfg.ServiceSettings.StorageDirectory + "teams/" + user.TeamId + "/users/" + user.Id + "/profile.png"
|
||||||
|
if err := os.Remove(path); err != nil {
|
||||||
|
t.Fatal("Couldn't remove file at " + path)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
body := &bytes.Buffer{}
|
body := &bytes.Buffer{}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user