| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | mod common; |
| 7 | |
| 8 | use axum::http::StatusCode; |
| 9 | use common::*; |
| 10 | use serde_json::{Value, json}; |
| 11 | |
| 12 | |
| 13 | async fn entry(app: &axum::Router, who: &str, id: &str) -> Value { |
| 14 | let res = send(app, "GET", "/work/me", Some(who), None).await; |
| 15 | assert_eq!(res.status, StatusCode::OK, "GET /work/me"); |
| 16 | items(&res.body) |
| 17 | .iter() |
| 18 | .find(|e| e["id"] == id) |
| 19 | .unwrap_or_else(|| panic!("entry {id} missing from the log")) |
| 20 | .clone() |
| 21 | } |
| 22 | |
| 23 | |
| 24 | async fn closed_entry(app: &axum::Router, who: &str) -> String { |
| 25 | let opened = send(app, "POST", "/work/check-in", Some(who), None).await; |
| 26 | assert_eq!(opened.status, StatusCode::CREATED, "check in"); |
| 27 | let closed = send(app, "POST", "/work/check-out", Some(who), None).await; |
| 28 | assert_eq!(closed.status, StatusCode::OK, "check out"); |
| 29 | id_of(&closed.body) |
| 30 | } |
| 31 | |
| 32 | #[tokio::test] |
| 33 | async fn concurrent_partial_patches_keep_both_instants() { |
| 34 | let (app, db) = app_and_db().await; |
| 35 | let who = login_as(&app, &db, "mudur", "manager").await; |
| 36 | |
| 37 | |
| 38 | let new_in = 1_000_000_000_000_i64; |
| 39 | let new_out = 2_000_000_000_000_i64; |
| 40 | |
| 41 | for round in 0..20 { |
| 42 | let id = closed_entry(&app, &who).await; |
| 43 | let uri = format!("/work/entries/{id}"); |
| 44 | |
| 45 | let a_patch = json!({ "check_in": new_in }); |
| 46 | let b_patch = json!({ "check_out": new_out }); |
| 47 | let (a, b) = tokio::join!( |
| 48 | send(&app, "PATCH", &uri, Some(&who), Some(a_patch)), |
| 49 | send(&app, "PATCH", &uri, Some(&who), Some(b_patch)), |
| 50 | ); |
| 51 | assert_eq!(a.status, StatusCode::OK, "round {round} check_in patch"); |
| 52 | assert_eq!(b.status, StatusCode::OK, "round {round} check_out patch"); |
| 53 | |
| 54 | let after = entry(&app, &who, &id).await; |
| 55 | assert_eq!( |
| 56 | after["check_in"], new_in, |
| 57 | "round {round}: check_in reverted" |
| 58 | ); |
| 59 | assert_eq!( |
| 60 | after["check_out"], new_out, |
| 61 | "round {round}: check_out reverted" |
| 62 | ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | #[tokio::test] |
| 69 | async fn patch_cannot_reassign_the_stint() { |
| 70 | let (app, db) = app_and_db().await; |
| 71 | let who = login_as(&app, &db, "mudur2", "manager").await; |
| 72 | let other = login_as(&app, &db, "hoca2", "teacher").await; |
| 73 | let other_id = me_id(&app, &other).await; |
| 74 | let mine = me_id(&app, &who).await; |
| 75 | |
| 76 | let id = closed_entry(&app, &who).await; |
| 77 | let res = send( |
| 78 | &app, |
| 79 | "PATCH", |
| 80 | &format!("/work/entries/{id}"), |
| 81 | Some(&who), |
| 82 | Some(json!({ "user": other_id, "id": "hijacked", "check_in": 1_000_000_000_000_i64 })), |
| 83 | ) |
| 84 | .await; |
| 85 | assert_eq!(res.status, StatusCode::OK); |
| 86 | assert_eq!(res.body["user"], mine, "user is server-owned"); |
| 87 | assert_eq!(res.body["id"], id, "id is server-owned"); |
| 88 | assert_eq!(res.body["check_in"], 1_000_000_000_000_i64); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | #[tokio::test] |
| 94 | async fn omitted_and_null_both_keep() { |
| 95 | let (app, db) = app_and_db().await; |
| 96 | let who = login_as(&app, &db, "mudur3", "manager").await; |
| 97 | |
| 98 | let id = closed_entry(&app, &who).await; |
| 99 | let uri = format!("/work/entries/{id}"); |
| 100 | let before = entry(&app, &who, &id).await; |
| 101 | |
| 102 | for body in [json!({}), json!({ "check_in": null, "check_out": null })] { |
| 103 | let res = send(&app, "PATCH", &uri, Some(&who), Some(body)).await; |
| 104 | assert_eq!(res.status, StatusCode::OK); |
| 105 | assert_eq!(res.body["check_in"], before["check_in"]); |
| 106 | assert_eq!(res.body["check_out"], before["check_out"]); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | #[tokio::test] |
| 113 | async fn range_check_still_sees_the_stored_other_side() { |
| 114 | let (app, db) = app_and_db().await; |
| 115 | let who = login_as(&app, &db, "mudur4", "manager").await; |
| 116 | |
| 117 | let id = closed_entry(&app, &who).await; |
| 118 | let res = send( |
| 119 | &app, |
| 120 | "PATCH", |
| 121 | &format!("/work/entries/{id}"), |
| 122 | Some(&who), |
| 123 | Some(json!({ "check_out": 1_000_000_000_000_i64 })), |
| 124 | ) |
| 125 | .await; |
| 126 | assert_eq!( |
| 127 | res.status, |
| 128 | StatusCode::BAD_REQUEST, |
| 129 | "check_out before the stored check_in" |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | #[tokio::test] |
| 139 | async fn concurrent_range_patches_never_invert_the_stored_stint() { |
| 140 | let (app, db) = app_and_db().await; |
| 141 | let who = login_as(&app, &db, "mudur5", "manager").await; |
| 142 | |
| 143 | let base = 1_000_000_000_000_i64; |
| 144 | let end = base + 3_600_000; |
| 145 | |
| 146 | for round in 0..20 { |
| 147 | let id = closed_entry(&app, &who).await; |
| 148 | let uri = format!("/work/entries/{id}"); |
| 149 | |
| 150 | |
| 151 | let widened = send( |
| 152 | &app, |
| 153 | "PATCH", |
| 154 | &uri, |
| 155 | Some(&who), |
| 156 | Some(json!({ "check_in": base, "check_out": end })), |
| 157 | ) |
| 158 | .await; |
| 159 | assert_eq!(widened.status, StatusCode::OK, "round {round} widen"); |
| 160 | |
| 161 | |
| 162 | |
| 163 | let (a, b) = tokio::join!( |
| 164 | send( |
| 165 | &app, |
| 166 | "PATCH", |
| 167 | &uri, |
| 168 | Some(&who), |
| 169 | Some(json!({ "check_in": end - 1000 })), |
| 170 | ), |
| 171 | send( |
| 172 | &app, |
| 173 | "PATCH", |
| 174 | &uri, |
| 175 | Some(&who), |
| 176 | Some(json!({ "check_out": base + 1000 })), |
| 177 | ), |
| 178 | ); |
| 179 | |
| 180 | let after = entry(&app, &who, &id).await; |
| 181 | let final_in = after["check_in"].as_i64().expect("check_in"); |
| 182 | let final_out = after["check_out"].as_i64().expect("check_out"); |
| 183 | assert!( |
| 184 | final_in <= final_out, |
| 185 | "round {round}: stored inverted stint check_in={final_in} \ |
| 186 | check_out={final_out} (statuses {} {})", |
| 187 | a.status, |
| 188 | b.status |
| 189 | ); |
| 190 | } |
| 191 | } |