Files
Gardomatic/lib/client/journal_test.go
T
kleiax 904d14b64c
CI / test (push) Canceled after 0s
Initial commit
2026-09-12 22:22:17 +02:00

57 lines
2.0 KiB
Go

package client
import (
"context"
"io"
"net/http"
"strings"
"testing"
)
func TestJournalClientAndAttachments(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && r.URL.Path == "/v1/gardens/4/journal":
_, _ = w.Write([]byte(`{"journal_entries":[{"id":8,"title":"Ernte"}]}`))
case r.Method == http.MethodPost && r.URL.Path == "/v1/gardens/4/journal/8/attachments":
if err := r.ParseMultipartForm(1024); err != nil {
t.Error(err)
http.Error(w, "bad multipart", 400)
return
}
file, header, err := r.FormFile("file")
if err != nil {
t.Error(err)
http.Error(w, "missing file", 400)
return
}
defer file.Close()
data, _ := io.ReadAll(file)
if header.Filename != "ernte.jpg" || string(data) != "jpeg" {
t.Errorf("unexpected upload: %q %q", header.Filename, data)
}
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte(`{"attachment":{"id":9,"entry_id":8,"file_name":"ernte.jpg","media_type":"image/jpeg","size":4}}`))
case r.Method == http.MethodGet && r.URL.Path == "/v1/gardens/4/journal/8/attachments/9":
w.Header().Set("Content-Type", "image/jpeg")
w.Header().Set("Content-Disposition", `inline; filename="ernte.jpg"`)
_, _ = w.Write([]byte("jpeg"))
default:
http.NotFound(w, r)
}
})
apiClient := newTestClient(t, handler)
entries, _, err := apiClient.JournalEntries(context.Background(), 4)
if err != nil || len(entries) != 1 || entries[0].Title != "Ernte" {
t.Fatalf("entries: %#v, %v", entries, err)
}
attachment, _, err := apiClient.UploadJournalAttachment(context.Background(), 4, 8, "ernte.jpg", "image/jpeg", []byte("jpeg"))
if err != nil || attachment.ID != 9 {
t.Fatalf("upload: %#v, %v", attachment, err)
}
download, _, err := apiClient.JournalAttachment(context.Background(), 4, 8, 9)
if err != nil || download.FileName != "ernte.jpg" || !strings.EqualFold(string(download.Data), "jpeg") {
t.Fatalf("download: %#v, %v", download, err)
}
}