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
2 changes: 1 addition & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn new_team(login: &str) -> NewTeam<'_> {
.build()
}

fn add_team_to_crate(
pub fn add_team_to_crate(
t: &Team,
krate: &Crate,
u: &User,
Expand Down
5 changes: 3 additions & 2 deletions src/typosquat/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ impl From<crate::models::Owner> for Owner {
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::add_team_to_crate;
use crate::typosquat::test_util::faker;
use crates_io_test_db::TestDatabase;
use thiserror::Error;
Expand All @@ -194,8 +195,8 @@ mod tests {

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

let mut async_conn = test_db.async_connect().await;
let top_crates = TopCrates::new(&mut async_conn, 2).await?;
Expand Down
29 changes: 4 additions & 25 deletions src/typosquat/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,15 @@ use diesel::{prelude::*, PgConnection};

use crate::tests::util::github::next_gh_id;
use crate::{
models::{Crate, CrateOwner, NewTeam, NewUser, Owner, OwnerKind, User},
schema::{crate_owners, users},
models::{Crate, NewTeam, NewUser, Team, User},
schema::users,
};

pub mod faker {
use super::*;
use crate::tests::builders::CrateBuilder;
use anyhow::anyhow;

pub fn add_crate_to_team(
conn: &mut PgConnection,
user: &User,
krate: &Crate,
team: &Owner,
) -> anyhow::Result<()> {
// We have to do a bunch of this by hand, since normally adding a team owner triggers
// various checks.
diesel::insert_into(crate_owners::table)
.values(&CrateOwner {
crate_id: krate.id,
owner_id: team.id(),
created_by: user.id,
owner_kind: OwnerKind::Team,
email_notifications: true,
})
.execute(conn)?;

Ok(())
}

pub fn crate_and_version(
conn: &mut PgConnection,
name: &str,
Expand All @@ -47,7 +26,7 @@ pub mod faker {
.map_err(|err| anyhow!(err.to_string()))
}

pub fn team(conn: &mut PgConnection, org: &str, team: &str) -> anyhow::Result<Owner> {
pub fn team(conn: &mut PgConnection, org: &str, team: &str) -> anyhow::Result<Team> {
let login = format!("github:{org}:{team}");
let team = NewTeam::builder()
.login(&login)
Expand All @@ -56,7 +35,7 @@ pub mod faker {
.name(team)
.build();

Ok(Owner::Team(team.create_or_update(conn)?))
Ok(team.create_or_update(conn)?)
}

pub fn user(conn: &mut PgConnection, login: &str) -> QueryResult<User> {
Expand Down