forge
Explore
Log in

Don't you dare go Hollow

Store tests share their store and merge into tables

Browse files at this commit

7824c81b27f2e5cb290cc91bf166f0330632a9e8Ahmet Kaan GümüşParent: 535510d
  • test
    • store tests share their store and merge into tables
Show full message
✅ test: store tests share their store and merge into tables

Scenarios that build the same fixture now run one after another in a
single test, so each store is opened once. Each scenario has its own
repo or user, and asserts keep their context.

Counts before -> after:
- store/mod.rs: 21 -> 11
- store/user_delete.rs: 7 -> 4
- store/transfers.rs: 7 -> 1
- store/suspension.rs: 3 -> 1
- store/snapshot.rs: 3 -> 2
- total: 41 -> 19

Pure tests (email shape, display name validation) and the
empty-store snapshot test are unchanged.
crates/forge-core/src/store/mod.rsmodified+233 -261
@@ -1833,20 +1833,18 @@ mod tests {
1833 }1833 }
1834 1834
1835 #[tokio::test]1835 #[tokio::test]
1836- async fn new_user_has_default_profile() {1836+ async fn profile_defaults_round_trip_and_validate() {
1837 let dir = tempfile::tempdir().unwrap();1837 let dir = tempfile::tempdir().unwrap();
1838 let (store, user) = store_with_user(&dir).await;1838 let (store, user) = store_with_user(&dir).await;
1839+
1840+ // Scenario: a new user has an empty bio and UTC.
1839 assert_eq!(user.about, "");1841 assert_eq!(user.about, "");
1840 assert_eq!(user.timezone, "UTC");1842 assert_eq!(user.timezone, "UTC");
1841 let fetched = store.user_by_id(&user.id).await.unwrap().unwrap();1843 let fetched = store.user_by_id(&user.id).await.unwrap().unwrap();
1842 assert_eq!(fetched.about, "");1844 assert_eq!(fetched.about, "");
1843 assert_eq!(fetched.timezone, "UTC");1845 assert_eq!(fetched.timezone, "UTC");
1844- }
1845 1846
1846- #[tokio::test]1847+ // Scenario: a valid profile is saved; invalid input is refused and changes nothing.
1847- async fn set_profile_round_trips_and_validates() {
1848- let dir = tempfile::tempdir().unwrap();
1849- let (store, user) = store_with_user(&dir).await;
1850 store1848 store
1851 .set_profile(&user.id, "Hello *world*", "Europe/Istanbul")1849 .set_profile(&user.id, "Hello *world*", "Europe/Istanbul")
1852 .await1850 .await
@@ -1934,12 +1932,13 @@ mod tests {
1934 }1932 }
1935 1933
1936 #[tokio::test]1934 #[tokio::test]
1937- async fn avatar_upsert_hash_delete() {1935+ async fn avatar_upsert_hash_delete_and_removed_with_its_user() {
1938 let dir = tempfile::tempdir().unwrap();1936 let dir = tempfile::tempdir().unwrap();
1939 let (store, user) = store_with_user(&dir).await;1937 let (store, user) = store_with_user(&dir).await;
1938+
1939+ // Scenario: upsert replaces the image and its hash; delete is idempotent.
1940 assert_eq!(store.avatar(&user.id).await.unwrap(), None);1940 assert_eq!(store.avatar(&user.id).await.unwrap(), None);
1941 assert_eq!(store.avatar_hash(&user.id).await.unwrap(), None);1941 assert_eq!(store.avatar_hash(&user.id).await.unwrap(), None);
1942-
1943 store.set_avatar(&user.id, b"png-1", "aaaa").await.unwrap();1942 store.set_avatar(&user.id, b"png-1", "aaaa").await.unwrap();
1944 store.set_avatar(&user.id, b"png-2", "bbbb").await.unwrap();1943 store.set_avatar(&user.id, b"png-2", "bbbb").await.unwrap();
1945 assert_eq!(1944 assert_eq!(
@@ -1950,31 +1949,33 @@ mod tests {
1950 store.avatar_hash(&user.id).await.unwrap().as_deref(),1949 store.avatar_hash(&user.id).await.unwrap().as_deref(),
1951 Some("bbbb")1950 Some("bbbb")
1952 );1951 );
1953-
1954 store.delete_avatar(&user.id).await.unwrap();1952 store.delete_avatar(&user.id).await.unwrap();
1955 store.delete_avatar(&user.id).await.unwrap();1953 store.delete_avatar(&user.id).await.unwrap();
1956 assert_eq!(store.avatar(&user.id).await.unwrap(), None);1954 assert_eq!(store.avatar(&user.id).await.unwrap(), None);
1957- }
1958 1955
1959- #[tokio::test]1956+ // Scenario: deleting the user row removes its avatar with it.
1960- async fn avatar_is_removed_with_its_user() {1957+ let doomed = store
1961- let dir = tempfile::tempdir().unwrap();1958+ .create_user("Doomed", None, "hash", false)
1962- let (store, user) = store_with_user(&dir).await;1959+ .await
1963- store.set_avatar(&user.id, b"png", "cccc").await.unwrap();1960+ .unwrap();
1961+ store.set_avatar(&doomed.id, b"png", "cccc").await.unwrap();
1964 {1962 {
1965 let conn = store.inner.conn.lock().await;1963 let conn = store.inner.conn.lock().await;
1966- conn.execute("DELETE FROM users WHERE id = ?1", (user.id.clone(),))1964+ conn.execute("DELETE FROM users WHERE id = ?1", (doomed.id.clone(),))
1967 .await1965 .await
1968 .unwrap();1966 .unwrap();
1969 }1967 }
1970- assert_eq!(store.avatar(&user.id).await.unwrap(), None);1968+ assert_eq!(store.avatar(&doomed.id).await.unwrap(), None);
1971 }1969 }
1972 1970
1973 #[tokio::test]1971 #[tokio::test]
1974- async fn delete_repo_removes_repo_and_children_atomically() {1972+ async fn delete_repo_scenarios() {
1975 let dir = tempfile::tempdir().unwrap();1973 let dir = tempfile::tempdir().unwrap();
1976 let (store, tahinli) = store_with_user(&dir).await;1974 let (store, tahinli) = store_with_user(&dir).await;
1977 let bob = store.create_user("Bob", None, "hash", false).await.unwrap();1975 let bob = store.create_user("Bob", None, "hash", false).await.unwrap();
1976+
1977+ // Scenario: deleting a repo removes its collaborators, push events and branch
1978+ // rules atomically, and leaves the other repo and its rows untouched.
1978 let keep = store1979 let keep = store
1979 .create_repo(&tahinli.id, "keep", "", Visibility::Public, "main")1980 .create_repo(&tahinli.id, "keep", "", Visibility::Public, "main")
1980 .await1981 .await
@@ -1987,14 +1988,16 @@ mod tests {
1987 .set_collaborator(&gone.id, &bob.id, Role::Write)1988 .set_collaborator(&gone.id, &bob.id, Role::Write)
1988 .await1989 .await
1989 .unwrap();1990 .unwrap();
1990- store1991+ for repo in [&gone, &keep] {
1991- .record_push_event(&gone.id, &tahinli.id, "refs/heads/main", "0", "1", "https")1992+ store
1992- .await1993+ .record_push_event(&repo.id, &tahinli.id, "refs/heads/main", "0", "1", "https")
1993- .unwrap();1994+ .await
1994- store1995+ .unwrap();
1995- .record_push_event(&keep.id, &tahinli.id, "refs/heads/main", "0", "1", "https")1996+ store
1996- .await1997+ .upsert_branch_rule(&repo.id, "main", RuleFlags::basic(false, false))
1997- .unwrap();1998+ .await
1999+ .unwrap();
2000+ }
1998 2001
1999 store.delete_repo(&gone.id).await.unwrap();2002 store.delete_repo(&gone.id).await.unwrap();
2000 2003
@@ -2008,14 +2011,18 @@ mod tests {
2008 for (sql, expect) in [2011 for (sql, expect) in [
2009 ("SELECT COUNT(*) FROM push_events WHERE repo_id = ?1", 0i64),2012 ("SELECT COUNT(*) FROM push_events WHERE repo_id = ?1", 0i64),
2010 ("SELECT COUNT(*) FROM collaborators WHERE repo_id = ?1", 0),2013 ("SELECT COUNT(*) FROM collaborators WHERE repo_id = ?1", 0),
2014+ (
2015+ "SELECT COUNT(*) FROM branch_protections WHERE repo_id = ?1",
2016+ 0,
2017+ ),
2011 ] {2018 ] {
2012 let mut rows = conn.query(sql, (gone.id.clone(),)).await.unwrap();2019 let mut rows = conn.query(sql, (gone.id.clone(),)).await.unwrap();
2013 let row = rows.next().await.unwrap().unwrap();2020 let row = rows.next().await.unwrap().unwrap();
2014 assert_eq!(col_int(&row, 0).unwrap(), expect, "{sql}");2021 assert_eq!(col_int(&row, 0).unwrap(), expect, "{sql}");
2015 }2022 }
2016 }2023 }
2017- // The other repo and its events are untouched.
2018 assert!(store.repo_by_id(&keep.id).await.unwrap().is_some());2024 assert!(store.repo_by_id(&keep.id).await.unwrap().is_some());
2025+ assert_eq!(store.list_branch_rules(&keep.id).await.unwrap().len(), 1);
2019 {2026 {
2020 let conn = store.inner.conn.lock().await;2027 let conn = store.inner.conn.lock().await;
2021 let mut rows = conn2028 let mut rows = conn
@@ -2028,70 +2035,175 @@ mod tests {
2028 let row = rows.next().await.unwrap().unwrap();2035 let row = rows.next().await.unwrap().unwrap();
2029 assert_eq!(col_int(&row, 0).unwrap(), 1);2036 assert_eq!(col_int(&row, 0).unwrap(), 1);
2030 }2037 }
2031- }
2032 2038
2033- #[tokio::test]2039+ // Scenario: a missing id is NotFound; deleting twice reports NotFound the second time.
2034- async fn delete_repo_missing_is_not_found_and_changes_nothing() {2040+ let later = store
2035- let dir = tempfile::tempdir().unwrap();2041+ .create_repo(&tahinli.id, "later", "", Visibility::Public, "main")
2036- let (store, tahinli) = store_with_user(&dir).await;
2037- let keep = store
2038- .create_repo(&tahinli.id, "keep", "", Visibility::Public, "main")
2039 .await2042 .await
2040 .unwrap();2043 .unwrap();
2041 assert!(matches!(2044 assert!(matches!(
2042 store.delete_repo("01m4yv0we1e0yew2ras7jnb9sk").await,2045 store.delete_repo("01m4yv0we1e0yew2ras7jnb9sk").await,
2043 Err(Error::NotFound)2046 Err(Error::NotFound)
2044 ));2047 ));
2045- assert!(store.repo_by_id(&keep.id).await.unwrap().is_some());2048+ assert!(store.repo_by_id(&later.id).await.unwrap().is_some());
2046- // Deleting twice: the second call reports NotFound.2049+ store.delete_repo(&later.id).await.unwrap();
2047- store.delete_repo(&keep.id).await.unwrap();
2048 assert!(matches!(2050 assert!(matches!(
2049- store.delete_repo(&keep.id).await,2051+ store.delete_repo(&later.id).await,
2050 Err(Error::NotFound)2052 Err(Error::NotFound)
2051 ));2053 ));
2052 }2054 }
2053 2055
2054 #[tokio::test]2056 #[tokio::test]
2055- async fn branch_rule_required_checks_round_trip_and_normalise() {2057+ async fn branch_rule_scenarios() {
2056 let dir = tempfile::tempdir().unwrap();2058 let dir = tempfile::tempdir().unwrap();
2057 let (store, owner) = store_with_user(&dir).await;2059 let (store, owner) = store_with_user(&dir).await;
2058- let repo = store
2059- .create_repo(&owner.id, "proj", "", Visibility::Public, "main")
2060- .await
2061- .unwrap();
2062- let flags = RuleFlags {
2063- required_checks: vec![
2064- " ci/test ".into(),
2065- "lint".into(),
2066- "ci/test".into(),
2067- " ".into(),
2068- ],
2069- ..RuleFlags::default()
2070- };
2071- let rule = store
2072- .upsert_branch_rule(&repo.id, "main", flags)
2073- .await
2074- .unwrap();
2075- assert_eq!(rule.required_checks, vec!["ci/test", "lint"]);
2076- assert_eq!(
2077- store.list_branch_rules(&repo.id).await.unwrap()[0].required_checks,
2078- vec!["ci/test", "lint"]
2079- );
2080 2060
2081- // Saving without checks clears them; a control character is refused.2061+ // Scenario: required checks are trimmed, de-duplicated and blanks dropped;
2082- let cleared = store2062+ // saving without checks clears them; a control character is refused.
2083- .upsert_branch_rule(&repo.id, "main", RuleFlags::default())2063+ {
2084- .await2064+ let repo = store
2085- .unwrap();2065+ .create_repo(&owner.id, "checks", "", Visibility::Public, "main")
2086- assert!(cleared.required_checks.is_empty());2066+ .await
2087- let bad = RuleFlags {2067+ .unwrap();
2088- required_checks: vec!["a\nb".into()],2068+ let flags = RuleFlags {
2089- ..RuleFlags::default()2069+ required_checks: vec![
2090- };2070+ " ci/test ".into(),
2091- assert!(matches!(2071+ "lint".into(),
2092- store.upsert_branch_rule(&repo.id, "main", bad).await,2072+ "ci/test".into(),
2093- Err(Error::Invalid(_))2073+ " ".into(),
2094- ));2074+ ],
2075+ ..RuleFlags::default()
2076+ };
2077+ let rule = store
2078+ .upsert_branch_rule(&repo.id, "main", flags)
2079+ .await
2080+ .unwrap();
2081+ assert_eq!(rule.required_checks, vec!["ci/test", "lint"]);
2082+ assert_eq!(
2083+ store.list_branch_rules(&repo.id).await.unwrap()[0].required_checks,
2084+ vec!["ci/test", "lint"]
2085+ );
2086+ let cleared = store
2087+ .upsert_branch_rule(&repo.id, "main", RuleFlags::default())
2088+ .await
2089+ .unwrap();
2090+ assert!(cleared.required_checks.is_empty());
2091+ let bad = RuleFlags {
2092+ required_checks: vec!["a\nb".into()],
2093+ ..RuleFlags::default()
2094+ };
2095+ assert!(matches!(
2096+ store.upsert_branch_rule(&repo.id, "main", bad).await,
2097+ Err(Error::Invalid(_))
2098+ ));
2099+ }
2100+
2101+ // Scenario: listing, upserting the same pattern updates in place, bad patterns refused.
2102+ {
2103+ let repo = store
2104+ .create_repo(&owner.id, "list", "", Visibility::Public, "main")
2105+ .await
2106+ .unwrap();
2107+ assert!(store.list_branch_rules(&repo.id).await.unwrap().is_empty());
2108+
2109+ let main = store
2110+ .upsert_branch_rule(&repo.id, "main", RuleFlags::basic(false, false))
2111+ .await
2112+ .unwrap();
2113+ store
2114+ .upsert_branch_rule(&repo.id, "release/*", RuleFlags::basic(true, false))
2115+ .await
2116+ .unwrap();
2117+ let rules = store.list_branch_rules(&repo.id).await.unwrap();
2118+ let patterns: Vec<&str> = rules.iter().map(|r| r.pattern.as_str()).collect();
2119+ assert_eq!(patterns, ["main", "release/*"]);
2120+
2121+ // Same pattern updates the flags in place and keeps the id and created_at.
2122+ let updated = store
2123+ .upsert_branch_rule(&repo.id, "main", RuleFlags::basic(true, true))
2124+ .await
2125+ .unwrap();
2126+ assert_eq!(updated.id, main.id);
2127+ assert_eq!(updated.created_at, main.created_at);
2128+ assert!(updated.allow_force_push && updated.allow_deletion);
2129+ let rules = store.list_branch_rules(&repo.id).await.unwrap();
2130+ assert_eq!(rules.len(), 2);
2131+ assert_eq!(rules[0], updated);
2132+
2133+ assert!(matches!(
2134+ store
2135+ .upsert_branch_rule(&repo.id, "refs/heads/x", RuleFlags::basic(false, false))
2136+ .await,
2137+ Err(Error::Invalid(_))
2138+ ));
2139+ }
2140+
2141+ // Scenario: merge and push fields round-trip; an out-of-range value is refused.
2142+ {
2143+ let repo = store
2144+ .create_repo(&owner.id, "fields", "", Visibility::Public, "main")
2145+ .await
2146+ .unwrap();
2147+ let flags = RuleFlags {
2148+ allow_force_push: false,
2149+ allow_deletion: false,
2150+ required_approvals: 20,
2151+ block_on_changes_requested: true,
2152+ require_up_to_date: true,
2153+ restrict_push: true,
2154+ require_merge_queue: true,
2155+ required_checks: Vec::new(),
2156+ };
2157+ let saved = store
2158+ .upsert_branch_rule(&repo.id, "main", flags.clone())
2159+ .await
2160+ .unwrap();
2161+ assert_eq!(saved.required_approvals, 20);
2162+ assert!(
2163+ saved.block_on_changes_requested && saved.require_up_to_date && saved.restrict_push
2164+ );
2165+ assert_eq!(
2166+ store.list_branch_rules(&repo.id).await.unwrap(),
2167+ vec![saved]
2168+ );
2169+
2170+ // Out of range is refused and stores nothing new.
2171+ let too_many = RuleFlags {
2172+ required_approvals: 21,
2173+ ..flags
2174+ };
2175+ assert!(matches!(
2176+ store.upsert_branch_rule(&repo.id, "main", too_many).await,
2177+ Err(Error::Invalid(_))
2178+ ));
2179+ let rules = store.list_branch_rules(&repo.id).await.unwrap();
2180+ assert_eq!(rules[0].required_approvals, 20);
2181+ }
2182+
2183+ // Scenario: deleting a rule is scoped to its own repo.
2184+ {
2185+ let a = store
2186+ .create_repo(&owner.id, "scope-a", "", Visibility::Public, "main")
2187+ .await
2188+ .unwrap();
2189+ let b = store
2190+ .create_repo(&owner.id, "scope-b", "", Visibility::Public, "main")
2191+ .await
2192+ .unwrap();
2193+ let rule_b = store
2194+ .upsert_branch_rule(&b.id, "main", RuleFlags::basic(false, false))
2195+ .await
2196+ .unwrap();
2197+
2198+ assert!(matches!(
2199+ store.delete_branch_rule(&a.id, &rule_b.id).await,
2200+ Err(Error::NotFound)
2201+ ));
2202+ assert_eq!(store.list_branch_rules(&b.id).await.unwrap().len(), 1);
2203+
2204+ store.delete_branch_rule(&b.id, &rule_b.id).await.unwrap();
2205+ assert!(store.list_branch_rules(&b.id).await.unwrap().is_empty());
2206+ }
2095 }2207 }
2096 2208
2097 #[tokio::test]2209 #[tokio::test]
@@ -2137,157 +2249,6 @@ mod tests {
2137 .is_empty()2249 .is_empty()
2138 );2250 );
2139 }2251 }
2140-
2141- #[tokio::test]
2142- async fn branch_rules_list_upsert_and_update() {
2143- let dir = tempfile::tempdir().unwrap();
2144- let (store, tahinli) = store_with_user(&dir).await;
2145- let repo = store
2146- .create_repo(&tahinli.id, "proj", "", Visibility::Public, "main")
2147- .await
2148- .unwrap();
2149- assert!(store.list_branch_rules(&repo.id).await.unwrap().is_empty());
2150-
2151- let main = store
2152- .upsert_branch_rule(&repo.id, "main", RuleFlags::basic(false, false))
2153- .await
2154- .unwrap();
2155- store
2156- .upsert_branch_rule(&repo.id, "release/*", RuleFlags::basic(true, false))
2157- .await
2158- .unwrap();
2159- let rules = store.list_branch_rules(&repo.id).await.unwrap();
2160- let patterns: Vec<&str> = rules.iter().map(|r| r.pattern.as_str()).collect();
2161- assert_eq!(patterns, ["main", "release/*"]);
2162-
2163- // Same pattern updates the flags in place and keeps the id and created_at.
2164- let updated = store
2165- .upsert_branch_rule(&repo.id, "main", RuleFlags::basic(true, true))
2166- .await
2167- .unwrap();
2168- assert_eq!(updated.id, main.id);
2169- assert_eq!(updated.created_at, main.created_at);
2170- assert!(updated.allow_force_push && updated.allow_deletion);
2171- let rules = store.list_branch_rules(&repo.id).await.unwrap();
2172- assert_eq!(rules.len(), 2);
2173- assert_eq!(rules[0], updated);
2174-
2175- assert!(matches!(
2176- store
2177- .upsert_branch_rule(&repo.id, "refs/heads/x", RuleFlags::basic(false, false))
2178- .await,
2179- Err(Error::Invalid(_))
2180- ));
2181- }
2182-
2183- #[tokio::test]
2184- async fn branch_rule_merge_and_push_fields_round_trip() {
2185- let dir = tempfile::tempdir().unwrap();
2186- let (store, tahinli) = store_with_user(&dir).await;
2187- let repo = store
2188- .create_repo(&tahinli.id, "proj", "", Visibility::Public, "main")
2189- .await
2190- .unwrap();
2191- let flags = RuleFlags {
2192- allow_force_push: false,
2193- allow_deletion: false,
2194- required_approvals: 20,
2195- block_on_changes_requested: true,
2196- require_up_to_date: true,
2197- restrict_push: true,
2198- require_merge_queue: true,
2199- required_checks: Vec::new(),
2200- };
2201- let saved = store
2202- .upsert_branch_rule(&repo.id, "main", flags.clone())
2203- .await
2204- .unwrap();
2205- assert_eq!(saved.required_approvals, 20);
2206- assert!(
2207- saved.block_on_changes_requested && saved.require_up_to_date && saved.restrict_push
2208- );
2209- assert_eq!(
2210- store.list_branch_rules(&repo.id).await.unwrap(),
2211- vec![saved]
2212- );
2213-
2214- // Out of range is refused and stores nothing new.
2215- let too_many = RuleFlags {
2216- required_approvals: 21,
2217- ..flags
2218- };
2219- assert!(matches!(
2220- store.upsert_branch_rule(&repo.id, "main", too_many).await,
2221- Err(Error::Invalid(_))
2222- ));
2223- let rules = store.list_branch_rules(&repo.id).await.unwrap();
2224- assert_eq!(rules[0].required_approvals, 20);
2225- }
2226-
2227- #[tokio::test]
2228- async fn branch_rule_delete_is_scoped_to_repo() {
2229- let dir = tempfile::tempdir().unwrap();
2230- let (store, tahinli) = store_with_user(&dir).await;
2231- let a = store
2232- .create_repo(&tahinli.id, "a", "", Visibility::Public, "main")
2233- .await
2234- .unwrap();
2235- let b = store
2236- .create_repo(&tahinli.id, "b", "", Visibility::Public, "main")
2237- .await
2238- .unwrap();
2239- let rule_b = store
2240- .upsert_branch_rule(&b.id, "main", RuleFlags::basic(false, false))
2241- .await
2242- .unwrap();
2243-
2244- assert!(matches!(
2245- store.delete_branch_rule(&a.id, &rule_b.id).await,
2246- Err(Error::NotFound)
2247- ));
2248- assert_eq!(store.list_branch_rules(&b.id).await.unwrap().len(), 1);
2249-
2250- store.delete_branch_rule(&b.id, &rule_b.id).await.unwrap();
2251- assert!(store.list_branch_rules(&b.id).await.unwrap().is_empty());
2252- }
2253-
2254- #[tokio::test]
2255- async fn delete_repo_removes_branch_rules() {
2256- let dir = tempfile::tempdir().unwrap();
2257- let (store, tahinli) = store_with_user(&dir).await;
2258- let gone = store
2259- .create_repo(&tahinli.id, "gone", "", Visibility::Public, "main")
2260- .await
2261- .unwrap();
2262- let keep = store
2263- .create_repo(&tahinli.id, "keep", "", Visibility::Public, "main")
2264- .await
2265- .unwrap();
2266- store
2267- .upsert_branch_rule(&gone.id, "main", RuleFlags::basic(false, false))
2268- .await
2269- .unwrap();
2270- store
2271- .upsert_branch_rule(&keep.id, "main", RuleFlags::basic(false, false))
2272- .await
2273- .unwrap();
2274-
2275- store.delete_repo(&gone.id).await.unwrap();
2276-
2277- let conn = store.inner.conn.lock().await;
2278- let mut rows = conn
2279- .query(
2280- "SELECT COUNT(*) FROM branch_protections WHERE repo_id = ?1",
2281- (gone.id.clone(),),
2282- )
2283- .await
2284- .unwrap();
2285- let row = rows.next().await.unwrap().unwrap();
2286- assert_eq!(col_int(&row, 0).unwrap(), 0);
2287- drop(rows);
2288- drop(conn);
2289- assert_eq!(store.list_branch_rules(&keep.id).await.unwrap().len(), 1);
2290- }
2291 }2252 }
2292 2253
2293 // collaborators (settings page)2254 // collaborators (settings page)
@@ -2598,8 +2559,10 @@ mod security_tests {
2598 }2559 }
2599 2560
2600 #[tokio::test]2561 #[tokio::test]
2601- async fn set_email_and_password_hash_persist() {2562+ async fn account_email_password_and_session_scenarios() {
2602 let (store, user, _dir) = store_with_user().await;2563 let (store, user, _dir) = store_with_user().await;
2564+
2565+ // Scenario: email and password hash persist; a bad email and a missing user are refused.
2603 store2566 store
2604 .set_email(&user.id, "tahinli@example.com")2567 .set_email(&user.id, "tahinli@example.com")
2605 .await2568 .await
@@ -2613,11 +2576,8 @@ mod security_tests {
2613 store.set_email("missing", "a@b.c").await,2576 store.set_email("missing", "a@b.c").await,
2614 Err(Error::NotFound)2577 Err(Error::NotFound)
2615 ));2578 ));
2616- }
2617 2579
2618- #[tokio::test]2580+ // Scenario: deleting sessions except one keeps only the named session.
2619- async fn delete_sessions_except_keeps_the_named_one() {
2620- let (store, user, _dir) = store_with_user().await;
2621 let expiry = crate::time::stamp_after(std::time::Duration::from_secs(3600));2581 let expiry = crate::time::stamp_after(std::time::Duration::from_secs(3600));
2622 let keep = store2582 let keep = store
2623 .create_session(&user.id, "h1", "c1", &expiry, None)2583 .create_session(&user.id, "h1", "c1", &expiry, None)
@@ -2644,8 +2604,15 @@ mod security_tests {
2644 }2604 }
2645 2605
2646 #[tokio::test]2606 #[tokio::test]
2647- async fn totp_enrollment_then_replay_and_recovery_rules() {2607+ async fn totp_scenarios() {
2648 let (store, user, _dir) = store_with_user().await;2608 let (store, user, _dir) = store_with_user().await;
2609+
2610+ // Scenario: enabling without a secret is refused and leaves TOTP off.
2611+ let err = store.enable_totp(&user.id, 1, &[]).await;
2612+ assert!(matches!(err, Err(Error::NotFound)));
2613+ assert!(!store.totp_state(&user.id).await.unwrap().unwrap().enabled);
2614+
2615+ // Scenario: enrollment, then replay and recovery-code rules.
2649 let fresh = store.totp_state(&user.id).await.unwrap().unwrap();2616 let fresh = store.totp_state(&user.id).await.unwrap().unwrap();
2650 assert_eq!(fresh.sealed_secret, None);2617 assert_eq!(fresh.sealed_secret, None);
2651 assert!(!fresh.enabled);2618 assert!(!fresh.enabled);
@@ -2699,40 +2666,36 @@ mod security_tests {
2699 assert!(!off.enabled);2666 assert!(!off.enabled);
2700 assert_eq!(off.last_step, 0);2667 assert_eq!(off.last_step, 0);
2701 assert_eq!(store.unused_recovery_codes(&user.id).await.unwrap(), 0);2668 assert_eq!(store.unused_recovery_codes(&user.id).await.unwrap(), 0);
2702- }
2703 2669
2704- #[tokio::test]2670+ // Scenario: device fields (added, last used, name) on a second user.
2705- async fn enable_without_a_secret_is_refused() {2671+ let device = store
2706- let (store, user, _dir) = store_with_user().await;2672+ .create_user("device-owner", None, "h", false)
2707- let err = store.enable_totp(&user.id, 1, &[]).await;2673+ .await
2708- assert!(matches!(err, Err(Error::NotFound)));2674+ .unwrap();
2709- assert!(!store.totp_state(&user.id).await.unwrap().unwrap().enabled);2675+ assert_eq!(store.password_changed_at(&device.id).await.unwrap(), None);
2710- }2676+ store
2711-2677+ .set_totp_secret(&device.id, "v1:sealed")
2712- #[tokio::test]2678+ .await
2713- async fn totp_device_tracks_added_last_used_and_name() {2679+ .unwrap();
2714- let (store, user, _dir) = store_with_user().await;2680+ let pending = store.totp_state(&device.id).await.unwrap().unwrap();
2715- assert_eq!(store.password_changed_at(&user.id).await.unwrap(), None);
2716- store.set_totp_secret(&user.id, "v1:sealed").await.unwrap();
2717- let pending = store.totp_state(&user.id).await.unwrap().unwrap();
2718 assert_eq!((pending.created_at, pending.last_used_at), (None, None));2681 assert_eq!((pending.created_at, pending.last_used_at), (None, None));
2719 2682
2720 store2683 store
2721- .enable_totp(&user.id, 100, &[recovery_hash("aaaa-bbbb")])2684+ .enable_totp(&device.id, 100, &[recovery_hash("aaaa-bbbb")])
2722 .await2685 .await
2723 .unwrap();2686 .unwrap();
2724- let on = store.totp_state(&user.id).await.unwrap().unwrap();2687+ let on = store.totp_state(&device.id).await.unwrap().unwrap();
2725 let added = on.created_at.clone().expect("created_at set on enable");2688 let added = on.created_at.clone().expect("created_at set on enable");
2726 assert_eq!(on.last_used_at.as_deref(), Some(added.as_str()));2689 assert_eq!(on.last_used_at.as_deref(), Some(added.as_str()));
2727 assert_eq!(on.name, None);2690 assert_eq!(on.name, None);
2728 2691
2729 store2692 store
2730- .set_totp_name(&user.id, Some("Work phone"))2693+ .set_totp_name(&device.id, Some("Work phone"))
2731 .await2694 .await
2732 .unwrap();2695 .unwrap();
2733 assert_eq!(2696 assert_eq!(
2734 store2697 store
2735- .totp_state(&user.id)2698+ .totp_state(&device.id)
2736 .await2699 .await
2737 .unwrap()2700 .unwrap()
2738 .unwrap()2701 .unwrap()
@@ -2743,17 +2706,17 @@ mod security_tests {
2743 2706
2744 // Stamps have nanosecond precision and sort as text, so a later use compares greater.2707 // Stamps have nanosecond precision and sort as text, so a later use compares greater.
2745 std::thread::sleep(std::time::Duration::from_millis(5));2708 std::thread::sleep(std::time::Duration::from_millis(5));
2746- assert!(store.accept_totp_step(&user.id, 101).await.unwrap());2709+ assert!(store.accept_totp_step(&device.id, 101).await.unwrap());
2747- let used = store.totp_state(&user.id).await.unwrap().unwrap();2710+ let used = store.totp_state(&device.id).await.unwrap().unwrap();
2748 assert!(used.last_used_at.as_deref().unwrap() > added.as_str());2711 assert!(used.last_used_at.as_deref().unwrap() > added.as_str());
2749 assert_eq!(used.created_at.as_deref(), Some(added.as_str()));2712 assert_eq!(used.created_at.as_deref(), Some(added.as_str()));
2750 2713
2751 // A replayed step is refused and leaves the last use alone.2714 // A replayed step is refused and leaves the last use alone.
2752 std::thread::sleep(std::time::Duration::from_millis(5));2715 std::thread::sleep(std::time::Duration::from_millis(5));
2753- assert!(!store.accept_totp_step(&user.id, 101).await.unwrap());2716+ assert!(!store.accept_totp_step(&device.id, 101).await.unwrap());
2754 assert_eq!(2717 assert_eq!(
2755 store2718 store
2756- .totp_state(&user.id)2719+ .totp_state(&device.id)
2757 .await2720 .await
2758 .unwrap()2721 .unwrap()
2759 .unwrap()2722 .unwrap()
@@ -2761,21 +2724,30 @@ mod security_tests {
2761 used.last_used_at2724 used.last_used_at
2762 );2725 );
2763 2726
2764- store.set_totp_name(&user.id, None).await.unwrap();2727+ store.set_totp_name(&device.id, None).await.unwrap();
2765 assert_eq!(2728 assert_eq!(
2766- store.totp_state(&user.id).await.unwrap().unwrap().name,2729+ store.totp_state(&device.id).await.unwrap().unwrap().name,
2767 None2730 None
2768 );2731 );
2769 2732
2770- store.disable_totp(&user.id).await.unwrap();2733+ store.disable_totp(&device.id).await.unwrap();
2771- let off = store.totp_state(&user.id).await.unwrap().unwrap();2734+ let off = store.totp_state(&device.id).await.unwrap().unwrap();
2772 assert_eq!(2735 assert_eq!(
2773 (off.name, off.created_at, off.last_used_at),2736 (off.name, off.created_at, off.last_used_at),
2774 (None, None, None)2737 (None, None, None)
2775 );2738 );
2776 2739
2777- store.set_password_hash(&user.id, "new-hash").await.unwrap();2740+ store
2778- assert!(store.password_changed_at(&user.id).await.unwrap().is_some());2741+ .set_password_hash(&device.id, "new-hash")
2742+ .await
2743+ .unwrap();
2744+ assert!(
2745+ store
2746+ .password_changed_at(&device.id)
2747+ .await
2748+ .unwrap()
2749+ .is_some()
2750+ );
2779 }2751 }
2780 2752
2781 fn recovery_hash(code: &str) -> String {2753 fn recovery_hash(code: &str) -> String {
crates/forge-core/src/store/snapshot.rsmodified+18 -24
@@ -193,7 +193,7 @@ mod tests {
193 use super::*;193 use super::*;
194 194
195 #[tokio::test]195 #[tokio::test]
196- async fn vacuum_into_snapshot_opens_read_only_with_its_data() {196+ async fn snapshot_verifies_good_copies_and_refuses_bad_ones() {
197 let dir = tempfile::tempdir().expect("tempdir");197 let dir = tempfile::tempdir().expect("tempdir");
198 let store = Store::open(&dir.path().join("forge.db"))198 let store = Store::open(&dir.path().join("forge.db"))
199 .await199 .await
@@ -213,6 +213,7 @@ mod tests {
213 .await213 .await
214 .expect("repo");214 .expect("repo");
215 215
216+ // Scenario: a snapshot holds all its data in the main file and opens read only.
216 let snap = dir.path().join("snap").join("forge.db");217 let snap = dir.path().join("snap").join("forge.db");
217 std::fs::create_dir_all(snap.parent().expect("parent")).expect("mkdir");218 std::fs::create_dir_all(snap.parent().expect("parent")).expect("mkdir");
218 store.snapshot_to(&snap).await.expect("snapshot");219 store.snapshot_to(&snap).await.expect("snapshot");
@@ -242,6 +243,22 @@ mod tests {
242 info.repos,243 info.repos,
243 vec![(repo.id.clone(), "Tahinli".to_string(), "demo".to_string())]244 vec![(repo.id.clone(), "Tahinli".to_string(), "demo".to_string())]
244 );245 );
246+
247+ // Scenario: a garbage file and a truncated copy both fail verification.
248+ let garbage = dir.path().join("garbage.db");
249+ std::fs::write(&garbage, b"not a database at all, just text padding").expect("write");
250+ assert!(
251+ verify_snapshot(&garbage).await.is_err(),
252+ "garbage must not verify"
253+ );
254+
255+ let bytes = std::fs::read(&snap).expect("read");
256+ let cut = dir.path().join("cut.db");
257+ std::fs::write(&cut, &bytes[..bytes.len() / 2]).expect("write");
258+ assert!(
259+ verify_snapshot(&cut).await.is_err(),
260+ "a truncated copy must not verify"
261+ );
245 }262 }
246 263
247 #[tokio::test]264 #[tokio::test]
@@ -255,27 +272,4 @@ mod tests {
255 let info = verify_snapshot(&snap).await.expect("verify");272 let info = verify_snapshot(&snap).await.expect("verify");
256 assert!(info.repos.is_empty());273 assert!(info.repos.is_empty());
257 }274 }
258-
259- #[tokio::test]
260- async fn truncated_or_garbage_snapshot_fails_verification() {
261- let dir = tempfile::tempdir().expect("tempdir");
262- let store = Store::open(&dir.path().join("forge.db"))
263- .await
264- .expect("open");
265- store
266- .create_user("Bob", None, "hash", false)
267- .await
268- .expect("user");
269- let snap = dir.path().join("snap.db");
270- store.snapshot_to(&snap).await.expect("snapshot");
271-
272- let garbage = dir.path().join("garbage.db");
273- std::fs::write(&garbage, b"not a database at all, just text padding").expect("write");
274- assert!(verify_snapshot(&garbage).await.is_err());
275-
276- let bytes = std::fs::read(&snap).expect("read");
277- let cut = dir.path().join("cut.db");
278- std::fs::write(&cut, &bytes[..bytes.len() / 2]).expect("write");
279- assert!(verify_snapshot(&cut).await.is_err());
280- }
281 }275 }
crates/forge-core/src/store/suspension.rsmodified+18 -21
@@ -208,8 +208,10 @@ mod tests {
208 }208 }
209 209
210 #[tokio::test]210 #[tokio::test]
211- async fn suspension_blocks_sessions_and_lifts() {211+ async fn suspension_scenarios() {
212- let (store, _admin, other, _d) = fixture().await;212+ let (store, admin, other, _d) = fixture().await;
213+
214+ // Scenario: suspending ends the user's sessions until it is lifted.
213 store215 store
214 .create_session(&other.id, "th", "csrf", "2999-01-01T00:00:00Z", None)216 .create_session(&other.id, "th", "csrf", "2999-01-01T00:00:00Z", None)
215 .await217 .await
@@ -223,26 +225,8 @@ mod tests {
223 );225 );
224 assert!(store.unsuspend_user(&other.id).await.unwrap());226 assert!(store.unsuspend_user(&other.id).await.unwrap());
225 assert!(store.suspension(&other.id).await.unwrap().is_none());227 assert!(store.suspension(&other.id).await.unwrap().is_none());
226- }
227 228
228- #[tokio::test]229+ // Scenario: suspending also revokes API tokens and SSH keys until lifted.
229- async fn last_admin_cannot_be_demoted_or_suspended() {
230- let (store, admin, other, _d) = fixture().await;
231- assert!(matches!(
232- store.set_admin(&admin.id, false).await,
233- Err(Error::Conflict(_))
234- ));
235- assert!(matches!(
236- store.suspend_user(&admin.id, "", None).await,
237- Err(Error::Conflict(_))
238- ));
239- store.set_admin(&other.id, true).await.unwrap();
240- store.set_admin(&admin.id, false).await.unwrap();
241- }
242-
243- #[tokio::test]
244- async fn suspension_stops_tokens_and_ssh_keys() {
245- let (store, _admin, other, _d) = fixture().await;
246 store230 store
247 .create_token(&other.id, "ci", "tokhash", &["repo".to_owned()], None)231 .create_token(&other.id, "ci", "tokhash", &["repo".to_owned()], None)
248 .await232 .await
@@ -259,5 +243,18 @@ mod tests {
259 store.unsuspend_user(&other.id).await.unwrap();243 store.unsuspend_user(&other.id).await.unwrap();
260 assert!(store.token_by_hash("tokhash").await.unwrap().is_some());244 assert!(store.token_by_hash("tokhash").await.unwrap().is_some());
261 assert!(store.ssh_key_by_blob(b"blob").await.unwrap().is_some());245 assert!(store.ssh_key_by_blob(b"blob").await.unwrap().is_some());
246+
247+ // Scenario: the last admin cannot be demoted or suspended.
248+ // Runs last because it promotes `other` to admin.
249+ assert!(matches!(
250+ store.set_admin(&admin.id, false).await,
251+ Err(Error::Conflict(_))
252+ ));
253+ assert!(matches!(
254+ store.suspend_user(&admin.id, "", None).await,
255+ Err(Error::Conflict(_))
256+ ));
257+ store.set_admin(&other.id, true).await.unwrap();
258+ store.set_admin(&admin.id, false).await.unwrap();
262 }259 }
263 }260 }
crates/forge-core/src/store/transfers.rsmodified+88 -142
@@ -275,217 +275,163 @@ mod tests {
275 }275 }
276 }276 }
277 277
278- async fn owner_of(fx: &Fixture) -> String {278+ async fn owner_of(store: &Store, repo_id: &str) -> String {
279+ store.repo_by_id(repo_id).await.unwrap().unwrap().owner_id
280+ }
281+
282+ /// A fresh repo owned by tahinli, so each scenario has its own transfer state.
283+ async fn new_repo(fx: &Fixture, name: &str) -> String {
279 fx.store284 fx.store
280- .repo_by_id(&fx.repo.id)285+ .create_repo(&fx.tahinli.id, name, "", Visibility::Private, "main")
281 .await286 .await
282 .unwrap()287 .unwrap()
283- .unwrap()288+ .id
284- .owner_id
285 }289 }
286 290
287 #[tokio::test]291 #[tokio::test]
288- async fn repo_stays_until_the_target_accepts() {292+ async fn transfer_scenarios() {
289 let fx = fixture().await;293 let fx = fixture().await;
294+
295+ // Scenario: an org target is visible to its owners only and accepted by them.
296+ let repo = new_repo(&fx, "for-acme").await;
297+ let org = fx.store.create_org("acme", &fx.carol.id).await.unwrap();
298+ assert_eq!(org.kind, AccountKind::Org);
290 fx.store299 fx.store
291- .initiate_transfer(&fx.repo.id, &fx.bob.id, &fx.tahinli.id)300+ .add_org_member(&org.id, &fx.bob.id, OrgMemberRole::Member)
292 .await301 .await
293 .unwrap();302 .unwrap();
294- let pending = fx303+ fx.store
295- .store304+ .initiate_transfer(&repo, &org.id, &fx.tahinli.id)
296- .pending_transfer(&fx.repo.id)
297 .await305 .await
298- .unwrap()
299 .unwrap();306 .unwrap();
300- assert_eq!(pending.to_owner_name, "bob");
301- assert_eq!(pending.initiated_by_name, "tahinli");
302- assert_eq!(pending.from_owner_name, "tahinli");
303- assert!(!pending.to_is_org);
304- assert_eq!(owner_of(&fx).await, fx.tahinli.id);
305 307
306- fx.store308+ let listed = fx
307- .accept_transfer(&fx.repo.id, &fx.bob.id)309+ .store
310+ .pending_transfers_for_user(&fx.bob.id)
308 .await311 .await
309 .unwrap();312 .unwrap();
310- assert_eq!(owner_of(&fx).await, fx.bob.id);
311 assert!(313 assert!(
312- fx.store314+ listed.is_empty(),
313- .pending_transfer(&fx.repo.id)315+ "a plain member does not see org transfers"
314- .await
315- .unwrap()
316- .is_none()
317 );316 );
318- }317+ let listed = fx
318+ .store
319+ .pending_transfers_for_user(&fx.carol.id)
320+ .await
321+ .unwrap();
322+ assert_eq!(listed.len(), 1);
323+ assert!(listed[0].to_is_org);
319 324
320- #[tokio::test]325+ assert!(matches!(
321- async fn only_the_target_can_accept() {326+ fx.store.accept_transfer(&repo, &fx.bob.id).await,
322- let fx = fixture().await;327+ Err(Error::NotFound)
328+ ));
329+ fx.store.accept_transfer(&repo, &fx.carol.id).await.unwrap();
330+ assert_eq!(owner_of(&fx.store, &repo).await, org.id);
331+
332+ // Scenario: the repo stays with its owner until the target accepts.
333+ let repo = fx.repo.id.clone();
323 fx.store334 fx.store
324- .initiate_transfer(&fx.repo.id, &fx.bob.id, &fx.tahinli.id)335+ .initiate_transfer(&repo, &fx.bob.id, &fx.tahinli.id)
325 .await336 .await
326 .unwrap();337 .unwrap();
327- let err = fx.store.accept_transfer(&fx.repo.id, &fx.carol.id).await;338+ let pending = fx.store.pending_transfer(&repo).await.unwrap().unwrap();
328- assert!(matches!(err, Err(Error::NotFound)));339+ assert_eq!(pending.to_owner_name, "bob");
329- let err = fx.store.accept_transfer(&fx.repo.id, &fx.tahinli.id).await;340+ assert_eq!(pending.initiated_by_name, "tahinli");
330- assert!(matches!(err, Err(Error::NotFound)));341+ assert_eq!(pending.from_owner_name, "tahinli");
331- assert_eq!(owner_of(&fx).await, fx.tahinli.id);342+ assert!(!pending.to_is_org);
332- }343+ assert_eq!(owner_of(&fx.store, &repo).await, fx.tahinli.id);
333 344
334- #[tokio::test]345+ fx.store.accept_transfer(&repo, &fx.bob.id).await.unwrap();
335- async fn decline_and_cancel_leave_the_repo_in_place() {346+ assert_eq!(owner_of(&fx.store, &repo).await, fx.bob.id);
336- let fx = fixture().await;347+ assert!(fx.store.pending_transfer(&repo).await.unwrap().is_none());
348+
349+ // Scenario: only the target can accept; the owner and strangers get NotFound.
350+ let repo = new_repo(&fx, "targeted").await;
337 fx.store351 fx.store
338- .initiate_transfer(&fx.repo.id, &fx.bob.id, &fx.tahinli.id)352+ .initiate_transfer(&repo, &fx.bob.id, &fx.tahinli.id)
339 .await353 .await
340 .unwrap();354 .unwrap();
355+ let err = fx.store.accept_transfer(&repo, &fx.carol.id).await;
356+ assert!(matches!(err, Err(Error::NotFound)));
357+ let err = fx.store.accept_transfer(&repo, &fx.tahinli.id).await;
358+ assert!(matches!(err, Err(Error::NotFound)));
359+ assert_eq!(owner_of(&fx.store, &repo).await, fx.tahinli.id);
360+
361+ // Scenario: a declined transfer and a cancelled one both leave the repo in place.
362+ let repo = new_repo(&fx, "declined").await;
341 fx.store363 fx.store
342- .decline_transfer(&fx.repo.id, &fx.bob.id)364+ .initiate_transfer(&repo, &fx.bob.id, &fx.tahinli.id)
343 .await365 .await
344 .unwrap();366 .unwrap();
345- assert_eq!(owner_of(&fx).await, fx.tahinli.id);367+ fx.store.decline_transfer(&repo, &fx.bob.id).await.unwrap();
346- assert!(368+ assert_eq!(owner_of(&fx.store, &repo).await, fx.tahinli.id);
347- fx.store369+ assert!(fx.store.pending_transfer(&repo).await.unwrap().is_none());
348- .pending_transfer(&fx.repo.id)
349- .await
350- .unwrap()
351- .is_none()
352- );
353 370
354 fx.store371 fx.store
355- .initiate_transfer(&fx.repo.id, &fx.bob.id, &fx.tahinli.id)372+ .initiate_transfer(&repo, &fx.bob.id, &fx.tahinli.id)
356 .await373 .await
357 .unwrap();374 .unwrap();
358- fx.store.cancel_transfer(&fx.repo.id).await.unwrap();375+ fx.store.cancel_transfer(&repo).await.unwrap();
359- assert!(376+ assert!(fx.store.pending_transfer(&repo).await.unwrap().is_none());
360- fx.store
361- .pending_transfer(&fx.repo.id)
362- .await
363- .unwrap()
364- .is_none()
365- );
366 assert!(matches!(377 assert!(matches!(
367- fx.store.cancel_transfer(&fx.repo.id).await,378+ fx.store.cancel_transfer(&repo).await,
368 Err(Error::NotFound)379 Err(Error::NotFound)
369 ));380 ));
370- assert_eq!(owner_of(&fx).await, fx.tahinli.id);381+ assert_eq!(owner_of(&fx.store, &repo).await, fx.tahinli.id);
371- }
372 382
373- #[tokio::test]383+ // Scenario: a second open transfer on the same repo conflicts.
374- async fn second_open_transfer_conflicts() {384+ let repo = new_repo(&fx, "contested").await;
375- let fx = fixture().await;
376 fx.store385 fx.store
377- .initiate_transfer(&fx.repo.id, &fx.bob.id, &fx.tahinli.id)386+ .initiate_transfer(&repo, &fx.bob.id, &fx.tahinli.id)
378 .await387 .await
379 .unwrap();388 .unwrap();
380 let again = fx389 let again = fx
381 .store390 .store
382- .initiate_transfer(&fx.repo.id, &fx.carol.id, &fx.tahinli.id)391+ .initiate_transfer(&repo, &fx.carol.id, &fx.tahinli.id)
383 .await;392 .await;
384 assert!(matches!(again, Err(Error::Conflict(_))));393 assert!(matches!(again, Err(Error::Conflict(_))));
385- }
386 394
387- #[tokio::test]395+ // Scenario: an expired transfer cannot be accepted and is replaced by a new one.
388- async fn expired_transfer_cannot_be_accepted_and_is_replaced() {396+ let repo = new_repo(&fx, "expiring").await;
389- let fx = fixture().await;
390 fx.store397 fx.store
391- .initiate_transfer(&fx.repo.id, &fx.bob.id, &fx.tahinli.id)398+ .initiate_transfer(&repo, &fx.bob.id, &fx.tahinli.id)
392 .await399 .await
393 .unwrap();400 .unwrap();
394 {401 {
395 let conn = fx.store.inner.conn.lock().await;402 let conn = fx.store.inner.conn.lock().await;
396 conn.execute(403 conn.execute(
397- "UPDATE repo_transfers SET expires_at = '2000-01-01T00:00:00.000000000Z'",404+ "UPDATE repo_transfers SET expires_at = '2000-01-01T00:00:00.000000000Z' WHERE repo_id = ?1",
398- (),405+ (repo.clone(),),
399 )406 )
400 .await407 .await
401 .unwrap();408 .unwrap();
402 }409 }
403- assert!(410+ assert!(fx.store.pending_transfer(&repo).await.unwrap().is_none());
404- fx.store
405- .pending_transfer(&fx.repo.id)
406- .await
407- .unwrap()
408- .is_none()
409- );
410 assert!(matches!(411 assert!(matches!(
411- fx.store.accept_transfer(&fx.repo.id, &fx.bob.id).await,412+ fx.store.accept_transfer(&repo, &fx.bob.id).await,
412 Err(Error::NotFound)413 Err(Error::NotFound)
413 ));414 ));
414- assert_eq!(owner_of(&fx).await, fx.tahinli.id);415+ assert_eq!(owner_of(&fx.store, &repo).await, fx.tahinli.id);
415 416
416 fx.store417 fx.store
417- .initiate_transfer(&fx.repo.id, &fx.carol.id, &fx.tahinli.id)418+ .initiate_transfer(&repo, &fx.carol.id, &fx.tahinli.id)
418 .await419 .await
419 .unwrap();420 .unwrap();
420- let pending = fx421+ let pending = fx.store.pending_transfer(&repo).await.unwrap().unwrap();
421- .store
422- .pending_transfer(&fx.repo.id)
423- .await
424- .unwrap()
425- .unwrap();
426 assert_eq!(pending.to_owner_name, "carol");422 assert_eq!(pending.to_owner_name, "carol");
427- }
428 423
429- #[tokio::test]424+ // Scenario: a target that already has a repo of the same name is refused up front.
430- async fn name_clash_at_the_target_is_refused_up_front() {425+ let repo = new_repo(&fx, "clash").await;
431- let fx = fixture().await;
432 fx.store426 fx.store
433- .create_repo(&fx.bob.id, "proj", "", Visibility::Private, "main")427+ .create_repo(&fx.bob.id, "clash", "", Visibility::Private, "main")
434 .await428 .await
435 .unwrap();429 .unwrap();
436 let err = fx430 let err = fx
437 .store431 .store
438- .initiate_transfer(&fx.repo.id, &fx.bob.id, &fx.tahinli.id)432+ .initiate_transfer(&repo, &fx.bob.id, &fx.tahinli.id)
439 .await;433 .await;
440 assert!(matches!(err, Err(Error::Conflict(_))));434 assert!(matches!(err, Err(Error::Conflict(_))));
441- assert!(435+ assert!(fx.store.pending_transfer(&repo).await.unwrap().is_none());
442- fx.store
443- .pending_transfer(&fx.repo.id)
444- .await
445- .unwrap()
446- .is_none()
447- );
448- }
449-
450- #[tokio::test]
451- async fn org_target_is_accepted_by_its_owners_only() {
452- let fx = fixture().await;
453- let org = fx.store.create_org("acme", &fx.carol.id).await.unwrap();
454- assert_eq!(org.kind, AccountKind::Org);
455- fx.store
456- .add_org_member(&org.id, &fx.bob.id, OrgMemberRole::Member)
457- .await
458- .unwrap();
459- fx.store
460- .initiate_transfer(&fx.repo.id, &org.id, &fx.tahinli.id)
461- .await
462- .unwrap();
463-
464- let listed = fx
465- .store
466- .pending_transfers_for_user(&fx.bob.id)
467- .await
468- .unwrap();
469- assert!(
470- listed.is_empty(),
471- "a plain member does not see org transfers"
472- );
473- let listed = fx
474- .store
475- .pending_transfers_for_user(&fx.carol.id)
476- .await
477- .unwrap();
478- assert_eq!(listed.len(), 1);
479- assert!(listed[0].to_is_org);
480-
481- assert!(matches!(
482- fx.store.accept_transfer(&fx.repo.id, &fx.bob.id).await,
483- Err(Error::NotFound)
484- ));
485- fx.store
486- .accept_transfer(&fx.repo.id, &fx.carol.id)
487- .await
488- .unwrap();
489- assert_eq!(owner_of(&fx).await, org.id);
490 }436 }
491 }437 }
crates/forge-core/src/store/user_delete.rsmodified+56 -59
@@ -429,8 +429,10 @@ mod tests {
429 }429 }
430 430
431 #[tokio::test]431 #[tokio::test]
432- async fn ghost_is_created_once_and_cannot_log_in() {432+ async fn ghost_account_and_refused_deletions() {
433 let fx = fixture().await;433 let fx = fixture().await;
434+
435+ // Scenario: the ghost is created once and cannot log in.
434 let again = fx.store.ensure_ghost_user().await.unwrap();436 let again = fx.store.ensure_ghost_user().await.unwrap();
435 assert_eq!(again.id, fx.ghost.id);437 assert_eq!(again.id, fx.ghost.id);
436 assert_eq!(fx.ghost.username, "ghost");438 assert_eq!(fx.ghost.username, "ghost");
@@ -442,11 +444,33 @@ mod tests {
442 "!",444 "!",
443 &fx.ghost.password_hash445 &fx.ghost.password_hash
444 ));446 ));
447+
448+ // Scenario: the ghost itself cannot be deleted.
449+ assert!(matches!(
450+ fx.store.delete_user(&fx.ghost.id, &fx.ghost.id).await,
451+ Err(Error::Forbidden(_))
452+ ));
453+
454+ // Scenario: a user who owns a repo is refused and nothing changes.
455+ let err = fx.store.delete_user(&fx.bob.id, &fx.ghost.id).await;
456+ assert!(
457+ matches!(err, Err(Error::Conflict(ref m)) if m == "transfer or delete their repositories first"),
458+ "{err:?}"
459+ );
460+ assert!(fx.store.user_by_id(&fx.bob.id).await.unwrap().is_some());
461+ assert!(fx.store.repo_by_id(&fx.repo_id).await.unwrap().is_some());
462+
463+ // Scenario: the last administrator is refused (before a second admin exists).
464+ let err = fx.store.delete_user(&fx.admin.id, &fx.ghost.id).await;
465+ assert!(matches!(err, Err(Error::Conflict(_))), "{err:?}");
466+ assert!(fx.store.user_by_id(&fx.admin.id).await.unwrap().is_some());
445 }467 }
446 468
447 #[tokio::test]469 #[tokio::test]
448- async fn authored_content_moves_to_ghost_and_stays_readable() {470+ async fn authored_content_moves_to_ghost_and_preview_changes_nothing() {
449 let fx = fixture().await;471 let fx = fixture().await;
472+
473+ // Scenario: authored content moves to the ghost and stays readable.
450 let issue = fx474 let issue = fx
451 .store475 .store
452 .create_issue(&fx.repo_id, &fx.tahinli.id, "Bug", "body")476 .create_issue(&fx.repo_id, &fx.tahinli.id, "Bug", "body")
@@ -518,18 +542,39 @@ mod tests {
518 0542 0
519 );543 );
520 assert!(fx.store.user_by_id(&fx.tahinli.id).await.unwrap().is_none());544 assert!(fx.store.user_by_id(&fx.tahinli.id).await.unwrap().is_none());
521- }
522 545
523- #[tokio::test]546+ // Scenario: a deletion preview reports counts and changes nothing.
524- async fn owning_a_repo_refuses_and_changes_nothing() {547+ let second_admin = fx
525- let fx = fixture().await;548+ .store
526- let err = fx.store.delete_user(&fx.bob.id, &fx.ghost.id).await;549+ .create_user("Carol", None, "hash", true)
550+ .await
551+ .unwrap();
552+ fx.store
553+ .create_issue(&fx.repo_id, &second_admin.id, "t", "b")
554+ .await
555+ .unwrap();
556+ let preview = fx
557+ .store
558+ .deletion_preview(&second_admin.id, Some(&fx.ghost.id))
559+ .await
560+ .unwrap();
561+ assert_eq!(reassigned(&preview, "issues"), Some(1));
527 assert!(562 assert!(
528- matches!(err, Err(Error::Conflict(ref m)) if m == "transfer or delete their repositories first"),563+ fx.store
529- "{err:?}"564+ .user_by_id(&second_admin.id)
565+ .await
566+ .unwrap()
567+ .is_some()
568+ );
569+ assert_eq!(
570+ scalar(
571+ &fx.store,
572+ "SELECT COUNT(*) FROM issues WHERE author_id = ?1",
573+ &second_admin.id
574+ )
575+ .await,
576+ 1
530 );577 );
531- assert!(fx.store.user_by_id(&fx.bob.id).await.unwrap().is_some());
532- assert!(fx.store.repo_by_id(&fx.repo_id).await.unwrap().is_some());
533 }578 }
534 579
535 #[tokio::test]580 #[tokio::test]
@@ -581,54 +626,6 @@ mod tests {
581 );626 );
582 }627 }
583 628
584- #[tokio::test]
585- async fn last_administrator_is_refused() {
586- let fx = fixture().await;
587- let err = fx.store.delete_user(&fx.admin.id, &fx.ghost.id).await;
588- assert!(matches!(err, Err(Error::Conflict(_))), "{err:?}");
589- assert!(fx.store.user_by_id(&fx.admin.id).await.unwrap().is_some());
590- }
591-
592- #[tokio::test]
593- async fn ghost_cannot_be_deleted_and_preview_changes_nothing() {
594- let fx = fixture().await;
595- assert!(matches!(
596- fx.store.delete_user(&fx.ghost.id, &fx.ghost.id).await,
597- Err(Error::Forbidden(_))
598- ));
599- let second_admin = fx
600- .store
601- .create_user("Carol", None, "hash", true)
602- .await
603- .unwrap();
604- fx.store
605- .create_issue(&fx.repo_id, &second_admin.id, "t", "b")
606- .await
607- .unwrap();
608- let preview = fx
609- .store
610- .deletion_preview(&second_admin.id, Some(&fx.ghost.id))
611- .await
612- .unwrap();
613- assert_eq!(reassigned(&preview, "issues"), Some(1));
614- assert!(
615- fx.store
616- .user_by_id(&second_admin.id)
617- .await
618- .unwrap()
619- .is_some()
620- );
621- assert_eq!(
622- scalar(
623- &fx.store,
624- "SELECT COUNT(*) FROM issues WHERE author_id = ?1",
625- &second_admin.id
626- )
627- .await,
628- 1
629- );
630- }
631-
632 #[tokio::test]629 #[tokio::test]
633 async fn discussions_and_comments_move_to_ghost() {630 async fn discussions_and_comments_move_to_ghost() {
634 let fx = fixture().await;631 let fx = fixture().await;