Repository navigation
Expand file tree
/
Copy pathadmin_test.go
More file actions
171 lines (152 loc) · 5.48 KB
/
Copy pathadmin_test.go
File metadata and controls
171 lines (152 loc) · 5.48 KB
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
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestRequireAuthRedirectsWithoutValidSession(t *testing.T) {
srv := &Server{sessionKey: []byte("01234567890123456789012345678901")}
req := httptest.NewRequest(http.MethodGet, "/admin/", nil)
req.RemoteAddr = "127.0.0.1:1234"
rec := httptest.NewRecorder()
srv.requireAuth(func(w http.ResponseWriter, r *http.Request) {
t.Fatal("wrapped handler should not be called without a valid session")
})(rec, req)
if rec.Code != http.StatusSeeOther {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusSeeOther)
}
if loc := rec.Header().Get("Location"); loc != "/admin/login" {
t.Fatalf("Location = %q, want /admin/login", loc)
}
}
func TestHandleAdminLoginSetsCookie(t *testing.T) {
sessionKey := []byte("01234567890123456789012345678901")
srv := &Server{config: Config{AdminPassword: testArgon2idHash("secret")}, sessionKey: sessionKey}
form := url.Values{"password": {"secret"}}
req := httptest.NewRequest(http.MethodPost, "/admin/login", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.RemoteAddr = "127.0.0.1:1234"
rec := httptest.NewRecorder()
srv.handleAdminLogin(rec, req)
if rec.Code != http.StatusSeeOther {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusSeeOther)
}
cookies := rec.Result().Cookies()
if len(cookies) != 1 {
t.Fatalf("cookies = %d, want 1", len(cookies))
}
cookie := cookies[0]
if cookie.Name != adminCookieName || !cookie.HttpOnly || !cookie.Secure || cookie.Path != "/admin" {
t.Fatalf("cookie = %#v, want secure admin session cookie", cookie)
}
if !validateSession(sessionKey, cookie.Value, "127.0.0.1", adminSessionMaxAge) {
t.Fatal("session cookie did not validate")
}
}
func TestHandleAdminDeleteValidatesPath(t *testing.T) {
srv := &Server{}
form := url.Values{"path": {"../bad"}}
req := httptest.NewRequest(http.MethodPost, "/admin/delete", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rec := httptest.NewRecorder()
srv.handleAdminDelete(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest)
}
}
func TestHandleAdminRendersDashboard(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/admin/", nil)
rec := httptest.NewRecorder()
(&Server{}).handleAdmin(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}
body := rec.Body.String()
for _, want := range []string{"Temporary Storage", "Permanent Storage"} {
if !strings.Contains(body, want) {
t.Fatalf("dashboard missing %q:\n%s", want, body)
}
}
}
func TestListStorageFilesPrefersExtractedDirectory(t *testing.T) {
key := strings.Repeat("B", keyLength)
keyDir := filepath.Join(storageBase, "t", key)
if err := os.MkdirAll(filepath.Join(keyDir, "archive"), 0o700); err != nil {
t.Skipf("cannot create storageBase fixture %s: %v", keyDir, err)
}
t.Cleanup(func() { os.RemoveAll(filepath.Join(storageBase, "t", key)) })
if err := os.WriteFile(filepath.Join(keyDir, "archive.tar"), []byte("tar"), 0o600); err != nil {
t.Fatalf("write archive: %v", err)
}
if err := os.WriteFile(filepath.Join(keyDir, "archive.tar.meta.json"), []byte("{}"), 0o600); err != nil {
t.Fatalf("write metadata: %v", err)
}
files, err := (&Server{}).listStorageFiles("t")
if err != nil {
t.Fatalf("listStorageFiles: %v", err)
}
for _, file := range files {
if file.Key == key {
if file.Name != "archive" || !file.IsDir {
t.Fatalf("entry = %#v, want extracted archive directory", file)
}
return
}
}
t.Fatalf("key %s not found in files: %#v", key, files)
}
func TestListStorageFilesUsesMetadataMTimeAndSortsNewestFirst(t *testing.T) {
base := filepath.Join(storageBase, "t")
if err := os.MkdirAll(base, 0o700); err != nil {
t.Skipf("cannot create storageBase fixture %s: %v", base, err)
}
oldKey := strings.Repeat("D", keyLength)
newKey := strings.Repeat("E", keyLength)
t.Cleanup(func() {
os.RemoveAll(filepath.Join(base, oldKey))
os.RemoveAll(filepath.Join(base, newKey))
})
writeAdminFixture(t, oldKey, "old.txt", time.Date(2026, 1, 1, 10, 0, 0, 0, time.UTC))
writeAdminFixture(t, newKey, "new.txt", time.Date(2026, 1, 2, 10, 0, 0, 0, time.UTC))
files, err := (&Server{}).listStorageFiles("t")
if err != nil {
t.Fatalf("listStorageFiles: %v", err)
}
var seen []adminFileEntry
for _, file := range files {
if file.Key == oldKey || file.Key == newKey {
seen = append(seen, file)
}
}
if len(seen) != 2 {
t.Fatalf("seen = %#v, want both fixture keys", seen)
}
if seen[0].Key != newKey || seen[1].Key != oldKey {
t.Fatalf("order = %#v, want newest metadata first", seen)
}
if !seen[0].Modified.After(seen[1].Modified) {
t.Fatalf("modified times not descending: %#v", seen)
}
}
func writeAdminFixture(t *testing.T, key, name string, metaTime time.Time) {
t.Helper()
keyDir := filepath.Join(storageBase, "t", key)
if err := os.MkdirAll(keyDir, 0o700); err != nil {
t.Fatalf("mkdir key fixture: %v", err)
}
if err := os.WriteFile(filepath.Join(keyDir, name), []byte("file"), 0o600); err != nil {
t.Fatalf("write file fixture: %v", err)
}
metaPath := filepath.Join(keyDir, name+".meta.json")
if err := os.WriteFile(metaPath, []byte("{}"), 0o600); err != nil {
t.Fatalf("write metadata fixture: %v", err)
}
if err := os.Chtimes(metaPath, metaTime, metaTime); err != nil {
t.Fatalf("chtimes metadata fixture: %v", err)
}
}