2015-10-08 12:27:09 -04:00
|
|
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
2015-06-14 23:53:32 -08:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2016-05-05 16:35:03 -04:00
|
|
|
"encoding/base64"
|
2015-06-14 23:53:32 -08:00
|
|
|
"fmt"
|
|
|
|
|
"github.com/goamz/goamz/aws"
|
|
|
|
|
"github.com/goamz/goamz/s3"
|
|
|
|
|
"github.com/mattermost/platform/model"
|
|
|
|
|
"github.com/mattermost/platform/utils"
|
|
|
|
|
"io"
|
|
|
|
|
"mime/multipart"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestUploadFile(t *testing.T) {
|
2016-04-21 22:37:01 -07:00
|
|
|
th := Setup().InitBasic()
|
|
|
|
|
Client := th.BasicClient
|
2016-06-30 14:53:36 -04:00
|
|
|
team := th.BasicTeam
|
2016-04-21 22:37:01 -07:00
|
|
|
user := th.BasicUser
|
|
|
|
|
channel := th.BasicChannel
|
2015-06-14 23:53:32 -08:00
|
|
|
|
|
|
|
|
body := &bytes.Buffer{}
|
|
|
|
|
writer := multipart.NewWriter(body)
|
2015-07-21 15:18:17 -04:00
|
|
|
part, err := writer.CreateFormFile("files", "../test.png")
|
2015-06-14 23:53:32 -08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 08:50:46 -04:00
|
|
|
path := utils.FindDir("tests")
|
2015-06-14 23:53:32 -08:00
|
|
|
file, err := os.Open(path + "/test.png")
|
2016-04-21 22:37:01 -07:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
_, err = io.Copy(part, file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
field, err := writer.CreateFormField("channel_id")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 22:37:01 -07:00
|
|
|
_, err = field.Write([]byte(channel.Id))
|
2015-06-14 23:53:32 -08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = writer.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 22:37:01 -07:00
|
|
|
resp, appErr := Client.UploadPostAttachment(body.Bytes(), writer.FormDataContentType())
|
2015-09-23 13:47:10 -07:00
|
|
|
if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 {
|
2015-06-14 23:53:32 -08:00
|
|
|
if appErr != nil {
|
|
|
|
|
t.Fatal(appErr)
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-16 12:50:38 -04:00
|
|
|
filenames := strings.Split(resp.Data.(*model.FileUploadResponse).Filenames[0], "/")
|
|
|
|
|
filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
|
2015-07-21 15:18:17 -04:00
|
|
|
if strings.Contains(filename, "../") {
|
|
|
|
|
t.Fatal("relative path should have been sanitized out")
|
|
|
|
|
}
|
2015-07-16 12:50:38 -04:00
|
|
|
fileId := strings.Split(filename, ".")[0]
|
2015-06-14 23:53:32 -08:00
|
|
|
|
|
|
|
|
var auth aws.Auth
|
2015-09-23 13:47:10 -07:00
|
|
|
auth.AccessKey = utils.Cfg.FileSettings.AmazonS3AccessKeyId
|
|
|
|
|
auth.SecretKey = utils.Cfg.FileSettings.AmazonS3SecretAccessKey
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-09-23 13:47:10 -07:00
|
|
|
s := s3.New(auth, aws.Regions[utils.Cfg.FileSettings.AmazonS3Region])
|
|
|
|
|
bucket := s.Bucket(utils.Cfg.FileSettings.AmazonS3Bucket)
|
2015-06-14 23:53:32 -08:00
|
|
|
|
|
|
|
|
// wait a bit for files to ready
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + filename)
|
2015-06-14 23:53:32 -08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_thumb.jpg")
|
2015-06-14 23:53:32 -08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_preview.jpg")
|
2015-06-14 23:53:32 -08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2015-09-23 13:47:10 -07:00
|
|
|
} else if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_LOCAL {
|
2015-07-16 12:50:38 -04:00
|
|
|
filenames := strings.Split(resp.Data.(*model.FileUploadResponse).Filenames[0], "/")
|
|
|
|
|
filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
|
2015-07-21 15:18:17 -04:00
|
|
|
if strings.Contains(filename, "../") {
|
|
|
|
|
t.Fatal("relative path should have been sanitized out")
|
|
|
|
|
}
|
2015-07-16 12:50:38 -04:00
|
|
|
fileId := strings.Split(filename, ".")[0]
|
|
|
|
|
|
|
|
|
|
// wait a bit for files to ready
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
path := utils.Cfg.FileSettings.Directory + "teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + filename
|
2015-07-16 12:50:38 -04:00
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
|
t.Fatal("Couldn't remove file at " + path)
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
path = utils.Cfg.FileSettings.Directory + "teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_thumb.jpg"
|
2015-07-16 12:50:38 -04:00
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
|
t.Fatal("Couldn't remove file at " + path)
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
path = utils.Cfg.FileSettings.Directory + "teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_preview.jpg"
|
2015-07-16 12:50:38 -04:00
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
|
t.Fatal("Couldn't remove file at " + path)
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
} else {
|
|
|
|
|
if appErr == nil {
|
2015-07-16 12:50:38 -04:00
|
|
|
t.Fatal("S3 and local storage not configured, should have failed")
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetFile(t *testing.T) {
|
2016-04-21 22:37:01 -07:00
|
|
|
th := Setup().InitBasic()
|
|
|
|
|
Client := th.BasicClient
|
2016-06-30 14:53:36 -04:00
|
|
|
team := th.BasicTeam
|
2016-04-21 22:37:01 -07:00
|
|
|
user := th.BasicUser
|
|
|
|
|
channel := th.BasicChannel
|
|
|
|
|
|
2015-09-23 13:47:10 -07:00
|
|
|
if utils.Cfg.FileSettings.DriverName != "" {
|
2015-06-14 23:53:32 -08:00
|
|
|
body := &bytes.Buffer{}
|
|
|
|
|
writer := multipart.NewWriter(body)
|
|
|
|
|
part, err := writer.CreateFormFile("files", "test.png")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 08:50:46 -04:00
|
|
|
path := utils.FindDir("tests")
|
2015-06-14 23:53:32 -08:00
|
|
|
file, err := os.Open(path + "/test.png")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
_, err = io.Copy(part, file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
field, err := writer.CreateFormField("channel_id")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 22:37:01 -07:00
|
|
|
_, err = field.Write([]byte(channel.Id))
|
2015-06-14 23:53:32 -08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = writer.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 22:37:01 -07:00
|
|
|
resp, upErr := Client.UploadPostAttachment(body.Bytes(), writer.FormDataContentType())
|
2015-06-14 23:53:32 -08:00
|
|
|
if upErr != nil {
|
|
|
|
|
t.Fatal(upErr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filenames := resp.Data.(*model.FileUploadResponse).Filenames
|
|
|
|
|
|
|
|
|
|
// wait a bit for files to ready
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
2015-07-21 19:21:05 -04:00
|
|
|
if _, downErr := Client.GetFile(filenames[0], false); downErr != nil {
|
|
|
|
|
t.Fatal(downErr)
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
2015-08-24 15:03:52 -07:00
|
|
|
if resp, downErr := Client.GetFileInfo(filenames[0]); downErr != nil {
|
|
|
|
|
t.Fatal(downErr)
|
|
|
|
|
} else {
|
2015-12-18 19:02:48 -05:00
|
|
|
info := resp.Data.(*model.FileInfo)
|
|
|
|
|
if info.Size == 0 {
|
|
|
|
|
t.Fatal("No file size returned")
|
2015-08-24 15:03:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-23 13:47:10 -07:00
|
|
|
if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 {
|
2015-07-16 12:50:38 -04:00
|
|
|
var auth aws.Auth
|
2015-09-23 13:47:10 -07:00
|
|
|
auth.AccessKey = utils.Cfg.FileSettings.AmazonS3AccessKeyId
|
|
|
|
|
auth.SecretKey = utils.Cfg.FileSettings.AmazonS3SecretAccessKey
|
2015-07-16 12:50:38 -04:00
|
|
|
|
2015-09-23 13:47:10 -07:00
|
|
|
s := s3.New(auth, aws.Regions[utils.Cfg.FileSettings.AmazonS3Region])
|
|
|
|
|
bucket := s.Bucket(utils.Cfg.FileSettings.AmazonS3Bucket)
|
2015-07-16 12:50:38 -04:00
|
|
|
|
|
|
|
|
filenames := strings.Split(resp.Data.(*model.FileUploadResponse).Filenames[0], "/")
|
|
|
|
|
filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
|
|
|
|
|
fileId := strings.Split(filename, ".")[0]
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + filename)
|
2015-07-16 12:50:38 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_thumb.jpg")
|
2015-07-16 12:50:38 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
err = bucket.Del("teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_preview.jpg")
|
2015-07-16 12:50:38 -04:00
|
|
|
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]
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
path := utils.Cfg.FileSettings.Directory + "teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + filename
|
2015-07-16 12:50:38 -04:00
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
|
t.Fatal("Couldn't remove file at " + path)
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
path = utils.Cfg.FileSettings.Directory + "teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_thumb.jpg"
|
2015-07-16 12:50:38 -04:00
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
|
t.Fatal("Couldn't remove file at " + path)
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
path = utils.Cfg.FileSettings.Directory + "teams/" + team.Id + "/channels/" + channel.Id + "/users/" + user.Id + "/" + fileId + "_preview.jpg"
|
2015-07-16 12:50:38 -04:00
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
|
t.Fatal("Couldn't remove file at " + path)
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if _, downErr := Client.GetFile("/files/get/yxebdmbz5pgupx7q6ez88rw11a/n3btzxu9hbnapqk36iwaxkjxhc/junk.jpg", false); downErr.StatusCode != http.StatusNotImplemented {
|
|
|
|
|
t.Fatal("Status code should have been 501 - Not Implemented")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
func TestGetPublicFile(t *testing.T) {
|
2016-04-21 22:37:01 -07:00
|
|
|
th := Setup().InitBasic()
|
|
|
|
|
Client := th.BasicClient
|
|
|
|
|
channel := th.BasicChannel
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
enablePublicLink := utils.Cfg.FileSettings.EnablePublicLink
|
|
|
|
|
driverName := utils.Cfg.FileSettings.DriverName
|
|
|
|
|
defer func() {
|
|
|
|
|
utils.Cfg.FileSettings.EnablePublicLink = enablePublicLink
|
|
|
|
|
utils.Cfg.FileSettings.DriverName = driverName
|
|
|
|
|
}()
|
|
|
|
|
utils.Cfg.FileSettings.EnablePublicLink = true
|
|
|
|
|
if driverName == "" {
|
|
|
|
|
driverName = model.IMAGE_DRIVER_LOCAL
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
filenames, err := uploadTestFile(Client, channel.Id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("failed to upload test file", err)
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
post1 := &model.Post{ChannelId: channel.Id, Message: "a" + model.NewId() + "a", Filenames: filenames}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
if rpost1, postErr := Client.CreatePost(post1); postErr != nil {
|
|
|
|
|
t.Fatal(postErr)
|
|
|
|
|
} else {
|
|
|
|
|
post1 = rpost1.Data.(*model.Post)
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
var link string
|
|
|
|
|
if result, err := Client.GetPublicLink(filenames[0]); err != nil {
|
|
|
|
|
t.Fatal("failed to get public link")
|
|
|
|
|
} else {
|
|
|
|
|
link = result.Data.(string)
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
// test a user that's logged in
|
|
|
|
|
if resp, err := http.Get(link); err != nil && resp.StatusCode != http.StatusOK {
|
|
|
|
|
t.Fatal("failed to get image with public link while logged in", err)
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
if resp, err := http.Get(link[:strings.LastIndex(link, "?")]); err == nil && resp.StatusCode != http.StatusBadRequest {
|
|
|
|
|
t.Fatal("should've failed to get image with public link while logged in without query params", resp.Status)
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
if resp, err := http.Get(link[:strings.LastIndex(link, "&")]); err == nil && resp.StatusCode != http.StatusBadRequest {
|
|
|
|
|
t.Fatal("should've failed to get image with public link while logged in without second query param")
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
if resp, err := http.Get(link[:strings.LastIndex(link, "?")] + "?" + link[strings.LastIndex(link, "&"):]); err == nil && resp.StatusCode != http.StatusBadRequest {
|
|
|
|
|
t.Fatal("should've failed to get image with public link while logged in without first query param")
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
utils.Cfg.FileSettings.EnablePublicLink = false
|
|
|
|
|
if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusNotImplemented {
|
|
|
|
|
t.Fatal("should've failed to get image with disabled public link while logged in")
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
utils.Cfg.FileSettings.EnablePublicLink = true
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
// test a user that's logged out
|
|
|
|
|
Client.Must(Client.Logout())
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
if resp, err := http.Get(link); err != nil && resp.StatusCode != http.StatusOK {
|
|
|
|
|
t.Fatal("failed to get image with public link while not logged in", err)
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
if resp, err := http.Get(link[:strings.LastIndex(link, "?")]); err == nil && resp.StatusCode != http.StatusBadRequest {
|
|
|
|
|
t.Fatal("should've failed to get image with public link while not logged in without query params")
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
if resp, err := http.Get(link[:strings.LastIndex(link, "&")]); err == nil && resp.StatusCode != http.StatusBadRequest {
|
|
|
|
|
t.Fatal("should've failed to get image with public link while not logged in without second query param")
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
if resp, err := http.Get(link[:strings.LastIndex(link, "?")] + "?" + link[strings.LastIndex(link, "&"):]); err == nil && resp.StatusCode != http.StatusBadRequest {
|
|
|
|
|
t.Fatal("should've failed to get image with public link while not logged in without first query param")
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
utils.Cfg.FileSettings.EnablePublicLink = false
|
|
|
|
|
if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusNotImplemented {
|
|
|
|
|
t.Fatal("should've failed to get image with disabled public link while not logged in")
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
utils.Cfg.FileSettings.EnablePublicLink = true
|
2016-04-21 22:37:01 -07:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
// test a user that's logged in after the salt has changed
|
|
|
|
|
utils.Cfg.FileSettings.PublicLinkSalt = model.NewId()
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
th.LoginBasic()
|
|
|
|
|
if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusBadRequest {
|
|
|
|
|
t.Fatal("should've failed to get image with public link while logged in after salt changed")
|
|
|
|
|
}
|
2015-07-16 12:50:38 -04:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
Client.Must(Client.Logout())
|
|
|
|
|
if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusBadRequest {
|
|
|
|
|
t.Fatal("should've failed to get image with public link while not logged in after salt changed")
|
|
|
|
|
}
|
2015-07-16 12:50:38 -04:00
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
if err := cleanupTestFile(filenames[0], th.BasicTeam.Id, channel.Id, th.BasicUser.Id); err != nil {
|
2016-05-05 16:35:03 -04:00
|
|
|
t.Fatal("failed to cleanup test file", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-16 12:50:38 -04:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
func TestGetPublicLink(t *testing.T) {
|
|
|
|
|
th := Setup().InitBasic()
|
|
|
|
|
Client := th.BasicClient
|
|
|
|
|
channel := th.BasicChannel
|
2015-07-16 12:50:38 -04:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
enablePublicLink := utils.Cfg.FileSettings.EnablePublicLink
|
|
|
|
|
driverName := utils.Cfg.FileSettings.DriverName
|
|
|
|
|
defer func() {
|
|
|
|
|
utils.Cfg.FileSettings.EnablePublicLink = enablePublicLink
|
|
|
|
|
utils.Cfg.FileSettings.DriverName = driverName
|
|
|
|
|
}()
|
|
|
|
|
if driverName == "" {
|
|
|
|
|
driverName = model.IMAGE_DRIVER_LOCAL
|
|
|
|
|
}
|
2015-07-16 12:50:38 -04:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
filenames, err := uploadTestFile(Client, channel.Id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("failed to upload test file", err)
|
|
|
|
|
}
|
2015-07-16 12:50:38 -04:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
post1 := &model.Post{ChannelId: channel.Id, Message: "a" + model.NewId() + "a", Filenames: filenames}
|
2015-07-16 12:50:38 -04:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
if rpost1, postErr := Client.CreatePost(post1); postErr != nil {
|
|
|
|
|
t.Fatal(postErr)
|
|
|
|
|
} else {
|
|
|
|
|
post1 = rpost1.Data.(*model.Post)
|
|
|
|
|
}
|
2015-07-16 12:50:38 -04:00
|
|
|
|
2016-05-05 16:35:03 -04:00
|
|
|
utils.Cfg.FileSettings.EnablePublicLink = false
|
|
|
|
|
if _, err := Client.GetPublicLink(filenames[0]); err == nil || err.StatusCode != http.StatusNotImplemented {
|
|
|
|
|
t.Fatal("should've failed when public links are disabled", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
utils.Cfg.FileSettings.EnablePublicLink = true
|
|
|
|
|
|
|
|
|
|
if _, err := Client.GetPublicLink("garbage"); err == nil {
|
|
|
|
|
t.Fatal("should've failed for invalid link")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := Client.GetPublicLink(filenames[0]); err != nil {
|
|
|
|
|
t.Fatal("should've gotten link for file", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
th.LoginBasic2()
|
|
|
|
|
|
|
|
|
|
if _, err := Client.GetPublicLink(filenames[0]); err == nil {
|
|
|
|
|
t.Fatal("should've failed, user not member of channel")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
th.LoginBasic()
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
if err := cleanupTestFile(filenames[0], th.BasicTeam.Id, channel.Id, th.BasicUser.Id); err != nil {
|
2016-05-05 16:35:03 -04:00
|
|
|
t.Fatal("failed to cleanup test file", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func uploadTestFile(Client *model.Client, channelId string) ([]string, error) {
|
|
|
|
|
body := &bytes.Buffer{}
|
|
|
|
|
writer := multipart.NewWriter(body)
|
|
|
|
|
part, err := writer.CreateFormFile("files", "test.png")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// base 64 encoded version of handtinywhite.gif from http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever
|
|
|
|
|
file, _ := base64.StdEncoding.DecodeString("R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=")
|
|
|
|
|
|
|
|
|
|
if _, err := io.Copy(part, bytes.NewReader(file)); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
field, err := writer.CreateFormField("channel_id")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := field.Write([]byte(channelId)); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := writer.Close(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if resp, err := Client.UploadPostAttachment(body.Bytes(), writer.FormDataContentType()); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
} else {
|
|
|
|
|
return resp.Data.(*model.FileUploadResponse).Filenames, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
func cleanupTestFile(fullFilename, teamId, channelId, userId string) error {
|
2016-05-05 16:35:03 -04:00
|
|
|
filenames := strings.Split(fullFilename, "/")
|
|
|
|
|
filename := filenames[len(filenames)-2] + "/" + filenames[len(filenames)-1]
|
|
|
|
|
fileId := strings.Split(filename, ".")[0]
|
|
|
|
|
|
|
|
|
|
if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 {
|
|
|
|
|
// perform clean-up on s3
|
|
|
|
|
var auth aws.Auth
|
|
|
|
|
auth.AccessKey = utils.Cfg.FileSettings.AmazonS3AccessKeyId
|
|
|
|
|
auth.SecretKey = utils.Cfg.FileSettings.AmazonS3SecretAccessKey
|
|
|
|
|
|
|
|
|
|
s := s3.New(auth, aws.Regions[utils.Cfg.FileSettings.AmazonS3Region])
|
|
|
|
|
bucket := s.Bucket(utils.Cfg.FileSettings.AmazonS3Bucket)
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
if err := bucket.Del("teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + filename); err != nil {
|
2016-05-05 16:35:03 -04:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
if err := bucket.Del("teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + fileId + "_thumb.jpg"); err != nil {
|
2016-05-05 16:35:03 -04:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
if err := bucket.Del("teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + fileId + "_preview.jpg"); err != nil {
|
2016-05-05 16:35:03 -04:00
|
|
|
return err
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
2016-06-30 14:53:36 -04:00
|
|
|
path := utils.Cfg.FileSettings.Directory + "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + filename
|
2016-05-05 16:35:03 -04:00
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
|
return fmt.Errorf("Couldn't remove file at " + path)
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
path = utils.Cfg.FileSettings.Directory + "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + fileId + "_thumb.jpg"
|
2016-05-05 16:35:03 -04:00
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
|
return fmt.Errorf("Couldn't remove file at " + path)
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 14:53:36 -04:00
|
|
|
path = utils.Cfg.FileSettings.Directory + "teams/" + teamId + "/channels/" + channelId + "/users/" + userId + "/" + fileId + "_preview.jpg"
|
2016-05-05 16:35:03 -04:00
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
|
return fmt.Errorf("Couldn't remove file at " + path)
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
}
|
2016-05-05 16:35:03 -04:00
|
|
|
|
|
|
|
|
return nil
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|