Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/auth-service/src/admin/tests/mutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ async fn mutations_recheck_the_confirmation_after_waiting() {
"the mutation waits while its caller is still confirmed"
);
let expired = auth_time + window + Duration::milliseconds(10) - Utc::now();
// wall-clock: the confirmation ends by the system clock, which paused
// tokio time doesn't move.
tokio::time::sleep(expired.to_std().unwrap_or_default()).await;
logout.commit().await.expect("logout");
}
Expand Down
21 changes: 17 additions & 4 deletions apps/auth-service/src/cli/serve/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use super::*;
use axum::routing::get;
use std::net::SocketAddr;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
sync::mpsc,
};

/// A listener on a free local port, serving `router` until the returned sender is
/// set, and the port's address.
Expand Down Expand Up @@ -38,15 +41,25 @@ async fn a_connection_that_never_finishes_its_headers_is_closed() {
/// then cuts off a request that never ends.
#[tokio::test(start_paused = true)]
async fn a_stop_cuts_off_requests_after_the_grace_period() {
let router = Router::new().route("/", get(std::future::pending::<()>));
let (reached, mut arrivals) = mpsc::unbounded_channel();
let handler = move || {
let reached = reached.clone();
async move {
reached.send(()).expect("the test listens");
std::future::pending::<()>().await;
}
};
let router = Router::new().route("/", get(handler));
let (address, stop, mut server) = serving(router).await;
let mut client = TcpStream::connect(address).await.expect("connect");
client
.write_all(b"GET / HTTP/1.1\r\nHost: auth\r\n\r\n")
.await
.expect("request");
// Let the request reach its handler before stopping.
tokio::task::yield_now().await;
arrivals
.recv()
.await
.expect("the request reaches its handler");
let started = Instant::now();
stop.send_replace(true);
server
Expand Down
7 changes: 5 additions & 2 deletions apps/auth-service/src/security/tests/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ async fn session_lock_rechecks_expiry_after_waiting() {
user_id,
authentication_methods: vec![AuthenticationMethod::Passkey],
authenticated_at: now,
expires_at: now + Duration::seconds(1),
expires_at: now + Duration::hours(1),
user_agent_family: RequestContext::TEST.user_agent_family,
ip: RequestContext::TEST.client_ip,
passkey_confirmed_at: Some(now),
Expand All @@ -151,14 +151,17 @@ async fn session_lock_rechecks_expiry_after_waiting() {
.await
.expect("account lock")
.expect("active account");
let id = session.id;
let change = tokio::spawn({
let database = database.clone();
async move {
let mut change = database.begin().await.expect("change transaction");
Session::lock(&mut change, &session).await.map(drop)
}
});
tokio::time::sleep(std::time::Duration::from_millis(1500)).await;
// The session expires while the change waits, after its transaction began.
holder.until_blocked(1).await;
database.sessions().expire(id).await.expect("expire");
holder.rollback().await.expect("release account");
assert!(matches!(
change.await.expect("change task"),
Expand Down

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

18 changes: 18 additions & 0 deletions crates/database/src/repositories/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,24 @@ impl SessionRepository<'_> {
)
}

/// Makes session `id` expire now: a fixture for expiry.
///
/// # Errors
///
/// Returns [`DatabaseError`] if the update fails.
#[cfg(any(test, feature = "test-util"))]
pub async fn expire(&mut self, id: SessionId) -> Result<(), DatabaseError> {
run!(
self,
execute,
sqlx::query!(
"UPDATE sessions SET expires_at = clock_timestamp() WHERE id = $1",
id.as_uuid(),
)
)?;
Ok(())
}

/// Revokes the active `scope` sessions of `user_id`, with the refresh token
/// families issued from every `scope` session, and returns the IDs of the
/// sessions it revoked. Sessions and families of other accounts are never
Expand Down
Loading