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_sessions.rs177 lines, 5.7 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 Some(json!({ "topic": "old topic", "starts_at": FUTURE })),
29 )
30 .await;
31 assert_eq!(created.status, StatusCode::CREATED, "round {round} create");
32 let uri = format!("/sessions/{}", id_of(&created.body));
33
34 // `topic` races `ends_at`: different fields, neither may lose.
35 let (a, b) = tokio::join!(
36 send(
37 &app,
38 "PATCH",
39 &uri,
40 Some(&who),
41 Some(json!({ "topic": "new topic" })),
42 ),
43 send(
44 &app,
45 "PATCH",
46 &uri,
47 Some(&who),
48 Some(json!({ "ends_at": FUTURE + 3_600_000 })),
49 ),
50 );
51 assert_eq!(a.status, StatusCode::OK, "round {round} topic patch");
52 assert_eq!(b.status, StatusCode::OK, "round {round} ends_at patch");
53
54 let after = send(&app, "GET", &uri, Some(&who), None).await.body;
55 assert_eq!(after["topic"], "new topic", "round {round}: topic reverted");
56 assert_eq!(
57 after["ends_at"],
58 FUTURE + 3_600_000,
59 "round {round}: ends_at reverted"
60 );
61 }
62}
63
64/// Each end moved *just inside* the other is valid on its own snapshot, so two
65/// concurrent one-ended PATCHes could each pass their check against the stored
66/// row and still commit an inverted lesson between them. Whatever each request
67/// returns (200/400 are both fine), the stored pair must stay ordered.
68#[tokio::test]
69async fn concurrent_range_patches_never_invert_the_stored_row() {
70 let (app, db) = app_and_db().await;
71 let who = login_as(&app, &db, "range", "teacher").await;
72 let mudur = login_as(&app, &db, "mudur", "manager").await;
73 let t = taught_under(&app, &mudur, &who, "math").await;
74 let ends = FUTURE + 3_600_000;
75
76 for round in 0..20 {
77 let created = send(
78 &app,
79 "POST",
80 &format!("/instances/{}/sessions", t.instance),
81 Some(&who),
82 Some(json!({ "starts_at": FUTURE, "ends_at": ends })),
83 )
84 .await;
85 assert_eq!(created.status, StatusCode::CREATED, "round {round} create");
86 let uri = format!("/sessions/{}", id_of(&created.body));
87
88 let (a, b) = tokio::join!(
89 send(
90 &app,
91 "PATCH",
92 &uri,
93 Some(&who),
94 Some(json!({ "starts_at": ends - 1000 })),
95 ),
96 send(
97 &app,
98 "PATCH",
99 &uri,
100 Some(&who),
101 Some(json!({ "ends_at": FUTURE + 1000 })),
102 ),
103 );
104
105 let after = send(&app, "GET", &uri, Some(&who), None).await.body;
106 let stored_starts = after["starts_at"].as_i64().expect("starts_at");
107 let stored_ends = after["ends_at"].as_i64().expect("ends_at");
108 assert!(
109 stored_starts <= stored_ends,
110 "round {round}: stored inverted lesson starts_at={stored_starts} \
111 ends_at={stored_ends} (statuses {} {})",
112 a.status,
113 b.status
114 );
115 }
116}
117
118/// `ends_at` stays genuinely nullable: omitted keeps it, an explicit `null`
119/// clears it — and a one-ended PATCH is still checked against the stored other
120/// end (read for the check, never written back).
121#[tokio::test]
122async fn ends_at_stays_clearable_and_range_checked() {
123 let (app, db) = app_and_db().await;
124 let who = login_as(&app, &db, "veli", "teacher").await;
125 let mudur = login_as(&app, &db, "mudur", "manager").await;
126 let t = taught_under(&app, &mudur, &who, "physics").await;
127 let session = create_session(&app, &who, &t.instance, FUTURE).await;
128 let uri = format!("/sessions/{session}");
129
130 let set = send(
131 &app,
132 "PATCH",
133 &uri,
134 Some(&who),
135 Some(json!({ "ends_at": FUTURE + 1_000 })),
136 )
137 .await;
138 assert_eq!(set.status, StatusCode::OK);
139 assert_eq!(set.body["ends_at"], FUTURE + 1_000);
140
141 let kept = send(
142 &app,
143 "PATCH",
144 &uri,
145 Some(&who),
146 Some(json!({ "topic": "kept ends" })),
147 )
148 .await;
149 assert_eq!(kept.status, StatusCode::OK);
150 assert_eq!(kept.body["starts_at"], FUTURE, "omitted starts_at kept");
151 assert_eq!(kept.body["ends_at"], FUTURE + 1_000, "omitted ends_at kept");
152
153 let bad = send(
154 &app,
155 "PATCH",
156 &uri,
157 Some(&who),
158 Some(json!({ "ends_at": FUTURE - 1_000 })),
159 )
160 .await;
161 assert_eq!(
162 bad.status,
163 StatusCode::BAD_REQUEST,
164 "ends_at before stored starts_at"
165 );
166
167 let cleared = send(
168 &app,
169 "PATCH",
170 &uri,
171 Some(&who),
172 Some(json!({ "ends_at": null })),
173 )
174 .await;
175 assert_eq!(cleared.status, StatusCode::OK);
176 assert_eq!(cleared.body["ends_at"], json!(null), "ends_at not cleared");
177}