Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0nkery committed Jul 18, 2023
1 parent 2ea63b2 commit c35b7f0
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 91 deletions.
71 changes: 23 additions & 48 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ sqlx = { version = "0.6", features = ["offline", "postgres", "chrono", "uuid", "
svc-agent = { version = "0.21", features = ["sqlx"] }
svc-authn = { version = "0.8", features = ["jose"] }
svc-authz = "0.12"
svc-events = { version = "0.10" }
svc-events = { version = "0.11" }
svc-error = { version = "0.6", features = [
"r2d2",
"svc-agent",
"svc-authn",
"svc-authz",
"sentry-extension",
] }
svc-nats-client = { version = "0.7" }
# svc-nats-client = { version = "0.7" }
svc-nats-client = { git = "https://github.com/foxford/svc-nats-client", branch = "add-test-client" }
svc-utils = { version = "0.8", features = ["cors-middleware", "authn-extractor"] }
thiserror = "1.0"
tokio = { version = "1", features = ["full"] }
Expand Down
10 changes: 5 additions & 5 deletions src/app/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub trait GlobalContext {
fn mqtt_gateway_client(&self) -> &MqttGatewayHttpClient;
fn conference_client(&self) -> &ConferenceHttpClient;
fn mqtt_client(&self) -> &Mutex<dyn MqttClient>;
fn nats_client(&self) -> Option<&dyn NatsClient>;
fn nats_client(&self) -> Option<Arc<dyn NatsClient>>;
fn get_conn(&self) -> BoxFuture<Result<sqlx::pool::PoolConnection<sqlx::Postgres>, AppError>> {
let db = self.db().clone();
async move {
Expand Down Expand Up @@ -89,7 +89,7 @@ impl GlobalContext for Arc<dyn GlobalContext> {
self.as_ref().mqtt_client()
}

fn nats_client(&self) -> Option<&dyn NatsClient> {
fn nats_client(&self) -> Option<Arc<dyn NatsClient>> {
self.as_ref().nats_client()
}
}
Expand Down Expand Up @@ -207,8 +207,8 @@ impl GlobalContext for AppContext {
self.mqtt_client.as_ref()
}

fn nats_client(&self) -> Option<&dyn NatsClient> {
self.nats_client.as_deref()
fn nats_client(&self) -> Option<Arc<dyn NatsClient>> {
self.nats_client.clone()
}
}

Expand Down Expand Up @@ -269,7 +269,7 @@ impl<'a, C: GlobalContext> GlobalContext for AppMessageContext<'a, C> {
self.global_context.mqtt_client()
}

fn nats_client(&self) -> Option<&dyn NatsClient> {
fn nats_client(&self) -> Option<Arc<dyn NatsClient>> {
self.global_context.nats_client()
}
}
Expand Down
53 changes: 53 additions & 0 deletions src/app/stage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,56 @@ async fn handle_ban_accepted(

Ok(())
}

#[cfg(test)]
mod tests {
use crate::test_helpers::{db::TestDb, prelude::*, test_deps::LocalDeps};

use super::*;

#[sqlx::test]
fn test_ban_accepted_handler_works(pool: sqlx::PgPool) {
let local_deps = LocalDeps::new();
let janus = local_deps.run_janus();

let (session_id, handle_id) = shared_helpers::init_janus(&janus.url).await;
let _user_handle = shared_helpers::create_handle(&janus.url, session_id).await;

let db = TestDb::new(pool);
let ctx = TestContext::new(db, TestAuthz::new()).await;
let agent = TestAgent::new("web", "agent", USR_AUDIENCE);

let mut conn = ctx.get_conn().await.unwrap();

let backend =
shared_helpers::insert_janus_backend(&mut conn, &janus.url, session_id, handle_id)
.await;
let room = shared_helpers::insert_room_with_backend_id(&mut conn, backend.id()).await;

let subject = Subject::new("test".to_string(), room.classroom_id(), "ban".to_string());
let event_id: EventId = ("ban".to_string(), "accepted".to_string(), 0).into();
let headers =
svc_nats_client::test_helpers::HeadersBuilder::new(event_id, agent.agent_id().clone())
.build();
let e = BanAcceptedV1 {
ban: true,
classroom_id: room.classroom_id(),
target_account: agent.account_id().clone(),
operation_id: 0,
};

handle_ban_accepted(&ctx, e, &room, subject, &headers)
.await
.expect("handler failed");

let pub_reqs = ctx.inspect_nats_client().get_publish_requests();
assert_eq!(pub_reqs.len(), 1);

let payload =
serde_json::from_slice::<Event>(&pub_reqs[0].payload).expect("failed to parse event");
assert!(matches!(
payload,
Event::V1(EventV1::BanVideoStreamingCompleted(..))
));
}
}
28 changes: 28 additions & 0 deletions src/db/ban_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,31 @@ impl<'a> FindQuery<'a> {
.await
}
}

#[cfg(test)]
mod tests {
use crate::test_helpers::{db, prelude::*};

use super::*;

#[sqlx::test]
fn finds_ban_account_entry(pool: sqlx::PgPool) {
let db = db::TestDb::new(pool);
let mut conn = db.get_conn().await;

let room = shared_helpers::insert_room(&mut conn).await;
let agent = TestAgent::new("web", "agent", USR_AUDIENCE);

InsertQuery::new(room.classroom_id(), agent.account_id())
.execute(&mut conn)
.await
.expect("failed to insert ban account entry");

let ban_account_entry = FindQuery::new(agent.agent_id())
.execute(&mut conn)
.await
.expect("failed to execute find query");

assert!(ban_account_entry.is_some());
}
}
21 changes: 21 additions & 0 deletions src/db/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,5 +774,26 @@ mod tests {
assert_eq!(rooms.len(), 1);
assert_eq!(rooms[0].0.id(), room2.id());
}

#[sqlx::test]
async fn selects_by_classroom_id_and_by_id(pool: sqlx::PgPool) {
let db = db::TestDb::new(pool);
let mut conn = db.get_conn().await;

let room = shared_helpers::insert_room(&mut conn).await;

let room_by_classroom_id = FindQuery::by_classroom_id(room.classroom_id)
.execute(&mut conn)
.await
.expect("failed to fetch room by classroom id")
.expect("room is not found by classroom id");
assert_eq!(room.id, room_by_classroom_id.id);

let room_by_id = FindQuery::by_id(room.id)
.execute(&mut conn)
.await
.expect("failed to fetch room by id");
assert!(room_by_id.is_some());
}
}
}
Loading

0 comments on commit c35b7f0

Please sign in to comment.