|
| 1 | +package handlers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/Prescott-Data/nexus-framework/nexus-broker/pkg/handlers" |
| 11 | + "github.com/google/uuid" |
| 12 | + "github.com/jmoiron/sqlx" |
| 13 | + sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1" |
| 14 | +) |
| 15 | + |
| 16 | +// newSqlxDB wraps a sqlmock connection in a sqlx.DB so handlers can use it. |
| 17 | +func newSqlxDB(t *testing.T) (*sqlx.DB, sqlmock.Sqlmock) { |
| 18 | + t.Helper() |
| 19 | + db, mock, err := sqlmock.New() |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("failed to create sqlmock: %v", err) |
| 22 | + } |
| 23 | + return sqlx.NewDb(db, "postgres"), mock |
| 24 | +} |
| 25 | + |
| 26 | +func TestAuditList_NoFilters_ReturnsDefaultLimit(t *testing.T) { |
| 27 | + db, mock := newSqlxDB(t) |
| 28 | + defer db.Close() |
| 29 | + |
| 30 | + id := uuid.New() |
| 31 | + connID := uuid.New() |
| 32 | + now := time.Now() |
| 33 | + |
| 34 | + rows := sqlmock.NewRows([]string{ |
| 35 | + "id", "connection_id", "event_type", "event_data", "ip_address", "user_agent", "created_at", |
| 36 | + }).AddRow( |
| 37 | + id, &connID, "provider.created", `{"name":"google"}`, "127.0.0.1", "curl/7.88", now, |
| 38 | + ) |
| 39 | + |
| 40 | + mock.ExpectQuery(`SELECT id, connection_id, event_type, event_data, ip_address, user_agent, created_at`). |
| 41 | + WithArgs(50). // default limit |
| 42 | + WillReturnRows(rows) |
| 43 | + |
| 44 | + handler := handlers.NewAuditHandler(db) |
| 45 | + req := httptest.NewRequest(http.MethodGet, "/audit", nil) |
| 46 | + w := httptest.NewRecorder() |
| 47 | + |
| 48 | + handler.List(w, req) |
| 49 | + |
| 50 | + if w.Code != http.StatusOK { |
| 51 | + t.Fatalf("expected 200, got %d", w.Code) |
| 52 | + } |
| 53 | + |
| 54 | + var result []map[string]interface{} |
| 55 | + if err := json.NewDecoder(w.Body).Decode(&result); err != nil { |
| 56 | + t.Fatalf("failed to decode response: %v", err) |
| 57 | + } |
| 58 | + if len(result) != 1 { |
| 59 | + t.Fatalf("expected 1 event, got %d", len(result)) |
| 60 | + } |
| 61 | + if result[0]["event_type"] != "provider.created" { |
| 62 | + t.Errorf("expected event_type provider.created, got %v", result[0]["event_type"]) |
| 63 | + } |
| 64 | + |
| 65 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 66 | + t.Errorf("unmet sqlmock expectations: %v", err) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestAuditList_EventTypeFilter(t *testing.T) { |
| 71 | + db, mock := newSqlxDB(t) |
| 72 | + defer db.Close() |
| 73 | + |
| 74 | + rows := sqlmock.NewRows([]string{ |
| 75 | + "id", "connection_id", "event_type", "event_data", "ip_address", "user_agent", "created_at", |
| 76 | + }) // empty result |
| 77 | + |
| 78 | + mock.ExpectQuery(`SELECT id, connection_id, event_type, event_data, ip_address, user_agent, created_at`). |
| 79 | + WithArgs("provider.deleted", 50). |
| 80 | + WillReturnRows(rows) |
| 81 | + |
| 82 | + handler := handlers.NewAuditHandler(db) |
| 83 | + req := httptest.NewRequest(http.MethodGet, "/audit?event_type=provider.deleted", nil) |
| 84 | + w := httptest.NewRecorder() |
| 85 | + |
| 86 | + handler.List(w, req) |
| 87 | + |
| 88 | + if w.Code != http.StatusOK { |
| 89 | + t.Fatalf("expected 200, got %d", w.Code) |
| 90 | + } |
| 91 | + |
| 92 | + // Empty result should return [] not null |
| 93 | + var result []map[string]interface{} |
| 94 | + if err := json.NewDecoder(w.Body).Decode(&result); err != nil { |
| 95 | + t.Fatalf("failed to decode response: %v", err) |
| 96 | + } |
| 97 | + if result == nil { |
| 98 | + t.Error("expected empty array, got null") |
| 99 | + } |
| 100 | + |
| 101 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 102 | + t.Errorf("unmet sqlmock expectations: %v", err) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestAuditList_InvalidSinceParam_Returns400(t *testing.T) { |
| 107 | + db, _ := newSqlxDB(t) |
| 108 | + defer db.Close() |
| 109 | + |
| 110 | + handler := handlers.NewAuditHandler(db) |
| 111 | + req := httptest.NewRequest(http.MethodGet, "/audit?since=not-a-date", nil) |
| 112 | + w := httptest.NewRecorder() |
| 113 | + |
| 114 | + handler.List(w, req) |
| 115 | + |
| 116 | + if w.Code != http.StatusBadRequest { |
| 117 | + t.Errorf("expected 400 for invalid since param, got %d", w.Code) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestAuditList_CustomLimit(t *testing.T) { |
| 122 | + db, mock := newSqlxDB(t) |
| 123 | + defer db.Close() |
| 124 | + |
| 125 | + rows := sqlmock.NewRows([]string{ |
| 126 | + "id", "connection_id", "event_type", "event_data", "ip_address", "user_agent", "created_at", |
| 127 | + }) |
| 128 | + |
| 129 | + mock.ExpectQuery(`SELECT id, connection_id, event_type, event_data, ip_address, user_agent, created_at`). |
| 130 | + WithArgs(10). |
| 131 | + WillReturnRows(rows) |
| 132 | + |
| 133 | + handler := handlers.NewAuditHandler(db) |
| 134 | + req := httptest.NewRequest(http.MethodGet, "/audit?limit=10", nil) |
| 135 | + w := httptest.NewRecorder() |
| 136 | + |
| 137 | + handler.List(w, req) |
| 138 | + |
| 139 | + if w.Code != http.StatusOK { |
| 140 | + t.Fatalf("expected 200, got %d", w.Code) |
| 141 | + } |
| 142 | + |
| 143 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 144 | + t.Errorf("unmet sqlmock expectations: %v", err) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func TestAuditList_LimitAboveMax_ClampedTo1000(t *testing.T) { |
| 149 | + db, mock := newSqlxDB(t) |
| 150 | + defer db.Close() |
| 151 | + |
| 152 | + rows := sqlmock.NewRows([]string{ |
| 153 | + "id", "connection_id", "event_type", "event_data", "ip_address", "user_agent", "created_at", |
| 154 | + }) |
| 155 | + |
| 156 | + // limit=9999 should be clamped to 50 (default, since > 1000 is rejected) |
| 157 | + mock.ExpectQuery(`SELECT id, connection_id, event_type, event_data, ip_address, user_agent, created_at`). |
| 158 | + WithArgs(50). |
| 159 | + WillReturnRows(rows) |
| 160 | + |
| 161 | + handler := handlers.NewAuditHandler(db) |
| 162 | + req := httptest.NewRequest(http.MethodGet, "/audit?limit=9999", nil) |
| 163 | + w := httptest.NewRecorder() |
| 164 | + |
| 165 | + handler.List(w, req) |
| 166 | + |
| 167 | + if w.Code != http.StatusOK { |
| 168 | + t.Fatalf("expected 200, got %d", w.Code) |
| 169 | + } |
| 170 | + |
| 171 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 172 | + t.Errorf("unmet sqlmock expectations: %v", err) |
| 173 | + } |
| 174 | +} |
0 commit comments