-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_test.go
246 lines (220 loc) · 5.54 KB
/
file_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package fstream
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"image"
"image/jpeg"
"mime/multipart"
"os"
"path/filepath"
"testing"
)
func TestIsAllowedExtension(t *testing.T) {
testCases := []struct {
fileExtension []string
fileName string
expected bool
expectedName string
}{
{
fileExtension: []string{".jpeg", ".jpg"},
fileName: "test.jpeg",
expected: true,
expectedName: "Success",
},
{
fileExtension: []string{".png", ".webp"},
fileName: "test.jpg",
expected: false,
expectedName: "Failed",
},
}
for _, tc := range testCases {
t.Run(tc.expectedName, func(t *testing.T) {
res := IsAllowExtension(tc.fileExtension, tc.fileName)
assert.Equal(t, tc.expected, res)
})
}
}
func TestRemoveUploadedFile(t *testing.T) {
// create test directory
err := os.MkdirAll("test", 0777)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll("test")
// simulate file upload
testFileName := "testfile.txt"
testFilePath := filepath.Join("test", testFileName)
_, err = os.Create(testFilePath)
if err != nil {
t.Fatal(err)
}
testCases := []struct {
uploadDir string
fileName string
expected error
expectedName string
}{
{
uploadDir: "test",
fileName: testFileName,
expected: nil,
expectedName: "Success",
},
}
for _, tc := range testCases {
t.Run(tc.expectedName, func(t *testing.T) {
err = RemoveUploadedFile(tc.uploadDir, tc.fileName)
assert.Equal(t, tc.expected, err)
if _, statErr := os.Stat(testFilePath); !os.IsNotExist(statErr) {
t.Errorf("File %s was not removed", testFilePath)
}
})
}
}
func TestStoreChunk(t *testing.T) {
testCases := []struct {
name string
fileContent []byte
maxRange int
fileUniqueName bool
expectError bool
expectedFileSize int
}{
{
name: "Successful file upload with unique name",
fileContent: []byte("This is a test chunk"),
maxRange: 19, // Full file uploaded
fileUniqueName: true,
expectError: false,
expectedFileSize: 19,
},
{
name: "Successful file upload without unique name",
fileContent: []byte("Another test chunk"),
maxRange: 20, // Full file uploaded
fileUniqueName: false,
expectError: false,
expectedFileSize: 20,
},
{
name: "Partial upload",
fileContent: []byte("Partial chunk"),
maxRange: 7, // Partial file uploaded
fileUniqueName: false,
expectError: false,
expectedFileSize: 0, // File should not be finalized
},
{
name: "Error due to maxRange exceeding file size",
fileContent: []byte("Invalid max range"),
maxRange: 50, // maxRange > FileSize
fileUniqueName: false,
expectError: true,
expectedFileSize: 0,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
file, err := os.CreateTemp("", "")
if err != nil {
assert.Error(t, err)
}
defer os.Remove(file.Name())
_, err = file.Write(tc.fileContent)
if err != nil {
assert.Error(t, err)
}
defer file.Close()
multipartFile, err := os.Open(file.Name())
if err != nil {
assert.Error(t, err)
}
defer multipartFile.Close()
fileHeader := &multipart.FileHeader{
Filename: filepath.Base(file.Name()),
Size: int64(len(tc.fileContent)),
}
r := &RFileRequest{
File: multipartFile,
UploadFile: fileHeader,
MaxRange: tc.maxRange,
FileSize: len(tc.fileContent),
UploadDirectory: t.TempDir(), // Temporary directory for test
FileUniqueName: tc.fileUniqueName,
}
resFile, err := StoreChunk(r)
if err != nil {
assert.Error(t, err)
}
expectedFilePath := filepath.Join(r.UploadDirectory + fileHeader.Filename)
fmt.Printf("Expected Path: %s\n", expectedFilePath)
assert.FileExists(t, expectedFilePath)
actualContent, err := os.ReadFile(expectedFilePath)
require.NoError(t, err)
assert.Equal(t, tc.fileContent, actualContent)
if resFile != nil {
assert.Equal(t, fileHeader.Filename, resFile.FileName)
assert.Equal(t, expectedFilePath, resFile.FilePath)
assert.Equal(t, filepath.Ext(fileHeader.Filename), resFile.FileExtension)
}
})
}
}
func TestRemoveExifMetadata(t *testing.T) {
testCases := []struct {
name string
setupFunc func() (string, error)
expectError bool
}{
{
name: "Exif metadata removed successfully",
setupFunc: func() (string, error) {
file, err := os.CreateTemp("", "*.jpg")
if err != nil {
return "", err
}
defer file.Close()
// create dummy image
img := image.NewRGBA(image.Rect(0, 0, 100, 100))
if err := jpeg.Encode(file, img, nil); err != nil {
return "", err
}
return file.Name(), nil
},
expectError: false,
},
{
name: "failed to open image",
setupFunc: func() (string, error) {
return "invalid_path.jpg", nil
},
expectError: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
inputPath, err := tc.setupFunc()
if err != nil {
t.Fatal(err)
}
err = RemoveExifMetadata(inputPath)
if tc.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
// Validate output file
file, err := os.Open(inputPath)
assert.NoError(t, err)
defer file.Close()
_, _, err = image.Decode(file)
assert.NoError(t, err)
}
if _, err = os.Stat(inputPath); err == nil {
os.Remove(inputPath)
}
})
}
}