This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
storage_test.go
101 lines (80 loc) · 2.43 KB
/
storage_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
package staticbackend
import (
"fmt"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"path"
"strings"
"testing"
"time"
"github.com/staticbackendhq/core/backend"
"github.com/staticbackendhq/core/internal"
"github.com/staticbackendhq/core/middleware"
)
func TestFileUpload(t *testing.T) {
pr, pw := io.Pipe()
writer := multipart.NewWriter(pw)
go func() {
defer writer.Close()
part, err := writer.CreateFormFile("file", "upload.test")
if err != nil {
t.Error(err)
}
if _, err := part.Write([]byte("testing file upload")); err != nil {
t.Error(err)
}
}()
req := httptest.NewRequest("POST", "/storage/upload", pr)
req.Header.Add("Content-Type", writer.FormDataContentType())
req.Header.Set("SB-PUBLIC-KEY", pubKey)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", adminToken))
stdAuth := []middleware.Middleware{
middleware.WithDB(backend.DB, backend.Cache, getStripePortalURL),
middleware.RequireAuth(backend.DB, backend.Cache),
}
// prevent DB (SQLite) from being busy
time.Sleep(35 * time.Millisecond)
w := httptest.NewRecorder()
h := middleware.Chain(http.HandlerFunc(upload), stdAuth...)
h.ServeHTTP(w, req)
if w.Code != 200 {
t.Fatalf("Expected 200, got %d", w.Code)
}
var data backend.SavedFile
if err := parseBody(w.Result().Body, &data); err != nil {
t.Error(err)
}
defer w.Result().Body.Close()
t.Log(data)
// let's remove the web-based prefix to test if file was saved
localFilePath := strings.Replace(data.URL, "http://localhost:8099/localfs", "", -1)
localFilePath = path.Join(os.TempDir(), localFilePath)
if _, err := os.Stat(localFilePath); os.IsNotExist(err) {
t.Errorf("Expected file %s to exists", localFilePath)
}
// test the delete file endpoint
delPath := fmt.Sprintf("/sudostorage/delete?id=%s", data.ID)
resp := dbReq(t, deleteFile, "DELETE", delPath, nil)
if resp.StatusCode != 200 {
t.Errorf("Expected 200, go %d", resp.StatusCode)
}
// the file should not exists anymore
if _, err := os.Stat(localFilePath); !os.IsNotExist(err) {
t.Errorf("Expected file %s to not exists", localFilePath)
}
}
func TestCleanUpFileName(t *testing.T) {
fakeNames := make(map[string]string)
fakeNames[""] = ""
fakeNames["abc.def"] = "abc"
fakeNames["ok!.test"] = "ok"
fakeNames["@file-name_here!.ext"] = "file-name_here"
for k, v := range fakeNames {
if clean := internal.CleanUpFileName(k); clean != v {
t.Errorf("expected %s got %s", v, clean)
}
}
}