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_subjects.rs89 lines, 3.1 KiBHistoryRawDownload
1//! Two concurrent partial PATCHes on the same subject 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 let course = create_course(&app, &who, "math").await;
17
18 for round in 0..20 {
19 let created = send(
20 &app,
21 "POST",
22 &format!("/courses/{course}/subjects"),
23 Some(&who),
24 Some(json!({ "name": "old name", "description": "old description" })),
25 )
26 .await;
27 assert_eq!(created.status, StatusCode::CREATED, "round {round} create");
28 let uri = format!("/subjects/{}", id_of(&created.body));
29
30 let (a, b) = tokio::join!(
31 send(
32 &app,
33 "PATCH",
34 &uri,
35 Some(&who),
36 Some(json!({ "name": "new name" })),
37 ),
38 send(
39 &app,
40 "PATCH",
41 &uri,
42 Some(&who),
43 Some(json!({ "description": "new description" })),
44 ),
45 );
46 assert_eq!(a.status, StatusCode::OK, "round {round} name patch");
47 assert_eq!(b.status, StatusCode::OK, "round {round} description patch");
48
49 let after = send(&app, "GET", &uri, Some(&who), None).await.body;
50 assert_eq!(after["name"], "new name", "round {round}: name reverted");
51 assert_eq!(
52 after["description"], "new description",
53 "round {round}: description reverted"
54 );
55 }
56}
57
58/// Omitting a field keeps it, and a PATCH still cannot land a value a POST
59/// would reject.
60#[tokio::test]
61async fn omitted_keeps_and_validation_still_bites() {
62 let (app, db) = app_and_db().await;
63 let who = login_as(&app, &db, "veli", "teacher").await;
64 let course = create_course(&app, &who, "physics").await;
65 let subject = create_subject(&app, &who, &course, "kept name").await;
66 let uri = format!("/subjects/{subject}");
67
68 let kept = send(
69 &app,
70 "PATCH",
71 &uri,
72 Some(&who),
73 Some(json!({ "description": "only this" })),
74 )
75 .await;
76 assert_eq!(kept.status, StatusCode::OK);
77 assert_eq!(kept.body["name"], "kept name", "omitted name kept");
78 assert_eq!(kept.body["description"], "only this");
79
80 // An empty PATCH writes nothing at all and reads the row back unchanged.
81 let empty = send(&app, "PATCH", &uri, Some(&who), Some(json!({}))).await;
82 assert_eq!(empty.status, StatusCode::OK);
83 assert_eq!(empty.body["name"], "kept name");
84 assert_eq!(empty.body["description"], "only this");
85
86 // A blank name is required at create, so it stays refused at patch.
87 let bad = send(&app, "PATCH", &uri, Some(&who), Some(json!({ "name": "" }))).await;
88 assert_eq!(bad.status, StatusCode::BAD_REQUEST, "blank name refused");
89}