forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB69d4295Ahmet Kaan Gümüş2 months ago536 commits

partial_patch_work.rs191 lines, 6.8 KiBHistoryRawDownload
1//! Two concurrent partial PATCHes on the same work entry must not revert each
2//! other. A handler that fills omitted fields from its own stale read writes
3//! back a value the client never sent, so whichever request lands last wipes
4//! the other's correction.
5
6mod common;
7
8use axum::http::StatusCode;
9use common::*;
10use serde_json::{Value, json};
11
12/// The caller's own stint with id `id`, from the paginated `/work/me` log.
13async 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/// Open then close a stint, returning the closed entry's id.
24async 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]
33async 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 // Both corrections straddle the server-stamped pair, so each one is
37 // independently valid against the *stored* other side.
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/// `user` and the open/closed bookkeeping stay server-owned: a PATCH body
67/// naming them changes nothing.
68#[tokio::test]
69async 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/// An empty PATCH keeps both instants; an explicit `null` means "keep" too —
92/// `check_out` is not clearable here, since an open stint is refused outright.
93#[tokio::test]
94async 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/// Correcting only one instant is still range-checked against the *stored*
111/// other side — reading it for a CHECK is fine, writing it back is not.
112#[tokio::test]
113async 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/// Each instant moved *just inside* the other is valid on its own snapshot, so
134/// two concurrent one-sided corrections can each pass their check against the
135/// stored row and still commit an inverted stint between them. Whatever each
136/// request returns (200/400/409 are all fine), the stored pair must stay
137/// ordered.
138#[tokio::test]
139async 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 // The server stamps both instants milliseconds apart; widen the stint
150 // first so the racing corrections have room to invert it.
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 // Each alone is valid: check_in lands before the stored check_out, and
162 // check_out after the stored check_in.
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}