use std::time::Duration;
use oxisql_postgres::{PgConnection, TlsMode};
const PG_CONN_STR: &str = "host=localhost port=5432 user=postgres";
#[tokio::test]
#[ignore = "requires a live PostgreSQL server"]
async fn test_listen_notify() {
let conn1 = PgConnection::connect(PG_CONN_STR, TlsMode::Disabled)
.await
.expect("conn1 connect");
let conn2 = PgConnection::connect(PG_CONN_STR, TlsMode::Disabled)
.await
.expect("conn2 connect");
let channel = "oxisql_test_ch";
let payload = "hello from oxisql";
let mut stream = conn1.listen(channel).await.expect("listen");
tokio::time::sleep(Duration::from_millis(50)).await;
conn2.notify(channel, payload).await.expect("notify");
let notif = stream
.recv_timeout(Duration::from_secs(2))
.await
.expect("expected notification, got None");
assert_eq!(notif.channel, channel);
assert_eq!(notif.payload, payload);
stream.unlisten().await.expect("unlisten");
}
#[tokio::test]
#[ignore = "requires a live PostgreSQL server"]
async fn test_listen_from_client_fails() {
let (client, connection) = tokio_postgres::connect(PG_CONN_STR, tokio_postgres::NoTls)
.await
.expect("connect raw");
tokio::spawn(async move {
let _: Result<_, _> = connection.await;
});
let conn = PgConnection::from_client(client);
let result = conn.listen("some_channel").await;
assert!(
result.is_err(),
"from_client connection should not support listen"
);
}
#[tokio::test]
#[ignore = "requires a live PostgreSQL server"]
async fn test_listen_invalid_channel() {
let conn = PgConnection::connect(PG_CONN_STR, TlsMode::Disabled)
.await
.expect("connect");
let result = conn.listen("bad channel").await;
assert!(result.is_err(), "channel with space should fail");
let result = conn.listen("").await;
assert!(result.is_err(), "empty channel should fail");
let result = conn.listen("ch; DROP TABLE users; --").await;
assert!(result.is_err(), "SQL injection channel should fail");
}
#[tokio::test]
#[ignore = "requires a live PostgreSQL server"]
async fn test_notify_invalid_channel() {
let conn = PgConnection::connect(PG_CONN_STR, TlsMode::Disabled)
.await
.expect("connect");
let result = conn.notify("bad channel!", "payload").await;
assert!(result.is_err(), "invalid channel should be rejected");
}