Skip to content
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

Use inline snapshots for error responses #9505

Merged
merged 2 commits into from
Sep 25, 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
23 changes: 8 additions & 15 deletions src/tests/account_lock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{util::RequestHelper, TestApp};
use chrono::{Duration, NaiveDateTime, Utc};
use chrono::{DateTime, Duration, NaiveDateTime, Utc};
use http::StatusCode;
use insta::assert_snapshot;

const URL: &str = "/api/v1/me";
const LOCK_REASON: &str = "test lock reason";
Expand Down Expand Up @@ -28,30 +29,22 @@ async fn account_locked_indefinitely() {

let response = user.get::<()>(URL).await;
assert_eq!(response.status(), StatusCode::FORBIDDEN);

let error_message = format!("This account is indefinitely locked. Reason: {LOCK_REASON}");
assert_eq!(
response.json(),
json!({ "errors": [{ "detail": error_message }] })
);
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"This account is indefinitely locked. Reason: test lock reason"}]}"#);
}

#[tokio::test(flavor = "multi_thread")]
async fn account_locked_with_future_expiry() {
let until = Utc::now().naive_utc() + Duration::days(1);
let until = "2099-12-12T12:12:12Z"
.parse::<DateTime<Utc>>()
.unwrap()
.naive_utc();

let (app, _anon, user) = TestApp::init().with_user();
lock_account(&app, user.as_model().id, Some(until));

let until = until.format("%Y-%m-%d at %H:%M:%S UTC");
let response = user.get::<()>(URL).await;
assert_eq!(response.status(), StatusCode::FORBIDDEN);

let error_message = format!("This account is locked until {until}. Reason: {LOCK_REASON}");
assert_eq!(
response.json(),
json!({ "errors": [{ "detail": error_message }] })
);
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"This account is locked until 2099-12-12 at 12:12:12 UTC. Reason: test lock reason"}]}"#);
}

#[tokio::test(flavor = "multi_thread")]
Expand Down
4 changes: 2 additions & 2 deletions src/tests/krate/publish/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crates_io::schema::api_tokens;
use diesel::{ExpressionMethods, RunQueryDsl};
use googletest::prelude::*;
use http::StatusCode;
use insta::{assert_json_snapshot, assert_snapshot};
use insta::assert_snapshot;

#[tokio::test(flavor = "multi_thread")]
async fn new_wrong_token() {
Expand Down Expand Up @@ -47,7 +47,7 @@ async fn new_krate_wrong_user() {

let response = another_user.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::FORBIDDEN);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"this crate exists but you don't seem to be an owner. If you believe this is a mistake, perhaps you need to accept an invitation to be an owner before publishing."}]}"#);

assert_that!(app.stored_files().await, empty());
assert_that!(app.emails(), empty());
Expand Down
2 changes: 1 addition & 1 deletion src/tests/krate/publish/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ async fn new_krate_duplicate_version() {
let crate_to_publish = PublishBuilder::new("foo_dupe", "1.0.0");
let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"crate version `1.0.0` is already uploaded"}]}"#);

assert_that!(app.stored_files().await, empty());
}
2 changes: 1 addition & 1 deletion src/tests/krate/publish/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ async fn too_many_categories() {
)
.await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"expected at most 5 categories per crate"}]}"#);
assert_that!(app.stored_files().await, empty());
}
24 changes: 12 additions & 12 deletions src/tests/krate/publish/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async fn invalid_dependency_name() {
.publish_crate(PublishBuilder::new("foo", "1.0.0").dependency(DependencyBuilder::new("🦀")))
.await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"invalid character `🦀` in dependency name: `🦀`, the first character must be an ASCII character"}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand Down Expand Up @@ -50,7 +50,7 @@ async fn invalid_dependency_rename() {
)
.await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"invalid character `💩` in dependency name: `💩`, the first character must be an ASCII character, or `_`"}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand All @@ -70,7 +70,7 @@ async fn invalid_dependency_name_starts_with_digit() {
)
.await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"the name `1-foo` cannot be used as a dependency name, the name cannot start with a digit"}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand All @@ -90,7 +90,7 @@ async fn invalid_dependency_name_contains_unicode_chars() {
)
.await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"invalid character `🦀` in dependency name: `foo-🦀-bar`, characters must be an ASCII alphanumeric characters, `-`, or `_`"}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand All @@ -110,7 +110,7 @@ async fn invalid_too_long_dependency_name() {
)
.await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"the dependency name `fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff` is too long (max 64 characters)"}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand All @@ -129,7 +129,7 @@ async fn empty_dependency_name() {
)
.await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"dependency name cannot be empty"}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand Down Expand Up @@ -202,7 +202,7 @@ async fn new_krate_with_broken_dependency_requirement() {
let crate_to_publish = PublishBuilder::new("new_dep", "1.0.0").dependency(dependency);
let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"\"broken\" is an invalid version requirement"}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand All @@ -221,7 +221,7 @@ async fn reject_new_krate_with_non_exact_dependency() {

let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"no known crate named `foo_dep`"}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand Down Expand Up @@ -249,7 +249,7 @@ async fn reject_new_crate_with_alternative_registry_dependency() {
PublishBuilder::new("depends-on-alt-registry", "1.0.0").dependency(dependency);
let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"Dependency `dep` is hosted on another registry. Cross-registry dependencies are not permitted on crates.io."}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand All @@ -268,7 +268,7 @@ async fn new_krate_with_wildcard_dependency() {

let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r##"{"errors":[{"detail":"wildcard (`*`) dependency constraints are not allowed on crates.io. Crate with this problem: `foo_wild` See https://doc.rust-lang.org/cargo/faq.html#can-libraries-use--as-a-version-for-their-dependencies for more information"}]}"##);
assert_that!(app.stored_files().await, empty());
}

Expand All @@ -283,7 +283,7 @@ async fn new_krate_dependency_missing() {

let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"no known crate named `bar_missing`"}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand Down Expand Up @@ -321,7 +321,7 @@ async fn invalid_feature_name() {
)
.await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"invalid character `🍺` in feature `🍺`, the first character must be a Unicode XID start character or digit (most letters or `_` or `0` to `9`)"}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand Down
6 changes: 3 additions & 3 deletions src/tests/krate/publish/emails.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crates_io::schema::emails;
use diesel::{delete, update, ExpressionMethods, RunQueryDsl};
use googletest::prelude::*;
use http::StatusCode;
use insta::assert_json_snapshot;
use insta::assert_snapshot;

#[tokio::test(flavor = "multi_thread")]
async fn new_krate_without_any_email_fails() {
Expand All @@ -18,7 +18,7 @@ async fn new_krate_without_any_email_fails() {

let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"A verified email address is required to publish crates to crates.io. Visit https://crates.io/settings/profile to set and verify your email address."}]}"#);
assert_that!(app.stored_files().await, empty());
assert_that!(app.emails(), empty());
}
Expand All @@ -38,7 +38,7 @@ async fn new_krate_with_unverified_email_fails() {

let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"A verified email address is required to publish crates to crates.io. Visit https://crates.io/settings/profile to set and verify your email address."}]}"#);
assert_that!(app.stored_files().await, empty());
assert_that!(app.emails(), empty());
}
18 changes: 9 additions & 9 deletions src/tests/krate/publish/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::builders::{CrateBuilder, DependencyBuilder, PublishBuilder};
use crate::util::{RequestHelper, TestApp};
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 features_version_2() {
Expand Down Expand Up @@ -60,7 +60,7 @@ async fn empty_feature_name() {
let crate_to_publish = PublishBuilder::new("foo", "1.0.0").feature("", &[]);
let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"feature cannot be empty"}]}"#);
assert!(app.stored_files().await.is_empty());
}

Expand All @@ -71,7 +71,7 @@ async fn invalid_feature_name1() {
let crate_to_publish = PublishBuilder::new("foo", "1.0.0").feature("~foo", &[]);
let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"invalid character `~` in feature `~foo`, the first character must be a Unicode XID start character or digit (most letters or `_` or `0` to `9`)"}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand All @@ -82,7 +82,7 @@ async fn invalid_feature_name2() {
let crate_to_publish = PublishBuilder::new("foo", "1.0.0").feature("foo", &["!bar"]);
let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"invalid character `!` in feature `!bar`, the first character must be a Unicode XID start character or digit (most letters or `_` or `0` to `9`)"}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand All @@ -92,7 +92,7 @@ async fn invalid_feature_name_start_with_hyphen() {
let crate_to_publish = PublishBuilder::new("foo", "1.0.0").feature("-foo1.bar", &[]);
let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"invalid character `-` in feature `-foo1.bar`, the first character must be a Unicode XID start character or digit (most letters or `_` or `0` to `9`)"}]}"#);
assert!(app.stored_files().await.is_empty());
}

Expand All @@ -112,7 +112,7 @@ async fn too_many_features() {
.feature("five", &[]);
let response = token.publish_crate(publish_builder).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"crates.io only allows a maximum number of 3 features, but your crate is declaring 5 features.\n\nTake a look at https://blog.rust-lang.org/2023/10/26/broken-badges-and-23k-keywords.html to understand why this restriction was introduced.\n\nIf you have a use case that requires an increase of this limit, please send us an email to help@crates.io to discuss the details."}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand All @@ -138,7 +138,7 @@ async fn too_many_features_with_custom_limit() {
.feature("five", &[]);
let response = token.publish_crate(publish_builder).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"crates.io only allows a maximum number of 4 features, but your crate is declaring 5 features.\n\nTake a look at https://blog.rust-lang.org/2023/10/26/broken-badges-and-23k-keywords.html to understand why this restriction was introduced.\n\nIf you have a use case that requires an increase of this limit, please send us an email to help@crates.io to discuss the details."}]}"#);
assert_that!(app.stored_files().await, empty());

let publish_builder = PublishBuilder::new("foo", "1.0.0")
Expand Down Expand Up @@ -169,7 +169,7 @@ async fn too_many_enabled_features() {
.feature("default", &["one", "two", "three", "four", "five"]);
let response = token.publish_crate(publish_builder).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"crates.io only allows a maximum number of 3 features or dependencies that another feature can enable, but the \"default\" feature of your crate is enabling 5 features or dependencies.\n\nTake a look at https://blog.rust-lang.org/2023/10/26/broken-badges-and-23k-keywords.html to understand why this restriction was introduced.\n\nIf you have a use case that requires an increase of this limit, please send us an email to help@crates.io to discuss the details."}]}"#);
assert_that!(app.stored_files().await, empty());
}

Expand All @@ -191,7 +191,7 @@ async fn too_many_enabled_features_with_custom_limit() {
.feature("default", &["one", "two", "three", "four", "five"]);
let response = token.publish_crate(publish_builder).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"crates.io only allows a maximum number of 4 features or dependencies that another feature can enable, but the \"default\" feature of your crate is enabling 5 features or dependencies.\n\nTake a look at https://blog.rust-lang.org/2023/10/26/broken-badges-and-23k-keywords.html to understand why this restriction was introduced.\n\nIf you have a use case that requires an increase of this limit, please send us an email to help@crates.io to discuss the details."}]}"#);
assert_that!(app.stored_files().await, empty());

let publish_builder =
Expand Down
6 changes: 3 additions & 3 deletions src/tests/krate/publish/inheritance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::builders::PublishBuilder;
use crate::util::{RequestHelper, TestApp};
use http::StatusCode;
use insta::assert_json_snapshot;
use insta::assert_snapshot;

#[tokio::test(flavor = "multi_thread")]
async fn workspace_inheritance() {
Expand All @@ -14,7 +14,7 @@ async fn workspace_inheritance() {
)
.await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"failed to parse `Cargo.toml` manifest file\n\nvalue from workspace hasn't been set"}]}"#);
}

#[tokio::test(flavor = "multi_thread")]
Expand All @@ -25,5 +25,5 @@ async fn workspace_inheritance_with_dep() {
"[package]\nname = \"foo\"\nversion = \"1.0.0\"\n\n[dependencies]\nserde.workspace = true\n",
)).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"failed to parse `Cargo.toml` manifest file\n\nvalue from workspace hasn't been set"}]}"#);
}
10 changes: 5 additions & 5 deletions src/tests/krate/publish/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::builders::PublishBuilder;
use crate::util::{RequestHelper, TestApp};
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 good_keywords() {
Expand All @@ -26,17 +26,17 @@ async fn bad_keywords() {
PublishBuilder::new("foo_bad_key", "1.0.0").keyword("super-long-keyword-name-oh-no");
let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"\"super-long-keyword-name-oh-no\" is an invalid keyword (keywords must have less than 20 characters)"}]}"#);

let crate_to_publish = PublishBuilder::new("foo_bad_key", "1.0.0").keyword("?@?%");
let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"\"?@?%\" is an invalid keyword"}]}"#);

let crate_to_publish = PublishBuilder::new("foo_bad_key", "1.0.0").keyword("áccênts");
let response = token.publish_crate(crate_to_publish).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"\"áccênts\" is an invalid keyword"}]}"#);
}

#[tokio::test(flavor = "multi_thread")]
Expand All @@ -54,6 +54,6 @@ async fn too_many_keywords() {
)
.await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_json_snapshot!(response.json());
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"expected at most 5 keywords per crate"}]}"#);
assert_that!(app.stored_files().await, empty());
}
Loading