Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/typosquat/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,26 +182,24 @@ mod tests {
#[tokio::test]
async fn top_crates() -> Result<(), Error> {
let test_db = TestDatabase::new();
let mut conn = test_db.connect();
let mut async_conn = test_db.async_connect().await;
let mut conn = test_db.async_connect().await;

// Set up two users.
let user_a = faker::user(&mut conn, "a")?;
let user_b = faker::user(&mut conn, "b")?;
let user_a = faker::user(&mut conn, "a").await?;
let user_b = faker::user(&mut conn, "b").await?;

// Set up three crates with various ownership schemes.
let _top_a = faker::crate_and_version(&mut async_conn, "a", "Hello", &user_a, 2).await?;
let _top_a = faker::crate_and_version(&mut conn, "a", "Hello", &user_a, 2).await?;
let top_b =
faker::crate_and_version(&mut async_conn, "b", "Yes, this is dog", &user_b, 1).await?;
let not_top_c =
faker::crate_and_version(&mut async_conn, "c", "Unpopular", &user_a, 0).await?;
faker::crate_and_version(&mut conn, "b", "Yes, this is dog", &user_b, 1).await?;
let not_top_c = faker::crate_and_version(&mut conn, "c", "Unpopular", &user_a, 0).await?;

// Let's set up a team that owns both b and c, but not a.
let not_the_a_team = faker::team(&mut async_conn, "org", "team").await?;
add_team_to_crate(&not_the_a_team, &top_b, &user_b, &mut async_conn).await?;
add_team_to_crate(&not_the_a_team, &not_top_c, &user_b, &mut async_conn).await?;
let not_the_a_team = faker::team(&mut conn, "org", "team").await?;
add_team_to_crate(&not_the_a_team, &top_b, &user_b, &mut conn).await?;
add_team_to_crate(&not_the_a_team, &not_top_c, &user_b, &mut conn).await?;

let top_crates = TopCrates::new(&mut async_conn, 2).await?;
let top_crates = TopCrates::new(&mut conn, 2).await?;

// Let's ensure the top crates include what we expect (which is a and b, since we asked for
// 2 crates and they're the most downloaded).
Expand All @@ -215,7 +213,7 @@ mod tests {
assert!(!pkg_a.shared_authors(pkg_b.authors()));

// Now let's go get package c and pretend it's a new package.
let pkg_c = Crate::from_name(&mut async_conn, "c").await?;
let pkg_c = Crate::from_name(&mut conn, "c").await?;

// c _does_ have an author in common with a.
assert!(pkg_a.shared_authors(pkg_c.authors()));
Expand Down
6 changes: 4 additions & 2 deletions src/typosquat/test_util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use diesel::{prelude::*, PgConnection};
use diesel::prelude::*;
use diesel_async::RunQueryDsl;

use crate::tests::util::github::next_gh_id;
use crate::{
Expand Down Expand Up @@ -40,11 +41,12 @@ pub mod faker {
Ok(team.async_create_or_update(conn).await?)
}

pub fn user(conn: &mut PgConnection, login: &str) -> QueryResult<User> {
pub async fn user(conn: &mut AsyncPgConnection, login: &str) -> QueryResult<User> {
let user = NewUser::new(next_gh_id(), login, None, None, "token");

diesel::insert_into(users::table)
.values(user)
.get_result(conn)
.await
}
}
9 changes: 4 additions & 5 deletions src/worker/jobs/typosquat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,19 @@ mod tests {
async fn integration() -> anyhow::Result<()> {
let emails = Emails::new_in_memory();
let test_db = TestDatabase::new();
let mut conn = test_db.connect();
let mut async_conn = test_db.async_connect().await;
let mut conn = test_db.async_connect().await;

// Set up a user and a popular crate to match against.
let user = faker::user(&mut conn, "a")?;
faker::crate_and_version(&mut async_conn, "my-crate", "It's awesome", &user, 100).await?;
let user = faker::user(&mut conn, "a").await?;
faker::crate_and_version(&mut conn, "my-crate", "It's awesome", &user, 100).await?;

// Prime the cache so it only includes the crate we just created.
let mut async_conn = test_db.async_connect().await;
let cache = Cache::new(vec!["[email protected]".to_string()], &mut async_conn).await?;
let cache = Arc::new(cache);

// Now we'll create new crates: one problematic, one not so.
let other_user = faker::user(&mut conn, "b")?;
let other_user = faker::user(&mut async_conn, "b").await?;
let angel = faker::crate_and_version(
&mut async_conn,
"innocent-crate",
Expand Down