Skip to content

tests: Replace assert_eq! with assert_snapshot! for status code checks #11346

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 13, 2025
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/controllers/helpers/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ mod tests {
let error = seek.after(&pagination.page).unwrap_err();
assert_eq!(error.to_string(), "invalid seek parameter");
let response = error.response();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_snapshot!(response.status(), @"400 Bad Request");

// Ensures it still encodes compactly with a field struct
#[derive(Debug, Default, Serialize, PartialEq)]
Expand Down
26 changes: 13 additions & 13 deletions src/controllers/krate/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ mod tests {
");

let response = delete_crate(&user, "foo").await;
assert_eq!(response.status(), StatusCode::NO_CONTENT);
assert_snapshot!(response.status(), @"204 No Content");
assert!(response.body().is_empty());

assert_snapshot!(app.emails_snapshot().await);
Expand Down Expand Up @@ -316,7 +316,7 @@ mod tests {
");

let response = delete_crate(&user, "foo").await;
assert_eq!(response.status(), StatusCode::NO_CONTENT);
assert_snapshot!(response.status(), @"204 No Content");
assert!(response.body().is_empty());

assert_snapshot!(app.emails_snapshot().await);
Expand Down Expand Up @@ -353,7 +353,7 @@ mod tests {
");

let response = delete_crate(&user, "foo").await;
assert_eq!(response.status(), StatusCode::NO_CONTENT);
assert_snapshot!(response.status(), @"204 No Content");
assert!(response.body().is_empty());

assert_snapshot!(app.emails_snapshot().await);
Expand All @@ -376,7 +376,7 @@ mod tests {
publish_crate(&user, "foo").await;

let response = delete_crate(&anon, "foo").await;
assert_eq!(response.status(), StatusCode::FORBIDDEN);
assert_snapshot!(response.status(), @"403 Forbidden");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"this action requires authentication"}]}"#);

assert_crate_exists(&anon, "foo", true).await;
Expand All @@ -391,7 +391,7 @@ mod tests {
publish_crate(&user, "foo").await;

let response = delete_crate(&token, "foo").await;
assert_eq!(response.status(), StatusCode::FORBIDDEN);
assert_snapshot!(response.status(), @"403 Forbidden");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"this action can only be performed on the crates.io website"}]}"#);

assert_crate_exists(&anon, "foo", true).await;
Expand All @@ -404,7 +404,7 @@ mod tests {
let (_app, _anon, user) = TestApp::full().with_user().await;

let response = delete_crate(&user, "foo").await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
assert_snapshot!(response.status(), @"404 Not Found");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"crate `foo` does not exist"}]}"#);

Ok(())
Expand All @@ -418,7 +418,7 @@ mod tests {
publish_crate(&user, "foo").await;

let response = delete_crate(&user2, "foo").await;
assert_eq!(response.status(), StatusCode::FORBIDDEN);
assert_snapshot!(response.status(), @"403 Forbidden");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"only owners have permission to delete crates"}]}"#);

assert_crate_exists(&anon, "foo", true).await;
Expand All @@ -437,10 +437,10 @@ mod tests {
// Add team owner
let body = json!({ "owners": ["github:test-org:all"] }).to_string();
let response = user.put::<()>("/api/v1/crates/foo/owners", body).await;
assert_eq!(response.status(), StatusCode::OK);
assert_snapshot!(response.status(), @"200 OK");

let response = delete_crate(&user2, "foo").await;
assert_eq!(response.status(), StatusCode::FORBIDDEN);
assert_snapshot!(response.status(), @"403 Forbidden");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"team members don't have permission to delete crates"}]}"#);

assert_crate_exists(&anon, "foo", true).await;
Expand Down Expand Up @@ -468,7 +468,7 @@ mod tests {
.await?;

let response = delete_crate(&user, "foo").await;
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
assert_snapshot!(response.status(), @"422 Unprocessable Entity");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"only crates with a single owner can be deleted after 72 hours"}]}"#);

assert_crate_exists(&anon, "foo", true).await;
Expand All @@ -486,7 +486,7 @@ mod tests {
adjust_downloads(&mut conn, crate_id, DOWNLOADS_PER_MONTH_LIMIT + 1).await?;

let response = delete_crate(&user, "foo").await;
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
assert_snapshot!(response.status(), @"422 Unprocessable Entity");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"only crates with less than 500 downloads per month can be deleted after 72 hours"}]}"#);

assert_crate_exists(&anon, "foo", true).await;
Expand All @@ -503,10 +503,10 @@ mod tests {
// Publish another crate
let pb = PublishBuilder::new("bar", "1.0.0").dependency(DependencyBuilder::new("foo"));
let response = user.publish_crate(pb).await;
assert_eq!(response.status(), StatusCode::OK);
assert_snapshot!(response.status(), @"200 OK");

let response = delete_crate(&user, "foo").await;
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
assert_snapshot!(response.status(), @"422 Unprocessable Entity");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"only crates without reverse dependencies can be deleted"}]}"#);

assert_crate_exists(&anon, "foo", true).await;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
---
source: src/controllers/trustpub/github_configs/create/tests.rs
expression: app.emails_snapshot().await
expression: response.json()
---
To: foo@example.com
From: crates.io <noreply@crates.io>
Subject: crates.io: Trusted Publishing configration added to foo
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Hello foo!

crates.io user foo has added a new "Trusted Publishing" configuration for GitHub Actions to a crate that you manage (foo). Trusted publishers act as trusted users and can publish new versions of the crate automatically.

Trusted Publishing configuration:

- Repository owner: rust-lang
- Repository name: foo-rs
- Workflow filename: publish.yml
- Environment: (not set)

If you did not make this change and you think it was made maliciously, you can remove the configuration from the crate via the "Settings" tab on the crate's page.

If you are unable to revert the change and need to do so, you can email help@crates.io to communicate with the crates.io support team.
{
"github_config": {
"crate": "foo",
"created_at": "[datetime]",
"environment": null,
"id": 1,
"repository_name": "foo-rs",
"repository_owner": "rust-lang",
"repository_owner_id": 42,
"workflow_filename": "publish.yml"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: src/controllers/trustpub/github_configs/create/tests.rs
expression: app.emails_snapshot().await
---
To: foo@example.com
From: crates.io <noreply@crates.io>
Subject: crates.io: Trusted Publishing configration added to foo
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Hello foo!

crates.io user foo has added a new "Trusted Publishing" configuration for GitHub Actions to a crate that you manage (foo). Trusted publishers act as trusted users and can publish new versions of the crate automatically.

Trusted Publishing configuration:

- Repository owner: rust-lang
- Repository name: foo-rs
- Workflow filename: publish.yml
- Environment: (not set)

If you did not make this change and you think it was made maliciously, you can remove the configuration from the crate via the "Settings" tab on the crate's page.

If you are unable to revert the change and need to do so, you can email help@crates.io to communicate with the crates.io support team.

This file was deleted.

31 changes: 15 additions & 16 deletions src/controllers/trustpub/github_configs/create/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crates_io_database::schema::{emails, trustpub_configs_github};
use crates_io_github::{GitHubError, GitHubUser, MockGitHubClient};
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use http::StatusCode;
use insta::{assert_json_snapshot, assert_snapshot};
use serde_json::json;

Expand Down Expand Up @@ -61,7 +60,7 @@ async fn test_happy_path() -> anyhow::Result<()> {
}))?;

let (app, response) = run_test(body).await;
assert_eq!(response.status(), StatusCode::OK);
assert_snapshot!(response.status(), @"200 OK");
assert_json_snapshot!(response.json(), { ".github_config.created_at" => "[datetime]" });

assert_snapshot!(app.emails_snapshot().await);
Expand Down Expand Up @@ -91,7 +90,7 @@ async fn test_happy_path_with_environment() -> anyhow::Result<()> {
}))?;

let (_app, response) = run_test(body).await;
assert_eq!(response.status(), StatusCode::OK);
assert_snapshot!(response.status(), @"200 OK");
assert_json_snapshot!(response.json(), { ".github_config.created_at" => "[datetime]" });

Ok(())
Expand All @@ -100,7 +99,7 @@ async fn test_happy_path_with_environment() -> anyhow::Result<()> {
#[tokio::test(flavor = "multi_thread")]
async fn test_empty_body() -> anyhow::Result<()> {
let (_app, response) = run_test("").await;
assert_eq!(response.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
assert_snapshot!(response.status(), @"415 Unsupported Media Type");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"Expected request with `Content-Type: application/json`"}]}"#);

Ok(())
Expand All @@ -109,7 +108,7 @@ async fn test_empty_body() -> anyhow::Result<()> {
#[tokio::test(flavor = "multi_thread")]
async fn test_empty_json_object() -> anyhow::Result<()> {
let (_app, response) = run_test("{}").await;
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
assert_snapshot!(response.status(), @"422 Unprocessable Entity");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"Failed to deserialize the JSON body into the target type: missing field `github_config` at line 1 column 2"}]}"#);

Ok(())
Expand All @@ -128,7 +127,7 @@ async fn test_invalid_owner() -> anyhow::Result<()> {
}))?;

let (_app, response) = run_test(body).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_snapshot!(response.status(), @"400 Bad Request");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"Invalid GitHub repository owner name"}]}"#);

Ok(())
Expand All @@ -147,7 +146,7 @@ async fn test_invalid_repo() -> anyhow::Result<()> {
}))?;

let (_app, response) = run_test(body).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_snapshot!(response.status(), @"400 Bad Request");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"Invalid GitHub repository name"}]}"#);

Ok(())
Expand All @@ -166,7 +165,7 @@ async fn test_invalid_workflow() -> anyhow::Result<()> {
}))?;

let (_app, response) = run_test(body).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_snapshot!(response.status(), @"400 Bad Request");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"Workflow filename must end with `.yml` or `.yaml`"}]}"#);

Ok(())
Expand All @@ -185,7 +184,7 @@ async fn test_invalid_environment() -> anyhow::Result<()> {
}))?;

let (_app, response) = run_test(body).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_snapshot!(response.status(), @"400 Bad Request");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"Environment name may not be empty (use `null` to omit)"}]}"#);

Ok(())
Expand Down Expand Up @@ -215,7 +214,7 @@ async fn test_unauthenticated() -> anyhow::Result<()> {
}))?;

let response = client.put::<()>(URL, body).await;
assert_eq!(response.status(), StatusCode::FORBIDDEN);
assert_snapshot!(response.status(), @"403 Forbidden");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"this action requires authentication"}]}"#);

Ok(())
Expand Down Expand Up @@ -245,7 +244,7 @@ async fn test_token_auth() -> anyhow::Result<()> {
}))?;

let response = token_client.put::<()>(URL, body).await;
assert_eq!(response.status(), StatusCode::FORBIDDEN);
assert_snapshot!(response.status(), @"403 Forbidden");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"this action can only be performed on the crates.io website"}]}"#);

Ok(())
Expand All @@ -269,7 +268,7 @@ async fn test_missing_crate() -> anyhow::Result<()> {
}))?;

let response = cookie_client.put::<()>(URL, body).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
assert_snapshot!(response.status(), @"404 Not Found");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"crate `foo` does not exist"}]}"#);

Ok(())
Expand Down Expand Up @@ -301,7 +300,7 @@ async fn test_non_owner() -> anyhow::Result<()> {
}))?;

let response = other_client.put::<()>(URL, body).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_snapshot!(response.status(), @"400 Bad Request");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"You are not an owner of this crate"}]}"#);

Ok(())
Expand Down Expand Up @@ -333,7 +332,7 @@ async fn test_unknown_github_user() -> anyhow::Result<()> {
}))?;

let response = cookie_client.put::<()>(URL, body).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
assert_snapshot!(response.status(), @"400 Bad Request");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"Unknown GitHub user or organization"}]}"#);

Ok(())
Expand Down Expand Up @@ -365,7 +364,7 @@ async fn test_github_error() -> anyhow::Result<()> {
}))?;

let response = cookie_client.put::<()>(URL, body).await;
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
assert_snapshot!(response.status(), @"500 Internal Server Error");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"Internal Server Error"}]}"#);

Ok(())
Expand Down Expand Up @@ -400,7 +399,7 @@ async fn test_unverified_email() -> anyhow::Result<()> {
}))?;

let response = cookie_client.put::<()>(URL, body).await;
assert_eq!(response.status(), StatusCode::FORBIDDEN);
assert_snapshot!(response.status(), @"403 Forbidden");
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"You must verify your email address to create a Trusted Publishing config"}]}"#);

Ok(())
Expand Down
Loading