L'API d'administration ne liste pas les objets : le serveur passe par l'API S3 (ListObjectsV2, délimiteur « / », 500 entrées par page) avec une clé temporaire garage-ui-lecture-... limitée à la lecture sur le bucket. La clé est réutilisée pendant la navigation, supprimée après 10 minutes sans requête, à l'arrêt (SIGTERM/SIGINT) et, si le serveur s'est arrêté brutalement, au démarrage suivant. Route GET /api/buckets/objects?id=&prefix=&token=. Interface : panneau « Objets » chargé à la demande (fil d'Ariane, filtre, pagination). Mock : ListObjectsV2 avec préfixe, délimiteur, taille et date. Tests : listing, pagination, droits en lecture seule, aucune écriture S3, expiration et recréation de la clé, purge des clés orphelines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LNfwm38nQMsuGSPgFuRqBy
116 lines
3.8 KiB
Go
116 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func browse(t *testing.T, c *config, ck interface{ String() string }, id, prefix, token string) (int, browsePage) {
|
|
t.Helper()
|
|
h := c.routes()
|
|
req := httptest.NewRequest("GET", "/api/buckets/objects?"+url.Values{"id": {id}, "prefix": {prefix}, "token": {token}}.Encode(), nil)
|
|
req.Header.Set("Cookie", ck.String())
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
var p browsePage
|
|
if rec.Code == 200 {
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return rec.Code, p
|
|
}
|
|
|
|
func TestBrowseBucket(t *testing.T) {
|
|
f := &fakeCluster{objects: map[string]bool{"racine.txt": true, "photos/2026/a.jpg": true, "photos/b.jpg": true, "docs/é x+.pdf": true}, keys: map[string]bool{}}
|
|
for i := 0; i < 700; i++ {
|
|
f.objects[fmt.Sprintf("gros/%03d", i)] = true
|
|
}
|
|
c := newTestConfig(t, f)
|
|
ck := login(t, c.routes())
|
|
id := strings.Repeat("ab", 32)
|
|
|
|
code, p := browse(t, c, ck, id, "", "")
|
|
if code != 200 {
|
|
t.Fatalf("code %d", code)
|
|
}
|
|
if strings.Join(p.Folders, ",") != "docs/,gros/,photos/" || len(p.Objects) != 1 || p.Objects[0].Key != "racine.txt" || p.Objects[0].Size != 10 || p.Objects[0].ETag != "abc" || p.NextToken != "" {
|
|
t.Fatalf("racine : %+v", p)
|
|
}
|
|
|
|
_, p = browse(t, c, ck, id, "photos/", "")
|
|
if strings.Join(p.Folders, ",") != "photos/2026/" || len(p.Objects) != 1 || p.Objects[0].Key != "photos/b.jpg" {
|
|
t.Fatalf("photos/ : %+v", p)
|
|
}
|
|
_, p = browse(t, c, ck, id, "docs/", "")
|
|
if len(p.Objects) != 1 || p.Objects[0].Key != "docs/é x+.pdf" {
|
|
t.Fatalf("docs/ : %+v", p)
|
|
}
|
|
|
|
// Pagination par 500.
|
|
_, p = browse(t, c, ck, id, "gros/", "")
|
|
if len(p.Objects) != browsePageSize || p.NextToken == "" {
|
|
t.Fatalf("gros/ page 1 : %d objets, jeton %q", len(p.Objects), p.NextToken)
|
|
}
|
|
_, p = browse(t, c, ck, id, "gros/", p.NextToken)
|
|
if len(p.Objects) != 200 || p.NextToken != "" {
|
|
t.Fatalf("gros/ page 2 : %d objets, jeton %q", len(p.Objects), p.NextToken)
|
|
}
|
|
|
|
// Une seule clé, en lecture seule, pour toutes ces requêtes ; aucune écriture S3.
|
|
if f.created != 1 || len(f.allow) != 1 || !strings.Contains(f.allow[0], `"read":true`) || !strings.Contains(f.allow[0], `"write":false`) || !strings.Contains(f.allow[0], `"owner":false`) {
|
|
t.Fatalf("%d clés créées, autorisations %v", f.created, f.allow)
|
|
}
|
|
if f.writes != 0 {
|
|
t.Fatalf("%d requêtes S3 d'écriture", f.writes)
|
|
}
|
|
for _, l := range f.lists {
|
|
if !strings.Contains(l, "delimiter=%2F") {
|
|
t.Fatalf("liste sans délimiteur : %s", l)
|
|
}
|
|
}
|
|
|
|
// Expiration : la clé est supprimée, puis recréée à la requête suivante.
|
|
c.expireBrowse(time.Now().Add(time.Hour))
|
|
if len(f.keys) != 0 {
|
|
t.Fatal("clé temporaire non supprimée à l'expiration")
|
|
}
|
|
browse(t, c, ck, id, "", "")
|
|
if f.created != 2 {
|
|
t.Fatalf("clé non recréée : %d", f.created)
|
|
}
|
|
|
|
// Clé supprimée par ailleurs (403) : recréée une fois, sans erreur pour l'utilisateur.
|
|
delete(f.keys, "GKtemp")
|
|
if code, _ := browse(t, c, ck, id, "", ""); code != 200 || f.created != 3 {
|
|
t.Fatalf("après 403 : code %d, %d clés créées", code, f.created)
|
|
}
|
|
|
|
// Paramètres invalides, session absente.
|
|
if code, _ := browse(t, c, ck, "../x", "", ""); code != 400 {
|
|
t.Fatalf("id invalide : %d", code)
|
|
}
|
|
if code, _ := browse(t, c, ck, strings.Repeat("AB", 32), "", ""); code != 400 {
|
|
t.Fatalf("id en majuscules : %d", code)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
c.routes().ServeHTTP(rec, httptest.NewRequest("GET", "/api/buckets/objects?id="+id, nil))
|
|
if rec.Code != 401 {
|
|
t.Fatalf("sans session : %d", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestPurgeBrowseKeys(t *testing.T) {
|
|
f := &fakeCluster{objects: map[string]bool{}, keys: map[string]bool{"GKold": true, "GKuser": true}}
|
|
c := newTestConfig(t, f)
|
|
c.purgeBrowseKeys(t.Context())
|
|
if f.keys["GKold"] || !f.keys["GKuser"] {
|
|
t.Fatalf("clés restantes : %v", f.keys)
|
|
}
|
|
}
|