api use
CI / lint-backend (push) Successful in 1m24s
CI / lint-mobile (push) Failing after 7m28s
CI / test-backend (push) Successful in 1m25s
CI / test-mobile (push) Successful in 9m9s
CI / build-backend (push) Successful in 45s
CI / docker (push) Successful in 33s
CI / lint-backend (push) Successful in 1m24s
CI / lint-mobile (push) Failing after 7m28s
CI / test-backend (push) Successful in 1m25s
CI / test-mobile (push) Successful in 9m9s
CI / build-backend (push) Successful in 45s
CI / docker (push) Successful in 33s
This commit is contained in:
@@ -747,3 +747,86 @@ func TestFilesContentStreaming(t *testing.T) {
|
||||
t.Errorf("content-sans-token: attendu 401, got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesSharesLinksListing(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
ownerDevice := repository.NewID()
|
||||
ownerToken, owner := registerAndLogin(t, r, repo, testUserUsername(ownerDevice, "sl"), "shares-list-password", ownerDevice)
|
||||
granteeDevice := repository.NewID()
|
||||
granteeToken, grantee := registerAndLogin(t, r, repo, testUserUsername(granteeDevice, "sg"), "shares-list-password-b", granteeDevice)
|
||||
outsiderDevice := repository.NewID()
|
||||
outsiderToken, _ := registerAndLogin(t, r, repo, testUserUsername(outsiderDevice, "sh"), "shares-list-password-c", outsiderDevice)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(owner, fileID, "shared.txt", "", 4, nil, nil); err != nil {
|
||||
t.Fatalf("insert file: %v", err)
|
||||
}
|
||||
|
||||
// Vide au départ (pas d'erreur).
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/shares", ownerToken, nil, "")
|
||||
env := expectOK(t, rec, "shares-empty")
|
||||
var emptyShares []map[string]any
|
||||
if err := json.Unmarshal(env.Data, &emptyShares); err != nil {
|
||||
t.Fatalf("shares-empty: unmarshal: %v", err)
|
||||
}
|
||||
if len(emptyShares) != 0 {
|
||||
t.Errorf("shares-empty = %d grants, attendu 0", len(emptyShares))
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/links", ownerToken, nil, "")
|
||||
env = expectOK(t, rec, "links-empty")
|
||||
var emptyLinks []map[string]any
|
||||
if err := json.Unmarshal(env.Data, &emptyLinks); err != nil {
|
||||
t.Fatalf("links-empty: unmarshal: %v", err)
|
||||
}
|
||||
if len(emptyLinks) != 0 {
|
||||
t.Errorf("links-empty = %d links, attendu 0", len(emptyLinks))
|
||||
}
|
||||
|
||||
// Un grant + un lien côté serveur (comme après des ops `share`/`create_link`).
|
||||
if err := repo.Shares.Upsert(fileID, grantee, "editor", true, nil, owner); err != nil {
|
||||
t.Fatalf("share: %v", err)
|
||||
}
|
||||
linkToken := repository.NewID()
|
||||
if err := repo.Shares.CreateLink(linkToken, fileID, "viewer", nil, owner); err != nil {
|
||||
t.Fatalf("create link: %v", err)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/shares", ownerToken, nil, "")
|
||||
env = expectOK(t, rec, "shares-owner")
|
||||
var grants []struct {
|
||||
GranteeUserID string `json:"granteeUserId"`
|
||||
Username string `json:"username"`
|
||||
Access string `json:"access"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &grants); err != nil {
|
||||
t.Fatalf("shares-owner: unmarshal: %v", err)
|
||||
}
|
||||
if len(grants) != 1 || grants[0].GranteeUserID != grantee || grants[0].Access != "editor" || grants[0].Username == "" {
|
||||
t.Errorf("shares-owner = %+v, attendu 1 grant editor du grantee", grants)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/links", ownerToken, nil, "")
|
||||
env = expectOK(t, rec, "links-owner")
|
||||
var links []struct {
|
||||
Token string `json:"token"`
|
||||
Access string `json:"access"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &links); err != nil {
|
||||
t.Fatalf("links-owner: unmarshal: %v", err)
|
||||
}
|
||||
if len(links) != 1 || links[0].Token != linkToken || links[0].Access != "viewer" {
|
||||
t.Errorf("links-owner = %+v, attendu 1 lien viewer", links)
|
||||
}
|
||||
|
||||
// Granté et outsider : 404 (pas d'énumération).
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/shares", granteeToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "shares-grantee")
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/links", granteeToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "links-grantee")
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/shares", outsiderToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "shares-outsider")
|
||||
|
||||
// ID invalide → 404.
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/not-hex/shares", ownerToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "shares-invalid-id")
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ func RegisterRoutes(r *gin.Engine) {
|
||||
protected.GET("/files/:id", FilesGet)
|
||||
protected.GET("/files/:id/ocr", FilesGetOcr)
|
||||
protected.GET("/files/:id/content", FilesContent)
|
||||
protected.GET("/files/:id/shares", SharesList)
|
||||
protected.GET("/files/:id/links", LinksList)
|
||||
protected.DELETE("/files/:id", FilesDelete)
|
||||
protected.POST("/files/upload", FilesUpload)
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
)
|
||||
|
||||
// SharesList exposes the active user-to-user grants of a resource the caller
|
||||
// OWNS : GET /files/:id/shares. Non-owner (or unknown resource) → 404
|
||||
// NOT_FOUND (no enumeration, same scoping as the share outbox ops).
|
||||
func SharesList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
grants, err := Store.ListShares(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, grants)
|
||||
}
|
||||
|
||||
// LinksList exposes the active public links of a resource the caller OWNS :
|
||||
// GET /files/:id/links. Same owner-only scoping as SharesList.
|
||||
func LinksList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
links, err := Store.ListLinks(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, links)
|
||||
}
|
||||
Reference in New Issue
Block a user