This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package shareapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
"github.com/itworx/pulse/internal/problem"
|
||||
"github.com/itworx/pulse/internal/share"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
Provider share.Provider
|
||||
MaxPageSize int
|
||||
}
|
||||
|
||||
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet || (r.URL.Path != "/api/v1/shares" && !strings.HasPrefix(r.URL.Path, "/api/v1/shares/")) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if _, ok := auth.PrincipalFromContext(r.Context()); !ok {
|
||||
problem.Write(w, r, http.StatusUnauthorized, "UNAUTHORIZED", "Authentication required", "Authentication is required to read shares.", nil)
|
||||
return
|
||||
}
|
||||
if err := r.Context().Err(); err != nil {
|
||||
return
|
||||
}
|
||||
snapshot, err := h.snapshot(r)
|
||||
if err != nil {
|
||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return
|
||||
}
|
||||
problem.Write(w, r, http.StatusServiceUnavailable, "SHARES_UNAVAILABLE", "Sharegegevens niet beschikbaar", "De sharegegevens konden niet worden gelezen.", nil)
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(r.URL.Path, "/api/v1/shares/") {
|
||||
id := strings.TrimPrefix(r.URL.Path, "/api/v1/shares/")
|
||||
item, ok := share.ShareByID(snapshot, id)
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
writeJSON(w, struct {
|
||||
Source share.Source `json:"source"`
|
||||
Share share.Share `json:"share"`
|
||||
}{snapshot.Source, item})
|
||||
return
|
||||
}
|
||||
limit := 50
|
||||
if value := r.URL.Query().Get("limit"); value != "" {
|
||||
parsed, parseErr := strconv.Atoi(value)
|
||||
if parseErr != nil {
|
||||
problem.Write(w, r, http.StatusBadRequest, "SHARE_QUERY_INVALID", "Invalid share query", "De sharelimiet is ongeldig.", nil)
|
||||
return
|
||||
}
|
||||
limit = parsed
|
||||
}
|
||||
max := h.MaxPageSize
|
||||
if max == 0 {
|
||||
max = 100
|
||||
}
|
||||
if limit < 1 || limit > max {
|
||||
problem.Write(w, r, http.StatusBadRequest, "SHARE_QUERY_INVALID", "Invalid share query", "De sharelimiet is ongeldig.", nil)
|
||||
return
|
||||
}
|
||||
if limit < len(snapshot.Shares) {
|
||||
snapshot.Shares = snapshot.Shares[:limit]
|
||||
}
|
||||
writeJSON(w, snapshot)
|
||||
}
|
||||
func (h Handler) snapshot(r *http.Request) (share.Snapshot, error) {
|
||||
if h.Provider == nil {
|
||||
return share.UnknownSnapshot(time.Now().UTC(), "shares", "unraid", "source_unavailable"), nil
|
||||
}
|
||||
return h.Provider.Snapshot(r.Context())
|
||||
}
|
||||
func writeJSON(w http.ResponseWriter, value any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "private, max-age=5")
|
||||
_ = json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package shareapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/itworx/pulse/internal/auth"
|
||||
"github.com/itworx/pulse/internal/share"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type provider struct{ snapshot share.Snapshot }
|
||||
|
||||
func (p provider) Snapshot(context.Context) (share.Snapshot, error) { return p.snapshot, nil }
|
||||
func request(method, path string) *http.Request {
|
||||
r := httptest.NewRequest(method, path, nil)
|
||||
return r.WithContext(auth.WithPrincipal(r.Context(), auth.Principal{Subject: "viewer", Role: auth.RoleViewer}))
|
||||
}
|
||||
func TestHandlerAuthUnknownAndMutationBoundary(t *testing.T) {
|
||||
unauth := httptest.NewRecorder()
|
||||
Handler{}.ServeHTTP(unauth, httptest.NewRequest(http.MethodGet, "/api/v1/shares", nil))
|
||||
if unauth.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status=%d", unauth.Code)
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
Handler{}.ServeHTTP(response, request(http.MethodGet, "/api/v1/shares"))
|
||||
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"state":"unknown"`) {
|
||||
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
|
||||
}
|
||||
mutation := httptest.NewRecorder()
|
||||
Handler{}.ServeHTTP(mutation, httptest.NewRequest(http.MethodPost, "/api/v1/shares", nil))
|
||||
if mutation.Code != http.StatusNotFound {
|
||||
t.Fatalf("mutation=%d", mutation.Code)
|
||||
}
|
||||
}
|
||||
func TestHandlerListAndDetail(t *testing.T) {
|
||||
snapshot := share.Snapshot{ContractVersion: share.ContractVersion, Source: share.Source{ID: "fixture-shares", Freshness: share.Fresh, State: share.StateHealthy}, Shares: []share.Share{{ID: "share-media", Name: "Media", SizeState: share.SizeCached}}, Total: 1, ObservedAt: time.Date(2026, 8, 1, 12, 0, 0, 0, time.UTC)}
|
||||
list := httptest.NewRecorder()
|
||||
Handler{Provider: provider{snapshot: snapshot}}.ServeHTTP(list, request(http.MethodGet, "/api/v1/shares?limit=10"))
|
||||
if list.Code != http.StatusOK || !strings.Contains(list.Body.String(), `"id":"share-media"`) {
|
||||
t.Fatalf("status=%d body=%s", list.Code, list.Body.String())
|
||||
}
|
||||
detail := httptest.NewRecorder()
|
||||
Handler{Provider: provider{snapshot: snapshot}}.ServeHTTP(detail, request(http.MethodGet, "/api/v1/shares/share-media"))
|
||||
if detail.Code != http.StatusOK || !strings.Contains(detail.Body.String(), `"name":"Media"`) {
|
||||
t.Fatalf("status=%d body=%s", detail.Code, detail.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user