From 7ce9a0edcd7aa138edd36dc47597af80a8e77b1f Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Mon, 14 Oct 2024 11:20:01 +0000 Subject: [PATCH 1/2] tests: Move `NEXT_GH_ID` to `tests/util/github` module --- src/tests/mod.rs | 10 ++++------ src/tests/util.rs | 2 +- src/tests/util/github.rs | 7 +++++++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index b0fb41da3b4..95d64dd11d4 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -7,8 +7,8 @@ use crate::{ EncodableOwner, EncodableVersion, GoodCrate, }, }; -use std::sync::atomic::{AtomicUsize, Ordering}; +use crate::tests::util::github::next_gh_id; use diesel::prelude::*; mod account_lock; @@ -86,11 +86,9 @@ pub struct OkBool { ok: bool, } -static NEXT_GH_ID: AtomicUsize = AtomicUsize::new(0); - fn new_user(login: &str) -> NewUser<'_> { NewUser { - gh_id: NEXT_GH_ID.fetch_add(1, Ordering::SeqCst) as i32, + gh_id: next_gh_id(), gh_login: login, name: None, gh_avatar: None, @@ -100,8 +98,8 @@ fn new_user(login: &str) -> NewUser<'_> { fn new_team(login: &str) -> NewTeam<'_> { NewTeam { - org_id: NEXT_GH_ID.fetch_add(1, Ordering::SeqCst) as i32, - github_id: NEXT_GH_ID.fetch_add(1, Ordering::SeqCst) as i32, + org_id: next_gh_id(), + github_id: next_gh_id(), login, name: None, avatar: None, diff --git a/src/tests/util.rs b/src/tests/util.rs index aa096506dd5..4b6913e56fa 100644 --- a/src/tests/util.rs +++ b/src/tests/util.rs @@ -41,7 +41,7 @@ use std::net::SocketAddr; use tower::ServiceExt; mod chaosproxy; -mod github; +pub mod github; pub mod insta; pub mod matchers; mod mock_request; diff --git a/src/tests/util/github.rs b/src/tests/util/github.rs index cb1c2401760..5d5e0697fb1 100644 --- a/src/tests/util/github.rs +++ b/src/tests/util/github.rs @@ -5,6 +5,13 @@ use crates_io_github::{ GitHubTeam, GitHubTeamMembership, GithubUser, }; use oauth2::AccessToken; +use std::sync::atomic::{AtomicUsize, Ordering}; + +static NEXT_GH_ID: AtomicUsize = AtomicUsize::new(0); + +pub fn next_gh_id() -> i32 { + NEXT_GH_ID.fetch_add(1, Ordering::SeqCst) as i32 +} pub(crate) const MOCK_GITHUB_DATA: MockData = MockData { orgs: &[MockOrg { From 9a3e35af6ab623ed8294f1229c785442c9be1cb0 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Mon, 14 Oct 2024 11:24:59 +0000 Subject: [PATCH 2/2] typosquat/test_util: Use `tests::util::github::next_gh_id()` --- src/lib.rs | 2 +- src/tests/mod.rs | 2 +- src/tests/util.rs | 1 + src/typosquat/test_util.rs | 14 ++++---------- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c75fb35a375..b652eb2cd04 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,7 +60,7 @@ pub mod tasks; pub mod team_repo; mod test_util; #[cfg(test)] -mod tests; +pub mod tests; pub mod typosquat; pub mod util; pub mod views; diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 95d64dd11d4..98e0cf81d82 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -32,7 +32,7 @@ mod team; mod token; mod unhealthy_database; mod user; -mod util; +pub mod util; mod version; mod worker; diff --git a/src/tests/util.rs b/src/tests/util.rs index 4b6913e56fa..c5d6c477937 100644 --- a/src/tests/util.rs +++ b/src/tests/util.rs @@ -85,6 +85,7 @@ pub fn encode_session_header(session_key: &cookie::Key, user_id: i32) -> String /// A collection of helper methods for the 3 authentication types /// /// Helper methods go through public APIs, and should not modify the database directly +#[allow(async_fn_in_trait)] pub trait RequestHelper { fn request_builder(&self, method: Method, path: &str) -> MockRequest; fn app(&self) -> &TestApp; diff --git a/src/typosquat/test_util.rs b/src/typosquat/test_util.rs index f097a5d63fa..f99f9a8dd04 100644 --- a/src/typosquat/test_util.rs +++ b/src/typosquat/test_util.rs @@ -1,5 +1,6 @@ use diesel::{prelude::*, PgConnection}; +use crate::tests::util::github::next_gh_id; use crate::{ models::{ Crate, CrateOwner, NewCrate, NewTeam, NewUser, NewVersion, Owner, OwnerKind, User, Version, @@ -10,14 +11,12 @@ use crate::{ pub struct Faker { emails: Emails, - id: i32, } impl Faker { pub fn new() -> Self { Self { emails: Emails::new_in_memory(), - id: Default::default(), } } @@ -83,8 +82,8 @@ impl Faker { Ok(Owner::Team( NewTeam::new( &format!("github:{org}:{team}"), - self.next_id(), - self.next_id(), + next_gh_id(), + next_gh_id(), Some(team.to_string()), None, ) @@ -94,16 +93,11 @@ impl Faker { pub fn user(&mut self, conn: &mut PgConnection, login: &str) -> anyhow::Result { Ok( - NewUser::new(self.next_id(), login, None, None, "token").create_or_update( + NewUser::new(next_gh_id(), login, None, None, "token").create_or_update( None, &self.emails, conn, )?, ) } - - fn next_id(&mut self) -> i32 { - self.id += 1; - self.id - } }