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_courses.rs146 lines, 4.6 KiBHistoryRawDownload
1//! Two concurrent partial PATCHes of *different* course fields must both
2//! survive. A handler that refills omitted fields from its own stale read
3//! reverts whatever the other writer just committed — the loop makes the
4//! interleaving reliable rather than lucky.
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 teacher = login_as(&app, &db, "course_patch_teacher", "teacher").await;
16
17 for round in 0..20 {
18 let id = create_course(&app, &teacher, &format!("original {round}")).await;
19 let uri = format!("/courses/{id}");
20 let title = format!("renamed {round}");
21 let description = format!("described {round}");
22 let (a, b) = tokio::join!(
23 send(
24 &app,
25 "PATCH",
26 &uri,
27 Some(&teacher),
28 Some(json!({ "title": title })),
29 ),
30 send(
31 &app,
32 "PATCH",
33 &uri,
34 Some(&teacher),
35 Some(json!({ "description": description })),
36 ),
37 );
38 assert_eq!(a.status, StatusCode::OK, "round {round} title patch");
39 assert_eq!(b.status, StatusCode::OK, "round {round} description patch");
40
41 let after = send(&app, "GET", &uri, Some(&teacher), None).await.body;
42 assert_eq!(after["title"], title, "round {round}: title reverted");
43 assert_eq!(
44 after["description"], description,
45 "round {round}: description reverted"
46 );
47 }
48}
49
50/// `capacity` and `term` are nullable, so an explicit `null` must still clear
51/// them — the absent-vs-null split must not make them unclearable.
52#[tokio::test]
53async fn capacity_and_term_stay_clearable() {
54 let (app, db) = app_and_db().await;
55 let manager = login_as(&app, &db, "course_patch_manager", "manager").await;
56
57 let term = send(
58 &app,
59 "POST",
60 "/terms",
61 Some(&manager),
62 Some(json!({
63 "name": "2026 spring",
64 "starts_at": 1_780_000_000_000_i64,
65 "ends_at": 1_790_000_000_000_i64,
66 })),
67 )
68 .await;
69 assert_eq!(term.status, StatusCode::CREATED, "create term");
70 let term_id = id_of(&term.body);
71
72 let id = create_course(&app, &manager, "clearable").await;
73 let uri = format!("/courses/{id}");
74
75 let set = send(
76 &app,
77 "PATCH",
78 &uri,
79 Some(&manager),
80 Some(json!({ "capacity": 12, "term_id": term_id })),
81 )
82 .await;
83 assert_eq!(set.status, StatusCode::OK, "set capacity and term");
84 assert_eq!(set.body["capacity"], 12);
85 assert_eq!(set.body["term"], term_id);
86
87 // An omitted field keeps its value...
88 let kept = send(
89 &app,
90 "PATCH",
91 &uri,
92 Some(&manager),
93 Some(json!({ "title": "still capped" })),
94 )
95 .await;
96 assert_eq!(kept.status, StatusCode::OK, "omit keeps");
97 assert_eq!(kept.body["capacity"], 12);
98 assert_eq!(kept.body["term"], term_id);
99
100 // ...an explicit null clears it.
101 let cleared = send(
102 &app,
103 "PATCH",
104 &uri,
105 Some(&manager),
106 Some(json!({ "capacity": null, "term_id": null })),
107 )
108 .await;
109 assert_eq!(cleared.status, StatusCode::OK, "clear capacity and term");
110 assert!(cleared.body["capacity"].is_null(), "capacity not cleared");
111 assert!(cleared.body["term"].is_null(), "term not cleared");
112}
113
114/// A plain field PATCH must never touch `teachers[]` — it has its own
115/// assign/unassign path.
116#[tokio::test]
117async fn field_patch_leaves_teachers_alone() {
118 let (app, db) = app_and_db().await;
119 let manager = login_as(&app, &db, "course_patch_owner", "manager").await;
120 let assistant = login_as(&app, &db, "course_patch_assistant", "teacher").await;
121 let assistant_id = me_id(&app, &assistant).await;
122
123 let id = create_course(&app, &manager, "team taught").await;
124 let uri = format!("/courses/{id}");
125 let assigned = send(
126 &app,
127 "POST",
128 &format!("{uri}/teachers"),
129 Some(&manager),
130 Some(json!({ "user_id": assistant_id })),
131 )
132 .await;
133 assert_eq!(assigned.status, StatusCode::OK, "assign teacher");
134
135 let patched = send(
136 &app,
137 "PATCH",
138 &uri,
139 Some(&manager),
140 Some(json!({ "title": "renamed" })),
141 )
142 .await;
143 assert_eq!(patched.status, StatusCode::OK, "title patch");
144 assert_eq!(patched.body["teachers"].as_array().unwrap().len(), 1);
145 assert_eq!(patched.body["teachers"][0]["id"], assistant_id);
146}