(t *testing.T)
| 1401 | } |
| 1402 | |
| 1403 | func TestGetDecisionsCount(t *testing.T) { |
| 1404 | ctx := t.Context() |
| 1405 | |
| 1406 | existingIP := "1.2.3.4" |
| 1407 | unknownIP := "1.2.3.5" |
| 1408 | |
| 1409 | rng, err := csnet.NewRange(existingIP) |
| 1410 | if err != nil { |
| 1411 | t.Errorf("unable to convert '%s' to int: %s", existingIP, err) |
| 1412 | } |
| 1413 | |
| 1414 | // Add sample data to DB |
| 1415 | dbClient = getDBClient(t) |
| 1416 | |
| 1417 | decision := dbClient.Ent.Decision.Create(). |
| 1418 | SetUntil(time.Now().Add(time.Hour)). |
| 1419 | SetScenario("crowdsec/test"). |
| 1420 | SetStartIP(rng.Start.Addr). |
| 1421 | SetStartSuffix(rng.Start.Sfx). |
| 1422 | SetEndIP(rng.End.Addr). |
| 1423 | SetEndSuffix(rng.End.Sfx). |
| 1424 | SetIPSize(int64(rng.Size())). |
| 1425 | SetType("ban"). |
| 1426 | SetScope("IP"). |
| 1427 | SetValue(existingIP). |
| 1428 | SetOrigin("CAPI"). |
| 1429 | SaveX(ctx) |
| 1430 | |
| 1431 | if decision == nil { |
| 1432 | require.Error(t, errors.New("Failed to create sample decision")) |
| 1433 | } |
| 1434 | |
| 1435 | err = Init(dbClient) |
| 1436 | require.NoError(t, err) |
| 1437 | |
| 1438 | tests := []struct { |
| 1439 | name string |
| 1440 | env map[string]any |
| 1441 | code string |
| 1442 | result string |
| 1443 | err string |
| 1444 | }{ |
| 1445 | { |
| 1446 | name: "GetDecisionsCount() test: existing IP count", |
| 1447 | env: map[string]any{ |
| 1448 | "Alert": &models.Alert{ |
| 1449 | Source: &models.Source{ |
| 1450 | Value: &existingIP, |
| 1451 | }, |
| 1452 | Decisions: []*models.Decision{ |
| 1453 | { |
| 1454 | Value: &existingIP, |
| 1455 | }, |
| 1456 | }, |
| 1457 | }, |
| 1458 | }, |
| 1459 | code: "Sprintf('%d', GetDecisionsCount(Alert.GetValue()))", |
| 1460 | result: "1", |
nothing calls this directly
no test coverage detected
searching dependent graphs…