Backend : token_version (000022, claim tv, 401 si révoqué, bump au password — fin limite V1), quota (000023 idempotente, colonne héritée de 000001, 413 QUOTA_EXCEEDED, GET /users/me/usage), devices (GET + DELETE révocation, EnforceDeviceExists en prod), renames concurrents transparents (baseUpdatedAt + conflicts[] non-fataux, tolérance 2 s). Tests service + handlers + migrations + auth. Mobile : pipeline conflits (DTO + SyncConflictStore + worker + dialogue), baseUpdatedAt prêt, liens 7j par défaut, écran Appareils (+ quota), import classeur unifié (scan/note/dossier), onboarding cercle Famille. Contrat docs/api-v1.md, V3.md (S6 livré).
153 lines
6.0 KiB
Go
153 lines
6.0 KiB
Go
package handlers_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/kazier/backend/handlers"
|
|
"github.com/kazier/backend/repository"
|
|
)
|
|
|
|
// TestPasswordChangeRevokesTokens : le bump de token_version invalide tous
|
|
// les tokens antérieurs (V3 S6) — l'ancien token devient 401, un nouveau
|
|
// login fonctionne.
|
|
func TestPasswordChangeRevokesTokens(t *testing.T) {
|
|
r, _, repo := setup(t)
|
|
device := repository.NewID()
|
|
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "rv"), "revoke-test-password", device)
|
|
|
|
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files", token, nil, "")
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("token initial rejeté: %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
|
|
body := `{"current_password":"revoke-test-password","new_password":"revoke-test-password-2"}`
|
|
rec, _ = doRequest(t, r, http.MethodPatch, "/api/v1/users/me/password", token, []byte(body), "application/json")
|
|
expectOK(t, rec, "password-change")
|
|
|
|
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files", token, nil, "")
|
|
expectError(t, rec, http.StatusUnauthorized, "UNAUTHORIZED", "revoked-token")
|
|
|
|
// Nouveau login (nouvelle version) : fonctionne.
|
|
body = fmt.Sprintf(`{"username":%q,"password":"revoke-test-password-2","device_id":%q}`,
|
|
testUserUsername(device, "rv"), device)
|
|
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/auth/login", "", []byte(body), "application/json")
|
|
expectOK(t, rec, "relogin")
|
|
var login struct {
|
|
Data struct {
|
|
Token string `json:"token"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &login); err != nil || login.Data.Token == "" {
|
|
t.Fatalf("relogin sans token: %s", rec.Body.String())
|
|
}
|
|
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files", login.Data.Token, nil, "")
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("nouveau token rejeté: %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
// TestDevicesListAndRevoke : liste les devices du compte, révoque (A1) —
|
|
// avec EnforceDeviceExists, le token du device révoqué devient 401 et son
|
|
// login est refusé (ré-enregistrement requis).
|
|
func TestDevicesListAndRevoke(t *testing.T) {
|
|
r, _, repo := setup(t)
|
|
handlers.EnforceDeviceExists = true
|
|
defer func() { handlers.EnforceDeviceExists = false }()
|
|
device := repository.NewID()
|
|
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "dv"), "devices-test-password", device)
|
|
|
|
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/devices", token, nil, "")
|
|
env := expectOK(t, rec, "devices-list")
|
|
var devices []struct {
|
|
DeviceID string `json:"deviceId"`
|
|
LastSeenAt *int64 `json:"lastSeenAt"`
|
|
}
|
|
if err := json.Unmarshal(env.Data, &devices); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
found := false
|
|
for _, d := range devices {
|
|
if d.DeviceID == device {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("device absent de la liste: %+v", devices)
|
|
}
|
|
|
|
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/devices/"+device, token, nil, "")
|
|
expectOK(t, rec, "devices-revoke")
|
|
|
|
// Token existant → 401 (device inconnu).
|
|
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files", token, nil, "")
|
|
expectError(t, rec, http.StatusUnauthorized, "UNAUTHORIZED", "revoked-device-token")
|
|
|
|
// Login refusé (device inconnu, indistinguable — 401).
|
|
body := fmt.Sprintf(`{"username":%q,"password":"devices-test-password","device_id":%q}`,
|
|
testUserUsername(device, "dv"), device)
|
|
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/auth/login", "", []byte(body), "application/json")
|
|
expectError(t, rec, http.StatusUnauthorized, "UNAUTHORIZED", "revoked-device-login")
|
|
|
|
// Révoquer l'inexistant / malformé → 404 (avec un token encore valide).
|
|
deviceB := repository.NewID()
|
|
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "dv2"), "devices-test-password", deviceB)
|
|
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/devices/"+repository.NewID(), tokenB, nil, "")
|
|
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "revoke-unknown")
|
|
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/devices/nope", tokenB, nil, "")
|
|
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "revoke-malformed")
|
|
}
|
|
|
|
// TestUsageAndQuota : GET /users/me/usage reflète l'état, et un upload
|
|
// au-delà du quota est rejeté en 413 QUOTA_EXCEEDED (V3 S6 A2).
|
|
func TestUsageAndQuota(t *testing.T) {
|
|
r, store, repo := setup(t)
|
|
_ = store
|
|
device := repository.NewID()
|
|
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "qu"), "quota-test-password", device)
|
|
|
|
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/users/me/usage", token, nil, "")
|
|
env := expectOK(t, rec, "usage-empty")
|
|
var usage struct {
|
|
UsedBytes int64 `json:"usedBytes"`
|
|
QuotaBytes int64 `json:"quotaBytes"`
|
|
FileCount int `json:"fileCount"`
|
|
TrashCount int `json:"trashCount"`
|
|
TrashedBytes int64 `json:"trashedBytes"`
|
|
}
|
|
if err := json.Unmarshal(env.Data, &usage); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if usage.UsedBytes != 0 || usage.QuotaBytes != 10737418240 || usage.FileCount != 0 {
|
|
t.Fatalf("usage initial inattendu: %+v", usage)
|
|
}
|
|
|
|
// Quota miniature (10 octets) → l'upload passe sous le seuil puis casse.
|
|
if _, err := repo.Users.DB.Exec(`UPDATE users SET storage_quota_bytes = 10 WHERE id = $1`, userID); err != nil {
|
|
t.Fatalf("shrink quota: %v", err)
|
|
}
|
|
rec = uploadMultipart(t, r, token, "", "a.txt", []byte("12345"))
|
|
expectOK(t, rec, "usage-under-quota")
|
|
|
|
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/users/me/usage", token, nil, "")
|
|
env = expectOK(t, rec, "usage-after")
|
|
usage = struct {
|
|
UsedBytes int64 `json:"usedBytes"`
|
|
QuotaBytes int64 `json:"quotaBytes"`
|
|
FileCount int `json:"fileCount"`
|
|
TrashCount int `json:"trashCount"`
|
|
TrashedBytes int64 `json:"trashedBytes"`
|
|
}{}
|
|
if err := json.Unmarshal(env.Data, &usage); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if usage.UsedBytes != 5 || usage.FileCount != 1 {
|
|
t.Fatalf("usage après upload: %+v", usage)
|
|
}
|
|
|
|
rec = uploadMultipart(t, r, token, "", "b.txt", []byte("123456"))
|
|
expectError(t, rec, http.StatusRequestEntityTooLarge, "QUOTA_EXCEEDED", "usage-over-quota")
|
|
}
|