| 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_profile_patches_keep_both_fields() { |
| 14 | let (app, db) = app_and_db().await; |
| 15 | let who = login_as(&app, &db, "ali", "student").await; |
| 16 | |
| 17 | for round in 0..20 { |
| 18 | let name = format!("name{round}"); |
| 19 | let surname = format!("surname{round}"); |
| 20 | let (a, b) = tokio::join!( |
| 21 | send( |
| 22 | &app, |
| 23 | "PATCH", |
| 24 | "/users/me", |
| 25 | Some(&who), |
| 26 | Some(json!({ "name": name })), |
| 27 | ), |
| 28 | send( |
| 29 | &app, |
| 30 | "PATCH", |
| 31 | "/users/me", |
| 32 | Some(&who), |
| 33 | Some(json!({ "surname": surname })), |
| 34 | ), |
| 35 | ); |
| 36 | assert_eq!(a.status, StatusCode::OK, "round {round} name patch"); |
| 37 | assert_eq!(b.status, StatusCode::OK, "round {round} surname patch"); |
| 38 | |
| 39 | |
| 40 | let after = send(&app, "GET", "/auth/me", Some(&who), None).await.body; |
| 41 | assert_eq!(after["name"], name, "round {round}: name reverted"); |
| 42 | assert_eq!(after["surname"], surname, "round {round}: surname reverted"); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | #[tokio::test] |
| 47 | async fn concurrent_preference_patches_keep_both_fields() { |
| 48 | let (app, db) = app_and_db().await; |
| 49 | let who = login_as(&app, &db, "veli", "student").await; |
| 50 | |
| 51 | for round in 0..20 { |
| 52 | |
| 53 | let (theme, language) = if round % 2 == 0 { |
| 54 | ("dark", "tr") |
| 55 | } else { |
| 56 | ("light", "en") |
| 57 | }; |
| 58 | let color = if round % 2 == 0 { "#283618" } else { "#fefae0" }; |
| 59 | let (a, b, c) = tokio::join!( |
| 60 | send( |
| 61 | &app, |
| 62 | "PATCH", |
| 63 | "/users/me/preferences", |
| 64 | Some(&who), |
| 65 | Some(json!({ "theme": theme })), |
| 66 | ), |
| 67 | send( |
| 68 | &app, |
| 69 | "PATCH", |
| 70 | "/users/me/preferences", |
| 71 | Some(&who), |
| 72 | Some(json!({ "language": language })), |
| 73 | ), |
| 74 | send( |
| 75 | &app, |
| 76 | "PATCH", |
| 77 | "/users/me/preferences", |
| 78 | Some(&who), |
| 79 | Some(json!({ "palette_color": color })), |
| 80 | ), |
| 81 | ); |
| 82 | assert_eq!(a.status, StatusCode::OK, "round {round} theme patch"); |
| 83 | assert_eq!(b.status, StatusCode::OK, "round {round} language patch"); |
| 84 | assert_eq!(c.status, StatusCode::OK, "round {round} palette patch"); |
| 85 | |
| 86 | let after = send(&app, "GET", "/auth/me", Some(&who), None).await.body; |
| 87 | assert_eq!(after["theme"], theme, "round {round}: theme reverted"); |
| 88 | assert_eq!( |
| 89 | after["language"], language, |
| 90 | "round {round}: language reverted" |
| 91 | ); |
| 92 | assert_eq!( |
| 93 | after["palette_color"], color, |
| 94 | "round {round}: palette_color reverted" |
| 95 | ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | |
| 101 | #[tokio::test] |
| 102 | async fn omitted_keeps_empty_clears_and_validation_still_bites() { |
| 103 | let (app, db) = app_and_db().await; |
| 104 | let who = login_as(&app, &db, "ayse", "student").await; |
| 105 | |
| 106 | let set = send( |
| 107 | &app, |
| 108 | "PATCH", |
| 109 | "/users/me", |
| 110 | Some(&who), |
| 111 | Some(json!({ "name": "Ada", "surname": "Lovelace", "email": "ada@example.com" })), |
| 112 | ) |
| 113 | .await; |
| 114 | assert_eq!(set.status, StatusCode::OK, "{}", set.body); |
| 115 | |
| 116 | |
| 117 | let kept = send( |
| 118 | &app, |
| 119 | "PATCH", |
| 120 | "/users/me", |
| 121 | Some(&who), |
| 122 | Some(json!({ "surname": null })), |
| 123 | ) |
| 124 | .await; |
| 125 | assert_eq!(kept.status, StatusCode::OK); |
| 126 | assert_eq!(kept.body["name"], "Ada", "omitted name kept"); |
| 127 | assert_eq!(kept.body["surname"], "Lovelace", "null keeps surname"); |
| 128 | assert_eq!(kept.body["email"], "ada@example.com"); |
| 129 | |
| 130 | |
| 131 | let empty = send(&app, "PATCH", "/users/me", Some(&who), Some(json!({}))).await; |
| 132 | assert_eq!(empty.status, StatusCode::OK); |
| 133 | assert_eq!(empty.body["name"], "Ada"); |
| 134 | assert_eq!(empty.body["email"], "ada@example.com"); |
| 135 | |
| 136 | |
| 137 | let cleared = send( |
| 138 | &app, |
| 139 | "PATCH", |
| 140 | "/users/me", |
| 141 | Some(&who), |
| 142 | Some(json!({ "email": "" })), |
| 143 | ) |
| 144 | .await; |
| 145 | assert_eq!(cleared.status, StatusCode::OK); |
| 146 | assert_eq!(cleared.body["email"], json!(null), "email not cleared"); |
| 147 | assert_eq!(cleared.body["name"], "Ada", "name lost to the clear"); |
| 148 | |
| 149 | |
| 150 | let bad = send( |
| 151 | &app, |
| 152 | "PATCH", |
| 153 | "/users/me", |
| 154 | Some(&who), |
| 155 | Some(json!({ "email": "not-an-email" })), |
| 156 | ) |
| 157 | .await; |
| 158 | assert_eq!(bad.status, StatusCode::BAD_REQUEST, "bad email refused"); |
| 159 | |
| 160 | let bad_theme = send( |
| 161 | &app, |
| 162 | "PATCH", |
| 163 | "/users/me/preferences", |
| 164 | Some(&who), |
| 165 | Some(json!({ "theme": "chartreuse" })), |
| 166 | ) |
| 167 | .await; |
| 168 | assert_eq!( |
| 169 | bad_theme.status, |
| 170 | StatusCode::BAD_REQUEST, |
| 171 | "bad theme refused" |
| 172 | ); |
| 173 | |
| 174 | |
| 175 | |
| 176 | let set = send( |
| 177 | &app, |
| 178 | "PATCH", |
| 179 | "/users/me/preferences", |
| 180 | Some(&who), |
| 181 | Some(json!({ "theme": "dark", "palette_color": "#FEFAE0" })), |
| 182 | ) |
| 183 | .await; |
| 184 | assert_eq!(set.status, StatusCode::OK, "{}", set.body); |
| 185 | assert_eq!(set.body["palette_color"], "#fefae0", "stored lowercase"); |
| 186 | |
| 187 | let cleared = send( |
| 188 | &app, |
| 189 | "PATCH", |
| 190 | "/users/me/preferences", |
| 191 | Some(&who), |
| 192 | Some(json!({ "palette_color": "" })), |
| 193 | ) |
| 194 | .await; |
| 195 | assert_eq!(cleared.status, StatusCode::OK); |
| 196 | assert_eq!(cleared.body["palette_color"], json!(null)); |
| 197 | assert_eq!(cleared.body["theme"], "dark", "theme lost to the clear"); |
| 198 | |
| 199 | let bad_color = send( |
| 200 | &app, |
| 201 | "PATCH", |
| 202 | "/users/me/preferences", |
| 203 | Some(&who), |
| 204 | Some(json!({ "palette_color": "#fff" })), |
| 205 | ) |
| 206 | .await; |
| 207 | assert_eq!( |
| 208 | bad_color.status, |
| 209 | StatusCode::BAD_REQUEST, |
| 210 | "bad palette color refused" |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | #[tokio::test] |
| 218 | async fn extra_profile_fields_merge_field_by_field() { |
| 219 | let (app, db) = app_and_db().await; |
| 220 | let who = login_as(&app, &db, "ece", "student").await; |
| 221 | |
| 222 | let set = send( |
| 223 | &app, |
| 224 | "PATCH", |
| 225 | "/users/me", |
| 226 | Some(&who), |
| 227 | Some(json!({ "gender": "other", "address": "Deniz Mah." })), |
| 228 | ) |
| 229 | .await; |
| 230 | assert_eq!(set.status, StatusCode::OK, "{}", set.body); |
| 231 | |
| 232 | |
| 233 | let kept = send( |
| 234 | &app, |
| 235 | "PATCH", |
| 236 | "/users/me", |
| 237 | Some(&who), |
| 238 | Some(json!({ "address": null })), |
| 239 | ) |
| 240 | .await; |
| 241 | assert_eq!(kept.status, StatusCode::OK); |
| 242 | assert_eq!(kept.body["gender"], "other", "omitted gender kept"); |
| 243 | assert_eq!(kept.body["address"], "Deniz Mah.", "null keeps address"); |
| 244 | |
| 245 | |
| 246 | let cleared = send( |
| 247 | &app, |
| 248 | "PATCH", |
| 249 | "/users/me", |
| 250 | Some(&who), |
| 251 | Some(json!({ "address": "" })), |
| 252 | ) |
| 253 | .await; |
| 254 | assert_eq!(cleared.status, StatusCode::OK); |
| 255 | assert_eq!(cleared.body["address"], json!(null)); |
| 256 | assert_eq!(cleared.body["gender"], "other", "gender lost to the clear"); |
| 257 | |
| 258 | |
| 259 | let refused = send( |
| 260 | &app, |
| 261 | "PATCH", |
| 262 | "/users/me", |
| 263 | Some(&who), |
| 264 | Some(json!({ "gender": "woman" })), |
| 265 | ) |
| 266 | .await; |
| 267 | assert_eq!(refused.status, StatusCode::BAD_REQUEST); |
| 268 | let after = send(&app, "GET", "/auth/me", Some(&who), None).await.body; |
| 269 | assert_eq!(after["gender"], "other", "a refused patch writes nothing"); |
| 270 | } |