|
|
@@ -0,0 +1,62 @@
|
|
|
+// agent_reject_unknown_slug_2219_test.go — #2219: un push con Bearer
|
|
|
+// incorrecto para un slug que no existe dejaba dos filas en kv (timestamp y
|
|
|
+// motivo) sin nada que las limpiara nunca. El badge existe para agentes
|
|
|
+// registrados, así que un slug desconocido no se anota.
|
|
|
+package httpapi_test
|
|
|
+
|
|
|
+import (
|
|
|
+ "net/http"
|
|
|
+ "strings"
|
|
|
+ "testing"
|
|
|
+)
|
|
|
+
|
|
|
+func postIngestRaw(t *testing.T, ts *agentTestServer, token, ip, payload string) int {
|
|
|
+ t.Helper()
|
|
|
+ req, _ := http.NewRequest("POST", ts.URL+"/api/ingest/agent", strings.NewReader(payload))
|
|
|
+ req.Header.Set("Content-Type", "application/json")
|
|
|
+ req.Header.Set("X-Forwarded-For", ip)
|
|
|
+ if token != "" {
|
|
|
+ req.Header.Set("Authorization", "Bearer "+token)
|
|
|
+ }
|
|
|
+ res, err := http.DefaultClient.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("ingest: %v", err)
|
|
|
+ }
|
|
|
+ defer res.Body.Close()
|
|
|
+ return res.StatusCode
|
|
|
+}
|
|
|
+
|
|
|
+func countKVLike(t *testing.T, ts *agentTestServer, like string) int {
|
|
|
+ t.Helper()
|
|
|
+ var n int
|
|
|
+ if err := ts.db.QueryRow("SELECT COUNT(*) FROM kv WHERE key LIKE ?", like).Scan(&n); err != nil {
|
|
|
+ t.Fatalf("contar %s: %v", like, err)
|
|
|
+ }
|
|
|
+ return n
|
|
|
+}
|
|
|
+
|
|
|
+func TestRejectForUnknownSlugWritesNothing(t *testing.T) {
|
|
|
+ ts := makeAgentTestServer(t)
|
|
|
+
|
|
|
+ unknown := `{"router":"no-existe","ts":1764000000,"version":"0.1.0","data":{}}`
|
|
|
+ if got := postIngestRaw(t, ts, "inventado", "10.9.2.1", unknown); got != 401 {
|
|
|
+ t.Fatalf("token inválido para slug desconocido: %d", got)
|
|
|
+ }
|
|
|
+ for _, prefix := range []string{"agent.reject_ts.%", "agent.reject_reason.%"} {
|
|
|
+ if n := countKVLike(t, ts, prefix); n != 0 {
|
|
|
+ t.Fatalf("%s dejó %d fila(s) para un slug sin token", prefix, n)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Un slug registrado sí deja constancia, que es para lo que existe el badge.
|
|
|
+ if status, _, _ := createAgentToken(t, ts, "patio"); status != 201 {
|
|
|
+ t.Fatalf("create: %d", status)
|
|
|
+ }
|
|
|
+ registered := strings.Replace(unknown, "no-existe", "patio", 1)
|
|
|
+ if got := postIngestRaw(t, ts, "inventado", "10.9.2.2", registered); got != 401 {
|
|
|
+ t.Fatalf("token inválido para patio: %d", got)
|
|
|
+ }
|
|
|
+ if n := countKVLike(t, ts, "agent.reject_ts.patio"); n != 1 {
|
|
|
+ t.Fatalf("un slug registrado debe dejar su marca de rechazo, filas=%d", n)
|
|
|
+ }
|
|
|
+}
|