Store tests share their store and merge into tables
Browse files at this commit535510d6963ae05d6f77e547194ea5c8aeecf589Parent: c389fb6- test
- store tests share their store and merge into tables
5 changed files +332 -340
- crates/forge-core/src/store/account_tokens.rs +41 -55
- crates/forge-core/src/store/mail.rs +18 -22
- crates/forge-core/src/store/orgs.rs +165 -157
- crates/forge-core/src/store/passkeys.rs +35 -37
- crates/forge-core/src/store/webhooks.rs +73 -69
crates/forge-core/src/store/account_tokens.rsmodified+41 -55
| @@ -350,9 +350,9 @@ mod tests { | |||
| 350 | (dir, store) | 350 | (dir, store) |
| 351 | } | 351 | } |
| 352 | 352 | ||
| 353 | - async fn tahinli(store: &Store) -> User { | 353 | + async fn new_user(store: &Store, name: &str, email: &str) -> User { |
| 354 | store | 354 | store |
| 355 | - .create_user("tahinli", Some("tahinli@example.com"), "hash", false) | 355 | + .create_user(name, Some(email), "hash", false) |
| 356 | .await | 356 | .await |
| 357 | .expect("user") | 357 | .expect("user") |
| 358 | } | 358 | } |
| @@ -368,10 +368,14 @@ mod tests { | |||
| 368 | .expect("expire"); | 368 | .expect("expire"); |
| 369 | } | 369 | } |
| 370 | 370 | ||
| 371 | + /// Reset tokens redeem once, expired and unknown ones are refused, and a | ||
| 372 | + /// new token replaces the unused one. Scenarios share one store and user. | ||
| 371 | #[tokio::test] | 373 | #[tokio::test] |
| 372 | - async fn reset_token_redeems_once() { | 374 | + async fn reset_tokens() { |
| 373 | let (_dir, store) = store().await; | 375 | let (_dir, store) = store().await; |
| 374 | - let user = tahinli(&store).await; | 376 | + let user = new_user(&store, "tahinli", "tahinli@example.com").await; |
| 377 | + | ||
| 378 | + // redeems once | ||
| 375 | let token = store.create_reset_token(&user.id).await.unwrap(); | 379 | let token = store.create_reset_token(&user.id).await.unwrap(); |
| 376 | assert!(store.reset_token_is_live(&token).await.unwrap()); | 380 | assert!(store.reset_token_is_live(&token).await.unwrap()); |
| 377 | assert_eq!( | 381 | assert_eq!( |
| @@ -380,32 +384,28 @@ mod tests { | |||
| 380 | ); | 384 | ); |
| 381 | assert_eq!(store.redeem_reset_token(&token).await.unwrap(), None); | 385 | assert_eq!(store.redeem_reset_token(&token).await.unwrap(), None); |
| 382 | assert!(!store.reset_token_is_live(&token).await.unwrap()); | 386 | assert!(!store.reset_token_is_live(&token).await.unwrap()); |
| 383 | - } | ||
| 384 | 387 | ||
| 385 | - #[tokio::test] | 388 | + // expired and unknown are refused |
| 386 | - async fn expired_and_unknown_reset_tokens_are_refused() { | ||
| 387 | - let (_dir, store) = store().await; | ||
| 388 | - let user = tahinli(&store).await; | ||
| 389 | let token = store.create_reset_token(&user.id).await.unwrap(); | 389 | let token = store.create_reset_token(&user.id).await.unwrap(); |
| 390 | expire(&store, "password_reset_tokens", &token).await; | 390 | expire(&store, "password_reset_tokens", &token).await; |
| 391 | assert_eq!(store.redeem_reset_token(&token).await.unwrap(), None); | 391 | assert_eq!(store.redeem_reset_token(&token).await.unwrap(), None); |
| 392 | assert_eq!(store.redeem_reset_token("forge_nope").await.unwrap(), None); | 392 | assert_eq!(store.redeem_reset_token("forge_nope").await.unwrap(), None); |
| 393 | - } | ||
| 394 | 393 | ||
| 395 | - #[tokio::test] | 394 | + // a new token replaces the unused one |
| 396 | - async fn a_new_reset_token_replaces_the_unused_one() { | ||
| 397 | - let (_dir, store) = store().await; | ||
| 398 | - let user = tahinli(&store).await; | ||
| 399 | let first = store.create_reset_token(&user.id).await.unwrap(); | 395 | let first = store.create_reset_token(&user.id).await.unwrap(); |
| 400 | let second = store.create_reset_token(&user.id).await.unwrap(); | 396 | let second = store.create_reset_token(&user.id).await.unwrap(); |
| 401 | assert_eq!(store.redeem_reset_token(&first).await.unwrap(), None); | 397 | assert_eq!(store.redeem_reset_token(&first).await.unwrap(), None); |
| 402 | assert!(store.redeem_reset_token(&second).await.unwrap().is_some()); | 398 | assert!(store.redeem_reset_token(&second).await.unwrap().is_some()); |
| 403 | } | 399 | } |
| 404 | 400 | ||
| 401 | + /// Email verification gates reset lookups, a token is void once the | ||
| 402 | + /// address changes, and clearing the mark unverifies the address. | ||
| 405 | #[tokio::test] | 403 | #[tokio::test] |
| 406 | - async fn only_a_verified_address_matches_a_reset_lookup() { | 404 | + async fn email_verification() { |
| 407 | let (_dir, store) = store().await; | 405 | let (_dir, store) = store().await; |
| 408 | - let user = tahinli(&store).await; | 406 | + |
| 407 | + // only a verified address matches a reset lookup | ||
| 408 | + let user = new_user(&store, "tahinli", "tahinli@example.com").await; | ||
| 409 | assert!( | 409 | assert!( |
| 410 | store | 410 | store |
| 411 | .verified_user_by_email("TAHINLI@example.com") | 411 | .verified_user_by_email("TAHINLI@example.com") |
| @@ -429,59 +429,53 @@ mod tests { | |||
| 429 | .unwrap() | 429 | .unwrap() |
| 430 | .expect("verified user"); | 430 | .expect("verified user"); |
| 431 | assert_eq!(found.id, user.id); | 431 | assert_eq!(found.id, user.id); |
| 432 | - } | ||
| 433 | 432 | ||
| 434 | - #[tokio::test] | 433 | + // single use, and refused after the address changes |
| 435 | - async fn verify_token_is_single_use_and_refused_after_the_address_changes() { | 434 | + let carol = new_user(&store, "carol", "carol@example.com").await; |
| 436 | - let (_dir, store) = store().await; | ||
| 437 | - let user = tahinli(&store).await; | ||
| 438 | let token = store | 435 | let token = store |
| 439 | - .create_verify_token(&user.id, "tahinli@example.com") | 436 | + .create_verify_token(&carol.id, "carol@example.com") |
| 440 | .await | 437 | .await |
| 441 | .unwrap(); | 438 | .unwrap(); |
| 442 | store | 439 | store |
| 443 | - .set_email(&user.id, "other@example.com") | 440 | + .set_email(&carol.id, "other@example.com") |
| 444 | .await | 441 | .await |
| 445 | .unwrap(); | 442 | .unwrap(); |
| 446 | assert_eq!(store.redeem_verify_token(&token).await.unwrap(), None); | 443 | assert_eq!(store.redeem_verify_token(&token).await.unwrap(), None); |
| 447 | - assert!(!store.email_is_verified(&user.id).await.unwrap()); | 444 | + assert!(!store.email_is_verified(&carol.id).await.unwrap()); |
| 448 | - | ||
| 449 | let fresh = store | 445 | let fresh = store |
| 450 | - .create_verify_token(&user.id, "other@example.com") | 446 | + .create_verify_token(&carol.id, "other@example.com") |
| 451 | .await | 447 | .await |
| 452 | .unwrap(); | 448 | .unwrap(); |
| 453 | assert!(store.redeem_verify_token(&fresh).await.unwrap().is_some()); | 449 | assert!(store.redeem_verify_token(&fresh).await.unwrap().is_some()); |
| 454 | assert_eq!(store.redeem_verify_token(&fresh).await.unwrap(), None); | 450 | assert_eq!(store.redeem_verify_token(&fresh).await.unwrap(), None); |
| 455 | - } | ||
| 456 | 451 | ||
| 457 | - #[tokio::test] | 452 | + // clearing the mark unverifies the address |
| 458 | - async fn clearing_the_mark_unverifies_the_address() { | 453 | + let dave = new_user(&store, "dave", "dave@example.com").await; |
| 459 | - let (_dir, store) = store().await; | ||
| 460 | - let user = tahinli(&store).await; | ||
| 461 | let token = store | 454 | let token = store |
| 462 | - .create_verify_token(&user.id, "tahinli@example.com") | 455 | + .create_verify_token(&dave.id, "dave@example.com") |
| 463 | .await | 456 | .await |
| 464 | .unwrap(); | 457 | .unwrap(); |
| 465 | store.redeem_verify_token(&token).await.unwrap(); | 458 | store.redeem_verify_token(&token).await.unwrap(); |
| 466 | - store.clear_email_verified(&user.id).await.unwrap(); | 459 | + store.clear_email_verified(&dave.id).await.unwrap(); |
| 467 | - assert!(!store.email_is_verified(&user.id).await.unwrap()); | 460 | + assert!(!store.email_is_verified(&dave.id).await.unwrap()); |
| 468 | } | 461 | } |
| 469 | 462 | ||
| 463 | + /// Invites: a taken address is refused, an invite makes one verified | ||
| 464 | + /// account and is single use, a taken username leaves it unused, and an | ||
| 465 | + /// expired invite is refused. | ||
| 470 | #[tokio::test] | 466 | #[tokio::test] |
| 471 | - async fn invite_for_a_taken_address_is_refused() { | 467 | + async fn invites() { |
| 472 | let (_dir, store) = store().await; | 468 | let (_dir, store) = store().await; |
| 473 | - let user = tahinli(&store).await; | 469 | + let admin = new_user(&store, "tahinli", "tahinli@example.com").await; |
| 470 | + | ||
| 471 | + // a taken address is refused | ||
| 474 | let err = store | 472 | let err = store |
| 475 | - .create_invite("TAHINLI@example.com", &user.id) | 473 | + .create_invite("TAHINLI@example.com", &admin.id) |
| 476 | .await | 474 | .await |
| 477 | .expect_err("taken"); | 475 | .expect_err("taken"); |
| 478 | assert!(matches!(err, Error::Conflict(_))); | 476 | assert!(matches!(err, Error::Conflict(_))); |
| 479 | - } | ||
| 480 | 477 | ||
| 481 | - #[tokio::test] | 478 | + // one verified account, single use |
| 482 | - async fn invite_creates_one_verified_account_and_is_single_use() { | ||
| 483 | - let (_dir, store) = store().await; | ||
| 484 | - let admin = tahinli(&store).await; | ||
| 485 | let token = store | 479 | let token = store |
| 486 | .create_invite("bob@example.com", &admin.id) | 480 | .create_invite("bob@example.com", &admin.id) |
| 487 | .await | 481 | .await |
| @@ -505,14 +499,10 @@ mod tests { | |||
| 505 | .unwrap() | 499 | .unwrap() |
| 506 | .is_none() | 500 | .is_none() |
| 507 | ); | 501 | ); |
| 508 | - } | ||
| 509 | 502 | ||
| 510 | - #[tokio::test] | 503 | + // a taken username leaves the invite unused |
| 511 | - async fn a_taken_username_leaves_the_invite_unused() { | ||
| 512 | - let (_dir, store) = store().await; | ||
| 513 | - let admin = tahinli(&store).await; | ||
| 514 | let token = store | 504 | let token = store |
| 515 | - .create_invite("bob@example.com", &admin.id) | 505 | + .create_invite("gus@example.com", &admin.id) |
| 516 | .await | 506 | .await |
| 517 | .unwrap(); | 507 | .unwrap(); |
| 518 | let err = store | 508 | let err = store |
| @@ -521,20 +511,16 @@ mod tests { | |||
| 521 | .expect_err("taken username"); | 511 | .expect_err("taken username"); |
| 522 | assert!(matches!(err, Error::Conflict(_))); | 512 | assert!(matches!(err, Error::Conflict(_))); |
| 523 | assert!(store.invite_email(&token).await.unwrap().is_some()); | 513 | assert!(store.invite_email(&token).await.unwrap().is_some()); |
| 524 | - } | ||
| 525 | 514 | ||
| 526 | - #[tokio::test] | 515 | + // an expired invite is refused |
| 527 | - async fn expired_invite_is_refused() { | ||
| 528 | - let (_dir, store) = store().await; | ||
| 529 | - let admin = tahinli(&store).await; | ||
| 530 | let token = store | 516 | let token = store |
| 531 | - .create_invite("bob@example.com", &admin.id) | 517 | + .create_invite("erin@example.com", &admin.id) |
| 532 | .await | 518 | .await |
| 533 | .unwrap(); | 519 | .unwrap(); |
| 534 | expire(&store, "invites", &token).await; | 520 | expire(&store, "invites", &token).await; |
| 535 | assert!( | 521 | assert!( |
| 536 | store | 522 | store |
| 537 | - .accept_invite(&token, "bob", "hash") | 523 | + .accept_invite(&token, "erin", "hash") |
| 538 | .await | 524 | .await |
| 539 | .unwrap() | 525 | .unwrap() |
| 540 | .is_none() | 526 | .is_none() |
crates/forge-core/src/store/mail.rsmodified+18 -22
| @@ -465,8 +465,9 @@ mod tests { | |||
| 465 | } | 465 | } |
| 466 | } | 466 | } |
| 467 | 467 | ||
| 468 | + /// A repeated key queues once, and bad recipients or an empty key are refused. | ||
| 468 | #[tokio::test] | 469 | #[tokio::test] |
| 469 | - async fn enqueue_with_the_same_key_queues_once() { | 470 | + async fn enqueue_dedupes_by_key_and_refuses_bad_input() { |
| 470 | let (_dir, store) = store_with_key().await; | 471 | let (_dir, store) = store_with_key().await; |
| 471 | assert!( | 472 | assert!( |
| 472 | store | 473 | store |
| @@ -487,11 +488,8 @@ mod tests { | |||
| 487 | .expect("other key") | 488 | .expect("other key") |
| 488 | ); | 489 | ); |
| 489 | assert_eq!(store.recent_mail(10).await.expect("rows").len(), 2); | 490 | assert_eq!(store.recent_mail(10).await.expect("rows").len(), 2); |
| 490 | - } | ||
| 491 | 491 | ||
| 492 | - #[tokio::test] | 492 | + // a bad recipient and an empty key are refused |
| 493 | - async fn enqueue_rejects_a_bad_recipient_and_an_empty_key() { | ||
| 494 | - let (_dir, store) = store_with_key().await; | ||
| 495 | assert!( | 493 | assert!( |
| 496 | store | 494 | store |
| 497 | .enqueue_mail("k", &mail("not an address")) | 495 | .enqueue_mail("k", &mail("not an address")) |
| @@ -611,8 +609,9 @@ mod tests { | |||
| 611 | assert_eq!(store.next_mail_due().await.expect("none"), None); | 609 | assert_eq!(store.next_mail_due().await.expect("none"), None); |
| 612 | } | 610 | } |
| 613 | 611 | ||
| 612 | + /// The password is sealed at rest and round-trips, and saving without one keeps it. | ||
| 614 | #[tokio::test] | 613 | #[tokio::test] |
| 615 | - async fn the_password_is_sealed_and_round_trips() { | 614 | + async fn mail_password_is_sealed_and_kept() { |
| 616 | let (_dir, store) = store_with_key().await; | 615 | let (_dir, store) = store_with_key().await; |
| 617 | let before = store.mail_settings().await.expect("defaults"); | 616 | let before = store.mail_settings().await.expect("defaults"); |
| 618 | assert!(!before.has_password); | 617 | assert!(!before.has_password); |
| @@ -633,22 +632,7 @@ mod tests { | |||
| 633 | Some("s3cret-smtp-pw".to_owned()) | 632 | Some("s3cret-smtp-pw".to_owned()) |
| 634 | ); | 633 | ); |
| 635 | 634 | ||
| 636 | - // The column holds ciphertext, not the password. | 635 | + // saving without a password keeps the stored one |
| 637 | - let conn = store.inner.conn.lock().await; | ||
| 638 | - let mut rows = conn | ||
| 639 | - .query("SELECT password_sealed FROM mail_settings WHERE id = 1", ()) | ||
| 640 | - .await | ||
| 641 | - .expect("query"); | ||
| 642 | - let row = rows.next().await.expect("next").expect("row"); | ||
| 643 | - let Value::Blob(bytes) = row.get_value(0).expect("value") else { | ||
| 644 | - panic!("the sealed password is a blob"); | ||
| 645 | - }; | ||
| 646 | - assert!(!bytes.windows(b"s3cret".len()).any(|w| w == b"s3cret")); | ||
| 647 | - } | ||
| 648 | - | ||
| 649 | - #[tokio::test] | ||
| 650 | - async fn saving_without_a_password_keeps_the_stored_one() { | ||
| 651 | - let (_dir, store) = store_with_key().await; | ||
| 652 | store | 636 | store |
| 653 | .update_mail_settings(&settings(Some("first-pw"))) | 637 | .update_mail_settings(&settings(Some("first-pw"))) |
| 654 | .await | 638 | .await |
| @@ -666,6 +650,18 @@ mod tests { | |||
| 666 | .map(|p| p.expose().to_owned()), | 650 | .map(|p| p.expose().to_owned()), |
| 667 | Some("first-pw".to_owned()) | 651 | Some("first-pw".to_owned()) |
| 668 | ); | 652 | ); |
| 653 | + | ||
| 654 | + // The column holds ciphertext, not the password. | ||
| 655 | + let conn = store.inner.conn.lock().await; | ||
| 656 | + let mut rows = conn | ||
| 657 | + .query("SELECT password_sealed FROM mail_settings WHERE id = 1", ()) | ||
| 658 | + .await | ||
| 659 | + .expect("query"); | ||
| 660 | + let row = rows.next().await.expect("next").expect("row"); | ||
| 661 | + let Value::Blob(bytes) = row.get_value(0).expect("value") else { | ||
| 662 | + panic!("the sealed password is a blob"); | ||
| 663 | + }; | ||
| 664 | + assert!(!bytes.windows(b"s3cret".len()).any(|w| w == b"s3cret")); | ||
| 669 | } | 665 | } |
| 670 | 666 | ||
| 671 | #[tokio::test] | 667 | #[tokio::test] |
crates/forge-core/src/store/orgs.rsmodified+165 -157
| @@ -729,19 +729,18 @@ mod tests { | |||
| 729 | use crate::model::{AccountKind, Visibility}; | 729 | use crate::model::{AccountKind, Visibility}; |
| 730 | use crate::store::user_delete::GHOST_USERNAME; | 730 | use crate::store::user_delete::GHOST_USERNAME; |
| 731 | 731 | ||
| 732 | - async fn fixture() -> (tempfile::TempDir, Store, User, User) { | 732 | + async fn store() -> (tempfile::TempDir, Store) { |
| 733 | let dir = tempfile::tempdir().unwrap(); | 733 | let dir = tempfile::tempdir().unwrap(); |
| 734 | let store = Store::open(&dir.path().join("forge.db")).await.unwrap(); | 734 | let store = Store::open(&dir.path().join("forge.db")).await.unwrap(); |
| 735 | - let tahinli = store | 735 | + (dir, store) |
| 736 | - .create_user("tahinli", None, "hash", false) | ||
| 737 | - .await | ||
| 738 | - .unwrap(); | ||
| 739 | - let bob = store.create_user("bob", None, "hash", false).await.unwrap(); | ||
| 740 | - (dir, store, tahinli, bob) | ||
| 741 | } | 736 | } |
| 742 | 737 | ||
| 743 | - async fn org_with_repo(store: &Store, owner: &User) -> (User, Repo) { | 738 | + async fn person(store: &Store, name: &str) -> User { |
| 744 | - let org = store.create_org("acme", &owner.id).await.unwrap(); | 739 | + store.create_user(name, None, "hash", false).await.unwrap() |
| 740 | + } | ||
| 741 | + | ||
| 742 | + async fn org_with_repo(store: &Store, owner: &User, org: &str) -> (User, Repo) { | ||
| 743 | + let org = store.create_org(org, &owner.id).await.unwrap(); | ||
| 745 | let repo = store | 744 | let repo = store |
| 746 | .create_repo(&org.id, "proj", "", Visibility::Private, "main") | 745 | .create_repo(&org.id, "proj", "", Visibility::Private, "main") |
| 747 | .await | 746 | .await |
| @@ -749,9 +748,13 @@ mod tests { | |||
| 749 | (org, repo) | 748 | (org, repo) |
| 750 | } | 749 | } |
| 751 | 750 | ||
| 751 | + /// Scenarios share one store. Each has its own people and org names. | ||
| 752 | #[tokio::test] | 752 | #[tokio::test] |
| 753 | - async fn create_org_makes_the_creator_an_owner_with_default_settings() { | 753 | + async fn org_creation_and_owner_rules() { |
| 754 | - let (_dir, store, tahinli, _bob) = fixture().await; | 754 | + let (_dir, store) = store().await; |
| 755 | + | ||
| 756 | + // the creator becomes an owner with default settings | ||
| 757 | + let tahinli = person(&store, "tahinli").await; | ||
| 755 | let org = store.create_org("Acme", &tahinli.id).await.unwrap(); | 758 | let org = store.create_org("Acme", &tahinli.id).await.unwrap(); |
| 756 | assert!(org.is_org()); | 759 | assert!(org.is_org()); |
| 757 | assert_eq!(org.kind, AccountKind::Org); | 760 | assert_eq!(org.kind, AccountKind::Org); |
| @@ -773,33 +776,122 @@ mod tests { | |||
| 773 | assert!(store.create_user("acme", None, "h", false).await.is_err()); | 776 | assert!(store.create_user("acme", None, "h", false).await.is_err()); |
| 774 | assert!(store.create_org("org", &tahinli.id).await.is_err()); | 777 | assert!(store.create_org("org", &tahinli.id).await.is_err()); |
| 775 | assert!(store.create_org("bad name", &tahinli.id).await.is_err()); | 778 | assert!(store.create_org("bad name", &tahinli.id).await.is_err()); |
| 776 | - } | ||
| 777 | 779 | ||
| 778 | - #[tokio::test] | 780 | + // only a person can create an org or join one |
| 779 | - async fn only_a_person_can_create_an_org_or_join_one() { | 781 | + let ada = person(&store, "ada").await; |
| 780 | - let (_dir, store, tahinli, _bob) = fixture().await; | 782 | + let umbrella = store.create_org("umbrella", &ada.id).await.unwrap(); |
| 781 | - let org = store.create_org("acme", &tahinli.id).await.unwrap(); | ||
| 782 | assert!(matches!( | 783 | assert!(matches!( |
| 783 | - store.create_org("other", &org.id).await, | 784 | + store.create_org("other", &umbrella.id).await, |
| 784 | Err(Error::Forbidden(_)) | 785 | Err(Error::Forbidden(_)) |
| 785 | )); | 786 | )); |
| 786 | - let second = store.create_org("second", &tahinli.id).await.unwrap(); | 787 | + let second = store.create_org("second", &ada.id).await.unwrap(); |
| 787 | assert!(matches!( | 788 | assert!(matches!( |
| 788 | store | 789 | store |
| 789 | - .add_org_member(&org.id, &second.id, OrgMemberRole::Member) | 790 | + .add_org_member(&umbrella.id, &second.id, OrgMemberRole::Member) |
| 790 | .await, | 791 | .await, |
| 791 | Err(Error::Invalid(_)) | 792 | Err(Error::Invalid(_)) |
| 792 | )); | 793 | )); |
| 794 | + | ||
| 795 | + // the last owner cannot be removed or demoted | ||
| 796 | + let grace = person(&store, "grace").await; | ||
| 797 | + let hopper = person(&store, "hopper").await; | ||
| 798 | + let initech = store.create_org("initech", &grace.id).await.unwrap(); | ||
| 799 | + store | ||
| 800 | + .add_org_member(&initech.id, &hopper.id, OrgMemberRole::Member) | ||
| 801 | + .await | ||
| 802 | + .unwrap(); | ||
| 803 | + assert!(matches!( | ||
| 804 | + store.remove_org_member(&initech.id, &grace.id).await, | ||
| 805 | + Err(Error::Conflict(_)) | ||
| 806 | + )); | ||
| 807 | + assert!(matches!( | ||
| 808 | + store | ||
| 809 | + .set_org_member_role(&initech.id, &grace.id, OrgMemberRole::Member) | ||
| 810 | + .await, | ||
| 811 | + Err(Error::Conflict(_)) | ||
| 812 | + )); | ||
| 813 | + // With a second owner, the first may go. | ||
| 814 | + store | ||
| 815 | + .set_org_member_role(&initech.id, &hopper.id, OrgMemberRole::Owner) | ||
| 816 | + .await | ||
| 817 | + .unwrap(); | ||
| 818 | + store | ||
| 819 | + .set_org_member_role(&initech.id, &grace.id, OrgMemberRole::Member) | ||
| 820 | + .await | ||
| 821 | + .unwrap(); | ||
| 822 | + store | ||
| 823 | + .remove_org_member(&initech.id, &grace.id) | ||
| 824 | + .await | ||
| 825 | + .unwrap(); | ||
| 826 | + assert_eq!( | ||
| 827 | + store.org_member_role(&initech.id, &grace.id).await.unwrap(), | ||
| 828 | + None | ||
| 829 | + ); | ||
| 830 | + assert!(matches!( | ||
| 831 | + store.remove_org_member(&initech.id, &grace.id).await, | ||
| 832 | + Err(Error::NotFound) | ||
| 833 | + )); | ||
| 834 | + let members = store.org_members(&initech.id).await.unwrap(); | ||
| 835 | + assert_eq!(members.len(), 1); | ||
| 836 | + assert_eq!(members[0].1, OrgMemberRole::Owner); | ||
| 837 | + | ||
| 838 | + // deleting the last owner of an org is refused | ||
| 839 | + let linus = person(&store, "linus").await; | ||
| 840 | + let ken = person(&store, "ken").await; | ||
| 841 | + let hooli = store.create_org("hooli", &linus.id).await.unwrap(); | ||
| 842 | + let ghost = store.ensure_ghost_user().await.unwrap(); | ||
| 843 | + assert_eq!(ghost.username, GHOST_USERNAME); | ||
| 844 | + let refused = store.delete_user(&linus.id, &ghost.id).await; | ||
| 845 | + assert!( | ||
| 846 | + matches!(&refused, Err(Error::Conflict(msg)) if msg == crate::store::user_delete::LAST_ORG_OWNER), | ||
| 847 | + "{refused:?}" | ||
| 848 | + ); | ||
| 849 | + assert!(store.user_by_id(&linus.id).await.unwrap().is_some()); | ||
| 850 | + // A second owner makes the deletion allowed. | ||
| 851 | + store | ||
| 852 | + .add_org_member(&hooli.id, &ken.id, OrgMemberRole::Owner) | ||
| 853 | + .await | ||
| 854 | + .unwrap(); | ||
| 855 | + store.delete_user(&linus.id, &ghost.id).await.unwrap(); | ||
| 856 | + assert!(store.user_by_id(&linus.id).await.unwrap().is_none()); | ||
| 857 | + assert_eq!( | ||
| 858 | + store.org_member_role(&hooli.id, &ken.id).await.unwrap(), | ||
| 859 | + Some(OrgMemberRole::Owner) | ||
| 860 | + ); | ||
| 861 | + | ||
| 862 | + // an org cannot be deleted as a user, and delete_org needs no repos | ||
| 863 | + let margaret = person(&store, "margaret").await; | ||
| 864 | + let (lambda, repo) = org_with_repo(&store, &margaret, "lambda").await; | ||
| 865 | + assert!(matches!( | ||
| 866 | + store.delete_user(&lambda.id, &ghost.id).await, | ||
| 867 | + Err(Error::Conflict(_)) | ||
| 868 | + )); | ||
| 869 | + assert!(matches!( | ||
| 870 | + store.delete_org(&lambda.id).await, | ||
| 871 | + Err(Error::Conflict(_)) | ||
| 872 | + )); | ||
| 873 | + store.delete_repo(&repo.id).await.unwrap(); | ||
| 874 | + store.delete_org(&lambda.id).await.unwrap(); | ||
| 875 | + assert!(store.user_by_id(&lambda.id).await.unwrap().is_none()); | ||
| 876 | + assert!(matches!( | ||
| 877 | + store.delete_org(&margaret.id).await, | ||
| 878 | + Err(Error::NotFound) | ||
| 879 | + )); | ||
| 793 | } | 880 | } |
| 794 | 881 | ||
| 882 | + /// Repo roles an org grants through its default role and teams, and the | ||
| 883 | + /// lookup of an org-owned repo by owner and name. | ||
| 795 | #[tokio::test] | 884 | #[tokio::test] |
| 796 | - async fn org_repo_role_matrix() { | 885 | + async fn org_repo_roles_and_lookup() { |
| 797 | - let (_dir, store, tahinli, bob) = fixture().await; | 886 | + let (_dir, store) = store().await; |
| 798 | - let (org, repo) = org_with_repo(&store, &tahinli).await; | 887 | + |
| 888 | + let frank = person(&store, "frank").await; | ||
| 889 | + let (org, repo) = org_with_repo(&store, &frank, "globex").await; | ||
| 799 | let other = store | 890 | let other = store |
| 800 | - .create_repo(&tahinli.id, "theirs", "", Visibility::Private, "main") | 891 | + .create_repo(&frank.id, "theirs", "", Visibility::Private, "main") |
| 801 | .await | 892 | .await |
| 802 | .unwrap(); | 893 | .unwrap(); |
| 894 | + let bob = person(&store, "bob").await; | ||
| 803 | store | 895 | store |
| 804 | .add_org_member(&org.id, &bob.id, OrgMemberRole::Member) | 896 | .add_org_member(&org.id, &bob.id, OrgMemberRole::Member) |
| 805 | .await | 897 | .await |
| @@ -808,7 +900,7 @@ mod tests { | |||
| 808 | // Owner gives admin, on every org-owned repo. | 900 | // Owner gives admin, on every org-owned repo. |
| 809 | assert_eq!( | 901 | assert_eq!( |
| 810 | store | 902 | store |
| 811 | - .org_repo_role(&org.id, &tahinli.id, &repo.id) | 903 | + .org_repo_role(&org.id, &frank.id, &repo.id) |
| 812 | .await | 904 | .await |
| 813 | .unwrap(), | 905 | .unwrap(), |
| 814 | Some(Role::Admin) | 906 | Some(Role::Admin) |
| @@ -824,16 +916,16 @@ mod tests { | |||
| 824 | // A repo the org does not own gets nothing from the org. | 916 | // A repo the org does not own gets nothing from the org. |
| 825 | assert_eq!( | 917 | assert_eq!( |
| 826 | store | 918 | store |
| 827 | - .org_repo_role(&org.id, &tahinli.id, &other.id) | 919 | + .org_repo_role(&org.id, &frank.id, &other.id) |
| 828 | .await | 920 | .await |
| 829 | .unwrap(), | 921 | .unwrap(), |
| 830 | None | 922 | None |
| 831 | ); | 923 | ); |
| 832 | // A stranger gets nothing. | 924 | // A stranger gets nothing. |
| 833 | - let stranger = store.create_user("carol", None, "h", false).await.unwrap(); | 925 | + let carol = person(&store, "carol").await; |
| 834 | assert_eq!( | 926 | assert_eq!( |
| 835 | store | 927 | store |
| 836 | - .org_repo_role(&org.id, &stranger.id, &repo.id) | 928 | + .org_repo_role(&org.id, &carol.id, &repo.id) |
| 837 | .await | 929 | .await |
| 838 | .unwrap(), | 930 | .unwrap(), |
| 839 | None | 931 | None |
| @@ -904,125 +996,53 @@ mod tests { | |||
| 904 | // An org that is not an org. | 996 | // An org that is not an org. |
| 905 | assert_eq!( | 997 | assert_eq!( |
| 906 | store | 998 | store |
| 907 | - .org_repo_role(&tahinli.id, &tahinli.id, &repo.id) | 999 | + .org_repo_role(&frank.id, &frank.id, &repo.id) |
| 908 | .await | 1000 | .await |
| 909 | .unwrap(), | 1001 | .unwrap(), |
| 910 | None | 1002 | None |
| 911 | ); | 1003 | ); |
| 912 | - } | ||
| 913 | 1004 | ||
| 914 | - #[tokio::test] | 1005 | + // An org-owned repo is found by its owner and name. |
| 915 | - async fn repo_by_owner_and_name_finds_an_org_owned_repo() { | 1006 | + let dana = person(&store, "dana").await; |
| 916 | - let (_dir, store, tahinli, _bob) = fixture().await; | 1007 | + let (soylent, soylent_repo) = org_with_repo(&store, &dana, "soylent").await; |
| 917 | - let (org, repo) = org_with_repo(&store, &tahinli).await; | ||
| 918 | let found = store | 1008 | let found = store |
| 919 | - .repo_by_owner_and_name("acme", "proj") | 1009 | + .repo_by_owner_and_name("soylent", "proj") |
| 920 | .await | 1010 | .await |
| 921 | .unwrap() | 1011 | .unwrap() |
| 922 | .expect("repo"); | 1012 | .expect("repo"); |
| 923 | - assert_eq!(found.id, repo.id); | 1013 | + assert_eq!(found.id, soylent_repo.id); |
| 924 | - assert_eq!(found.owner_id, org.id); | 1014 | + assert_eq!(found.owner_id, soylent.id); |
| 925 | - assert_eq!(found.owner_name, "acme"); | 1015 | + assert_eq!(found.owner_name, "soylent"); |
| 926 | } | 1016 | } |
| 927 | 1017 | ||
| 1018 | + /// The org listing, teams and their members and repo links, and the orgs a | ||
| 1019 | + /// user belongs to. The listing comes first because it starts empty. | ||
| 928 | #[tokio::test] | 1020 | #[tokio::test] |
| 929 | - async fn last_owner_cannot_be_removed_or_demoted() { | 1021 | + async fn teams_memberships_and_org_listing() { |
| 930 | - let (_dir, store, tahinli, bob) = fixture().await; | 1022 | + let (_dir, store) = store().await; |
| 931 | - let org = store.create_org("acme", &tahinli.id).await.unwrap(); | ||
| 932 | - store | ||
| 933 | - .add_org_member(&org.id, &bob.id, OrgMemberRole::Member) | ||
| 934 | - .await | ||
| 935 | - .unwrap(); | ||
| 936 | - assert!(matches!( | ||
| 937 | - store.remove_org_member(&org.id, &tahinli.id).await, | ||
| 938 | - Err(Error::Conflict(_)) | ||
| 939 | - )); | ||
| 940 | - assert!(matches!( | ||
| 941 | - store | ||
| 942 | - .set_org_member_role(&org.id, &tahinli.id, OrgMemberRole::Member) | ||
| 943 | - .await, | ||
| 944 | - Err(Error::Conflict(_)) | ||
| 945 | - )); | ||
| 946 | - // With a second owner, the first may go. | ||
| 947 | - store | ||
| 948 | - .set_org_member_role(&org.id, &bob.id, OrgMemberRole::Owner) | ||
| 949 | - .await | ||
| 950 | - .unwrap(); | ||
| 951 | - store | ||
| 952 | - .set_org_member_role(&org.id, &tahinli.id, OrgMemberRole::Member) | ||
| 953 | - .await | ||
| 954 | - .unwrap(); | ||
| 955 | - store.remove_org_member(&org.id, &tahinli.id).await.unwrap(); | ||
| 956 | - assert_eq!( | ||
| 957 | - store.org_member_role(&org.id, &tahinli.id).await.unwrap(), | ||
| 958 | - None | ||
| 959 | - ); | ||
| 960 | - assert!(matches!( | ||
| 961 | - store.remove_org_member(&org.id, &tahinli.id).await, | ||
| 962 | - Err(Error::NotFound) | ||
| 963 | - )); | ||
| 964 | - let members = store.org_members(&org.id).await.unwrap(); | ||
| 965 | - assert_eq!(members.len(), 1); | ||
| 966 | - assert_eq!(members[0].1, OrgMemberRole::Owner); | ||
| 967 | - } | ||
| 968 | 1023 | ||
| 969 | - #[tokio::test] | 1024 | + assert!(store.list_orgs().await.unwrap().is_empty()); |
| 970 | - async fn deleting_the_last_owner_of_an_org_is_refused() { | 1025 | + let ivan = person(&store, "ivan").await; |
| 971 | - let (_dir, store, tahinli, bob) = fixture().await; | 1026 | + let acme = store.create_org("acme", &ivan.id).await.unwrap(); |
| 972 | - let org = store.create_org("acme", &tahinli.id).await.unwrap(); | 1027 | + let names: Vec<String> = store |
| 973 | - let ghost = store.ensure_ghost_user().await.unwrap(); | 1028 | + .list_orgs() |
| 974 | - assert_eq!(ghost.username, GHOST_USERNAME); | ||
| 975 | - let refused = store.delete_user(&tahinli.id, &ghost.id).await; | ||
| 976 | - assert!( | ||
| 977 | - matches!(&refused, Err(Error::Conflict(msg)) if msg == crate::store::user_delete::LAST_ORG_OWNER), | ||
| 978 | - "{refused:?}" | ||
| 979 | - ); | ||
| 980 | - assert!(store.user_by_id(&tahinli.id).await.unwrap().is_some()); | ||
| 981 | - // A second owner makes the deletion allowed. | ||
| 982 | - store | ||
| 983 | - .add_org_member(&org.id, &bob.id, OrgMemberRole::Owner) | ||
| 984 | .await | 1029 | .await |
| 985 | - .unwrap(); | 1030 | + .unwrap() |
| 986 | - store.delete_user(&tahinli.id, &ghost.id).await.unwrap(); | 1031 | + .into_iter() |
| 987 | - assert!(store.user_by_id(&tahinli.id).await.unwrap().is_none()); | 1032 | + .map(|org| org.username) |
| 988 | - assert_eq!( | 1033 | + .collect(); |
| 989 | - store.org_member_role(&org.id, &bob.id).await.unwrap(), | 1034 | + assert_eq!(names, vec![acme.username.clone()]); |
| 990 | - Some(OrgMemberRole::Owner) | ||
| 991 | - ); | ||
| 992 | - } | ||
| 993 | - | ||
| 994 | - #[tokio::test] | ||
| 995 | - async fn an_org_cannot_be_deleted_as_a_user_and_delete_org_needs_no_repos() { | ||
| 996 | - let (_dir, store, tahinli, _bob) = fixture().await; | ||
| 997 | - let (org, repo) = org_with_repo(&store, &tahinli).await; | ||
| 998 | - let ghost = store.ensure_ghost_user().await.unwrap(); | ||
| 999 | - assert!(matches!( | ||
| 1000 | - store.delete_user(&org.id, &ghost.id).await, | ||
| 1001 | - Err(Error::Conflict(_)) | ||
| 1002 | - )); | ||
| 1003 | - assert!(matches!( | ||
| 1004 | - store.delete_org(&org.id).await, | ||
| 1005 | - Err(Error::Conflict(_)) | ||
| 1006 | - )); | ||
| 1007 | - store.delete_repo(&repo.id).await.unwrap(); | ||
| 1008 | - store.delete_org(&org.id).await.unwrap(); | ||
| 1009 | - assert!(store.user_by_id(&org.id).await.unwrap().is_none()); | ||
| 1010 | - assert!(matches!( | ||
| 1011 | - store.delete_org(&tahinli.id).await, | ||
| 1012 | - Err(Error::NotFound) | ||
| 1013 | - )); | ||
| 1014 | - } | ||
| 1015 | 1035 | ||
| 1016 | - #[tokio::test] | 1036 | + // teams |
| 1017 | - async fn teams_crud_membership_and_repo_links() { | 1037 | + let nina = person(&store, "nina").await; |
| 1018 | - let (_dir, store, tahinli, bob) = fixture().await; | 1038 | + let (org, repo) = org_with_repo(&store, &nina, "wayne").await; |
| 1019 | - let (org, repo) = org_with_repo(&store, &tahinli).await; | ||
| 1020 | let theirs = store | 1039 | let theirs = store |
| 1021 | - .create_repo(&tahinli.id, "theirs", "", Visibility::Private, "main") | 1040 | + .create_repo(&nina.id, "theirs", "", Visibility::Private, "main") |
| 1022 | .await | 1041 | .await |
| 1023 | .unwrap(); | 1042 | .unwrap(); |
| 1043 | + let oscar = person(&store, "oscar").await; | ||
| 1024 | store | 1044 | store |
| 1025 | - .add_org_member(&org.id, &bob.id, OrgMemberRole::Member) | 1045 | + .add_org_member(&org.id, &oscar.id, OrgMemberRole::Member) |
| 1026 | .await | 1046 | .await |
| 1027 | .unwrap(); | 1047 | .unwrap(); |
| 1028 | 1048 | ||
| @@ -1047,13 +1067,13 @@ mod tests { | |||
| 1047 | assert_eq!(store.teams_for_org(&org.id).await.unwrap().len(), 1); | 1067 | assert_eq!(store.teams_for_org(&org.id).await.unwrap().len(), 1); |
| 1048 | 1068 | ||
| 1049 | // Only org members join teams. | 1069 | // Only org members join teams. |
| 1050 | - let stranger = store.create_user("carol", None, "h", false).await.unwrap(); | 1070 | + let pam = person(&store, "pam").await; |
| 1051 | assert!(matches!( | 1071 | assert!(matches!( |
| 1052 | - store.add_team_member(&team.id, &stranger.id).await, | 1072 | + store.add_team_member(&team.id, &pam.id).await, |
| 1053 | Err(Error::Invalid(_)) | 1073 | Err(Error::Invalid(_)) |
| 1054 | )); | 1074 | )); |
| 1055 | - store.add_team_member(&team.id, &bob.id).await.unwrap(); | 1075 | + store.add_team_member(&team.id, &oscar.id).await.unwrap(); |
| 1056 | - assert!(store.add_team_member(&team.id, &bob.id).await.is_err()); | 1076 | + assert!(store.add_team_member(&team.id, &oscar.id).await.is_err()); |
| 1057 | assert_eq!(store.team_members(&team.id).await.unwrap().len(), 1); | 1077 | assert_eq!(store.team_members(&team.id).await.unwrap().len(), 1); |
| 1058 | 1078 | ||
| 1059 | // Only repos the org owns can be linked. | 1079 | // Only repos the org owns can be linked. |
| @@ -1073,45 +1093,33 @@ mod tests { | |||
| 1073 | store.delete_team(&team.id).await.unwrap(); | 1093 | store.delete_team(&team.id).await.unwrap(); |
| 1074 | assert!(store.team_by_id(&team.id).await.unwrap().is_none()); | 1094 | assert!(store.team_by_id(&team.id).await.unwrap().is_none()); |
| 1075 | assert!(store.team_members(&team.id).await.unwrap().is_empty()); | 1095 | assert!(store.team_members(&team.id).await.unwrap().is_empty()); |
| 1076 | - assert!(store.user_by_id(&bob.id).await.unwrap().is_some()); | 1096 | + assert!(store.user_by_id(&oscar.id).await.unwrap().is_some()); |
| 1077 | assert!(store.repo_by_id(&repo.id).await.unwrap().is_some()); | 1097 | assert!(store.repo_by_id(&repo.id).await.unwrap().is_some()); |
| 1078 | assert!(matches!( | 1098 | assert!(matches!( |
| 1079 | store.delete_team(&team.id).await, | 1099 | store.delete_team(&team.id).await, |
| 1080 | Err(Error::NotFound) | 1100 | Err(Error::NotFound) |
| 1081 | )); | 1101 | )); |
| 1082 | - } | ||
| 1083 | 1102 | ||
| 1084 | - #[tokio::test] | 1103 | + // orgs a user belongs to |
| 1085 | - async fn orgs_for_user_lists_memberships() { | 1104 | + let quinn = person(&store, "quinn").await; |
| 1086 | - let (_dir, store, tahinli, bob) = fixture().await; | 1105 | + let rita = person(&store, "rita").await; |
| 1087 | - let acme = store.create_org("acme", &tahinli.id).await.unwrap(); | 1106 | + let tyrell = store.create_org("tyrell", &quinn.id).await.unwrap(); |
| 1088 | - let initech = store.create_org("initech", &tahinli.id).await.unwrap(); | 1107 | + let cyberdyne = store.create_org("cyberdyne", &quinn.id).await.unwrap(); |
| 1089 | store | 1108 | store |
| 1090 | - .add_org_member(&acme.id, &bob.id, OrgMemberRole::Member) | 1109 | + .add_org_member(&tyrell.id, &rita.id, OrgMemberRole::Member) |
| 1091 | .await | 1110 | .await |
| 1092 | .unwrap(); | 1111 | .unwrap(); |
| 1093 | - let mine = store.orgs_for_user(&bob.id).await.unwrap(); | 1112 | + let mine = store.orgs_for_user(&rita.id).await.unwrap(); |
| 1094 | assert_eq!(mine.len(), 1); | 1113 | assert_eq!(mine.len(), 1); |
| 1095 | - assert_eq!(mine[0].0.id, acme.id); | 1114 | + assert_eq!(mine[0].0.id, tyrell.id); |
| 1096 | assert_eq!(mine[0].1, OrgMemberRole::Member); | 1115 | assert_eq!(mine[0].1, OrgMemberRole::Member); |
| 1097 | - assert_eq!(store.orgs_for_user(&tahinli.id).await.unwrap().len(), 2); | 1116 | + assert_eq!(store.orgs_for_user(&quinn.id).await.unwrap().len(), 2); |
| 1098 | - store.delete_org(&initech.id).await.unwrap(); | 1117 | + store.delete_org(&cyberdyne.id).await.unwrap(); |
| 1099 | - assert_eq!(store.orgs_for_user(&tahinli.id).await.unwrap().len(), 1); | 1118 | + assert_eq!(store.orgs_for_user(&quinn.id).await.unwrap().len(), 1); |
| 1100 | } | 1119 | } |
| 1101 | 1120 | ||
| 1102 | - #[tokio::test] | 1121 | + #[test] |
| 1103 | - async fn list_orgs_returns_only_orgs_and_the_role_words_round_trip() { | 1122 | + fn role_words_round_trip() { |
| 1104 | - let (_dir, store, tahinli, _bob) = fixture().await; | ||
| 1105 | - assert!(store.list_orgs().await.unwrap().is_empty()); | ||
| 1106 | - let acme = store.create_org("acme", &tahinli.id).await.unwrap(); | ||
| 1107 | - let names: Vec<String> = store | ||
| 1108 | - .list_orgs() | ||
| 1109 | - .await | ||
| 1110 | - .unwrap() | ||
| 1111 | - .into_iter() | ||
| 1112 | - .map(|org| org.username) | ||
| 1113 | - .collect(); | ||
| 1114 | - assert_eq!(names, vec![acme.username.clone()]); | ||
| 1115 | assert_eq!( | 1123 | assert_eq!( |
| 1116 | OrgMemberRole::parse(OrgMemberRole::Owner.as_str()).unwrap(), | 1124 | OrgMemberRole::parse(OrgMemberRole::Owner.as_str()).unwrap(), |
| 1117 | OrgMemberRole::Owner | 1125 | OrgMemberRole::Owner |
crates/forge-core/src/store/passkeys.rsmodified+35 -37
| @@ -206,20 +206,18 @@ impl Store { | |||
| 206 | mod tests { | 206 | mod tests { |
| 207 | use super::*; | 207 | use super::*; |
| 208 | 208 | ||
| 209 | - async fn store_with_users() -> (Store, String, String, tempfile::TempDir) { | 209 | + async fn user(store: &Store, name: &str) -> String { |
| 210 | - let dir = tempfile::tempdir().unwrap(); | 210 | + store.create_user(name, None, "x", false).await.unwrap().id |
| 211 | - let store = Store::open(&dir.path().join("forge.db")).await.unwrap(); | ||
| 212 | - let a = store | ||
| 213 | - .create_user("tahinli", None, "x", false) | ||
| 214 | - .await | ||
| 215 | - .unwrap(); | ||
| 216 | - let b = store.create_user("other", None, "x", false).await.unwrap(); | ||
| 217 | - (store, a.id, b.id, dir) | ||
| 218 | } | 211 | } |
| 219 | 212 | ||
| 213 | + /// Scenarios share one store; each uses its own users and credential ids. | ||
| 220 | #[tokio::test] | 214 | #[tokio::test] |
| 221 | - async fn add_list_touch_rename_delete() { | 215 | + async fn passkey_lifecycle_ownership_and_limits() { |
| 222 | - let (store, a, _b, _dir) = store_with_users().await; | 216 | + let dir = tempfile::tempdir().unwrap(); |
| 217 | + let store = Store::open(&dir.path().join("forge.db")).await.unwrap(); | ||
| 218 | + | ||
| 219 | + // add, list, touch, rename, delete | ||
| 220 | + let a = user(&store, "tahinli").await; | ||
| 223 | let row = store | 221 | let row = store |
| 224 | .add_passkey(&a, "cred-1", "{\"n\":1}", " Laptop ") | 222 | .add_passkey(&a, "cred-1", "{\"n\":1}", " Laptop ") |
| 225 | .await | 223 | .await |
| @@ -244,50 +242,50 @@ mod tests { | |||
| 244 | assert!(store.rename_passkey(&a, &row.id, " ").await.is_err()); | 242 | assert!(store.rename_passkey(&a, &row.id, " ").await.is_err()); |
| 245 | assert!(store.delete_passkey(&a, &row.id).await.unwrap()); | 243 | assert!(store.delete_passkey(&a, &row.id).await.unwrap()); |
| 246 | assert_eq!(store.passkey_count(&a).await.unwrap(), 0); | 244 | assert_eq!(store.passkey_count(&a).await.unwrap(), 0); |
| 247 | - } | ||
| 248 | 245 | ||
| 249 | - #[tokio::test] | 246 | + // other accounts cannot rename or delete |
| 250 | - async fn other_accounts_cannot_rename_or_delete() { | 247 | + let c = user(&store, "carol").await; |
| 251 | - let (store, a, b, _dir) = store_with_users().await; | 248 | + let d = user(&store, "dave").await; |
| 252 | let row = store | 249 | let row = store |
| 253 | - .add_passkey(&a, "cred-1", "{}", "Laptop") | 250 | + .add_passkey(&c, "cred-2", "{}", "Laptop") |
| 254 | .await | 251 | .await |
| 255 | .unwrap(); | 252 | .unwrap(); |
| 256 | - assert!(!store.rename_passkey(&b, &row.id, "Mine").await.unwrap()); | 253 | + assert!(!store.rename_passkey(&d, &row.id, "Mine").await.unwrap()); |
| 257 | - assert!(!store.delete_passkey(&b, &row.id).await.unwrap()); | 254 | + assert!(!store.delete_passkey(&d, &row.id).await.unwrap()); |
| 258 | - assert_eq!(store.list_passkeys(&a).await.unwrap()[0].name, "Laptop"); | 255 | + assert_eq!(store.list_passkeys(&c).await.unwrap()[0].name, "Laptop"); |
| 259 | - } | ||
| 260 | 256 | ||
| 261 | - #[tokio::test] | 257 | + // duplicate credential and the per-account limit are refused |
| 262 | - async fn duplicate_credential_and_limit_are_refused() { | 258 | + let f = user(&store, "frank").await; |
| 263 | - let (store, a, b, _dir) = store_with_users().await; | 259 | + let g = user(&store, "gus").await; |
| 264 | - store.add_passkey(&a, "cred-1", "{}", "One").await.unwrap(); | 260 | + store.add_passkey(&f, "cred-3", "{}", "One").await.unwrap(); |
| 265 | assert!(matches!( | 261 | assert!(matches!( |
| 266 | - store.add_passkey(&b, "cred-1", "{}", "Two").await, | 262 | + store.add_passkey(&g, "cred-3", "{}", "Two").await, |
| 267 | Err(Error::Conflict(_)) | 263 | Err(Error::Conflict(_)) |
| 268 | )); | 264 | )); |
| 269 | for i in 1..MAX_PASSKEYS { | 265 | for i in 1..MAX_PASSKEYS { |
| 270 | store | 266 | store |
| 271 | - .add_passkey(&a, &format!("c{i}"), "{}", "More") | 267 | + .add_passkey(&f, &format!("c{i}"), "{}", "More") |
| 272 | .await | 268 | .await |
| 273 | .unwrap(); | 269 | .unwrap(); |
| 274 | } | 270 | } |
| 275 | assert!( | 271 | assert!( |
| 276 | store | 272 | store |
| 277 | - .add_passkey(&a, "overflow", "{}", "More") | 273 | + .add_passkey(&f, "overflow", "{}", "More") |
| 278 | .await | 274 | .await |
| 279 | .is_err() | 275 | .is_err() |
| 280 | ); | 276 | ); |
| 281 | - } | ||
| 282 | 277 | ||
| 283 | - #[tokio::test] | 278 | + // delete all removes only that user's passkeys |
| 284 | - async fn delete_all_removes_only_that_users_passkeys() { | 279 | + let h = user(&store, "hana").await; |
| 285 | - let (store, a, b, _dir) = store_with_users().await; | 280 | + let i = user(&store, "ivan").await; |
| 286 | - store.add_passkey(&a, "c1", "{}", "One").await.unwrap(); | 281 | + store.add_passkey(&h, "cred-4", "{}", "One").await.unwrap(); |
| 287 | - store.add_passkey(&a, "c2", "{}", "Two").await.unwrap(); | 282 | + store.add_passkey(&h, "cred-5", "{}", "Two").await.unwrap(); |
| 288 | - store.add_passkey(&b, "c3", "{}", "Three").await.unwrap(); | 283 | + store |
| 289 | - assert_eq!(store.delete_all_passkeys(&a).await.unwrap(), 2); | 284 | + .add_passkey(&i, "cred-6", "{}", "Three") |
| 290 | - assert_eq!(store.passkey_count(&a).await.unwrap(), 0); | 285 | + .await |
| 291 | - assert_eq!(store.passkey_count(&b).await.unwrap(), 1); | 286 | + .unwrap(); |
| 287 | + assert_eq!(store.delete_all_passkeys(&h).await.unwrap(), 2); | ||
| 288 | + assert_eq!(store.passkey_count(&h).await.unwrap(), 0); | ||
| 289 | + assert_eq!(store.passkey_count(&i).await.unwrap(), 1); | ||
| 292 | } | 290 | } |
| 293 | } | 291 | } |
crates/forge-core/src/store/webhooks.rsmodified+73 -69
| @@ -652,9 +652,15 @@ mod tests { | |||
| 652 | col_int(&row, 0).unwrap() | 652 | col_int(&row, 0).unwrap() |
| 653 | } | 653 | } |
| 654 | 654 | ||
| 655 | + /// Hook CRUD, the sealed secret and event filters. Each scenario has its own | ||
| 656 | + /// repo, since listings and filters are per repo. | ||
| 655 | #[tokio::test] | 657 | #[tokio::test] |
| 656 | - async fn crud_round_trip() { | 658 | + async fn hook_crud_secret_and_event_filters() { |
| 657 | - let (store, tahinli, repo, _dir) = setup().await; | 659 | + let (store, tahinli, _repo, _dir) = setup().await; |
| 660 | + let repo = store | ||
| 661 | + .create_repo(&tahinli.id, "crud", "", Visibility::Public, "main") | ||
| 662 | + .await | ||
| 663 | + .unwrap(); | ||
| 658 | let created = store | 664 | let created = store |
| 659 | .create_webhook( | 665 | .create_webhook( |
| 660 | &repo.id, | 666 | &repo.id, |
| @@ -706,70 +712,74 @@ mod tests { | |||
| 706 | .await, | 712 | .await, |
| 707 | Err(Error::NotFound) | 713 | Err(Error::NotFound) |
| 708 | )); | 714 | )); |
| 709 | - } | ||
| 710 | 715 | ||
| 711 | - #[tokio::test] | 716 | + // the sealed secret |
| 712 | - async fn secret_round_trip_and_blank_keeps() { | 717 | + let repo = store |
| 713 | - let (store, tahinli, repo, _dir) = setup().await; | 718 | + .create_repo(&tahinli.id, "secret", "", Visibility::Public, "main") |
| 714 | - let hook = hook(&store, &tahinli, &repo, &[WebhookEvent::Push]).await; | 719 | + .await |
| 715 | - assert_eq!( | 720 | + .unwrap(); |
| 716 | - store.webhook_secret(&hook.id, &KEY).await.unwrap(), | 721 | + { |
| 717 | - "s3cr3t" | 722 | + let hook = hook(&store, &tahinli, &repo, &[WebhookEvent::Push]).await; |
| 718 | - ); | 723 | + assert_eq!( |
| 724 | + store.webhook_secret(&hook.id, &KEY).await.unwrap(), | ||
| 725 | + "s3cr3t" | ||
| 726 | + ); | ||
| 727 | + | ||
| 728 | + // The stored bytes are the sealed form, never the plaintext. | ||
| 729 | + let raw = { | ||
| 730 | + let conn = store.inner.conn.lock().await; | ||
| 731 | + let mut rows = conn | ||
| 732 | + .query( | ||
| 733 | + "SELECT secret_sealed FROM webhooks WHERE id = ?1", | ||
| 734 | + (hook.id.clone(),), | ||
| 735 | + ) | ||
| 736 | + .await | ||
| 737 | + .unwrap(); | ||
| 738 | + col_blob(&rows.next().await.unwrap().unwrap(), 0).unwrap() | ||
| 739 | + }; | ||
| 740 | + assert!(raw.starts_with(b"v1:")); | ||
| 741 | + assert!(!String::from_utf8(raw).unwrap().contains("s3cr3t")); | ||
| 719 | 742 | ||
| 720 | - // The stored bytes are the sealed form, never the plaintext. | 743 | + assert!(matches!( |
| 721 | - let raw = { | 744 | + store.webhook_secret(&hook.id, &[1u8; 32]).await, |
| 722 | - let conn = store.inner.conn.lock().await; | 745 | + Err(Error::Crypto(_)) |
| 723 | - let mut rows = conn | 746 | + )); |
| 724 | - .query( | 747 | + |
| 725 | - "SELECT secret_sealed FROM webhooks WHERE id = ?1", | 748 | + store |
| 726 | - (hook.id.clone(),), | 749 | + .update_webhook(&hook.id, &hook.url, None, &hook.events, true) |
| 727 | - ) | ||
| 728 | .await | 750 | .await |
| 729 | .unwrap(); | 751 | .unwrap(); |
| 730 | - col_blob(&rows.next().await.unwrap().unwrap(), 0).unwrap() | 752 | + assert_eq!( |
| 731 | - }; | 753 | + store.webhook_secret(&hook.id, &KEY).await.unwrap(), |
| 732 | - assert!(raw.starts_with(b"v1:")); | 754 | + "s3cr3t" |
| 733 | - assert!(!String::from_utf8(raw).unwrap().contains("s3cr3t")); | 755 | + ); |
| 734 | 756 | ||
| 735 | - assert!(matches!( | 757 | + store |
| 736 | - store.webhook_secret(&hook.id, &[1u8; 32]).await, | 758 | + .update_webhook( |
| 737 | - Err(Error::Crypto(_)) | 759 | + &hook.id, |
| 738 | - )); | 760 | + &hook.url, |
| 739 | - | 761 | + Some(&sealed("rotated")), |
| 740 | - store | 762 | + &hook.events, |
| 741 | - .update_webhook(&hook.id, &hook.url, None, &hook.events, true) | 763 | + true, |
| 742 | - .await | 764 | + ) |
| 743 | - .unwrap(); | 765 | + .await |
| 744 | - assert_eq!( | 766 | + .unwrap(); |
| 745 | - store.webhook_secret(&hook.id, &KEY).await.unwrap(), | 767 | + assert_eq!( |
| 746 | - "s3cr3t" | 768 | + store.webhook_secret(&hook.id, &KEY).await.unwrap(), |
| 747 | - ); | 769 | + "rotated" |
| 770 | + ); | ||
| 771 | + | ||
| 772 | + assert!(matches!( | ||
| 773 | + store.webhook_secret("missing", &KEY).await, | ||
| 774 | + Err(Error::NotFound) | ||
| 775 | + )); | ||
| 776 | + } | ||
| 748 | 777 | ||
| 749 | - store | 778 | + // event filters |
| 750 | - .update_webhook( | 779 | + let repo = store |
| 751 | - &hook.id, | 780 | + .create_repo(&tahinli.id, "events", "", Visibility::Public, "main") |
| 752 | - &hook.url, | ||
| 753 | - Some(&sealed("rotated")), | ||
| 754 | - &hook.events, | ||
| 755 | - true, | ||
| 756 | - ) | ||
| 757 | .await | 781 | .await |
| 758 | .unwrap(); | 782 | .unwrap(); |
| 759 | - assert_eq!( | ||
| 760 | - store.webhook_secret(&hook.id, &KEY).await.unwrap(), | ||
| 761 | - "rotated" | ||
| 762 | - ); | ||
| 763 | - | ||
| 764 | - assert!(matches!( | ||
| 765 | - store.webhook_secret("missing", &KEY).await, | ||
| 766 | - Err(Error::NotFound) | ||
| 767 | - )); | ||
| 768 | - } | ||
| 769 | - | ||
| 770 | - #[tokio::test] | ||
| 771 | - async fn hooks_for_event_filters_by_event_and_active() { | ||
| 772 | - let (store, tahinli, repo, _dir) = setup().await; | ||
| 773 | let a = hook( | 783 | let a = hook( |
| 774 | &store, | 784 | &store, |
| 775 | &tahinli, | 785 | &tahinli, |
| @@ -1002,12 +1012,8 @@ mod tests { | |||
| 1002 | .await, | 1012 | .await, |
| 1003 | Err(Error::NotFound) | 1013 | Err(Error::NotFound) |
| 1004 | )); | 1014 | )); |
| 1005 | - } | ||
| 1006 | 1015 | ||
| 1007 | - #[tokio::test] | 1016 | + // a response body is cut to 64 KiB on a character boundary |
| 1008 | - async fn response_body_is_truncated_to_64_kib() { | ||
| 1009 | - let (store, tahinli, repo, _dir) = setup().await; | ||
| 1010 | - let hook = hook(&store, &tahinli, &repo, &[WebhookEvent::Push]).await; | ||
| 1011 | let d = store | 1017 | let d = store |
| 1012 | .enqueue_delivery(&hook.id, WebhookEvent::Push, "", "{}", "{}") | 1018 | .enqueue_delivery(&hook.id, WebhookEvent::Push, "", "{}", "{}") |
| 1013 | .await | 1019 | .await |
| @@ -1036,8 +1042,9 @@ mod tests { | |||
| 1036 | assert!(stored.len() > MAX_RESPONSE_BODY_BYTES - 4); | 1042 | assert!(stored.len() > MAX_RESPONSE_BODY_BYTES - 4); |
| 1037 | } | 1043 | } |
| 1038 | 1044 | ||
| 1045 | + /// Redelivery copies the request with a new GUID and drops the worker's headers. | ||
| 1039 | #[tokio::test] | 1046 | #[tokio::test] |
| 1040 | - async fn redeliver_copies_request_with_new_guid() { | 1047 | + async fn redelivery_copies_and_drops_worker_headers() { |
| 1041 | let (store, tahinli, repo, _dir) = setup().await; | 1048 | let (store, tahinli, repo, _dir) = setup().await; |
| 1042 | let hook = hook(&store, &tahinli, &repo, &[WebhookEvent::PullRequest]).await; | 1049 | let hook = hook(&store, &tahinli, &repo, &[WebhookEvent::PullRequest]).await; |
| 1043 | let headers = |guid: &str| { | 1050 | let headers = |guid: &str| { |
| @@ -1097,12 +1104,9 @@ mod tests { | |||
| 1097 | store.redeliver("missing").await, | 1104 | store.redeliver("missing").await, |
| 1098 | Err(Error::NotFound) | 1105 | Err(Error::NotFound) |
| 1099 | )); | 1106 | )); |
| 1100 | - } | ||
| 1101 | 1107 | ||
| 1102 | - #[tokio::test] | 1108 | + // headers the worker sent replace the stored ones, and redelivery drops them |
| 1103 | - async fn sent_headers_replace_stored_headers_and_redeliver_drops_worker_headers() { | 1109 | + let hook = hook_at(&store, &tahinli, &repo, "https://sent.example").await; |
| 1104 | - let (store, tahinli, repo, _dir) = setup().await; | ||
| 1105 | - let hook = hook(&store, &tahinli, &repo, &[WebhookEvent::Push]).await; | ||
| 1106 | let queued = store | 1110 | let queued = store |
| 1107 | .enqueue_delivery( | 1111 | .enqueue_delivery( |
| 1108 | &hook.id, | 1112 | &hook.id, |