Skip to content

Commit

Permalink
WIP adding tests: cfg(test) broken?
Browse files Browse the repository at this point in the history
  • Loading branch information
cikzh committed Feb 11, 2025
1 parent 4afe270 commit 7d8219f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
3 changes: 1 addition & 2 deletions backend/src/authentication/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ pub async fn development_login(
Ok((updated_jar, Json(LoginResponse::from(&user))))
}

#[derive(Serialize, ToSchema)]
#[cfg_attr(test, derive(Deserialize))]
#[derive(Serialize, Deserialize, ToSchema)]
pub struct UserListResponse {
pub users: Vec<User>,
}
Expand Down
3 changes: 3 additions & 0 deletions backend/src/authentication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use utoipa::ToSchema;

pub use self::api::*;

#[cfg(test)]
pub use self::session::Sessions;

mod api;
pub mod error;
mod password;
Expand Down
28 changes: 16 additions & 12 deletions backend/src/authentication/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use axum::{
};
use axum_extra::extract::CookieJar;
use chrono::{DateTime, Utc};
use hyper::Method;
use serde::{Deserialize, Serialize};
use sqlx::{query, query_as, Error, FromRow, SqlitePool};
use utoipa::ToSchema;
Expand Down Expand Up @@ -50,6 +49,10 @@ impl User {
&self.username
}

pub fn last_activity_at(&self) -> Option<DateTime<Utc>> {
self.last_activity_at
}

#[cfg(test)]
pub fn fullname(&self) -> Option<&str> {
self.fullname.as_deref()
Expand Down Expand Up @@ -82,19 +85,20 @@ where

// Update the last_activity_at field when the method is something other than GET
// We also only update if it hasn't been updated in a while
if parts.method != Method::GET {
match user.last_activity_at {
Some(last_activity_at)
if chrono::offset::Utc::now()
.signed_duration_since(last_activity_at)
.num_seconds()
> MIN_UPDATE_LAST_ACTIVITY_AT_SECS =>
{ /* Do nothing */ }
None | Some(_) => {
users.update_last_activity_at(user.id()).await?;
}
// TODO: Enable once #676 is implemented
//if parts.method != Method::GET {
match user.last_activity_at {
Some(last_activity_at)
if chrono::offset::Utc::now()
.signed_duration_since(last_activity_at)
.num_seconds()
> MIN_UPDATE_LAST_ACTIVITY_AT_SECS =>
{ /* Do nothing */ }
None | Some(_) => {
users.update_last_activity_at(user.id()).await?;
}
}
//}

Ok(user)
}
Expand Down
42 changes: 42 additions & 0 deletions backend/tests/user_integration_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#![cfg(test)]

use abacus::authentication::{Sessions, UserListResponse, SESSION_LIFE_TIME};
use hyper::StatusCode;
use sqlx::SqlitePool;
use test_log::test;
use utils::serve_api;

pub mod shared;
pub mod utils;
#[test(sqlx::test(fixtures(path = "../fixtures", scripts("election_2", "users"))))]
async fn test_user_last_activity_at_updating(pool: SqlitePool) {
// Assert the user has no last activity timestamp yet
let addr = serve_api(pool).await;
let url = format!("http://{addr}/api/user");
let response = reqwest::Client::new().get(&url).send().await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let body: UserListResponse = response.json().await.unwrap();
let user = body.users.first().unwrap();
assert!(user.last_activity_at().is_none());

// Call an endpoint using the `FromRequestParts` for `User`
let url = format!("http://{addr}/api/user/whoami");
let sessions = Sessions::new(pool);
let session = sessions.create(1, SESSION_LIFE_TIME).await.unwrap();
let mut cookie = session.get_cookie();
let response = reqwest::Client::new()
.get(&url)
.header("cookie", &cookie.encoded().to_string())
.send()
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);

// Test that a timestamp is present
let url = format!("http://{addr}/api/user");
let response = reqwest::Client::new().get(&url).send().await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let body: UserListResponse = response.json().await.unwrap();
let user = body.users.first().unwrap();
assert!(user.last_activity_at().is_some());
}

0 comments on commit 7d8219f

Please sign in to comment.