@@ -1443,6 +1443,134 @@ func TestUpdateTenant(t *testing.T) {
1443
1443
"UpdateTenant mismatch (-want +got)" )
1444
1444
}
1445
1445
1446
+ func TestUpdateCheckAlerts (t * testing.T ) {
1447
+ orgs := orgs ()
1448
+ testTenant := orgs .findTenantByOrg (1000 )
1449
+ testTenantID := testTenant .id
1450
+ testCheckID := int64 (42 )
1451
+
1452
+ url , mux , cleanup := newTestServer (t )
1453
+ defer cleanup ()
1454
+ mux .Handle ("/api/v1/check/42/alerts" , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1455
+ if err := requireMethod (w , r , http .MethodPut ); err != nil {
1456
+ return
1457
+ }
1458
+
1459
+ if _ , err := requireAuth (orgs , w , r , testTenantID ); err != nil {
1460
+ return
1461
+ }
1462
+
1463
+ var req []model.CheckAlert
1464
+ if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil {
1465
+ errorResponse (w , http .StatusBadRequest , "cannot decode request" )
1466
+ return
1467
+ }
1468
+
1469
+ writeResponse (w , http .StatusOK , req )
1470
+ }))
1471
+
1472
+ c := NewClient (url , testTenant .token , http .DefaultClient )
1473
+
1474
+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
1475
+ defer cancel ()
1476
+
1477
+ alerts := []model.CheckAlert {
1478
+ {
1479
+ Name : "ProbeFailedExecutionsTooHigh" ,
1480
+ Threshold : 95.0 ,
1481
+ Period : "5m" ,
1482
+ Created : 1234567890 ,
1483
+ Modified : 1234567890 ,
1484
+ },
1485
+ {
1486
+ Name : "TLSTargetCertificateCloseToExpiring" ,
1487
+ Threshold : 7.0 , // days until expiration
1488
+ Created : 1234567890 ,
1489
+ Modified : 1234567890 ,
1490
+ },
1491
+ }
1492
+
1493
+ result , err := c .UpdateCheckAlerts (ctx , testCheckID , alerts )
1494
+ require .NoError (t , err )
1495
+ require .NotNil (t , result )
1496
+ require .Len (t , result , 2 )
1497
+
1498
+ // Check first alert
1499
+ require .Equal (t , "ProbeFailedExecutionsTooHigh" , result [0 ].Name )
1500
+ require .Equal (t , 95.0 , result [0 ].Threshold )
1501
+ require .Equal (t , "5m" , result [0 ].Period )
1502
+ require .Equal (t , int64 (1234567890 ), result [0 ].Created )
1503
+ require .Equal (t , int64 (1234567890 ), result [0 ].Modified )
1504
+
1505
+ // Check second alert
1506
+ require .Equal (t , "TLSTargetCertificateCloseToExpiring" , result [1 ].Name )
1507
+ require .Equal (t , 7.0 , result [1 ].Threshold )
1508
+ require .Empty (t , result [1 ].Period )
1509
+ require .Equal (t , int64 (1234567890 ), result [1 ].Created )
1510
+ require .Equal (t , int64 (1234567890 ), result [1 ].Modified )
1511
+ }
1512
+
1513
+ func TestGetCheckAlerts (t * testing.T ) {
1514
+ orgs := orgs ()
1515
+ testTenant := orgs .findTenantByOrg (1000 )
1516
+ testTenantID := testTenant .id
1517
+ testCheckID := int64 (42 )
1518
+
1519
+ url , mux , cleanup := newTestServer (t )
1520
+ defer cleanup ()
1521
+ mux .Handle ("/api/v1/check/42/alerts" , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1522
+ if err := requireMethod (w , r , http .MethodGet ); err != nil {
1523
+ return
1524
+ }
1525
+
1526
+ if _ , err := requireAuth (orgs , w , r , testTenantID ); err != nil {
1527
+ return
1528
+ }
1529
+
1530
+ alerts := []model.CheckAlert {
1531
+ {
1532
+ Name : "ProbeFailedExecutionsTooHigh" ,
1533
+ Threshold : 95.0 ,
1534
+ Period : "5m" ,
1535
+ Created : 1234567890 ,
1536
+ Modified : 1234567890 ,
1537
+ },
1538
+ {
1539
+ Name : "TLSTargetCertificateCloseToExpiring" ,
1540
+ Threshold : 7.0 , // days until expiration
1541
+ Created : 1234567890 ,
1542
+ Modified : 1234567890 ,
1543
+ },
1544
+ }
1545
+
1546
+ writeResponse (w , http .StatusOK , alerts )
1547
+ }))
1548
+
1549
+ c := NewClient (url , testTenant .token , http .DefaultClient )
1550
+
1551
+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
1552
+ defer cancel ()
1553
+
1554
+ result , err := c .GetCheckAlerts (ctx , testCheckID )
1555
+ require .NoError (t , err )
1556
+ require .NotNil (t , result )
1557
+ require .Len (t , result , 2 )
1558
+
1559
+ // Check first alert
1560
+ require .Equal (t , "ProbeFailedExecutionsTooHigh" , result [0 ].Name )
1561
+ require .Equal (t , 95.0 , result [0 ].Threshold )
1562
+ require .Equal (t , "5m" , result [0 ].Period )
1563
+ require .Equal (t , int64 (1234567890 ), result [0 ].Created )
1564
+ require .Equal (t , int64 (1234567890 ), result [0 ].Modified )
1565
+
1566
+ // Check second alert
1567
+ require .Equal (t , "TLSTargetCertificateCloseToExpiring" , result [1 ].Name )
1568
+ require .Equal (t , 7.0 , result [1 ].Threshold )
1569
+ require .Empty (t , result [1 ].Period )
1570
+ require .Equal (t , int64 (1234567890 ), result [1 ].Created )
1571
+ require .Equal (t , int64 (1234567890 ), result [1 ].Modified )
1572
+ }
1573
+
1446
1574
func newTestServer (t * testing.T ) (string , * http.ServeMux , func ()) {
1447
1575
t .Helper ()
1448
1576
0 commit comments