4
0

rules_api_test.go 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package httpapi_test
  2. import (
  3. "net/http"
  4. "testing"
  5. "github.com/gnacho/netpulse/server-go/internal/alerts"
  6. )
  7. func TestAlertRulesAPI(t *testing.T) {
  8. ts := makeTestServer(t)
  9. _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
  10. res := get(t, ts.URL, "/api/alert-rules", cookie)
  11. if res.StatusCode != http.StatusOK {
  12. t.Fatalf("list empty: %d", res.StatusCode)
  13. }
  14. res.Body.Close()
  15. body := `{"name":"CPU alto","category":"router","enabled":true,"condition":{"metric":"cpu","operator":"gt","threshold":90,"duration":600000000000},"scope":{"type":"global"},"severity":"warn"}`
  16. res = doJSON(t, "POST", ts.URL, "/api/alert-rules", cookie, body)
  17. if res.StatusCode != http.StatusCreated {
  18. t.Fatalf("create: %d", res.StatusCode)
  19. }
  20. res.Body.Close()
  21. rules, err := alerts.ListRules(ts.db)
  22. if err != nil || len(rules) != 1 {
  23. t.Fatalf("list after create: err=%v len=%d", err, len(rules))
  24. }
  25. ruleID := rules[0].ID
  26. res = get(t, ts.URL, "/api/alert-rules/"+ruleID, cookie)
  27. if res.StatusCode != http.StatusOK {
  28. t.Fatalf("get: %d", res.StatusCode)
  29. }
  30. res.Body.Close()
  31. body = `{"name":"CPU muy alto","category":"router","enabled":false,"condition":{"metric":"cpu","operator":"gt","threshold":95,"duration":300000000000},"scope":{"type":"global"},"severity":"critical"}`
  32. res = doJSON(t, "PUT", ts.URL, "/api/alert-rules/"+ruleID, cookie, body)
  33. if res.StatusCode != http.StatusOK {
  34. t.Fatalf("update: %d", res.StatusCode)
  35. }
  36. res.Body.Close()
  37. got, _ := alerts.GetRule(ts.db, ruleID)
  38. if got.Enabled || got.Name != "CPU muy alto" {
  39. t.Fatalf("update not applied: %+v", got)
  40. }
  41. res = doJSON(t, "DELETE", ts.URL, "/api/alert-rules/"+ruleID, cookie, "")
  42. if res.StatusCode != http.StatusOK {
  43. t.Fatalf("delete: %d", res.StatusCode)
  44. }
  45. res.Body.Close()
  46. rules, _ = alerts.ListRules(ts.db)
  47. if len(rules) != 0 {
  48. t.Fatalf("expected 0 rules after delete, got %d", len(rules))
  49. }
  50. }
  51. func TestAlertRulesValidation(t *testing.T) {
  52. ts := makeTestServer(t)
  53. _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
  54. cases := []struct {
  55. name string
  56. body string
  57. }{
  58. {"empty name", `{"name":"","category":"router","enabled":true,"condition":{"metric":"cpu","operator":"gt","threshold":90,"duration":600000000000},"scope":{"type":"global"},"severity":"warn"}`},
  59. {"bad category", `{"name":"x","category":"wifi","enabled":true,"condition":{"metric":"cpu","operator":"gt","threshold":90,"duration":600000000000},"scope":{"type":"global"},"severity":"warn"}`},
  60. {"bad operator", `{"name":"x","category":"router","enabled":true,"condition":{"metric":"cpu","operator":"neq","threshold":90,"duration":600000000000},"scope":{"type":"global"},"severity":"warn"}`},
  61. {"no metric", `{"name":"x","category":"router","enabled":true,"condition":{"metric":"","operator":"gt","threshold":90,"duration":600000000000},"scope":{"type":"global"},"severity":"warn"}`},
  62. {"zero duration", `{"name":"x","category":"router","enabled":true,"condition":{"metric":"cpu","operator":"gt","threshold":90,"duration":0},"scope":{"type":"global"},"severity":"warn"}`},
  63. {"bad scope", `{"name":"x","category":"router","enabled":true,"condition":{"metric":"cpu","operator":"gt","threshold":90,"duration":600000000000},"scope":{"type":"magic"},"severity":"warn"}`},
  64. {"router scope no ids", `{"name":"x","category":"router","enabled":true,"condition":{"metric":"cpu","operator":"gt","threshold":90,"duration":600000000000},"scope":{"type":"router"},"severity":"warn"}`},
  65. {"bad severity", `{"name":"x","category":"router","enabled":true,"condition":{"metric":"cpu","operator":"gt","threshold":90,"duration":600000000000},"scope":{"type":"global"},"severity":"panic"}`},
  66. }
  67. for _, tc := range cases {
  68. t.Run(tc.name, func(t *testing.T) {
  69. res := doJSON(t, "POST", ts.URL, "/api/alert-rules", cookie, tc.body)
  70. if res.StatusCode != http.StatusBadRequest {
  71. t.Fatalf("expected 400, got %d", res.StatusCode)
  72. }
  73. res.Body.Close()
  74. })
  75. }
  76. }