forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB914d095Ahmet Kaan Gümüş2 months ago454 commits

partial_patch_users.rs212 lines, 6.6 KiBHistoryRawDownload
1//! Two concurrent partial PATCHes on the same user row must not revert each
2//! other. The profile and preference handlers merged the request over their own
3//! stale read and wrote every column, so a name edit racing a surname edit lost
4//! one of them — both 200.
5
6mod common;
7
8use axum::http::StatusCode;
9use common::*;
10use serde_json::json;
11
12#[tokio::test]
13async fn concurrent_profile_patches_keep_both_fields() {
14 let (app, db) = app_and_db().await;
15 let who = login_as(&app, &db, "ali", "student").await;
16
17 for round in 0..20 {
18 let name = format!("name{round}");
19 let surname = format!("surname{round}");
20 let (a, b) = tokio::join!(
21 send(
22 &app,
23 "PATCH",
24 "/users/me",
25 Some(&who),
26 Some(json!({ "name": name })),
27 ),
28 send(
29 &app,
30 "PATCH",
31 "/users/me",
32 Some(&who),
33 Some(json!({ "surname": surname })),
34 ),
35 );
36 assert_eq!(a.status, StatusCode::OK, "round {round} name patch");
37 assert_eq!(b.status, StatusCode::OK, "round {round} surname patch");
38
39 // `/auth/me` is the self-read (there is no `GET /users/me`).
40 let after = send(&app, "GET", "/auth/me", Some(&who), None).await.body;
41 assert_eq!(after["name"], name, "round {round}: name reverted");
42 assert_eq!(after["surname"], surname, "round {round}: surname reverted");
43 }
44}
45
46#[tokio::test]
47async fn concurrent_preference_patches_keep_both_fields() {
48 let (app, db) = app_and_db().await;
49 let who = login_as(&app, &db, "veli", "student").await;
50
51 for round in 0..20 {
52 // Alternate both values so a revert to the previous round is visible.
53 let (theme, language) = if round % 2 == 0 {
54 ("dark", "tr")
55 } else {
56 ("light", "en")
57 };
58 let color = if round % 2 == 0 { "#283618" } else { "#fefae0" };
59 let (a, b, c) = tokio::join!(
60 send(
61 &app,
62 "PATCH",
63 "/users/me/preferences",
64 Some(&who),
65 Some(json!({ "theme": theme })),
66 ),
67 send(
68 &app,
69 "PATCH",
70 "/users/me/preferences",
71 Some(&who),
72 Some(json!({ "language": language })),
73 ),
74 send(
75 &app,
76 "PATCH",
77 "/users/me/preferences",
78 Some(&who),
79 Some(json!({ "palette_color": color })),
80 ),
81 );
82 assert_eq!(a.status, StatusCode::OK, "round {round} theme patch");
83 assert_eq!(b.status, StatusCode::OK, "round {round} language patch");
84 assert_eq!(c.status, StatusCode::OK, "round {round} palette patch");
85
86 let after = send(&app, "GET", "/auth/me", Some(&who), None).await.body;
87 assert_eq!(after["theme"], theme, "round {round}: theme reverted");
88 assert_eq!(
89 after["language"], language,
90 "round {round}: language reverted"
91 );
92 assert_eq!(
93 after["palette_color"], color,
94 "round {round}: palette_color reverted"
95 );
96 }
97}
98
99/// The field semantics are unchanged: omitted (and `null`) keeps, `""` clears,
100/// and anything else runs through the same validators a create would use.
101#[tokio::test]
102async fn omitted_keeps_empty_clears_and_validation_still_bites() {
103 let (app, db) = app_and_db().await;
104 let who = login_as(&app, &db, "ayse", "student").await;
105
106 let set = send(
107 &app,
108 "PATCH",
109 "/users/me",
110 Some(&who),
111 Some(json!({ "name": "Ada", "surname": "Lovelace", "email": "ada@example.com" })),
112 )
113 .await;
114 assert_eq!(set.status, StatusCode::OK, "{}", set.body);
115
116 // Omitted keeps, and an explicit null keeps too (the documented contract).
117 let kept = send(
118 &app,
119 "PATCH",
120 "/users/me",
121 Some(&who),
122 Some(json!({ "surname": null })),
123 )
124 .await;
125 assert_eq!(kept.status, StatusCode::OK);
126 assert_eq!(kept.body["name"], "Ada", "omitted name kept");
127 assert_eq!(kept.body["surname"], "Lovelace", "null keeps surname");
128 assert_eq!(kept.body["email"], "ada@example.com");
129
130 // An empty request writes nothing at all.
131 let empty = send(&app, "PATCH", "/users/me", Some(&who), Some(json!({}))).await;
132 assert_eq!(empty.status, StatusCode::OK);
133 assert_eq!(empty.body["name"], "Ada");
134 assert_eq!(empty.body["email"], "ada@example.com");
135
136 // `""` clears — and only the field it was sent for.
137 let cleared = send(
138 &app,
139 "PATCH",
140 "/users/me",
141 Some(&who),
142 Some(json!({ "email": "" })),
143 )
144 .await;
145 assert_eq!(cleared.status, StatusCode::OK);
146 assert_eq!(cleared.body["email"], json!(null), "email not cleared");
147 assert_eq!(cleared.body["name"], "Ada", "name lost to the clear");
148
149 // A malformed value is refused, exactly as at create.
150 let bad = send(
151 &app,
152 "PATCH",
153 "/users/me",
154 Some(&who),
155 Some(json!({ "email": "not-an-email" })),
156 )
157 .await;
158 assert_eq!(bad.status, StatusCode::BAD_REQUEST, "bad email refused");
159
160 let bad_theme = send(
161 &app,
162 "PATCH",
163 "/users/me/preferences",
164 Some(&who),
165 Some(json!({ "theme": "chartreuse" })),
166 )
167 .await;
168 assert_eq!(
169 bad_theme.status,
170 StatusCode::BAD_REQUEST,
171 "bad theme refused"
172 );
173
174 // The accent color joins them: set, cleared by `""`, refused when malformed
175 // — and never touching the other two preferences.
176 let set = send(
177 &app,
178 "PATCH",
179 "/users/me/preferences",
180 Some(&who),
181 Some(json!({ "theme": "dark", "palette_color": "#FEFAE0" })),
182 )
183 .await;
184 assert_eq!(set.status, StatusCode::OK, "{}", set.body);
185 assert_eq!(set.body["palette_color"], "#fefae0", "stored lowercase");
186
187 let cleared = send(
188 &app,
189 "PATCH",
190 "/users/me/preferences",
191 Some(&who),
192 Some(json!({ "palette_color": "" })),
193 )
194 .await;
195 assert_eq!(cleared.status, StatusCode::OK);
196 assert_eq!(cleared.body["palette_color"], json!(null));
197 assert_eq!(cleared.body["theme"], "dark", "theme lost to the clear");
198
199 let bad_color = send(
200 &app,
201 "PATCH",
202 "/users/me/preferences",
203 Some(&who),
204 Some(json!({ "palette_color": "#fff" })),
205 )
206 .await;
207 assert_eq!(
208 bad_color.status,
209 StatusCode::BAD_REQUEST,
210 "bad palette color refused"
211 );
212}