| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | mod common; |
| 7 | |
| 8 | use axum::http::StatusCode; |
| 9 | use common::*; |
| 10 | use serde_json::json; |
| 11 | |
| 12 | #[tokio::test] |
| 13 | async 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 | |
| 51 | |
| 52 | |
| 53 | #[tokio::test] |
| 54 | async fn field_patch_leaves_members_alone() { |
| 55 | let (app, db) = app_and_db().await; |
| 56 | let manager = login_as(&app, &db, "course_patch_owner", "manager").await; |
| 57 | let student = login(&app, "course_patch_member").await; |
| 58 | let student_id = me_id(&app, &student).await; |
| 59 | |
| 60 | let created = send( |
| 61 | &app, |
| 62 | "POST", |
| 63 | "/courses", |
| 64 | Some(&manager), |
| 65 | Some(json!({ "title": "robotics club", "kind": "club" })), |
| 66 | ) |
| 67 | .await; |
| 68 | assert_eq!(created.status, StatusCode::CREATED, "{}", created.body); |
| 69 | let id = id_of(&created.body); |
| 70 | let uri = format!("/courses/{id}"); |
| 71 | let joined = send( |
| 72 | &app, |
| 73 | "POST", |
| 74 | &format!("{uri}/members"), |
| 75 | Some(&manager), |
| 76 | Some(json!({ "user_id": student_id })), |
| 77 | ) |
| 78 | .await; |
| 79 | assert_eq!( |
| 80 | joined.status, |
| 81 | StatusCode::OK, |
| 82 | "join member: {}", |
| 83 | joined.body |
| 84 | ); |
| 85 | |
| 86 | let patched = send( |
| 87 | &app, |
| 88 | "PATCH", |
| 89 | &uri, |
| 90 | Some(&manager), |
| 91 | Some(json!({ "title": "renamed" })), |
| 92 | ) |
| 93 | .await; |
| 94 | assert_eq!(patched.status, StatusCode::OK, "title patch"); |
| 95 | assert_eq!( |
| 96 | patched.body["course_membership_count"], 1, |
| 97 | "the counter a field PATCH may not clobber: {}", |
| 98 | patched.body |
| 99 | ); |
| 100 | let listed = send(&app, "GET", &format!("{uri}/members"), Some(&manager), None).await; |
| 101 | assert_eq!(listed.status, StatusCode::OK, "{}", listed.body); |
| 102 | assert_eq!(items(&listed.body).len(), 1, "{}", listed.body); |
| 103 | assert_eq!( |
| 104 | items(&listed.body)[0]["user"]["id"], |
| 105 | student_id, |
| 106 | "the member a plain field PATCH must leave standing: {}", |
| 107 | listed.body |
| 108 | ); |
| 109 | } |