feat(files): project/task Documents API + oo projects|tasks files
Library (files.go):
- Types: FileEntry, FolderEntry, ProjectFilesResponse.
- GetProjectFiles, GetTaskFiles, GetFile, UploadProjectFile (POST .../upload),
AttachFilesToTask (POST form files=), UploadTaskFile (upload+attach),
DetachTaskFile (DELETE ?fileid=), RenameFile, DeleteFiles, DownloadFile.
- Helpers: FileEntryNumericID, FileEntryTitle, SafeLocalFileName, projectIDFromTaskMap.
http.go: putJSON for JSON PUT bodies.
CLI: oo projects files {list,upload,download,rename,delete}, oo tasks files
{list,upload,detach}; projects_files.go + tasks_files.go.
Tests: files_integration_test.go (live), files_test.go + testdata (decode).
Docs: CHANGELOG 0.6.0, README, AGENTS.md, cmd/oo/main.go tree.
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package onlyoffice
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDecodeProjectFilesEnvelope(t *testing.T) {
|
||||
raw, err := os.ReadFile("testdata/project_files_response.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := responseField(json.RawMessage(raw), "response")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var pf ProjectFilesResponse
|
||||
if err := json.Unmarshal(resp, &pf); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(pf.Folders) != 1 || pf.Folders[0].Title == nil || *pf.Folders[0].Title != "Subfolder" {
|
||||
t.Fatalf("folders: %+v", pf.Folders)
|
||||
}
|
||||
if len(pf.Files) != 1 || pf.Files[0].Title == nil || *pf.Files[0].Title != "readme.txt" {
|
||||
t.Fatalf("files: %+v", pf.Files)
|
||||
}
|
||||
if pf.Files[0].ID == nil || pf.Files[0].ID.String() != "100" {
|
||||
t.Fatalf("file id: %v", pf.Files[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeTaskFilesEnvelope(t *testing.T) {
|
||||
raw, err := os.ReadFile("testdata/task_files_response.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := responseField(json.RawMessage(raw), "response")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var list []*FileEntry
|
||||
if err := json.Unmarshal(resp, &list); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(list) != 1 || list[0].Title == nil || *list[0].Title != "attach.pdf" {
|
||||
t.Fatalf("list: %+v", list)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectIDFromTaskMap(t *testing.T) {
|
||||
m := map[string]any{
|
||||
"projectOwner": map[string]any{"id": float64(33)},
|
||||
}
|
||||
if got := projectIDFromTaskMap(m); got != "33" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
if projectIDFromTaskMap(nil) != "" {
|
||||
t.Fatal("expected empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSafeLocalFileName(t *testing.T) {
|
||||
if got := SafeLocalFileName(" foo/bar.txt "); got != "bar.txt" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
if got := SafeLocalFileName(""); got != "download" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user