-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
125 lines (111 loc) · 3.2 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"bytes"
"encoding/json"
"fmt"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoadConfig(t *testing.T) {
loadConfig("testdata/conf.json")
if assert.NotEmpty(t, conf) {
assert.Equal(t, "uploads/", conf.FilePrefix)
}
}
func TestLoadFileMetadata(t *testing.T) {
expectedFileMetadata := FileMetadata{Name: "baxter.jpg", Tags: []string{}, Link: "baxter.jpg"}
expected := UploadedFiles{Files: []FileMetadata{expectedFileMetadata}}
uploadedFiles = loadFileMetadata("testdata/metadata.json")
if assert.NotEmpty(t, uploadedFiles) {
assert.Equal(t, expected, uploadedFiles)
}
}
func TestLoadMissingFileMetadata(t *testing.T) {
testMetadataPath := "testdata/nonexistant_metadata.json"
expected := UploadedFiles{}
uploadedFiles = loadFileMetadata(testMetadataPath)
if assert.NotNil(t, uploadedFiles) {
assert.Equal(t, expected, uploadedFiles)
}
// cleanup
err := os.Remove(testMetadataPath)
if err != nil {
fmt.Println("Error removing test metadata file")
panic(err)
}
}
func TestListFiles(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/files", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
assert.NoError(t, listFiles(c))
require.Equal(t, http.StatusOK, rec.Code)
}
func TestSaveFile(t *testing.T) {
e := echo.New()
// create a multipart form for test request
buf := new(bytes.Buffer)
w := multipart.NewWriter(buf)
fw, _ := w.CreateFormField("name")
_, err := fw.Write([]byte("baxter.jpg"))
if err != nil {
fmt.Println("Error writing form field")
panic(err)
}
fw, _ = w.CreateFormField("size")
_, err = fw.Write([]byte("12345"))
if err != nil {
fmt.Println("Error writing form field 'size'")
panic(err)
}
fw, _ = w.CreateFormFile("file", "baxter.jpg")
// load baxter.jpg into memory
testFilePath := "testdata/baxter.jpg"
fileData, err := os.ReadFile(testFilePath)
if err != nil {
fmt.Printf("Error reading file %s\n", testFilePath)
panic(err)
}
_, err = fw.Write(fileData)
if err != nil {
fmt.Println("Error writing image data to form file")
panic(err)
}
fw, _ = w.CreateFormField("tags")
_, err = fw.Write([]byte("dog, baxter"))
if err != nil {
fmt.Println("Error writing form field 'tags'")
panic(err)
}
w.Close()
// setup test request and recorder
req := httptest.NewRequest(http.MethodPost, "/file/upload", buf)
req.Header.Set("Content-Type", w.FormDataContentType())
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
assert.NoError(t, saveFile(c))
require.Equal(t, http.StatusOK, rec.Code)
}
func TestUpdateFileTagsNotFound(t *testing.T) {
e := echo.New()
tags := Tags{Tags: []string{"dog", "baxter"}}
body, err := json.Marshal(tags)
if err != nil {
fmt.Println("Error marshalling tags")
panic(err)
}
req := httptest.NewRequest(http.MethodPatch, "/file/baxter.jpg", bytes.NewBuffer(body))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
assert.NoError(t, updateFileTags(c))
assert.Equal(t, "File not found", rec.Body.String())
require.Equal(t, http.StatusNotFound, rec.Code)
}