forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB0346717Ahmet Kaan Gümüş3 months ago230 commits

partial_patch_homework.rs177 lines, 6.1 KiBHistoryRawDownload
1//! Two concurrent partial `PATCH /homework/{id}` requests touching *different*
2//! fields must both stick. A handler that fills the omitted fields from the row
3//! it read before the write re-sends a stale value, so whichever request lands
4//! second silently reverts the other's field — no error, the change is just
5//! gone. Looped, because a single round can get lucky on the interleaving.
6
7mod common;
8
9use axum::http::StatusCode;
10use common::*;
11use hezarfen_backend::domain::timestamp::Timestamp;
12use serde_json::json;
13
14const ROUNDS: usize = 20;
15
16/// Set up teacher + course + subject + two enrolled students, once per test.
17async fn world(
18 app: &axum::Router,
19 db: &hezarfen_backend::database::Database,
20) -> (String, String, String, String) {
21 let teacher = login_as(app, db, "teacher", "teacher").await;
22 let ali = login(app, "ali").await;
23 let ali_id = me_id(app, &ali).await;
24 let veli = login(app, "veli").await;
25 let veli_id = me_id(app, &veli).await;
26 let course = create_course(app, &teacher, "math").await;
27 let subject = create_subject(app, &teacher, &course, "algebra").await;
28 enroll(app, &teacher, &course, &ali_id).await;
29 enroll(app, &teacher, &course, &veli_id).await;
30 (teacher, course, subject, ali_id)
31}
32
33/// Title and due date are independent scalar columns: patching one must not
34/// roll the other back.
35#[tokio::test]
36async fn concurrent_title_and_due_at_patches_both_stick() {
37 let (app, db) = app_and_db().await;
38 let (teacher, course, subject, _) = world(&app, &db).await;
39
40 for round in 0..ROUNDS {
41 let due = Timestamp::now().as_millis() + 3_600_000;
42 let id = create_homework(&app, &teacher, &course, &subject, "hw", due).await;
43 let uri = format!("/homework/{id}");
44 let new_due = due + 86_400_000;
45 let (a, b) = tokio::join!(
46 send(
47 &app,
48 "PATCH",
49 &uri,
50 Some(&teacher),
51 Some(json!({ "title": "renamed" })),
52 ),
53 send(
54 &app,
55 "PATCH",
56 &uri,
57 Some(&teacher),
58 Some(json!({ "due_at": new_due })),
59 ),
60 );
61 assert_eq!(a.status, StatusCode::OK, "round {round} title patch");
62 assert_eq!(b.status, StatusCode::OK, "round {round} due_at patch");
63
64 let after = send(&app, "GET", &uri, Some(&teacher), None).await.body;
65 assert_eq!(after["title"], "renamed", "round {round}: title reverted");
66 assert_eq!(after["due_at"], new_due, "round {round}: due_at reverted");
67 }
68}
69
70/// The audience subset is the interesting one: a title-only PATCH carries no
71/// `assigned` at all, so it must not write the subset it happened to read —
72/// that would silently re-narrow (or re-widen) the audience of a homework
73/// whose scope a concurrent PATCH just changed, bypassing the orphan guard.
74#[tokio::test]
75async fn concurrent_title_and_assigned_patches_both_stick() {
76 let (app, db) = app_and_db().await;
77 let (teacher, course, subject, ali_id) = world(&app, &db).await;
78
79 for round in 0..ROUNDS {
80 let due = Timestamp::now().as_millis() + 3_600_000;
81 let id = create_homework(&app, &teacher, &course, &subject, "hw", due).await;
82 let uri = format!("/homework/{id}");
83 let (a, b) = tokio::join!(
84 send(
85 &app,
86 "PATCH",
87 &uri,
88 Some(&teacher),
89 Some(json!({ "title": "renamed" })),
90 ),
91 send(
92 &app,
93 "PATCH",
94 &uri,
95 Some(&teacher),
96 Some(json!({ "assigned": [ali_id.clone()] })),
97 ),
98 );
99 assert_eq!(a.status, StatusCode::OK, "round {round} title patch");
100 assert_eq!(b.status, StatusCode::OK, "round {round} assigned patch");
101
102 let after = send(&app, "GET", &uri, Some(&teacher), None).await.body;
103 assert_eq!(after["title"], "renamed", "round {round}: title reverted");
104 assert_eq!(
105 after["assigned"],
106 json!([ali_id]),
107 "round {round}: assigned reverted to whole-course"
108 );
109 }
110}
111
112/// `assigned: null` (and `[]`) is an explicit "whole course", not "keep" —
113/// the double-option must survive the partial-PATCH conversion, or a subset
114/// homework becomes impossible to widen back.
115#[tokio::test]
116async fn assigned_null_still_clears_the_subset() {
117 let (app, db) = app_and_db().await;
118 let (teacher, course, subject, ali_id) = world(&app, &db).await;
119 let due = Timestamp::now().as_millis() + 3_600_000;
120 let res = create_homework_with(
121 &app,
122 &teacher,
123 &course,
124 json!({ "title": "hw", "subject_id": subject, "due_at": due, "assigned": [ali_id] }),
125 )
126 .await;
127 assert_eq!(res.status, StatusCode::CREATED, "{}", res.body);
128 let uri = format!("/homework/{}", id_of(&res.body));
129
130 // Omitted: keeps the subset.
131 let kept = send(
132 &app,
133 "PATCH",
134 &uri,
135 Some(&teacher),
136 Some(json!({ "title": "kept" })),
137 )
138 .await;
139 assert_eq!(kept.status, StatusCode::OK, "{}", kept.body);
140 assert_eq!(kept.body["assigned"], json!([ali_id]), "omitted cleared it");
141
142 // Explicit null: widens back to the whole course.
143 let cleared = send(
144 &app,
145 "PATCH",
146 &uri,
147 Some(&teacher),
148 Some(json!({ "assigned": null })),
149 )
150 .await;
151 assert_eq!(cleared.status, StatusCode::OK, "{}", cleared.body);
152 assert_eq!(
153 cleared.body["assigned"],
154 json!(null),
155 "explicit null did not clear the subset"
156 );
157
158 // Same for description: omit keeps, null clears.
159 let desc = send(
160 &app,
161 "PATCH",
162 &uri,
163 Some(&teacher),
164 Some(json!({ "description": "read it" })),
165 )
166 .await;
167 assert_eq!(desc.body["description"], "read it", "{}", desc.body);
168 let cleared = send(
169 &app,
170 "PATCH",
171 &uri,
172 Some(&teacher),
173 Some(json!({ "description": null })),
174 )
175 .await;
176 assert_eq!(cleared.body["description"], json!(null), "{}", cleared.body);
177}