feat: add share handlers with password protection and expiry

This commit is contained in:
2026-08-26 23:31:58 +08:00
parent 04d054afdc
commit 7c10fdb50b
2 changed files with 260 additions and 0 deletions
+100
View File
@@ -0,0 +1,100 @@
package server
import (
"bytes"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"yoresee_dropbox/internal/store"
)
func TestCreateShare(t *testing.T) {
dir := t.TempDir()
workspace := filepath.Join(dir, "workspace")
os.MkdirAll(filepath.Join(workspace, "inbox"), 0755)
s, _ := store.Open(filepath.Join(dir, "test.db"))
defer s.Close()
f := &store.File{ID: "file-1", OriginalName: "doc.pdf", StorageName: "uuid-1", Dir: "inbox", Size: 100, CreatedAt: 1750000000}
s.FileCreate(f)
srv := &Server{store: s, workspace: workspace, sessions: make(map[string]int64), shareSessions: make(map[string]string)}
body := bytes.NewBufferString(`{"file_id":"file-1"}`)
req := httptest.NewRequest("POST", "/api/files/file-1/share", body)
req.Header.Set("Content-Type", "application/json")
req.AddCookie(&http.Cookie{Name: "ydropbox_session", Value: "valid"})
srv.sessions["valid"] = 1
mux := http.NewServeMux()
mux.HandleFunc("POST /api/files/{id}/share", srv.handleCreateShare)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusCreated {
t.Errorf("Status = %d, want %d", w.Code, http.StatusCreated)
}
}
func TestShareAccessNoPassword(t *testing.T) {
dir := t.TempDir()
workspace := filepath.Join(dir, "workspace")
os.MkdirAll(filepath.Join(workspace, "inbox"), 0755)
os.WriteFile(filepath.Join(workspace, "inbox", "uuid-1"), []byte("content"), 0644)
s, _ := store.Open(filepath.Join(dir, "test.db"))
defer s.Close()
f := &store.File{ID: "file-1", OriginalName: "doc.pdf", StorageName: "uuid-1", Dir: "inbox", Size: 7, CreatedAt: 1750000000}
s.FileCreate(f)
sh := &store.Share{ID: "share-1", FileID: "file-1", Token: "tok-abc", CreatedAt: 1750000000}
s.ShareCreate(sh)
srv := &Server{store: s, workspace: workspace, sessions: make(map[string]int64), shareSessions: make(map[string]string)}
req := httptest.NewRequest("GET", "/s/tok-abc", nil)
mux := http.NewServeMux()
mux.HandleFunc("GET /s/{token}", srv.handleShareAccess)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Status = %d, want %d", w.Code, http.StatusOK)
}
}
func TestShareAccessExpired(t *testing.T) {
dir := t.TempDir()
workspace := filepath.Join(dir, "workspace")
os.MkdirAll(filepath.Join(workspace, "inbox"), 0755)
s, _ := store.Open(filepath.Join(dir, "test.db"))
defer s.Close()
f := &store.File{ID: "file-1", OriginalName: "doc.pdf", StorageName: "uuid-1", Dir: "inbox", Size: 7, CreatedAt: 1750000000}
s.FileCreate(f)
sh := &store.Share{ID: "share-1", FileID: "file-1", Token: "tok-exp", ExpiresAt: 1, CreatedAt: 1750000000}
s.ShareCreate(sh)
srv := &Server{store: s, workspace: workspace, sessions: make(map[string]int64), shareSessions: make(map[string]string)}
req := httptest.NewRequest("GET", "/s/tok-exp", nil)
mux := http.NewServeMux()
mux.HandleFunc("GET /s/{token}", srv.handleShareAccess)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusGone {
t.Errorf("Status = %d, want %d", w.Code, http.StatusGone)
}
}