v3 : une session dure jusqu'à ce qu'on la ferme, l'administration ferme celles d'une identité
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,8 @@ use tower::ServiceExt;
|
||||
struct Client {
|
||||
router: axum::Router,
|
||||
cookie: Option<String>,
|
||||
/// The `Set-Cookie` of the last answer, whole.
|
||||
set_cookie: Option<String>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
@@ -17,11 +19,11 @@ impl Client {
|
||||
let db = open_db("sqlite::memory:").await.expect("db");
|
||||
let state = AppState::new(Config::for_tests(), db).await.expect("state");
|
||||
seed::ensure(&state).await.expect("seed");
|
||||
(Self { router: app(state.clone()), cookie: None }, state)
|
||||
(Self { router: app(state.clone()), cookie: None, set_cookie: None }, state)
|
||||
}
|
||||
|
||||
fn with(&self, cookie: Option<String>) -> Self {
|
||||
Self { router: self.router.clone(), cookie }
|
||||
Self { router: self.router.clone(), cookie, set_cookie: None }
|
||||
}
|
||||
|
||||
async fn call(&mut self, method: &str, uri: &str, body: Option<Value>) -> (StatusCode, Value) {
|
||||
@@ -36,6 +38,7 @@ impl Client {
|
||||
.expect("request");
|
||||
let res = self.router.clone().oneshot(req).await.expect("response");
|
||||
let status = res.status();
|
||||
self.set_cookie = res.headers().get(header::SET_COOKIE).map(|v| v.to_str().expect("cookie").to_owned());
|
||||
if let Some(set) = res.headers().get(header::SET_COOKIE) {
|
||||
let raw = set.to_str().expect("cookie").split(';').next().unwrap_or_default().to_owned();
|
||||
if raw.starts_with("ld_session=") {
|
||||
@@ -470,6 +473,63 @@ async fn access_management_is_for_administrators_only() {
|
||||
assert_eq!((s, v["error"].as_str()), (StatusCode::CONFLICT, Some("publication_off")));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_session_lasts_until_it_is_closed() {
|
||||
const FOUR_HUNDRED_DAYS: &str = "Max-Age=34560000";
|
||||
let (mut chiara, state) = Client::new().await;
|
||||
chiara.login("Chiara", false).await;
|
||||
assert!(chiara.set_cookie.as_deref().is_some_and(|c| c.contains(FOUR_HUNDRED_DAYS)), "{:?}", chiara.set_cookie);
|
||||
// Renewed once a day at most: a visit right after changes nothing.
|
||||
chiara.call("GET", "/api/me", None).await;
|
||||
assert_eq!(chiara.set_cookie, None);
|
||||
// Ten days left: the next visit starts the 400 days again, here and in the browser.
|
||||
sqlx::query("UPDATE sessions SET expires_at = strftime('%Y-%m-%dT%H:%M:%SZ', 'now', '+10 days')")
|
||||
.execute(&state.db)
|
||||
.await
|
||||
.expect("age the session");
|
||||
let (_, v) = chiara.call("GET", "/api/me", None).await;
|
||||
assert_eq!(v["me"]["displayName"], "Chiara");
|
||||
assert!(chiara.set_cookie.as_deref().is_some_and(|c| c.contains(FOUR_HUNDRED_DAYS)), "{:?}", chiara.set_cookie);
|
||||
let far: i64 = sqlx::query_scalar(
|
||||
"SELECT COUNT(*) FROM sessions WHERE expires_at > strftime('%Y-%m-%dT%H:%M:%SZ', 'now', '+399 days')",
|
||||
)
|
||||
.fetch_one(&state.db)
|
||||
.await
|
||||
.expect("count");
|
||||
assert_eq!(far, 1);
|
||||
|
||||
// Until an administrator closes them: a lost device, a person who left.
|
||||
let mut phone = chiara.with(None);
|
||||
phone.login("Chiara", false).await;
|
||||
let (s, _) = chiara.call("DELETE", "/api/access/identities/dev:chiara/sessions", None).await;
|
||||
assert_eq!(s, StatusCode::FORBIDDEN);
|
||||
let mut admin = chiara.with(None);
|
||||
admin.login("Yvv", true).await;
|
||||
let sessions_of = |ov: &Value, id: &str| {
|
||||
ov["identities"].as_array().expect("i").iter().find(|i| i["id"] == id).expect("identity")["sessions"].clone()
|
||||
};
|
||||
let (_, ov) = admin.call("GET", "/api/access", None).await;
|
||||
assert_eq!(sessions_of(&ov, "dev:chiara"), 2);
|
||||
assert_eq!(sessions_of(&ov, "dev:yvv"), 0, "the session this page is read with is not counted");
|
||||
let (s, v) = admin.call("DELETE", "/api/access/identities/dev:chiara/sessions", None).await;
|
||||
assert_eq!((s, v["closed"].as_u64()), (StatusCode::OK, Some(2)));
|
||||
for c in [&mut chiara, &mut phone] {
|
||||
let (_, v) = c.call("GET", "/api/me", None).await;
|
||||
assert_eq!(v["me"], Value::Null);
|
||||
}
|
||||
// An administrator's own: the others close, this one stays open.
|
||||
let mut laptop = admin.with(None);
|
||||
laptop.login("Yvv", true).await;
|
||||
let (s, v) = admin.call("DELETE", "/api/access/identities/dev:yvv/sessions", None).await;
|
||||
assert_eq!((s, v["closed"].as_u64()), (StatusCode::OK, Some(1)));
|
||||
let (_, v) = admin.call("GET", "/api/me", None).await;
|
||||
assert_eq!(v["me"]["displayName"], "Yvv");
|
||||
let (_, v) = laptop.call("GET", "/api/me", None).await;
|
||||
assert_eq!(v["me"], Value::Null);
|
||||
let (s, _) = admin.call("DELETE", "/api/access/identities/nobody/sessions", None).await;
|
||||
assert_eq!(s, StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_team_link_makes_one_identity_per_opening() {
|
||||
let (mut admin, _) = Client::new().await;
|
||||
|
||||
Reference in New Issue
Block a user