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_events.rs182 lines, 5.8 KiBHistoryRawDownload
1//! Two concurrent partial PATCHes on the same event 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 field.
5
6mod common;
7
8use axum::http::StatusCode;
9use common::*;
10use serde_json::json;
11
12#[tokio::test]
13async fn concurrent_partial_patches_keep_both_fields() {
14 let (app, db) = app_and_db().await;
15 let who = login_as(&app, &db, "ali", "teacher").await;
16
17 for round in 0..20 {
18 let created = send(
19 &app,
20 "POST",
21 "/events",
22 Some(&who),
23 Some(json!({ "title": "old title", "description": "old description" })),
24 )
25 .await;
26 assert_eq!(created.status, StatusCode::CREATED, "round {round} create");
27 let id = id_of(&created.body);
28 let uri = format!("/events/{id}");
29
30 let a_patch = json!({ "title": "new title" });
31 let b_patch = json!({ "description": "new description" });
32 let (a, b) = tokio::join!(
33 send(&app, "PATCH", &uri, Some(&who), Some(a_patch)),
34 send(&app, "PATCH", &uri, Some(&who), Some(b_patch)),
35 );
36 assert_eq!(a.status, StatusCode::OK, "round {round} title patch");
37 assert_eq!(b.status, StatusCode::OK, "round {round} description patch");
38
39 let after = send(&app, "GET", &uri, Some(&who), None).await.body;
40 assert_eq!(after["title"], "new title", "round {round}: title reverted");
41 assert_eq!(
42 after["description"], "new description",
43 "round {round}: description reverted"
44 );
45 }
46}
47
48/// The schedule columns are genuinely nullable: an explicit `null` must still
49/// clear them, and an omitted one must still keep them.
50#[tokio::test]
51async fn schedule_stays_clearable_and_keepable() {
52 let (app, db) = app_and_db().await;
53 let who = login_as(&app, &db, "veli", "teacher").await;
54 let future = 1_900_000_000_000_i64;
55
56 let created = send(
57 &app,
58 "POST",
59 "/events",
60 Some(&who),
61 Some(json!({ "title": "timed", "starts_at": future, "ends_at": future + 3_600_000 })),
62 )
63 .await;
64 assert_eq!(created.status, StatusCode::CREATED);
65 let uri = format!("/events/{}", id_of(&created.body));
66
67 // Omitted: both kept.
68 let kept = send(
69 &app,
70 "PATCH",
71 &uri,
72 Some(&who),
73 Some(json!({ "title": "t" })),
74 )
75 .await;
76 assert_eq!(kept.status, StatusCode::OK);
77 assert_eq!(kept.body["starts_at"], future);
78 assert_eq!(kept.body["ends_at"], future + 3_600_000);
79
80 // Explicit null: cleared.
81 let cleared = send(
82 &app,
83 "PATCH",
84 &uri,
85 Some(&who),
86 Some(json!({ "starts_at": null, "ends_at": null })),
87 )
88 .await;
89 assert_eq!(cleared.status, StatusCode::OK);
90 assert_eq!(cleared.body["starts_at"], json!(null));
91 assert_eq!(cleared.body["ends_at"], json!(null));
92}
93
94/// Setting only `ends_at` is still range-checked against the *stored*
95/// `starts_at` — reading the other side for a CHECK is fine, writing it back
96/// is not.
97#[tokio::test]
98async fn range_check_still_sees_the_stored_other_side() {
99 let (app, db) = app_and_db().await;
100 let who = login_as(&app, &db, "ayse", "teacher").await;
101 let future = 1_900_000_000_000_i64;
102
103 let created = send(
104 &app,
105 "POST",
106 "/events",
107 Some(&who),
108 Some(json!({ "title": "timed", "starts_at": future })),
109 )
110 .await;
111 assert_eq!(created.status, StatusCode::CREATED);
112 let uri = format!("/events/{}", id_of(&created.body));
113
114 let bad = send(
115 &app,
116 "PATCH",
117 &uri,
118 Some(&who),
119 Some(json!({ "ends_at": future - 1000 })),
120 )
121 .await;
122 assert_eq!(
123 bad.status,
124 StatusCode::BAD_REQUEST,
125 "ends_at before stored starts_at"
126 );
127}
128
129/// Each end moved *just inside* the other is valid on its own snapshot, so two
130/// concurrent one-ended PATCHes can each pass their check against the stored
131/// row and still commit an inverted range between them. Whatever each request
132/// returns (200/400/409 are all fine), the stored pair must stay ordered.
133#[tokio::test]
134async fn concurrent_range_patches_never_invert_the_stored_row() {
135 let (app, db) = app_and_db().await;
136 let who = login_as(&app, &db, "range", "teacher").await;
137 let starts = 1_900_000_000_000_i64;
138 let ends = starts + 3_600_000;
139
140 for round in 0..20 {
141 let created = send(
142 &app,
143 "POST",
144 "/events",
145 Some(&who),
146 Some(json!({ "title": "timed", "starts_at": starts, "ends_at": ends })),
147 )
148 .await;
149 assert_eq!(created.status, StatusCode::CREATED, "round {round} create");
150 let uri = format!("/events/{}", id_of(&created.body));
151
152 // Each alone is valid: starts_at lands before the stored ends_at, and
153 // ends_at lands after the stored starts_at.
154 let (a, b) = tokio::join!(
155 send(
156 &app,
157 "PATCH",
158 &uri,
159 Some(&who),
160 Some(json!({ "starts_at": ends - 1000 })),
161 ),
162 send(
163 &app,
164 "PATCH",
165 &uri,
166 Some(&who),
167 Some(json!({ "ends_at": starts + 1000 })),
168 ),
169 );
170
171 let after = send(&app, "GET", &uri, Some(&who), None).await.body;
172 let stored_starts = after["starts_at"].as_i64().expect("starts_at");
173 let stored_ends = after["ends_at"].as_i64().expect("ends_at");
174 assert!(
175 stored_starts <= stored_ends,
176 "round {round}: stored inverted range starts_at={stored_starts} \
177 ends_at={stored_ends} (statuses {} {})",
178 a.status,
179 b.status
180 );
181 }
182}