| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | mod common; |
| 8 | |
| 9 | use axum::http::StatusCode; |
| 10 | use common::*; |
| 11 | use hezarfen_backend::domain::timestamp::Timestamp; |
| 12 | use serde_json::json; |
| 13 | |
| 14 | const ROUNDS: usize = 20; |
| 15 | |
| 16 | |
| 17 | async fn world( |
| 18 | app: &axum::Router, |
| 19 | db: &hezarfen_backend::database::Database, |
| 20 | ) -> (String, String, String, String) { |
| 21 | let teacher = login_as(app, db, "teacher", "teacher").await; |
| 22 | let ali = login(app, "ali").await; |
| 23 | let ali_id = me_id(app, &ali).await; |
| 24 | let veli = login(app, "veli").await; |
| 25 | let veli_id = me_id(app, &veli).await; |
| 26 | let course = create_course(app, &teacher, "math").await; |
| 27 | let subject = create_subject(app, &teacher, &course, "algebra").await; |
| 28 | enroll(app, &teacher, &course, &ali_id).await; |
| 29 | enroll(app, &teacher, &course, &veli_id).await; |
| 30 | (teacher, course, subject, ali_id) |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | #[tokio::test] |
| 36 | async fn concurrent_title_and_due_at_patches_both_stick() { |
| 37 | let (app, db) = app_and_db().await; |
| 38 | let (teacher, course, subject, _) = world(&app, &db).await; |
| 39 | |
| 40 | for round in 0..ROUNDS { |
| 41 | let due = Timestamp::now().as_millis() + 3_600_000; |
| 42 | let id = create_homework(&app, &teacher, &course, &subject, "hw", due).await; |
| 43 | let uri = format!("/homework/{id}"); |
| 44 | let new_due = due + 86_400_000; |
| 45 | let (a, b) = tokio::join!( |
| 46 | send( |
| 47 | &app, |
| 48 | "PATCH", |
| 49 | &uri, |
| 50 | Some(&teacher), |
| 51 | Some(json!({ "title": "renamed" })), |
| 52 | ), |
| 53 | send( |
| 54 | &app, |
| 55 | "PATCH", |
| 56 | &uri, |
| 57 | Some(&teacher), |
| 58 | Some(json!({ "due_at": new_due })), |
| 59 | ), |
| 60 | ); |
| 61 | assert_eq!(a.status, StatusCode::OK, "round {round} title patch"); |
| 62 | assert_eq!(b.status, StatusCode::OK, "round {round} due_at patch"); |
| 63 | |
| 64 | let after = send(&app, "GET", &uri, Some(&teacher), None).await.body; |
| 65 | assert_eq!(after["title"], "renamed", "round {round}: title reverted"); |
| 66 | assert_eq!(after["due_at"], new_due, "round {round}: due_at reverted"); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | #[tokio::test] |
| 75 | async fn concurrent_title_and_assigned_patches_both_stick() { |
| 76 | let (app, db) = app_and_db().await; |
| 77 | let (teacher, course, subject, ali_id) = world(&app, &db).await; |
| 78 | |
| 79 | for round in 0..ROUNDS { |
| 80 | let due = Timestamp::now().as_millis() + 3_600_000; |
| 81 | let id = create_homework(&app, &teacher, &course, &subject, "hw", due).await; |
| 82 | let uri = format!("/homework/{id}"); |
| 83 | let (a, b) = tokio::join!( |
| 84 | send( |
| 85 | &app, |
| 86 | "PATCH", |
| 87 | &uri, |
| 88 | Some(&teacher), |
| 89 | Some(json!({ "title": "renamed" })), |
| 90 | ), |
| 91 | send( |
| 92 | &app, |
| 93 | "PATCH", |
| 94 | &uri, |
| 95 | Some(&teacher), |
| 96 | Some(json!({ "assigned": [ali_id.clone()] })), |
| 97 | ), |
| 98 | ); |
| 99 | assert_eq!(a.status, StatusCode::OK, "round {round} title patch"); |
| 100 | assert_eq!(b.status, StatusCode::OK, "round {round} assigned patch"); |
| 101 | |
| 102 | let after = send(&app, "GET", &uri, Some(&teacher), None).await.body; |
| 103 | assert_eq!(after["title"], "renamed", "round {round}: title reverted"); |
| 104 | assert_eq!( |
| 105 | after["assigned"], |
| 106 | json!([ali_id]), |
| 107 | "round {round}: assigned reverted to whole-course" |
| 108 | ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | #[tokio::test] |
| 116 | async fn assigned_null_still_clears_the_subset() { |
| 117 | let (app, db) = app_and_db().await; |
| 118 | let (teacher, course, subject, ali_id) = world(&app, &db).await; |
| 119 | let due = Timestamp::now().as_millis() + 3_600_000; |
| 120 | let res = create_homework_with( |
| 121 | &app, |
| 122 | &teacher, |
| 123 | &course, |
| 124 | json!({ "title": "hw", "subject_id": subject, "due_at": due, "assigned": [ali_id] }), |
| 125 | ) |
| 126 | .await; |
| 127 | assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); |
| 128 | let uri = format!("/homework/{}", id_of(&res.body)); |
| 129 | |
| 130 | |
| 131 | let kept = send( |
| 132 | &app, |
| 133 | "PATCH", |
| 134 | &uri, |
| 135 | Some(&teacher), |
| 136 | Some(json!({ "title": "kept" })), |
| 137 | ) |
| 138 | .await; |
| 139 | assert_eq!(kept.status, StatusCode::OK, "{}", kept.body); |
| 140 | assert_eq!(kept.body["assigned"], json!([ali_id]), "omitted cleared it"); |
| 141 | |
| 142 | |
| 143 | let cleared = send( |
| 144 | &app, |
| 145 | "PATCH", |
| 146 | &uri, |
| 147 | Some(&teacher), |
| 148 | Some(json!({ "assigned": null })), |
| 149 | ) |
| 150 | .await; |
| 151 | assert_eq!(cleared.status, StatusCode::OK, "{}", cleared.body); |
| 152 | assert_eq!( |
| 153 | cleared.body["assigned"], |
| 154 | json!(null), |
| 155 | "explicit null did not clear the subset" |
| 156 | ); |
| 157 | |
| 158 | |
| 159 | let desc = send( |
| 160 | &app, |
| 161 | "PATCH", |
| 162 | &uri, |
| 163 | Some(&teacher), |
| 164 | Some(json!({ "description": "read it" })), |
| 165 | ) |
| 166 | .await; |
| 167 | assert_eq!(desc.body["description"], "read it", "{}", desc.body); |
| 168 | let cleared = send( |
| 169 | &app, |
| 170 | "PATCH", |
| 171 | &uri, |
| 172 | Some(&teacher), |
| 173 | Some(json!({ "description": null })), |
| 174 | ) |
| 175 | .await; |
| 176 | assert_eq!(cleared.body["description"], json!(null), "{}", cleared.body); |
| 177 | } |