|
1 | 1 | //! WebSocket API tests |
2 | 2 |
|
| 3 | +use actix::Actor; |
| 4 | +use actix_test::start; |
| 5 | +use actix_web::{web, App}; |
| 6 | +use awc::ws::Frame; |
| 7 | +use chrono::Utc; |
| 8 | +use futures_util::StreamExt; |
| 9 | +use serde_json::Value; |
| 10 | +use stackdog::alerting::alert::{AlertSeverity, AlertStatus, AlertType}; |
| 11 | +use stackdog::api::websocket::{self, WebSocketHub}; |
| 12 | +use stackdog::database::models::Alert; |
| 13 | +use stackdog::database::{create_alert, create_pool, init_database}; |
| 14 | + |
| 15 | +async fn read_text_frame<S>(framed: &mut S) -> Value |
| 16 | +where |
| 17 | + S: futures_util::Stream<Item = Result<Frame, awc::error::WsProtocolError>> + Unpin, |
| 18 | +{ |
| 19 | + loop { |
| 20 | + match framed |
| 21 | + .next() |
| 22 | + .await |
| 23 | + .expect("expected websocket frame") |
| 24 | + .expect("valid websocket frame") |
| 25 | + { |
| 26 | + Frame::Text(bytes) => { |
| 27 | + return serde_json::from_slice(&bytes).expect("valid websocket json"); |
| 28 | + } |
| 29 | + Frame::Ping(_) | Frame::Pong(_) => continue, |
| 30 | + other => panic!("unexpected websocket frame: {other:?}"), |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +fn sample_alert(id: &str) -> Alert { |
| 36 | + Alert { |
| 37 | + id: id.to_string(), |
| 38 | + alert_type: AlertType::ThreatDetected, |
| 39 | + severity: AlertSeverity::High, |
| 40 | + message: format!("alert-{id}"), |
| 41 | + status: AlertStatus::New, |
| 42 | + timestamp: Utc::now().to_rfc3339(), |
| 43 | + metadata: None, |
| 44 | + } |
| 45 | +} |
| 46 | + |
3 | 47 | #[cfg(test)] |
4 | 48 | mod tests { |
| 49 | + use super::*; |
| 50 | + |
5 | 51 | #[actix_rt::test] |
6 | | - async fn test_websocket_connection() { |
7 | | - // TODO: Implement when API is ready |
8 | | - assert!(true, "Test placeholder"); |
| 52 | + async fn test_websocket_connection_receives_initial_stats_snapshot() { |
| 53 | + let pool = create_pool(":memory:").unwrap(); |
| 54 | + init_database(&pool).unwrap(); |
| 55 | + create_alert(&pool, sample_alert("a1")).await.unwrap(); |
| 56 | + |
| 57 | + let hub = WebSocketHub::new().start(); |
| 58 | + let pool_for_app = pool.clone(); |
| 59 | + let hub_for_app = hub.clone(); |
| 60 | + let server = start(move || { |
| 61 | + App::new() |
| 62 | + .app_data(web::Data::new(pool_for_app.clone())) |
| 63 | + .app_data(web::Data::new(hub_for_app.clone())) |
| 64 | + .configure(websocket::configure_routes) |
| 65 | + }); |
| 66 | + |
| 67 | + let (_response, mut framed) = awc::Client::new() |
| 68 | + .ws(server.url("/ws")) |
| 69 | + .connect() |
| 70 | + .await |
| 71 | + .unwrap(); |
| 72 | + |
| 73 | + let message = read_text_frame(&mut framed).await; |
| 74 | + assert_eq!(message["type"], "stats:updated"); |
| 75 | + assert_eq!(message["payload"]["alerts_new"], 1); |
9 | 76 | } |
10 | 77 |
|
11 | 78 | #[actix_rt::test] |
12 | | - async fn test_websocket_subscribe() { |
13 | | - // TODO: Implement when API is ready |
14 | | - assert!(true, "Test placeholder"); |
| 79 | + async fn test_websocket_receives_broadcast_events() { |
| 80 | + let pool = create_pool(":memory:").unwrap(); |
| 81 | + init_database(&pool).unwrap(); |
| 82 | + |
| 83 | + let hub = WebSocketHub::new().start(); |
| 84 | + let pool_for_app = pool.clone(); |
| 85 | + let hub_for_app = hub.clone(); |
| 86 | + let server = start(move || { |
| 87 | + App::new() |
| 88 | + .app_data(web::Data::new(pool_for_app.clone())) |
| 89 | + .app_data(web::Data::new(hub_for_app.clone())) |
| 90 | + .configure(websocket::configure_routes) |
| 91 | + }); |
| 92 | + |
| 93 | + let (_response, mut framed) = awc::Client::new() |
| 94 | + .ws(server.url("/ws")) |
| 95 | + .connect() |
| 96 | + .await |
| 97 | + .unwrap(); |
| 98 | + |
| 99 | + let _initial = read_text_frame(&mut framed).await; |
| 100 | + |
| 101 | + websocket::broadcast_event( |
| 102 | + &hub, |
| 103 | + "alert:created", |
| 104 | + serde_json::json!({ "id": "alert-1" }), |
| 105 | + ) |
| 106 | + .await; |
| 107 | + |
| 108 | + let message = read_text_frame(&mut framed).await; |
| 109 | + assert_eq!(message["type"], "alert:created"); |
| 110 | + assert_eq!(message["payload"]["id"], "alert-1"); |
15 | 111 | } |
16 | 112 |
|
17 | 113 | #[actix_rt::test] |
18 | | - async fn test_websocket_receive_events() { |
19 | | - // TODO: Implement when API is ready |
20 | | - assert!(true, "Test placeholder"); |
| 114 | + async fn test_websocket_receives_broadcast_stats_updates() { |
| 115 | + let pool = create_pool(":memory:").unwrap(); |
| 116 | + init_database(&pool).unwrap(); |
| 117 | + |
| 118 | + let hub = WebSocketHub::new().start(); |
| 119 | + let pool_for_app = pool.clone(); |
| 120 | + let hub_for_app = hub.clone(); |
| 121 | + let server = start(move || { |
| 122 | + App::new() |
| 123 | + .app_data(web::Data::new(pool_for_app.clone())) |
| 124 | + .app_data(web::Data::new(hub_for_app.clone())) |
| 125 | + .configure(websocket::configure_routes) |
| 126 | + }); |
| 127 | + |
| 128 | + let (_response, mut framed) = awc::Client::new() |
| 129 | + .ws(server.url("/ws")) |
| 130 | + .connect() |
| 131 | + .await |
| 132 | + .unwrap(); |
| 133 | + |
| 134 | + let initial = read_text_frame(&mut framed).await; |
| 135 | + assert_eq!(initial["type"], "stats:updated"); |
| 136 | + assert_eq!(initial["payload"]["alerts_new"], 0); |
| 137 | + |
| 138 | + create_alert(&pool, sample_alert("a2")).await.unwrap(); |
| 139 | + websocket::broadcast_stats(&hub, &pool).await.unwrap(); |
| 140 | + |
| 141 | + let updated = read_text_frame(&mut framed).await; |
| 142 | + assert_eq!(updated["type"], "stats:updated"); |
| 143 | + assert_eq!(updated["payload"]["alerts_new"], 1); |
21 | 144 | } |
22 | 145 | } |
0 commit comments