101 lines
3.0 KiB
Go
101 lines
3.0 KiB
Go
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)
|
|
}
|
|
}
|