From 798f694c4c586e2ed16e18202e9000d70b69a872 Mon Sep 17 00:00:00 2001 From: Sergey Khorbin Date: Mon, 17 Jun 2024 21:43:03 +0200 Subject: [PATCH] test: added tests for uploading documents and images. (#130) * test: added tests for image upload * test: added tests for documents upload * refactor(test): fixed comments --- src/database/database.go | 9 +- src/handlers/docs/handleDocUpload_test.go | 250 ++++++++++++++++++ src/handlers/image/handleImageUpload_test.go | 253 +++++++++++++++++++ 3 files changed, 510 insertions(+), 2 deletions(-) create mode 100644 src/handlers/docs/handleDocUpload_test.go create mode 100644 src/handlers/image/handleImageUpload_test.go diff --git a/src/database/database.go b/src/database/database.go index 80f3cd3..4a4219c 100644 --- a/src/database/database.go +++ b/src/database/database.go @@ -11,12 +11,17 @@ import ( "gorm.io/gorm" ) +const ( + DbFolder = "db_data" + DbName = "main.db" +) + var DB *gorm.DB func ConnectToDB() { - dbPath := fmt.Sprintf("%v/db_data", util.ExPath) + dbPath := fmt.Sprintf("%v/%s", util.ExPath, DbFolder) - _, err := os.Stat(fmt.Sprintf("%v/main.db", dbPath)) + _, err := os.Stat(fmt.Sprintf("%v/%s", dbPath, DbName)) if err != nil { os.Mkdir(dbPath, 0o755) log.Printf("DB not found, creating at %v/main.db...", dbPath) diff --git a/src/handlers/docs/handleDocUpload_test.go b/src/handlers/docs/handleDocUpload_test.go new file mode 100644 index 0000000..c93a54d --- /dev/null +++ b/src/handlers/docs/handleDocUpload_test.go @@ -0,0 +1,250 @@ +package handlers + +import ( + "fmt" + "io" + "mime/multipart" + "net/http" + "net/http/httptest" + "os" + "testing" + + "github.com/gin-gonic/gin" + "github.com/kevinanielsen/go-fast-cdn/src/database" + "github.com/kevinanielsen/go-fast-cdn/src/util" + "github.com/stretchr/testify/require" +) + +var testDataFile []byte = []byte(`Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`) + +func TestHandleDocUpload_NoError(t *testing.T) { + // create tmpFile + pipeRead, pipeWriter := io.Pipe() + writer := multipart.NewWriter(pipeWriter) + go func() { + defer writer.Close() + part, _ := writer.CreateFormFile("doc", "filename.txt") + part.Write(testDataFile) + }() + + // init request + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + c.Request = httptest.NewRequest(http.MethodPost, "/api/cdn/upload/doc", pipeRead) + c.Request.Header.Add("Content-Type", writer.FormDataContentType()) + + // init database + util.ExPath = os.TempDir() + database.ConnectToDB() + defer func() { + filePath := fmt.Sprintf("%s/%s/%s", util.ExPath, database.DbFolder, database.DbName) + err := os.Remove(filePath) + if err != nil { + t.Error(err) + } + }() + + // handling + HandleDocUpload(c) + + // assert + require.Equal(t, http.StatusOK, w.Result().StatusCode) +} + +func TestHandleDocUpload_ReadFailed_NoFile(t *testing.T) { + // create tmpFile + pipeRead, pipeWriter := io.Pipe() + writer := multipart.NewWriter(pipeWriter) + go func() { + defer writer.Close() + part, _ := writer.CreateFormFile("docx", "filename.txt") + part.Write(testDataFile) + }() + + // init request + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + c.Request = httptest.NewRequest(http.MethodPost, "/api/cdn/upload/doc", pipeRead) + c.Request.Header.Add("Content-Type", writer.FormDataContentType()) + + // init database + util.ExPath = os.TempDir() + database.ConnectToDB() + defer func() { + filePath := fmt.Sprintf("%s/%s/%s", util.ExPath, database.DbFolder, database.DbName) + err := os.Remove(filePath) + if err != nil { + t.Error(err) + } + }() + + // handling + HandleDocUpload(c) + + // assert + require.Equal(t, http.StatusBadRequest, w.Result().StatusCode) + require.Equal(t, w.Body.String(), "Failed to read file: http: no such file") +} + +func TestHandleDocUpload_ReadFailed_EOF(t *testing.T) { + // create tmpFile + pipeRead, pipeWriter := io.Pipe() + writer := multipart.NewWriter(pipeWriter) + go func() { + defer writer.Close() + part, _ := writer.CreateFormFile("doc", "filename.txt") + part.Write([]byte("")) + }() + + // init request + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + c.Request = httptest.NewRequest(http.MethodPost, "/api/cdn/upload/doc", pipeRead) + c.Request.Header.Add("Content-Type", writer.FormDataContentType()) + + // init database + util.ExPath = os.TempDir() + database.ConnectToDB() + defer func() { + filePath := fmt.Sprintf("%s/%s/%s", util.ExPath, database.DbFolder, database.DbName) + err := os.Remove(filePath) + if err != nil { + t.Error(err) + } + }() + + // handling + HandleDocUpload(c) + + // assert + require.Equal(t, http.StatusInternalServerError, w.Result().StatusCode) + require.Equal(t, w.Body.String(), "Failed to read file: EOF") +} + +func TestHandleDocUpload_InvalidType(t *testing.T) { + // create tmpFile + pipeRead, pipeWriter := io.Pipe() + writer := multipart.NewWriter(pipeWriter) + go func() { + defer writer.Close() + part, _ := writer.CreateFormFile("doc", "filename.txt") + part.Write([]byte("testDataFile")) + }() + + // init request + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + c.Request = httptest.NewRequest(http.MethodPost, "/api/cdn/upload/doc", pipeRead) + c.Request.Header.Add("Content-Type", writer.FormDataContentType()) + + // init database + util.ExPath = os.TempDir() + database.ConnectToDB() + defer func() { + filePath := fmt.Sprintf("%s/%s/%s", util.ExPath, database.DbFolder, database.DbName) + err := os.Remove(filePath) + if err != nil { + t.Error(err) + } + }() + + // handling + HandleDocUpload(c) + + // assert + require.Equal(t, http.StatusBadRequest, w.Result().StatusCode) + require.Equal(t, w.Body.String(), "Invalid file type: application/octet-stream") +} + +func TestHandleDocUpload_InvalidFilename(t *testing.T) { + // create tmpFile + pipeRead, pipeWriter := io.Pipe() + writer := multipart.NewWriter(pipeWriter) + go func() { + defer writer.Close() + part, _ := writer.CreateFormFile("doc", "file.name.txt") + part.Write(testDataFile) + }() + + // init request + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + c.Request = httptest.NewRequest(http.MethodPost, "/api/cdn/upload/doc", pipeRead) + c.Request.Header.Add("Content-Type", writer.FormDataContentType()) + + // init database + util.ExPath = os.TempDir() + database.ConnectToDB() + defer func() { + filePath := fmt.Sprintf("%s/%s/%s", util.ExPath, database.DbFolder, database.DbName) + err := os.Remove(filePath) + if err != nil { + t.Error(err) + } + }() + + // handling + HandleDocUpload(c) + + // assert + require.Equal(t, http.StatusBadRequest, w.Result().StatusCode) + require.Equal(t, w.Body.String(), "filename cannot contain more than one period character") +} + +func TestHandleDocUpload_FileExist(t *testing.T) { + // create tmpFile + pipeRead, pipeWriter := io.Pipe() + writer := multipart.NewWriter(pipeWriter) + go func() { + defer writer.Close() + part, _ := writer.CreateFormFile("doc", "filename.txt") + part.Write(testDataFile) + }() + + // init database + util.ExPath = os.TempDir() + database.ConnectToDB() + defer func() { + filePath := fmt.Sprintf("%s/%s/%s", util.ExPath, database.DbFolder, database.DbName) + err := os.Remove(filePath) + if err != nil { + t.Error(err) + } + }() + + // init request + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + c.Request = httptest.NewRequest(http.MethodPost, "/api/cdn/upload/doc", pipeRead) + c.Request.Header.Add("Content-Type", writer.FormDataContentType()) + + // first handling + HandleDocUpload(c) + + // first statement + require.Equal(t, http.StatusOK, w.Result().StatusCode) + + // init request + ww := httptest.NewRecorder() + cc, _ := gin.CreateTestContext(ww) + go func() { + defer writer.Close() + part, _ := writer.CreateFormFile("doc", "filename.txt") + part.Write(testDataFile) + }() + cc.Request = httptest.NewRequest(http.MethodPost, "/api/cdn/upload/doc", pipeRead) + cc.Request.Header.Add("Content-Type", writer.FormDataContentType()) + + // second handling + HandleDocUpload(cc) + + // second statement + require.Equal(t, http.StatusConflict, ww.Result().StatusCode) + require.Equal(t, ww.Body.String(), "\"File already exists\"") +} diff --git a/src/handlers/image/handleImageUpload_test.go b/src/handlers/image/handleImageUpload_test.go new file mode 100644 index 0000000..692276d --- /dev/null +++ b/src/handlers/image/handleImageUpload_test.go @@ -0,0 +1,253 @@ +package handlers + +import ( + "fmt" + "io" + "mime/multipart" + "net/http" + "net/http/httptest" + "os" + "testing" + + "github.com/gin-gonic/gin" + "github.com/kevinanielsen/go-fast-cdn/src/database" + "github.com/kevinanielsen/go-fast-cdn/src/util" + "github.com/stretchr/testify/require" +) + +func TestHandleImageUpload_NoError(t *testing.T) { + // create tmpFile + pipeRead, pipeWriter := io.Pipe() + writer := multipart.NewWriter(pipeWriter) + go func() { + defer writer.Close() + part, _ := writer.CreateFormFile("image", "image.img") + img, _ := createDummyImage(200, 200) + _ = EncodeImage(part, img) + }() + + // init request + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + c.Request = httptest.NewRequest(http.MethodPost, "/api/cdn/upload/image", pipeRead) + c.Request.Header.Add("Content-Type", writer.FormDataContentType()) + + // init database + util.ExPath = os.TempDir() + database.ConnectToDB() + defer func() { + filePath := fmt.Sprintf("%s/%s/%s", util.ExPath, database.DbFolder, database.DbName) + err := os.Remove(filePath) + if err != nil { + t.Error(err) + } + }() + + // handling + HandleImageUpload(c) + + // assert + require.Equal(t, http.StatusOK, w.Result().StatusCode) +} + +func TestHandleImageUpload_ReadFailed_NoFile(t *testing.T) { + // create tmpFile + pipeRead, pipeWriter := io.Pipe() + writer := multipart.NewWriter(pipeWriter) + go func() { + defer writer.Close() + part, _ := writer.CreateFormFile("png", "img.img") + img, _ := createDummyImage(200, 200) + _ = EncodeImage(part, img) + }() + + // init request + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + c.Request = httptest.NewRequest(http.MethodPost, "/api/cdn/upload/image", pipeRead) + c.Request.Header.Add("Content-Type", writer.FormDataContentType()) + + // init database + util.ExPath = os.TempDir() + database.ConnectToDB() + defer func() { + filePath := fmt.Sprintf("%s/%s/%s", util.ExPath, database.DbFolder, database.DbName) + err := os.Remove(filePath) + if err != nil { + t.Error(err) + } + }() + + // handling + HandleImageUpload(c) + + // assert + require.Equal(t, http.StatusBadRequest, w.Result().StatusCode) + require.Equal(t, w.Body.String(), "Failed to read file: http: no such file") +} + +func TestHandleImageUpload_ReadFailed_EOF(t *testing.T) { + // create tmpFile + pipeRead, pipeWriter := io.Pipe() + writer := multipart.NewWriter(pipeWriter) + go func() { + defer writer.Close() + part, _ := writer.CreateFormFile("image", "image.img") + part.Write([]byte("")) + }() + + // init request + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + c.Request = httptest.NewRequest(http.MethodPost, "/api/cdn/upload/image", pipeRead) + c.Request.Header.Add("Content-Type", writer.FormDataContentType()) + + // init database + util.ExPath = os.TempDir() + database.ConnectToDB() + defer func() { + filePath := fmt.Sprintf("%s/%s/%s", util.ExPath, database.DbFolder, database.DbName) + err := os.Remove(filePath) + if err != nil { + t.Error(err) + } + }() + + // handling + HandleImageUpload(c) + + // assert + require.Equal(t, http.StatusInternalServerError, w.Result().StatusCode) + require.Equal(t, w.Body.String(), "Failed to read file: EOF") +} + +func TestHandleImageUpload_InvalidType(t *testing.T) { + // create tmpFile + pipeRead, pipeWriter := io.Pipe() + writer := multipart.NewWriter(pipeWriter) + go func() { + defer writer.Close() + part, _ := writer.CreateFormFile("image", "img.docx") + part.Write([]byte("dsf")) + }() + + // init request + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + c.Request = httptest.NewRequest(http.MethodPost, "/api/cdn/upload/image", pipeRead) + c.Request.Header.Add("Content-Type", writer.FormDataContentType()) + + // init database + util.ExPath = os.TempDir() + database.ConnectToDB() + defer func() { + filePath := fmt.Sprintf("%s/%s/%s", util.ExPath, database.DbFolder, database.DbName) + err := os.Remove(filePath) + if err != nil { + t.Error(err) + } + }() + + // handling + HandleImageUpload(c) + + // assert + require.Equal(t, http.StatusBadRequest, w.Result().StatusCode) + require.Equal(t, w.Body.String(), "Invalid file type") +} + +func TestHandleImageUpload_InvalidFilename(t *testing.T) { + // create tmpFile + pipeRead, pipeWriter := io.Pipe() + writer := multipart.NewWriter(pipeWriter) + go func() { + defer writer.Close() + part, _ := writer.CreateFormFile("image", "img.image.img") + img, _ := createDummyImage(200, 200) + _ = EncodeImage(part, img) + }() + + // init request + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + c.Request = httptest.NewRequest(http.MethodPost, "/api/cdn/upload/image", pipeRead) + c.Request.Header.Add("Content-Type", writer.FormDataContentType()) + + // init database + util.ExPath = os.TempDir() + database.ConnectToDB() + defer func() { + filePath := fmt.Sprintf("%s/%s/%s", util.ExPath, database.DbFolder, database.DbName) + err := os.Remove(filePath) + if err != nil { + t.Error(err) + } + }() + + // handling + HandleImageUpload(c) + + // assert + require.Equal(t, http.StatusBadRequest, w.Result().StatusCode) + require.Equal(t, w.Body.String(), "filename cannot contain more than one period character") +} + +func TestHandleImageUpload_FileExist(t *testing.T) { + // create tmpFile + pipeRead, pipeWriter := io.Pipe() + writer := multipart.NewWriter(pipeWriter) + go func() { + defer writer.Close() + part, _ := writer.CreateFormFile("image", "image.img") + img, _ := createDummyImage(200, 200) + _ = EncodeImage(part, img) + }() + + // init database + util.ExPath = os.TempDir() + database.ConnectToDB() + defer func() { + filePath := fmt.Sprintf("%s/%s/%s", util.ExPath, database.DbFolder, database.DbName) + err := os.Remove(filePath) + if err != nil { + t.Error(err) + } + }() + + // init request + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + + c.Request = httptest.NewRequest(http.MethodPost, "/api/cdn/upload/image", pipeRead) + c.Request.Header.Add("Content-Type", writer.FormDataContentType()) + + // first handling + HandleImageUpload(c) + + // first statement + require.Equal(t, http.StatusOK, w.Result().StatusCode) + + // init request + ww := httptest.NewRecorder() + cc, _ := gin.CreateTestContext(ww) + go func() { + defer writer.Close() + part, _ := writer.CreateFormFile("image", "image.img") + img, _ := createDummyImage(200, 200) + _ = EncodeImage(part, img) + }() + cc.Request = httptest.NewRequest(http.MethodPost, "/api/cdn/upload/image", pipeRead) + cc.Request.Header.Add("Content-Type", writer.FormDataContentType()) + + // second handling + HandleImageUpload(cc) + + // second statement + require.Equal(t, http.StatusConflict, ww.Result().StatusCode) + require.Equal(t, ww.Body.String(), "\"File already exists\"") +}