| 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 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 | |
| 59 | |
| 60 | #[tokio::test] |
| 61 | async 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 | |
| 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 | |
| 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 | } |