update_history_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // update_history_test.go — GET /api/updates/history (issue #159).
  2. package httpapi_test
  3. import (
  4. "testing"
  5. "time"
  6. )
  7. func TestUpdateHistoryEndpoint(t *testing.T) {
  8. if !gitAvailable() {
  9. t.Skip("git no disponible")
  10. }
  11. root := t.TempDir()
  12. writeDeployScript(t, root)
  13. writeGitHead(t, root)
  14. ts := makeTestServerWithUpdater(t, root)
  15. // Sembrar una entrada directamente (como la escribiría el updater).
  16. if _, err := ts.db.Exec(
  17. `INSERT INTO update_history
  18. (event_id, ts, action, channel, version_from, version_to, initiated_by, status)
  19. VALUES ('upd-123', ?, 'apply', 'rolling', 'aaa1111', 'bbb2222', 'admin', 'success')`,
  20. time.Now().UnixMilli()); err != nil {
  21. t.Fatal(err)
  22. }
  23. cookie := adminCookie(t, ts)
  24. res := get(t, ts.URL, "/api/updates/history", cookie)
  25. if res.StatusCode != 200 {
  26. t.Fatalf("status: got %d want 200", res.StatusCode)
  27. }
  28. body := readJSON(t, res)
  29. hist, ok := body["history"].([]any)
  30. if !ok || len(hist) == 0 {
  31. t.Fatalf("history: %+v", body)
  32. }
  33. first := hist[0].(map[string]any)
  34. if first["eventId"] != "upd-123" || first["status"] != "success" ||
  35. first["versionFrom"] != "aaa1111" || first["versionTo"] != "bbb2222" {
  36. t.Errorf("entrada: %+v", first)
  37. }
  38. if first["initiatedBy"] != "admin" {
  39. t.Errorf("initiatedBy: %+v", first["initiatedBy"])
  40. }
  41. }
  42. func TestUpdateHistoryRequiresAdmin(t *testing.T) {
  43. if !gitAvailable() {
  44. t.Skip("git no disponible")
  45. }
  46. root := t.TempDir()
  47. writeDeployScript(t, root)
  48. writeGitHead(t, root)
  49. ts := makeTestServerWithUpdater(t, root)
  50. admin := adminCookie(t, ts)
  51. userCookie := createUserAndLogin(t, ts.URL, admin, "viewer", "clave12345", "user")
  52. if got := get(t, ts.URL, "/api/updates/history", userCookie).StatusCode; got != 403 {
  53. t.Errorf("GET history como user: got %d want 403", got)
  54. }
  55. }