hezarfen_backend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_backend.git
0 watching·0 forks·Rust·8.3 MiB·6d68754·Ahmet Kaan Gümüş·2 months ago·566 commits
| 1 | //! Field validators. Each newtype's `try_new` calls one of these before wrapping |
| 2 | //! the value, so an existing newtype is always valid (parse, don't validate). |
| 3 | |
| 4 | use crate::constant::{ |
| 5 | COURSE_KINDS, EXAM_MODES, HOMEWORK_STATUSES, MAX_EMAIL_LEN, MAX_EXAM_ATTEMPTS, |
| 6 | MAX_EXAM_DURATION_MS, MAX_MARK, MAX_PASSWORD_LEN, MAX_PAYMENT_REQUEST_KEY_LEN, |
| 7 | MAX_PHONE_DIGITS, MAX_QUESTION_POINTS, MAX_USERNAME_LEN, MIN_EXAM_DURATION_MS, MIN_MARK, |
| 8 | MIN_PASSWORD_LEN, MIN_PHONE_DIGITS, MIN_QUESTION_POINTS, MIN_USERNAME_LEN, QUESTION_KINDS, |
| 9 | UNLIMITED_EXAM_ATTEMPTS, USERNAME_SEPARATORS, |
| 10 | }; |
| 11 | use crate::error::ValidationError; |
| 12 | |
| 13 | pub fn validate_username(value: &str) -> Result<(), ValidationError> { |
| 14 | // Length is measured on the trimmed value so padding can't defeat the |
| 15 | // minimum (e.g. "a " is a one-character name, not a three-character one). |
| 16 | let value = value.trim(); |
| 17 | if value.is_empty() { |
| 18 | return Err(ValidationError::Empty("username")); |
| 19 | } |
| 20 | if !value.is_ascii() { |
| 21 | return Err(ValidationError::NotAscii("username")); |
| 22 | } |
| 23 | let len = value.chars().count(); |
| 24 | if len < MIN_USERNAME_LEN { |
| 25 | return Err(ValidationError::TooShort { |
| 26 | field: "username", |
| 27 | min: MIN_USERNAME_LEN, |
| 28 | got: len, |
| 29 | }); |
| 30 | } |
| 31 | if len > MAX_USERNAME_LEN { |
| 32 | return Err(ValidationError::TooLong { |
| 33 | field: "username", |
| 34 | max: MAX_USERNAME_LEN, |
| 35 | got: len, |
| 36 | }); |
| 37 | } |
| 38 | if value.chars().any(|c| c.is_ascii_uppercase()) { |
| 39 | return Err(ValidationError::Invalid { |
| 40 | field: "username", |
| 41 | reason: "must be all lowercase", |
| 42 | }); |
| 43 | } |
| 44 | // Allowlist, not "any ASCII": control characters, spaces, quotes, and HTML |
| 45 | // metacharacters inside a username enable log injection, impersonation |
| 46 | // ("admin support"), and stored XSS in sloppy frontends. |
| 47 | if !value |
| 48 | .chars() |
| 49 | .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || USERNAME_SEPARATORS.contains(&c)) |
| 50 | { |
| 51 | return Err(ValidationError::Invalid { |
| 52 | field: "username", |
| 53 | reason: "may contain only lowercase letters, digits, '.', '_' and '-'", |
| 54 | }); |
| 55 | } |
| 56 | if !value.starts_with(|c: char| c.is_ascii_alphanumeric()) |
| 57 | || !value.ends_with(|c: char| c.is_ascii_alphanumeric()) |
| 58 | { |
| 59 | return Err(ValidationError::Invalid { |
| 60 | field: "username", |
| 61 | reason: "must start and end with a letter or digit", |
| 62 | }); |
| 63 | } |
| 64 | let mut chars = value.chars().peekable(); |
| 65 | while let Some(c) = chars.next() { |
| 66 | if USERNAME_SEPARATORS.contains(&c) |
| 67 | && chars |
| 68 | .peek() |
| 69 | .is_some_and(|n| USERNAME_SEPARATORS.contains(n)) |
| 70 | { |
| 71 | return Err(ValidationError::Invalid { |
| 72 | field: "username", |
| 73 | reason: "must not contain consecutive '.', '_' or '-'", |
| 74 | }); |
| 75 | } |
| 76 | } |
| 77 | Ok(()) |
| 78 | } |
| 79 | |
| 80 | pub fn validate_password(value: &str) -> Result<(), ValidationError> { |
| 81 | // A password may contain anything, including whitespace, so it is not |
| 82 | // trimmed — but length is still measured in characters, not UTF-8 bytes. |
| 83 | let len = value.chars().count(); |
| 84 | if len < MIN_PASSWORD_LEN { |
| 85 | return Err(ValidationError::TooShort { |
| 86 | field: "password", |
| 87 | min: MIN_PASSWORD_LEN, |
| 88 | got: len, |
| 89 | }); |
| 90 | } |
| 91 | if len > MAX_PASSWORD_LEN { |
| 92 | return Err(ValidationError::TooLong { |
| 93 | field: "password", |
| 94 | max: MAX_PASSWORD_LEN, |
| 95 | got: len, |
| 96 | }); |
| 97 | } |
| 98 | Ok(()) |
| 99 | } |
| 100 | |
| 101 | /// A required free-text field: non-blank and within `max`. |
| 102 | pub fn validate_required( |
| 103 | field: &'static str, |
| 104 | value: &str, |
| 105 | max: usize, |
| 106 | ) -> Result<(), ValidationError> { |
| 107 | if value.trim().is_empty() { |
| 108 | return Err(ValidationError::Empty(field)); |
| 109 | } |
| 110 | if value.chars().count() > max { |
| 111 | return Err(ValidationError::TooLong { |
| 112 | field, |
| 113 | max, |
| 114 | got: value.chars().count(), |
| 115 | }); |
| 116 | } |
| 117 | Ok(()) |
| 118 | } |
| 119 | |
| 120 | /// An optional free-text field: may be empty, but within `max`. |
| 121 | pub fn validate_optional( |
| 122 | field: &'static str, |
| 123 | value: &str, |
| 124 | max: usize, |
| 125 | ) -> Result<(), ValidationError> { |
| 126 | if value.chars().count() > max { |
| 127 | return Err(ValidationError::TooLong { |
| 128 | field, |
| 129 | max, |
| 130 | got: value.chars().count(), |
| 131 | }); |
| 132 | } |
| 133 | Ok(()) |
| 134 | } |
| 135 | |
| 136 | /// Pragmatic email shape check: one `@` with non-empty sides, a dot inside the |
| 137 | /// domain, ASCII, no whitespace. Deliverability is not provable here — this only |
| 138 | /// rejects values that cannot be an address. |
| 139 | pub fn validate_email(value: &str) -> Result<(), ValidationError> { |
| 140 | let value = value.trim(); |
| 141 | if value.is_empty() { |
| 142 | return Err(ValidationError::Empty("email")); |
| 143 | } |
| 144 | if !value.is_ascii() { |
| 145 | return Err(ValidationError::NotAscii("email")); |
| 146 | } |
| 147 | if value.chars().count() > MAX_EMAIL_LEN { |
| 148 | return Err(ValidationError::TooLong { |
| 149 | field: "email", |
| 150 | max: MAX_EMAIL_LEN, |
| 151 | got: value.chars().count(), |
| 152 | }); |
| 153 | } |
| 154 | let invalid = ValidationError::Invalid { |
| 155 | field: "email", |
| 156 | reason: "must look like name@example.com", |
| 157 | }; |
| 158 | if value.contains(char::is_whitespace) || value.matches('@').count() != 1 { |
| 159 | return Err(invalid); |
| 160 | } |
| 161 | match value.split_once('@') { |
| 162 | Some((local, domain)) |
| 163 | if !local.is_empty() |
| 164 | && domain.contains('.') |
| 165 | && !domain.starts_with('.') |
| 166 | && !domain.ends_with('.') => |
| 167 | { |
| 168 | Ok(()) |
| 169 | } |
| 170 | _ => Err(invalid), |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /// Phone numbers: an optional leading `+`, then digits with cosmetic spaces, |
| 175 | /// dashes, or parentheses; 7–15 digits total (E.164's ceiling). |
| 176 | pub fn validate_phone(value: &str) -> Result<(), ValidationError> { |
| 177 | let value = value.trim(); |
| 178 | if value.is_empty() { |
| 179 | return Err(ValidationError::Empty("phone")); |
| 180 | } |
| 181 | let rest = value.strip_prefix('+').unwrap_or(value); |
| 182 | if rest |
| 183 | .chars() |
| 184 | .any(|c| !c.is_ascii_digit() && !matches!(c, ' ' | '-' | '(' | ')')) |
| 185 | { |
| 186 | return Err(ValidationError::Invalid { |
| 187 | field: "phone", |
| 188 | reason: "may contain digits, spaces, dashes, parentheses, and a leading +", |
| 189 | }); |
| 190 | } |
| 191 | let digits = rest.chars().filter(char::is_ascii_digit).count(); |
| 192 | if !(MIN_PHONE_DIGITS..=MAX_PHONE_DIGITS).contains(&digits) { |
| 193 | return Err(ValidationError::Invalid { |
| 194 | field: "phone", |
| 195 | reason: "must contain 7 to 15 digits", |
| 196 | }); |
| 197 | } |
| 198 | Ok(()) |
| 199 | } |
| 200 | |
| 201 | /// A client-chosen idempotence key. It is concatenated into a record id whose |
| 202 | /// parts are joined by `_`, so the charset is an allowlist that **excludes the |
| 203 | /// separator itself**: a key containing `_` could spell another id's grammar |
| 204 | /// (a refund keyed `abc_r` would derive the id a reversal of that refund must |
| 205 | /// own), and two different ids meeting is money landing on the wrong line. |
| 206 | pub fn validate_request_key(value: &str) -> Result<(), ValidationError> { |
| 207 | let len = value.chars().count(); |
| 208 | if len == 0 { |
| 209 | return Err(ValidationError::Empty("request_key")); |
| 210 | } |
| 211 | if len > MAX_PAYMENT_REQUEST_KEY_LEN { |
| 212 | return Err(ValidationError::TooLong { |
| 213 | field: "request_key", |
| 214 | max: MAX_PAYMENT_REQUEST_KEY_LEN, |
| 215 | got: len, |
| 216 | }); |
| 217 | } |
| 218 | if !value.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') { |
| 219 | return Err(ValidationError::Invalid { |
| 220 | field: "request_key", |
| 221 | reason: "may contain only letters, digits and '-'", |
| 222 | }); |
| 223 | } |
| 224 | Ok(()) |
| 225 | } |
| 226 | |
| 227 | pub fn validate_course_kind(value: &str) -> Result<(), ValidationError> { |
| 228 | if COURSE_KINDS.contains(&value) { |
| 229 | Ok(()) |
| 230 | } else { |
| 231 | Err(ValidationError::Invalid { |
| 232 | field: "kind", |
| 233 | reason: "must be one of: course, study, club", |
| 234 | }) |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | pub fn validate_exam_mode(value: &str) -> Result<(), ValidationError> { |
| 239 | if EXAM_MODES.contains(&value) { |
| 240 | Ok(()) |
| 241 | } else { |
| 242 | Err(ValidationError::Invalid { |
| 243 | field: "mode", |
| 244 | reason: "must be one of: sync, async, open", |
| 245 | }) |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | pub fn validate_homework_status(value: &str) -> Result<(), ValidationError> { |
| 250 | if HOMEWORK_STATUSES.contains(&value) { |
| 251 | Ok(()) |
| 252 | } else { |
| 253 | Err(ValidationError::Invalid { |
| 254 | field: "status", |
| 255 | reason: "must be one of: done, incomplete, missing", |
| 256 | }) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | pub fn validate_attempt_limit(value: i64) -> Result<(), ValidationError> { |
| 261 | if value == UNLIMITED_EXAM_ATTEMPTS || (1..=MAX_EXAM_ATTEMPTS).contains(&value) { |
| 262 | Ok(()) |
| 263 | } else { |
| 264 | Err(ValidationError::Invalid { |
| 265 | field: "max_attempts", |
| 266 | reason: "must be between 1 and 100, or 0 for unlimited", |
| 267 | }) |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | pub fn validate_exam_duration(value: i64) -> Result<(), ValidationError> { |
| 272 | if (MIN_EXAM_DURATION_MS..=MAX_EXAM_DURATION_MS).contains(&value) { |
| 273 | Ok(()) |
| 274 | } else { |
| 275 | Err(ValidationError::Invalid { |
| 276 | field: "duration_ms", |
| 277 | reason: "must be between 60000 (1 minute) and 86400000 (24 hours) milliseconds", |
| 278 | }) |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | pub fn validate_question_kind(value: &str) -> Result<(), ValidationError> { |
| 283 | if QUESTION_KINDS.contains(&value) { |
| 284 | Ok(()) |
| 285 | } else { |
| 286 | Err(ValidationError::Invalid { |
| 287 | field: "kind", |
| 288 | reason: "must be one of: choice, text", |
| 289 | }) |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | pub fn validate_question_points(value: i64) -> Result<(), ValidationError> { |
| 294 | if (MIN_QUESTION_POINTS..=MAX_QUESTION_POINTS).contains(&value) { |
| 295 | Ok(()) |
| 296 | } else { |
| 297 | Err(ValidationError::Invalid { |
| 298 | field: "points", |
| 299 | reason: "must be between 1 and 100", |
| 300 | }) |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | pub fn validate_mark(value: i64) -> Result<(), ValidationError> { |
| 305 | if (MIN_MARK..=MAX_MARK).contains(&value) { |
| 306 | Ok(()) |
| 307 | } else { |
| 308 | Err(ValidationError::Invalid { |
| 309 | field: "mark", |
| 310 | reason: "must be between 0 and 100", |
| 311 | }) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | #[cfg(test)] |
| 316 | mod tests { |
| 317 | use super::*; |
| 318 | |
| 319 | #[tokio::test] |
| 320 | async fn username_rules() { |
| 321 | assert!(validate_username("ali").is_ok()); |
| 322 | assert!(validate_username("ab").is_err()); // too short |
| 323 | assert!(validate_username(" ").is_err()); // blank |
| 324 | assert!(validate_username(&"x".repeat(33)).is_err()); // too long |
| 325 | assert!(validate_username("naïve").is_err()); // non-ascii |
| 326 | assert!(validate_username("a ").is_err()); // padding can't defeat the minimum |
| 327 | assert!(validate_username("Ali").is_err()); // uppercase |
| 328 | assert!(validate_username("aLi").is_err()); // uppercase inside |
| 329 | assert!(validate_username("ali9").is_ok()); // digits fine, no case |
| 330 | assert!(validate_username("a-li").is_ok()); // separator inside is fine |
| 331 | assert!(validate_username("a.li").is_ok()); |
| 332 | assert!(validate_username("a_li").is_ok()); |
| 333 | assert!(validate_username("a.b-c_d").is_ok()); // mixed single separators |
| 334 | assert!(validate_username("-ali").is_err()); // must start alphanumeric |
| 335 | assert!(validate_username("ali-").is_err()); // must end alphanumeric |
| 336 | assert!(validate_username("_ali_").is_err()); |
| 337 | assert!(validate_username("9ali").is_ok()); // digit edges count as alphanumeric |
| 338 | assert!(validate_username(" ali ").is_ok()); // trimmed before edge check |
| 339 | } |
| 340 | |
| 341 | #[tokio::test] |
| 342 | async fn username_interior_is_an_allowlist_not_any_ascii() { |
| 343 | // ASCII control characters and metacharacters between valid edges must |
| 344 | // die here: they enable log injection, spoofing, and stored XSS. |
| 345 | assert!(validate_username("a b").is_err()); // interior space |
| 346 | assert!(validate_username("a\nb").is_err()); // newline survives trim mid-string |
| 347 | assert!(validate_username("a\x1bb").is_err()); // terminal escape |
| 348 | assert!(validate_username("a\x00b").is_err()); // null byte |
| 349 | assert!(validate_username("a<b>c").is_err()); // html metacharacters |
| 350 | assert!(validate_username("a\"b").is_err()); // quote |
| 351 | assert!(validate_username("a@b.c").is_err()); // looks like an email |
| 352 | assert!(validate_username("a/b").is_err()); |
| 353 | } |
| 354 | |
| 355 | #[tokio::test] |
| 356 | async fn username_separators_must_not_repeat() { |
| 357 | assert!(validate_username("a--b").is_err()); |
| 358 | assert!(validate_username("a..b").is_err()); |
| 359 | assert!(validate_username("a__b").is_err()); |
| 360 | assert!(validate_username("a.-b").is_err()); // mixed pairs count too |
| 361 | assert!(validate_username("a_-b").is_err()); |
| 362 | assert!(validate_username("a-b-c").is_ok()); // separated singles are fine |
| 363 | } |
| 364 | |
| 365 | #[tokio::test] |
| 366 | async fn length_is_measured_in_characters_not_bytes() { |
| 367 | // "é" is two UTF-8 bytes; a 4-char password of them is under the 6-char |
| 368 | // minimum and must be rejected as too short, not accepted on byte count. |
| 369 | assert!(validate_password("éééé").is_err()); |
| 370 | // Exactly `max` multi-byte characters is allowed (bytes would overflow). |
| 371 | assert!(validate_required("title", &"é".repeat(5), 5).is_ok()); |
| 372 | assert!(validate_required("title", &"é".repeat(6), 5).is_err()); |
| 373 | assert!(validate_optional("content", &"é".repeat(5), 5).is_ok()); |
| 374 | assert!(validate_optional("content", &"é".repeat(6), 5).is_err()); |
| 375 | } |
| 376 | |
| 377 | #[tokio::test] |
| 378 | async fn password_rules() { |
| 379 | assert!(validate_password("secret1").is_ok()); |
| 380 | assert!(validate_password("12345").is_err()); |
| 381 | assert!(validate_password(&"x".repeat(129)).is_err()); |
| 382 | } |
| 383 | |
| 384 | #[tokio::test] |
| 385 | async fn required_rules() { |
| 386 | assert!(validate_required("title", "hi", 10).is_ok()); |
| 387 | assert!(validate_required("title", " ", 10).is_err()); |
| 388 | assert!(validate_required("title", "toolong", 3).is_err()); |
| 389 | } |
| 390 | |
| 391 | #[tokio::test] |
| 392 | async fn optional_rules() { |
| 393 | assert!(validate_optional("content", "", 10).is_ok()); |
| 394 | assert!(validate_optional("content", "toolong", 3).is_err()); |
| 395 | } |
| 396 | |
| 397 | #[tokio::test] |
| 398 | async fn email_rules() { |
| 399 | assert!(validate_email("ada@example.com").is_ok()); |
| 400 | assert!(validate_email(" ada@example.com ").is_ok()); // trimmed before checking |
| 401 | assert!(validate_email("").is_err()); |
| 402 | assert!(validate_email("ada").is_err()); // no @ |
| 403 | assert!(validate_email("@example.com").is_err()); // empty local part |
| 404 | assert!(validate_email("ada@").is_err()); // empty domain |
| 405 | assert!(validate_email("ada@example").is_err()); // no dot in domain |
| 406 | assert!(validate_email("ada@.com").is_err()); // dot at domain edge |
| 407 | assert!(validate_email("ada@example.com.").is_err()); |
| 408 | assert!(validate_email("a da@example.com").is_err()); // whitespace inside |
| 409 | assert!(validate_email("ada@ex@ample.com").is_err()); // two @ |
| 410 | assert!(validate_email("adä@example.com").is_err()); // non-ascii |
| 411 | assert!(validate_email(&format!("{}@example.com", "x".repeat(250))).is_err()); |
| 412 | } |
| 413 | |
| 414 | #[tokio::test] |
| 415 | async fn phone_rules() { |
| 416 | assert!(validate_phone("+90 555 123 45 67").is_ok()); |
| 417 | assert!(validate_phone("05551234567").is_ok()); |
| 418 | assert!(validate_phone("(555) 123-4567").is_ok()); |
| 419 | assert!(validate_phone("").is_err()); |
| 420 | assert!(validate_phone("123456").is_err()); // 6 digits, too few |
| 421 | assert!(validate_phone("1234567890123456").is_err()); // 16 digits, too many |
| 422 | assert!(validate_phone("call-me-maybe").is_err()); // letters |
| 423 | assert!(validate_phone("55+5123456").is_err()); // + only allowed in front |
| 424 | } |
| 425 | |
| 426 | #[tokio::test] |
| 427 | async fn request_key_rules() { |
| 428 | assert!(validate_request_key("invoice-2026-114").is_ok()); |
| 429 | assert!(validate_request_key(&"x".repeat(MAX_PAYMENT_REQUEST_KEY_LEN)).is_ok()); |
| 430 | assert!(validate_request_key("").is_err()); |
| 431 | assert!(validate_request_key(&"x".repeat(MAX_PAYMENT_REQUEST_KEY_LEN + 1)).is_err()); |
| 432 | // `_` is the record-id separator, so a key carrying one could spell |
| 433 | // another line's id: `abc_r` under a refund is the id that refund's |
| 434 | // reversal must own, and `a_kr_b` under a charge is the id of a refund |
| 435 | // against that charge's `a`-keyed credit. |
| 436 | assert!(validate_request_key("abc_r").is_err()); |
| 437 | assert!(validate_request_key("a_kr_b").is_err()); |
| 438 | // Anything that could break — or alias — a record id. |
| 439 | assert!(validate_request_key("a b").is_err()); |
| 440 | assert!(validate_request_key("a:b").is_err()); |
| 441 | assert!(validate_request_key("a/b").is_err()); |
| 442 | assert!(validate_request_key("naïve").is_err()); |
| 443 | } |
| 444 | |
| 445 | #[tokio::test] |
| 446 | async fn course_kind_rules() { |
| 447 | for kind in ["course", "study", "club"] { |
| 448 | assert!(validate_course_kind(kind).is_ok()); |
| 449 | } |
| 450 | assert!(validate_course_kind("etut").is_err()); |
| 451 | assert!(validate_course_kind("kulup").is_err()); |
| 452 | assert!(validate_course_kind("").is_err()); |
| 453 | } |
| 454 | |
| 455 | #[tokio::test] |
| 456 | async fn exam_mode_rules() { |
| 457 | for mode in ["sync", "async", "open"] { |
| 458 | assert!(validate_exam_mode(mode).is_ok()); |
| 459 | } |
| 460 | assert!(validate_exam_mode("live").is_err()); |
| 461 | assert!(validate_exam_mode("").is_err()); |
| 462 | } |
| 463 | |
| 464 | #[tokio::test] |
| 465 | async fn attempt_limit_rules() { |
| 466 | assert!(validate_attempt_limit(UNLIMITED_EXAM_ATTEMPTS).is_ok()); |
| 467 | for limit in [1, 2, 50, MAX_EXAM_ATTEMPTS] { |
| 468 | assert!(validate_attempt_limit(limit).is_ok()); |
| 469 | } |
| 470 | assert!(validate_attempt_limit(-1).is_err()); |
| 471 | assert!(validate_attempt_limit(MAX_EXAM_ATTEMPTS + 1).is_err()); |
| 472 | } |
| 473 | |
| 474 | #[tokio::test] |
| 475 | async fn exam_duration_rules() { |
| 476 | assert!(validate_exam_duration(MIN_EXAM_DURATION_MS).is_ok()); |
| 477 | assert!(validate_exam_duration(90 * 60 * 1000).is_ok()); |
| 478 | assert!(validate_exam_duration(MAX_EXAM_DURATION_MS).is_ok()); |
| 479 | assert!(validate_exam_duration(MIN_EXAM_DURATION_MS - 1).is_err()); |
| 480 | assert!(validate_exam_duration(MAX_EXAM_DURATION_MS + 1).is_err()); |
| 481 | assert!(validate_exam_duration(0).is_err()); |
| 482 | assert!(validate_exam_duration(-1).is_err()); |
| 483 | } |
| 484 | |
| 485 | #[tokio::test] |
| 486 | async fn question_kind_rules() { |
| 487 | for kind in ["choice", "text"] { |
| 488 | assert!(validate_question_kind(kind).is_ok()); |
| 489 | } |
| 490 | assert!(validate_question_kind("essay").is_err()); |
| 491 | assert!(validate_question_kind("").is_err()); |
| 492 | } |
| 493 | |
| 494 | #[tokio::test] |
| 495 | async fn question_points_rules() { |
| 496 | for points in [1, 50, 100] { |
| 497 | assert!(validate_question_points(points).is_ok()); |
| 498 | } |
| 499 | assert!(validate_question_points(0).is_err()); |
| 500 | assert!(validate_question_points(-1).is_err()); |
| 501 | assert!(validate_question_points(101).is_err()); |
| 502 | } |
| 503 | |
| 504 | #[tokio::test] |
| 505 | async fn mark_rules() { |
| 506 | for mark in [0, 1, 50, 99, 100] { |
| 507 | assert!(validate_mark(mark).is_ok()); |
| 508 | } |
| 509 | assert!(validate_mark(-1).is_err()); |
| 510 | assert!(validate_mark(101).is_err()); |
| 511 | } |
| 512 | } |