forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiBebffd41Ahmet Kaan Gümüş2 months ago509 commits

partial_patch_homework.rs184 lines, 6.5 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 şube instance + subject + two enrolled students, once per test, plus
17/// the teacher who acts on it.
18///
19/// Homework keys on the class×course *instance*, not the catalog course, so the
20/// fixture mints a şube and attaches the course to it. The plain `teacher`
21/// cookie acts through the şube's own sınıf öğretmeni seat, which is what
22/// [`taught_under`] names them for.
23async fn world(
24 app: &axum::Router,
25 db: &hezarfen_backend::database::Database,
26) -> (String, String, String, String) {
27 let teacher = login_as(app, db, "teacher", "teacher").await;
28 let mudur = login_as(app, db, "mudur", "manager").await;
29 let ali = login(app, "ali").await;
30 let ali_id = me_id(app, &ali).await;
31 let veli = login(app, "veli").await;
32 let veli_id = me_id(app, &veli).await;
33 let t = taught_under(app, &mudur, &teacher, "math").await;
34 let subject = create_subject(app, &teacher, &t.course, "algebra").await;
35 enroll(app, &teacher, &t.instance, &ali_id).await;
36 enroll(app, &teacher, &t.instance, &veli_id).await;
37 (teacher, t.instance, subject, ali_id)
38}
39
40/// Title and due date are independent scalar columns: patching one must not
41/// roll the other back.
42#[tokio::test]
43async fn concurrent_title_and_due_at_patches_both_stick() {
44 let (app, db) = app_and_db().await;
45 let (teacher, instance, subject, _) = world(&app, &db).await;
46
47 for round in 0..ROUNDS {
48 let due = Timestamp::now().as_millis() + 3_600_000;
49 let id = create_homework(&app, &teacher, &instance, &subject, "hw", due).await;
50 let uri = format!("/homework/{id}");
51 let new_due = due + 86_400_000;
52 let (a, b) = tokio::join!(
53 send(
54 &app,
55 "PATCH",
56 &uri,
57 Some(&teacher),
58 Some(json!({ "title": "renamed" })),
59 ),
60 send(
61 &app,
62 "PATCH",
63 &uri,
64 Some(&teacher),
65 Some(json!({ "due_at": new_due })),
66 ),
67 );
68 assert_eq!(a.status, StatusCode::OK, "round {round} title patch");
69 assert_eq!(b.status, StatusCode::OK, "round {round} due_at patch");
70
71 let after = send(&app, "GET", &uri, Some(&teacher), None).await.body;
72 assert_eq!(after["title"], "renamed", "round {round}: title reverted");
73 assert_eq!(after["due_at"], new_due, "round {round}: due_at reverted");
74 }
75}
76
77/// The audience subset is the interesting one: a title-only PATCH carries no
78/// `assigned` at all, so it must not write the subset it happened to read —
79/// that would silently re-narrow (or re-widen) the audience of a homework
80/// whose scope a concurrent PATCH just changed, bypassing the orphan guard.
81#[tokio::test]
82async fn concurrent_title_and_assigned_patches_both_stick() {
83 let (app, db) = app_and_db().await;
84 let (teacher, instance, subject, ali_id) = world(&app, &db).await;
85
86 for round in 0..ROUNDS {
87 let due = Timestamp::now().as_millis() + 3_600_000;
88 let id = create_homework(&app, &teacher, &instance, &subject, "hw", due).await;
89 let uri = format!("/homework/{id}");
90 let (a, b) = tokio::join!(
91 send(
92 &app,
93 "PATCH",
94 &uri,
95 Some(&teacher),
96 Some(json!({ "title": "renamed" })),
97 ),
98 send(
99 &app,
100 "PATCH",
101 &uri,
102 Some(&teacher),
103 Some(json!({ "assigned": [ali_id.clone()] })),
104 ),
105 );
106 assert_eq!(a.status, StatusCode::OK, "round {round} title patch");
107 assert_eq!(b.status, StatusCode::OK, "round {round} assigned patch");
108
109 let after = send(&app, "GET", &uri, Some(&teacher), None).await.body;
110 assert_eq!(after["title"], "renamed", "round {round}: title reverted");
111 assert_eq!(
112 after["assigned"],
113 json!([ali_id]),
114 "round {round}: assigned reverted to whole-course"
115 );
116 }
117}
118
119/// `assigned: null` (and `[]`) is an explicit "whole course", not "keep" —
120/// the double-option must survive the partial-PATCH conversion, or a subset
121/// homework becomes impossible to widen back.
122#[tokio::test]
123async fn assigned_null_still_clears_the_subset() {
124 let (app, db) = app_and_db().await;
125 let (teacher, instance, subject, ali_id) = world(&app, &db).await;
126 let due = Timestamp::now().as_millis() + 3_600_000;
127 let res = create_homework_with(
128 &app,
129 &teacher,
130 &instance,
131 json!({ "title": "hw", "subject_id": subject, "due_at": due, "assigned": [ali_id] }),
132 )
133 .await;
134 assert_eq!(res.status, StatusCode::CREATED, "{}", res.body);
135 let uri = format!("/homework/{}", id_of(&res.body));
136
137 // Omitted: keeps the subset.
138 let kept = send(
139 &app,
140 "PATCH",
141 &uri,
142 Some(&teacher),
143 Some(json!({ "title": "kept" })),
144 )
145 .await;
146 assert_eq!(kept.status, StatusCode::OK, "{}", kept.body);
147 assert_eq!(kept.body["assigned"], json!([ali_id]), "omitted cleared it");
148
149 // Explicit null: widens back to the whole course.
150 let cleared = send(
151 &app,
152 "PATCH",
153 &uri,
154 Some(&teacher),
155 Some(json!({ "assigned": null })),
156 )
157 .await;
158 assert_eq!(cleared.status, StatusCode::OK, "{}", cleared.body);
159 assert_eq!(
160 cleared.body["assigned"],
161 json!(null),
162 "explicit null did not clear the subset"
163 );
164
165 // Same for description: omit keeps, null clears.
166 let desc = send(
167 &app,
168 "PATCH",
169 &uri,
170 Some(&teacher),
171 Some(json!({ "description": "read it" })),
172 )
173 .await;
174 assert_eq!(desc.body["description"], "read it", "{}", desc.body);
175 let cleared = send(
176 &app,
177 "PATCH",
178 &uri,
179 Some(&teacher),
180 Some(json!({ "description": null })),
181 )
182 .await;
183 assert_eq!(cleared.body["description"], json!(null), "{}", cleared.body);
184}