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
26 changes: 13 additions & 13 deletions src/tests/account_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ const URL: &str = "/api/v1/me";
const LOCK_REASON: &str = "test lock reason";

fn lock_account(app: &TestApp, user_id: i32, until: Option<NaiveDateTime>) {
app.db(|conn| {
use crate::schema::users;
use diesel::prelude::*;

diesel::update(users::table)
.set((
users::account_lock_reason.eq(LOCK_REASON),
users::account_lock_until.eq(until),
))
.filter(users::id.eq(user_id))
.execute(conn)
.unwrap();
});
use crate::schema::users;
use diesel::prelude::*;

let mut conn = app.db_conn();

diesel::update(users::table)
.set((
users::account_lock_reason.eq(LOCK_REASON),
users::account_lock_until.eq(until),
))
.filter(users::id.eq(user_id))
.execute(&mut conn)
.unwrap();
}

#[tokio::test(flavor = "multi_thread")]
Expand Down
20 changes: 10 additions & 10 deletions src/tests/blocked_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ async fn test_non_blocked_download_route() {
})
.with_user();

app.db(|conn| {
CrateBuilder::new("foo", user.as_model().id)
.version(VersionBuilder::new("1.0.0"))
.expect_build(conn);
});
let mut conn = app.db_conn();

CrateBuilder::new("foo", user.as_model().id)
.version(VersionBuilder::new("1.0.0"))
.expect_build(&mut conn);

let status = anon
.get::<()>("/api/v1/crates/foo/1.0.0/download")
Expand All @@ -34,11 +34,11 @@ async fn test_blocked_download_route() {
})
.with_user();

app.db(|conn| {
CrateBuilder::new("foo", user.as_model().id)
.version(VersionBuilder::new("1.0.0"))
.expect_build(conn);
});
let mut conn = app.db_conn();

CrateBuilder::new("foo", user.as_model().id)
.version(VersionBuilder::new("1.0.0"))
.expect_build(&mut conn);

let status = anon
.get::<()>("/api/v1/crates/foo/1.0.0/download")
Expand Down
7 changes: 3 additions & 4 deletions src/tests/dump_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ static PATH_DATE_RE: LazyLock<Regex> =
#[tokio::test(flavor = "multi_thread")]
async fn test_dump_db_job() {
let (app, _, _, token) = TestApp::full().with_token();
let mut conn = app.db_conn();

app.db(|conn| {
CrateBuilder::new("test-crate", token.as_model().user_id).expect_build(conn);
CrateBuilder::new("test-crate", token.as_model().user_id).expect_build(&mut conn);

DumpDb.enqueue(conn).unwrap();
});
DumpDb.enqueue(&mut conn).unwrap();

app.run_pending_background_jobs().await;

Expand Down
135 changes: 62 additions & 73 deletions src/tests/github_secret_scanning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,25 @@ static GITHUB_PUBLIC_KEY_SIGNATURE: &str = "MEUCIFLZzeK++IhS+y276SRk2Pe5LfDrfvTX
#[tokio::test(flavor = "multi_thread")]
async fn github_secret_alert_revokes_token() {
let (app, anon, user, token) = TestApp::init().with_token();
let mut conn = app.db_conn();

// Ensure no emails were sent up to this point
assert_eq!(app.emails().len(), 0);

// Ensure that the token currently exists in the database
app.db(|conn| {
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(conn));
assert_that!(tokens, len(eq(1)));
assert_eq!(tokens[0].name, token.as_model().name);
});
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(&mut conn));
assert_that!(tokens, len(eq(1)));
assert_eq!(tokens[0].name, token.as_model().name);

// Set token to expected value in signed request
app.db(|conn| {
let hashed_token = HashedToken::hash("some_token");
diesel::update(api_tokens::table)
.set(api_tokens::token.eq(hashed_token))
.execute(conn)
.unwrap();
});
let hashed_token = HashedToken::hash("some_token");
diesel::update(api_tokens::table)
.set(api_tokens::token.eq(hashed_token))
.execute(&mut conn)
.unwrap();

let mut request = anon.post_request(URL);
*request.body_mut() = GITHUB_ALERT.into();
Expand All @@ -51,18 +48,17 @@ async fn github_secret_alert_revokes_token() {
assert_json_snapshot!(response.json());

// Ensure that the token was revoked
app.db(|conn| {
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(conn));
assert_that!(tokens, empty());
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(true))
.load(conn));
assert_that!(tokens, len(eq(1)));
});
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(&mut conn));
assert_that!(tokens, empty());

let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(true))
.load(&mut conn));
assert_that!(tokens, len(eq(1)));

// Ensure exactly one email was sent
assert_snapshot!(app.emails_snapshot());
Expand All @@ -71,31 +67,28 @@ async fn github_secret_alert_revokes_token() {
#[tokio::test(flavor = "multi_thread")]
async fn github_secret_alert_for_revoked_token() {
let (app, anon, user, token) = TestApp::init().with_token();
let mut conn = app.db_conn();

// Ensure no emails were sent up to this point
assert_eq!(app.emails().len(), 0);

// Ensure that the token currently exists in the database
app.db(|conn| {
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(conn));
assert_that!(tokens, len(eq(1)));
assert_eq!(tokens[0].name, token.as_model().name);
});
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(&mut conn));
assert_that!(tokens, len(eq(1)));
assert_eq!(tokens[0].name, token.as_model().name);

// Set token to expected value in signed request and revoke it
app.db(|conn| {
let hashed_token = HashedToken::hash("some_token");
diesel::update(api_tokens::table)
.set((
api_tokens::token.eq(hashed_token),
api_tokens::revoked.eq(true),
))
.execute(conn)
.unwrap();
});
let hashed_token = HashedToken::hash("some_token");
diesel::update(api_tokens::table)
.set((
api_tokens::token.eq(hashed_token),
api_tokens::revoked.eq(true),
))
.execute(&mut conn)
.unwrap();

let mut request = anon.post_request(URL);
*request.body_mut() = GITHUB_ALERT.into();
Expand All @@ -106,18 +99,17 @@ async fn github_secret_alert_for_revoked_token() {
assert_json_snapshot!(response.json());

// Ensure that the token is still revoked
app.db(|conn| {
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(conn));
assert_that!(tokens, empty());
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(true))
.load(conn));
assert_that!(tokens, len(eq(1)));
});
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(&mut conn));
assert_that!(tokens, empty());

let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(true))
.load(&mut conn));
assert_that!(tokens, len(eq(1)));

// Ensure still no emails were sent
assert_eq!(app.emails().len(), 0);
Expand All @@ -126,19 +118,18 @@ async fn github_secret_alert_for_revoked_token() {
#[tokio::test(flavor = "multi_thread")]
async fn github_secret_alert_for_unknown_token() {
let (app, anon, user, token) = TestApp::init().with_token();
let mut conn = app.db_conn();

// Ensure no emails were sent up to this point
assert_eq!(app.emails().len(), 0);

// Ensure that the token currently exists in the database
app.db(|conn| {
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(conn));
assert_that!(tokens, len(eq(1)));
assert_eq!(tokens[0].name, token.as_model().name);
});
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(&mut conn));
assert_that!(tokens, len(eq(1)));
assert_eq!(tokens[0].name, token.as_model().name);

let mut request = anon.post_request(URL);
*request.body_mut() = GITHUB_ALERT.into();
Expand All @@ -149,14 +140,12 @@ async fn github_secret_alert_for_unknown_token() {
assert_json_snapshot!(response.json());

// Ensure that the token was not revoked
app.db(|conn| {
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(conn));
assert_that!(tokens, len(eq(1)));
assert_eq!(tokens[0].name, token.as_model().name);
});
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(&mut conn));
assert_that!(tokens, len(eq(1)));
assert_eq!(tokens[0].name, token.as_model().name);

// Ensure still no emails were sent
assert_eq!(app.emails().len(), 0);
Expand Down
17 changes: 7 additions & 10 deletions src/tests/krate/following.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ async fn test_unauthenticated_requests() {
const CRATE_NAME: &str = "foo";

let (app, anon, user) = TestApp::init().with_user();
let mut conn = app.db_conn();

app.db(|conn| {
CrateBuilder::new(CRATE_NAME, user.as_model().id).expect_build(conn);
});
CrateBuilder::new(CRATE_NAME, user.as_model().id).expect_build(&mut conn);

let response = anon
.get::<()>(&format!("/api/v1/crates/{CRATE_NAME}/following"))
Expand All @@ -62,10 +61,9 @@ async fn test_following() {
const CRATE_NAME: &str = "foo_following";

let (app, _, user) = TestApp::init().with_user();
let mut conn = app.db_conn();

app.db(|conn| {
CrateBuilder::new(CRATE_NAME, user.as_model().id).expect_build(conn);
});
CrateBuilder::new(CRATE_NAME, user.as_model().id).expect_build(&mut conn);

// Check that initially we are not following the crate yet.
assert_is_following(CRATE_NAME, false, &user).await;
Expand Down Expand Up @@ -119,12 +117,11 @@ async fn test_api_token_auth() {
const CRATE_NOT_TO_FOLLOW: &str = "another_crate";

let (app, _, user, token) = TestApp::init().with_token();
let mut conn = app.db_conn();
let api_token = token.as_model();

app.db(|conn| {
CrateBuilder::new(CRATE_TO_FOLLOW, api_token.user_id).expect_build(conn);
CrateBuilder::new(CRATE_NOT_TO_FOLLOW, api_token.user_id).expect_build(conn);
});
CrateBuilder::new(CRATE_TO_FOLLOW, api_token.user_id).expect_build(&mut conn);
CrateBuilder::new(CRATE_NOT_TO_FOLLOW, api_token.user_id).expect_build(&mut conn);

follow(CRATE_TO_FOLLOW, &token).await;

Expand Down
3 changes: 2 additions & 1 deletion src/tests/krate/publish/audit_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ async fn publish_records_an_audit_action() {

let (app, anon, _, token) = TestApp::full().with_token();

app.db(|conn| assert!(VersionOwnerAction::all(conn).unwrap().is_empty()));
let mut conn = app.db_conn();
assert!(VersionOwnerAction::all(&mut conn).unwrap().is_empty());

// Upload a new crate, putting it in the git index
let crate_to_publish = PublishBuilder::new("fyk", "1.0.0");
Expand Down
18 changes: 8 additions & 10 deletions src/tests/krate/publish/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use insta::assert_snapshot;
#[tokio::test(flavor = "multi_thread")]
async fn new_wrong_token() {
let (app, anon, _, token) = TestApp::full().with_token();
let mut conn = app.db_conn();

// Try to publish without a token
let crate_to_publish = PublishBuilder::new("foo", "1.0.0");
Expand All @@ -17,12 +18,10 @@ async fn new_wrong_token() {
assert_snapshot!(response.text(), @r###"{"errors":[{"detail":"this action requires authentication"}]}"###);

// Try to publish with the wrong token (by changing the token in the database)
app.db(|conn| {
diesel::update(api_tokens::table)
.set(api_tokens::token.eq(b"bad" as &[u8]))
.execute(conn)
.unwrap();
});
diesel::update(api_tokens::table)
.set(api_tokens::token.eq(b"bad" as &[u8]))
.execute(&mut conn)
.unwrap();

let crate_to_publish = PublishBuilder::new("foo", "1.0.0");
let response = token.publish_crate(crate_to_publish).await;
Expand All @@ -35,11 +34,10 @@ async fn new_wrong_token() {
#[tokio::test(flavor = "multi_thread")]
async fn new_krate_wrong_user() {
let (app, _, user) = TestApp::full().with_user();
let mut conn = app.db_conn();

app.db(|conn| {
// Create the foo_wrong crate with one user
CrateBuilder::new("foo_wrong", user.as_model().id).expect_build(conn);
});
// Create the foo_wrong crate with one user
CrateBuilder::new("foo_wrong", user.as_model().id).expect_build(&mut conn);

// Then try to publish with a different user
let another_user = app.db_new_user("another").db_new_token("bar");
Expand Down
Loading