forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB08aa13eAhmet Kaan Gümüş2 months ago378 commits

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