|
|
|
@@ -2,15 +2,13 @@ package upload
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"bytes"
|
|
|
|
"image"
|
|
|
|
i "image"
|
|
|
|
"image/color"
|
|
|
|
"image/color"
|
|
|
|
"image/png"
|
|
|
|
"image/png"
|
|
|
|
"io/ioutil"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
// "github.com/tardisx/discord-auto-upload/config"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// https://www.thegreatcodeadventure.com/mocking-http-requests-in-golang/
|
|
|
|
// https://www.thegreatcodeadventure.com/mocking-http-requests-in-golang/
|
|
|
|
@@ -38,52 +36,50 @@ func DoTooBigUpload(req *http.Request) (*http.Response, error) {
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestSuccessfulUpload(t *testing.T) {
|
|
|
|
// func TestSuccessfulUpload(t *testing.T) {
|
|
|
|
// create temporary file, processUpload requires that it exists, even though
|
|
|
|
// // create temporary file, processUpload requires that it exists, even though
|
|
|
|
// we will not really be uploading it here
|
|
|
|
// // we will not really be uploading it here
|
|
|
|
f, _ := os.CreateTemp("", "dautest-upload-*")
|
|
|
|
// f, _ := os.CreateTemp("", "dautest-upload-*")
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
// defer os.Remove(f.Name())
|
|
|
|
u := Upload{webhookURL: "https://127.0.0.1/", OriginalFilename: f.Name()}
|
|
|
|
// u := Upload{webhookURL: "https://127.0.0.1/", Image: &image.Store{OriginalFilename: f.Name()}}
|
|
|
|
u.Client = &MockClient{DoFunc: DoGoodUpload}
|
|
|
|
// u.Client = &MockClient{DoFunc: DoGoodUpload}
|
|
|
|
err := u.processUpload()
|
|
|
|
// err := u.processUpload()
|
|
|
|
if err != nil {
|
|
|
|
// if err != nil {
|
|
|
|
t.Errorf("error occured: %s", err)
|
|
|
|
// t.Errorf("error occured: %s", err)
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
if u.Width != 640 || u.Height != 640 {
|
|
|
|
|
|
|
|
t.Error("dimensions wrong")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Url != "https://cdn.discordapp.com/attachments/849615269706203171/851092588332449812/dau480457962.png" {
|
|
|
|
|
|
|
|
t.Error("URL wrong")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestTooBigUpload(t *testing.T) {
|
|
|
|
// if u.Url != "https://cdn.discordapp.com/attachments/849615269706203171/851092588332449812/dau480457962.png" {
|
|
|
|
// create temporary file, processUpload requires that it exists, even though
|
|
|
|
// t.Error("URL wrong")
|
|
|
|
// we will not really be uploading it here
|
|
|
|
// }
|
|
|
|
f, _ := os.CreateTemp("", "dautest-upload-*")
|
|
|
|
// }
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
|
|
|
|
u := Upload{webhookURL: "https://127.0.0.1/", OriginalFilename: f.Name()}
|
|
|
|
// func TestTooBigUpload(t *testing.T) {
|
|
|
|
u.Client = &MockClient{DoFunc: DoTooBigUpload}
|
|
|
|
// // create temporary file, processUpload requires that it exists, even though
|
|
|
|
err := u.processUpload()
|
|
|
|
// // we will not really be uploading it here
|
|
|
|
if err == nil {
|
|
|
|
// f, _ := os.CreateTemp("", "dautest-upload-*")
|
|
|
|
t.Error("error did not occur?")
|
|
|
|
// defer os.Remove(f.Name())
|
|
|
|
} else if err.Error() != "received 413 - file too large" {
|
|
|
|
// u := Upload{webhookURL: "https://127.0.0.1/", Image: &image.Store{OriginalFilename: f.Name()}}
|
|
|
|
t.Errorf("wrong error occurred: %s", err.Error())
|
|
|
|
// u.Client = &MockClient{DoFunc: DoTooBigUpload}
|
|
|
|
}
|
|
|
|
// err := u.processUpload()
|
|
|
|
if u.State != StateFailed {
|
|
|
|
// if err == nil {
|
|
|
|
t.Error("upload should have been marked failed")
|
|
|
|
// t.Error("error did not occur?")
|
|
|
|
}
|
|
|
|
// } else if err.Error() != "received 413 - file too large" {
|
|
|
|
}
|
|
|
|
// t.Errorf("wrong error occurred: %s", err.Error())
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if u.State != StateFailed {
|
|
|
|
|
|
|
|
// t.Error("upload should have been marked failed")
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
func tempImageGt8Mb() {
|
|
|
|
func tempImageGt8Mb() {
|
|
|
|
// about 12Mb
|
|
|
|
// about 12Mb
|
|
|
|
width := 2000
|
|
|
|
width := 2000
|
|
|
|
height := 2000
|
|
|
|
height := 2000
|
|
|
|
|
|
|
|
|
|
|
|
upLeft := image.Point{0, 0}
|
|
|
|
upLeft := i.Point{0, 0}
|
|
|
|
lowRight := image.Point{width, height}
|
|
|
|
lowRight := i.Point{width, height}
|
|
|
|
|
|
|
|
|
|
|
|
img := image.NewRGBA(image.Rectangle{upLeft, lowRight})
|
|
|
|
img := i.NewRGBA(i.Rectangle{upLeft, lowRight})
|
|
|
|
|
|
|
|
|
|
|
|
// Colors are defined by Red, Green, Blue, Alpha uint8 values.
|
|
|
|
// Colors are defined by Red, Green, Blue, Alpha uint8 values.
|
|
|
|
|
|
|
|
|
|
|
|
|