4
0

backup_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // backup_test.go — issue #218: el download de la BD completa queda
  2. // restringido a admin y avisa explícitamente de que contiene secrets kv.
  3. // Issue #741: BackupDue (frecuencia y hora fija) y validación del time.
  4. package httpapi_test
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "net/http"
  9. "testing"
  10. "time"
  11. "github.com/gnacho/netpulse/server-go/internal/httpapi"
  12. )
  13. func TestBackupDownloadAdminHeaderYGate(t *testing.T) {
  14. srv := makeTestServer(t)
  15. _, adminCookie, _ := loginCookie(t, srv.URL, "admin", "test123456")
  16. // Admin: 200 + aviso explícito de que la descarga lleva secrets.
  17. req, _ := http.NewRequest("GET", srv.URL+"/api/backup/download", nil)
  18. req.AddCookie(&http.Cookie{Name: "session", Value: adminCookie})
  19. res, err := http.DefaultClient.Do(req)
  20. if err != nil {
  21. t.Fatalf("download: %v", err)
  22. }
  23. res.Body.Close()
  24. if res.StatusCode != http.StatusOK {
  25. t.Fatalf("download admin: got %d want 200", res.StatusCode)
  26. }
  27. if res.Header.Get("X-Netpulse-Backup-Contains-Credentials") != "true" {
  28. t.Fatal("la descarga debe avisar de que contiene credenciales")
  29. }
  30. // Sin sesión: no debe servir el backup.
  31. res2, err := http.Get(srv.URL + "/api/backup/download")
  32. if err != nil {
  33. t.Fatalf("download sin sesión: %v", err)
  34. }
  35. res2.Body.Close()
  36. if res2.StatusCode == http.StatusOK {
  37. t.Fatal("el download sin sesión debe rechazarse")
  38. }
  39. }
  40. func TestBackupDueFrequency(t *testing.T) {
  41. now := time.Date(2026, 9, 11, 12, 0, 0, 0, time.UTC)
  42. cases := []struct {
  43. name string
  44. cfg httpapi.BackupConfig
  45. lastRun time.Time
  46. want bool
  47. }{
  48. {"disabled never", httpapi.BackupConfig{Enabled: false, FrequencyH: 24}, now.Add(-48 * time.Hour), false},
  49. {"never ran and enabled", httpapi.BackupConfig{Enabled: true, FrequencyH: 24}, time.Time{}, true},
  50. {"fresh run", httpapi.BackupConfig{Enabled: true, FrequencyH: 24}, now.Add(-1 * time.Hour), false},
  51. {"expired run", httpapi.BackupConfig{Enabled: true, FrequencyH: 24}, now.Add(-25 * time.Hour), true},
  52. {"exact boundary", httpapi.BackupConfig{Enabled: true, FrequencyH: 1}, now.Add(-1 * time.Hour), true},
  53. }
  54. for _, tc := range cases {
  55. t.Run(tc.name, func(t *testing.T) {
  56. if got := httpapi.BackupDue(tc.cfg, tc.lastRun, now); got != tc.want {
  57. t.Fatalf("BackupDue = %v, want %v", got, tc.want)
  58. }
  59. })
  60. }
  61. }
  62. func TestBackupDueDailyTime(t *testing.T) {
  63. cfg := httpapi.BackupConfig{Enabled: true, FrequencyH: 24, Time: "04:30"}
  64. at := func(h, m int) time.Time { return time.Date(2026, 9, 11, h, m, 0, 0, time.UTC) }
  65. yesterday := time.Date(2026, 9, 10, 20, 0, 0, 0, time.UTC)
  66. cases := []struct {
  67. name string
  68. now time.Time
  69. lastRun time.Time
  70. want bool
  71. }{
  72. {"before window", at(3, 0), yesterday, false},
  73. {"after window, not run today", at(5, 0), yesterday, true},
  74. {"after window, already run today", at(5, 0), at(4, 35), false},
  75. {"exactly at window", at(4, 30), yesterday, true},
  76. {"invalid time falls back to frequency", at(5, 0), yesterday, false}, // freq 24h, ayer 20:00
  77. }
  78. for _, tc := range cases {
  79. t.Run(tc.name, func(t *testing.T) {
  80. c := cfg
  81. if tc.name == "invalid time falls back to frequency" {
  82. c.Time = "9x:99"
  83. }
  84. if got := httpapi.BackupDue(c, tc.lastRun, tc.now); got != tc.want {
  85. t.Fatalf("BackupDue = %v, want %v", got, tc.want)
  86. }
  87. })
  88. }
  89. }
  90. func TestBackupTimeSettingValidation(t *testing.T) {
  91. srv := makeTestServer(t)
  92. _, adminCookie, _ := loginCookie(t, srv.URL, "admin", "test123456")
  93. put := func(body string) (int, httpapi.BackupConfig) {
  94. req, _ := http.NewRequest("PUT", srv.URL+"/api/settings/backup", bytes.NewBufferString(body))
  95. req.Header.Set("Content-Type", "application/json")
  96. req.AddCookie(&http.Cookie{Name: "session", Value: adminCookie})
  97. res, err := http.DefaultClient.Do(req)
  98. if err != nil {
  99. t.Fatalf("put: %v", err)
  100. }
  101. defer res.Body.Close()
  102. var cfg httpapi.BackupConfig
  103. if res.StatusCode == http.StatusOK {
  104. if err := json.NewDecoder(res.Body).Decode(&cfg); err != nil {
  105. t.Fatalf("decode: %v", err)
  106. }
  107. }
  108. return res.StatusCode, cfg
  109. }
  110. if st, cfg := put(`{"time":"04:30"}`); st != http.StatusOK || cfg.Time != "04:30" {
  111. t.Fatalf("time válido: status %d cfg %+v", st, cfg)
  112. }
  113. if st, _ := put(`{"time":"25:99"}`); st != http.StatusBadRequest {
  114. t.Fatalf("time inválido: got %d want 400", st)
  115. }
  116. if st, cfg := put(`{"time":""}`); st != http.StatusOK || cfg.Time != "" {
  117. t.Fatalf("time vacío debe limpiar: status %d cfg %+v", st, cfg)
  118. }
  119. }