| 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 | #[tokio::test] |
| 53 | async 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 | |
| 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 | |
| 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 | |
| 115 | |
| 116 | #[tokio::test] |
| 117 | async 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 | } |