forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

partial_patch_sessions.rs186 lines, 6.2 KiBHistoryRawDownload
1//! Two concurrent partial PATCHes on the same session must not revert each
2//! other, and — since only the fields a request carried are written — the
3//! `starts_at`/`ends_at` pair must not be invertible by two one-ended PATCHes
4//! either (that is what `FieldUpdate::ordered`'s in-statement range guard is
5//! for).
6
7mod common;
8
9use axum::http::StatusCode;
10use common::*;
11use serde_json::json;
12
13const FUTURE: i64 = 1_900_000_000_000;
14
15#[tokio::test]
16async fn concurrent_partial_patches_keep_both_fields() {
17 let (app, db) = app_and_db().await;
18 let who = login_as(&app, &db, "ali", "teacher").await;
19 let mudur = login_as(&app, &db, "mudur", "manager").await;
20 let t = taught_under(&app, &mudur, &who, "math").await;
21
22 for round in 0..20 {
23 let created = send(
24 &app,
25 "POST",
26 &format!("/instances/{}/sessions", t.instance),
27 Some(&who),
28 // `UNIQUE (class_course, starts_at)`: each round takes its own
29 // instant — descending, all still future, all before the fixed
30 // ends patch below.
31 Some(
32 json!({ "topic": "old topic", "starts_at": FUTURE - round as i64 * 3_600_000 }),
33 ),
34 )
35 .await;
36 assert_eq!(created.status, StatusCode::CREATED, "round {round} create");
37 let uri = format!("/sessions/{}", id_of(&created.body));
38
39 // `topic` races `ends_at`: different fields, neither may lose.
40 let (a, b) = tokio::join!(
41 send(
42 &app,
43 "PATCH",
44 &uri,
45 Some(&who),
46 Some(json!({ "topic": "new topic" })),
47 ),
48 send(
49 &app,
50 "PATCH",
51 &uri,
52 Some(&who),
53 Some(json!({ "ends_at": FUTURE + 3_600_000 })),
54 ),
55 );
56 assert_eq!(a.status, StatusCode::OK, "round {round} topic patch");
57 assert_eq!(b.status, StatusCode::OK, "round {round} ends_at patch");
58
59 let after = send(&app, "GET", &uri, Some(&who), None).await.body;
60 assert_eq!(after["topic"], "new topic", "round {round}: topic reverted");
61 assert_eq!(
62 after["ends_at"],
63 FUTURE + 3_600_000,
64 "round {round}: ends_at reverted"
65 );
66 }
67}
68
69/// Each end moved *just inside* the other is valid on its own snapshot, so two
70/// concurrent one-ended PATCHes could each pass their check against the stored
71/// row and still commit an inverted lesson between them. Whatever each request
72/// returns (200/400 are both fine), the stored pair must stay ordered.
73#[tokio::test]
74async fn concurrent_range_patches_never_invert_the_stored_row() {
75 let (app, db) = app_and_db().await;
76 let who = login_as(&app, &db, "range", "teacher").await;
77 let mudur = login_as(&app, &db, "mudur", "manager").await;
78 let t = taught_under(&app, &mudur, &who, "math").await;
79
80 for round in 0..20 {
81 // `UNIQUE (class_course, starts_at)`: each round takes its own
82 // instant — descending, all still future, ends = starts + 1h kept.
83 let starts = FUTURE - round as i64 * 3_600_000;
84 let ends = starts + 3_600_000;
85
86 let created = send(
87 &app,
88 "POST",
89 &format!("/instances/{}/sessions", t.instance),
90 Some(&who),
91 Some(json!({ "starts_at": starts, "ends_at": ends })),
92 )
93 .await;
94 assert_eq!(created.status, StatusCode::CREATED, "round {round} create");
95 let uri = format!("/sessions/{}", id_of(&created.body));
96
97 let (a, b) = tokio::join!(
98 send(
99 &app,
100 "PATCH",
101 &uri,
102 Some(&who),
103 Some(json!({ "starts_at": ends - 1000 })),
104 ),
105 send(
106 &app,
107 "PATCH",
108 &uri,
109 Some(&who),
110 Some(json!({ "ends_at": FUTURE + 1000 })),
111 ),
112 );
113
114 let after = send(&app, "GET", &uri, Some(&who), None).await.body;
115 let stored_starts = after["starts_at"].as_i64().expect("starts_at");
116 let stored_ends = after["ends_at"].as_i64().expect("ends_at");
117 assert!(
118 stored_starts <= stored_ends,
119 "round {round}: stored inverted lesson starts_at={stored_starts} \
120 ends_at={stored_ends} (statuses {} {})",
121 a.status,
122 b.status
123 );
124 }
125}
126
127/// `ends_at` stays genuinely nullable: omitted keeps it, an explicit `null`
128/// clears it — and a one-ended PATCH is still checked against the stored other
129/// end (read for the check, never written back).
130#[tokio::test]
131async fn ends_at_stays_clearable_and_range_checked() {
132 let (app, db) = app_and_db().await;
133 let who = login_as(&app, &db, "veli", "teacher").await;
134 let mudur = login_as(&app, &db, "mudur", "manager").await;
135 let t = taught_under(&app, &mudur, &who, "physics").await;
136 let session = create_session(&app, &who, &t.instance, FUTURE).await;
137 let uri = format!("/sessions/{session}");
138
139 let set = send(
140 &app,
141 "PATCH",
142 &uri,
143 Some(&who),
144 Some(json!({ "ends_at": FUTURE + 1_000 })),
145 )
146 .await;
147 assert_eq!(set.status, StatusCode::OK);
148 assert_eq!(set.body["ends_at"], FUTURE + 1_000);
149
150 let kept = send(
151 &app,
152 "PATCH",
153 &uri,
154 Some(&who),
155 Some(json!({ "topic": "kept ends" })),
156 )
157 .await;
158 assert_eq!(kept.status, StatusCode::OK);
159 assert_eq!(kept.body["starts_at"], FUTURE, "omitted starts_at kept");
160 assert_eq!(kept.body["ends_at"], FUTURE + 1_000, "omitted ends_at kept");
161
162 let bad = send(
163 &app,
164 "PATCH",
165 &uri,
166 Some(&who),
167 Some(json!({ "ends_at": FUTURE - 1_000 })),
168 )
169 .await;
170 assert_eq!(
171 bad.status,
172 StatusCode::BAD_REQUEST,
173 "ends_at before stored starts_at"
174 );
175
176 let cleared = send(
177 &app,
178 "PATCH",
179 &uri,
180 Some(&who),
181 Some(json!({ "ends_at": null })),
182 )
183 .await;
184 assert_eq!(cleared.status, StatusCode::OK);
185 assert_eq!(cleared.body["ends_at"], json!(null), "ends_at not cleared");
186}