Skip to content

Use snapshot tests for app.stored_files() assertions #8909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2024
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
8 changes: 4 additions & 4 deletions src/tests/dump_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ async fn test_dump_db_job() {

app.run_pending_background_jobs().await;

let stored_files = app.stored_files().await;
assert_eq!(stored_files.len(), 2);
assert_eq!(stored_files[0], "db-dump.tar.gz");
assert_eq!(stored_files[1], "db-dump.zip");
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
db-dump.tar.gz
db-dump.zip
"###);

let path = object_store::path::Path::parse("db-dump.tar.gz").unwrap();
let result = app.as_inner().storage.as_inner().get(&path).await.unwrap();
Expand Down
34 changes: 18 additions & 16 deletions src/tests/krate/publish/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crates_io::schema::versions_published_by;
use diesel::{QueryDsl, RunQueryDsl};
use googletest::prelude::*;
use http::StatusCode;
use insta::assert_json_snapshot;
use insta::{assert_json_snapshot, assert_snapshot};

#[tokio::test(flavor = "multi_thread")]
async fn new_krate() {
Expand All @@ -21,8 +21,10 @@ async fn new_krate() {
let crates = app.crates_from_index_head("foo_new");
assert_json_snapshot!(crates);

let expected_files = vec!["crates/foo_new/foo_new-1.0.0.crate", "index/fo/o_/foo_new"];
assert_eq!(app.stored_files().await, expected_files);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/foo_new/foo_new-1.0.0.crate
index/fo/o_/foo_new
"###);

app.db(|conn| {
let email: String = versions_published_by::table
Expand All @@ -45,8 +47,10 @@ async fn new_krate_with_token() {
".crate.updated_at" => "[datetime]",
});

let expected_files = vec!["crates/foo_new/foo_new-1.0.0.crate", "index/fo/o_/foo_new"];
assert_eq!(app.stored_files().await, expected_files);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/foo_new/foo_new-1.0.0.crate
index/fo/o_/foo_new
"###);
}

#[tokio::test(flavor = "multi_thread")]
Expand All @@ -61,11 +65,10 @@ async fn new_krate_weird_version() {
".crate.updated_at" => "[datetime]",
});

let expected_files = vec![
"crates/foo_weird/foo_weird-0.0.0-pre.crate",
"index/fo/o_/foo_weird",
];
assert_eq!(app.stored_files().await, expected_files);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/foo_weird/foo_weird-0.0.0-pre.crate
index/fo/o_/foo_weird
"###);
}

#[tokio::test(flavor = "multi_thread")]
Expand All @@ -87,12 +90,11 @@ async fn new_krate_twice() {
let crates = app.crates_from_index_head("foo_twice");
assert_json_snapshot!(crates);

let expected_files = vec![
"crates/foo_twice/foo_twice-0.99.0.crate",
"crates/foo_twice/foo_twice-2.0.0.crate",
"index/fo/o_/foo_twice",
];
assert_eq!(app.stored_files().await, expected_files);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/foo_twice/foo_twice-0.99.0.crate
crates/foo_twice/foo_twice-2.0.0.crate
index/fo/o_/foo_twice
"###);
}

#[tokio::test(flavor = "multi_thread")]
Expand Down
10 changes: 5 additions & 5 deletions src/tests/krate/publish/git.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::builders::PublishBuilder;
use crate::util::{RequestHelper, TestApp};
use insta::assert_snapshot;

#[tokio::test(flavor = "multi_thread")]
async fn new_krate_git_upload_with_conflicts() {
Expand All @@ -10,9 +11,8 @@ async fn new_krate_git_upload_with_conflicts() {
let crate_to_publish = PublishBuilder::new("foo_conflicts", "1.0.0");
token.publish_crate(crate_to_publish).await.good();

let expected_files = vec![
"crates/foo_conflicts/foo_conflicts-1.0.0.crate",
"index/fo/o_/foo_conflicts",
];
assert_eq!(app.stored_files().await, expected_files);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/foo_conflicts/foo_conflicts-1.0.0.crate
index/fo/o_/foo_conflicts
"###);
}
16 changes: 9 additions & 7 deletions src/tests/krate/publish/max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crates_io_tarball::TarballBuilder;
use flate2::Compression;
use googletest::prelude::*;
use http::StatusCode;
use insta::assert_json_snapshot;
use insta::{assert_json_snapshot, assert_snapshot};

#[tokio::test(flavor = "multi_thread")]
async fn tarball_between_default_axum_limit_and_max_upload_size() {
Expand Down Expand Up @@ -49,7 +49,10 @@ async fn tarball_between_default_axum_limit_and_max_upload_size() {
".crate.created_at" => "[datetime]",
".crate.updated_at" => "[datetime]",
});
assert_eq!(app.stored_files().await.len(), 2);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/foo/foo-1.1.0.crate
index/3/f/foo
"###);
}

#[tokio::test(flavor = "multi_thread")]
Expand Down Expand Up @@ -140,9 +143,8 @@ async fn new_krate_too_big_but_whitelisted() {

token.publish_crate(crate_to_publish).await.good();

let expected_files = vec![
"crates/foo_whitelist/foo_whitelist-1.1.0.crate",
"index/fo/o_/foo_whitelist",
];
assert_eq!(app.stored_files().await, expected_files);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/foo_whitelist/foo_whitelist-1.1.0.crate
index/fo/o_/foo_whitelist
"###);
}
59 changes: 49 additions & 10 deletions src/tests/krate/publish/rate_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crates_io::rate_limiter::LimitedAction;
use crates_io::schema::{publish_limit_buckets, publish_rate_overrides};
use diesel::{ExpressionMethods, RunQueryDsl};
use http::StatusCode;
use insta::assert_snapshot;
use std::thread;
use std::time::Duration;

Expand Down Expand Up @@ -65,7 +66,10 @@ async fn publish_new_crate_ratelimit_expires() {
let crate_to_publish = PublishBuilder::new("rate_limited", "1.0.0");
token.publish_crate(crate_to_publish).await.good();

assert_eq!(app.stored_files().await.len(), 2);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/rate_limited/rate_limited-1.0.0.crate
index/ra/te/rate_limited
"###);

let json = anon.show_crate("rate_limited").await;
assert_eq!(json.krate.max_version, "1.0.0");
Expand Down Expand Up @@ -98,15 +102,23 @@ async fn publish_new_crate_override_loosens_ratelimit() {
let crate_to_publish = PublishBuilder::new("rate_limited1", "1.0.0");
token.publish_crate(crate_to_publish).await.good();

assert_eq!(app.stored_files().await.len(), 2);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/rate_limited1/rate_limited1-1.0.0.crate
index/ra/te/rate_limited1
"###);

let json = anon.show_crate("rate_limited1").await;
assert_eq!(json.krate.max_version, "1.0.0");

let crate_to_publish = PublishBuilder::new("rate_limited2", "1.0.0");
token.publish_crate(crate_to_publish).await.good();

assert_eq!(app.stored_files().await.len(), 4);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/rate_limited1/rate_limited1-1.0.0.crate
crates/rate_limited2/rate_limited2-1.0.0.crate
index/ra/te/rate_limited1
index/ra/te/rate_limited2
"###);

let json = anon.show_crate("rate_limited2").await;
assert_eq!(json.krate.max_version, "1.0.0");
Expand All @@ -117,7 +129,12 @@ async fn publish_new_crate_override_loosens_ratelimit() {
.await
.assert_rate_limited(LimitedAction::PublishNew);

assert_eq!(app.stored_files().await.len(), 4);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/rate_limited1/rate_limited1-1.0.0.crate
crates/rate_limited2/rate_limited2-1.0.0.crate
index/ra/te/rate_limited1
index/ra/te/rate_limited2
"###);

let response = anon.get::<()>("/api/v1/crates/rate_limited3").await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
Expand Down Expand Up @@ -151,7 +168,10 @@ async fn publish_new_crate_expired_override_ignored() {
let crate_to_publish = PublishBuilder::new("rate_limited1", "1.0.0");
token.publish_crate(crate_to_publish).await.good();

assert_eq!(app.stored_files().await.len(), 2);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/rate_limited1/rate_limited1-1.0.0.crate
index/ra/te/rate_limited1
"###);

let json = anon.show_crate("rate_limited1").await;
assert_eq!(json.krate.max_version, "1.0.0");
Expand All @@ -162,7 +182,10 @@ async fn publish_new_crate_expired_override_ignored() {
.await
.assert_rate_limited(LimitedAction::PublishNew);

assert_eq!(app.stored_files().await.len(), 2);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/rate_limited1/rate_limited1-1.0.0.crate
index/ra/te/rate_limited1
"###);

let response = anon.get::<()>("/api/v1/crates/rate_limited2").await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
Expand Down Expand Up @@ -194,15 +217,22 @@ async fn publish_existing_crate_rate_limited() {

let json = anon.show_crate("rate_limited1").await;
assert_eq!(json.krate.max_version, "1.0.0");
assert_eq!(app.stored_files().await.len(), 2);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/rate_limited1/rate_limited1-1.0.0.crate
index/ra/te/rate_limited1
"###);

// Uploading the first update to the crate works
let crate_to_publish = PublishBuilder::new("rate_limited1", "1.0.1");
token.publish_crate(crate_to_publish).await.good();

let json = anon.show_crate("rate_limited1").await;
assert_eq!(json.krate.max_version, "1.0.1");
assert_eq!(app.stored_files().await.len(), 3);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/rate_limited1/rate_limited1-1.0.0.crate
crates/rate_limited1/rate_limited1-1.0.1.crate
index/ra/te/rate_limited1
"###);

// Uploading the second update to the crate is rate limited
let crate_to_publish = PublishBuilder::new("rate_limited1", "1.0.2");
Expand All @@ -214,7 +244,11 @@ async fn publish_existing_crate_rate_limited() {
// Check that version 1.0.2 was not published
let json = anon.show_crate("rate_limited1").await;
assert_eq!(json.krate.max_version, "1.0.1");
assert_eq!(app.stored_files().await.len(), 3);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/rate_limited1/rate_limited1-1.0.0.crate
crates/rate_limited1/rate_limited1-1.0.1.crate
index/ra/te/rate_limited1
"###);

// Wait for the limit to be up
thread::sleep(Duration::from_millis(500));
Expand All @@ -224,7 +258,12 @@ async fn publish_existing_crate_rate_limited() {

let json = anon.show_crate("rate_limited1").await;
assert_eq!(json.krate.max_version, "1.0.2");
assert_eq!(app.stored_files().await.len(), 4);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/rate_limited1/rate_limited1-1.0.0.crate
crates/rate_limited1/rate_limited1-1.0.1.crate
crates/rate_limited1/rate_limited1-1.0.2.crate
index/ra/te/rate_limited1
"###);
}

#[tokio::test(flavor = "multi_thread")]
Expand Down
33 changes: 15 additions & 18 deletions src/tests/krate/publish/readme.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::builders::{CrateBuilder, PublishBuilder};
use crate::util::{RequestHelper, TestApp};
use http::StatusCode;
use insta::assert_json_snapshot;
use insta::{assert_json_snapshot, assert_snapshot};

#[tokio::test(flavor = "multi_thread")]
async fn new_krate_with_readme() {
Expand All @@ -15,12 +15,11 @@ async fn new_krate_with_readme() {
".crate.updated_at" => "[datetime]",
});

let expected_files = vec![
"crates/foo_readme/foo_readme-1.0.0.crate",
"index/fo/o_/foo_readme",
"readmes/foo_readme/foo_readme-1.0.0.html",
];
assert_eq!(app.stored_files().await, expected_files);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/foo_readme/foo_readme-1.0.0.crate
index/fo/o_/foo_readme
readmes/foo_readme/foo_readme-1.0.0.html
"###);
}

#[tokio::test(flavor = "multi_thread")]
Expand All @@ -35,11 +34,10 @@ async fn new_krate_with_empty_readme() {
".crate.updated_at" => "[datetime]",
});

let expected_files = vec![
"crates/foo_readme/foo_readme-1.0.0.crate",
"index/fo/o_/foo_readme",
];
assert_eq!(app.stored_files().await, expected_files);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/foo_readme/foo_readme-1.0.0.crate
index/fo/o_/foo_readme
"###);
}

#[tokio::test(flavor = "multi_thread")]
Expand All @@ -54,12 +52,11 @@ async fn new_krate_with_readme_and_plus_version() {
".crate.updated_at" => "[datetime]",
});

let expected_files = vec![
"crates/foo_readme/foo_readme-1.0.0+foo.crate",
"index/fo/o_/foo_readme",
"readmes/foo_readme/foo_readme-1.0.0+foo.html",
];
assert_eq!(app.stored_files().await, expected_files);
assert_snapshot!(app.stored_files().await.join("\n"), @r###"
crates/foo_readme/foo_readme-1.0.0+foo.crate
index/fo/o_/foo_readme
readmes/foo_readme/foo_readme-1.0.0+foo.html
"###);
}

#[tokio::test(flavor = "multi_thread")]
Expand Down