Skip to content

Commit dab7125

Browse files
authored
tests: Replace assert_eq! with assert_snapshot! for status code checks (#11346)
This commit updates test assertions to use `insta::assert_snapshot!` for HTTP status code checks across various test files. This makes it easier to update the assertions in case status codes for certain errors are changed/fixed. Unfortunately, this changes the numbering of the file-based snapshots, which causes the diff to look much larger than it actually is... 🙈
1 parent f7af9f5 commit dab7125

File tree

165 files changed

+1185
-1234
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

165 files changed

+1185
-1234
lines changed

src/controllers/helpers/pagination.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ mod tests {
709709
let error = seek.after(&pagination.page).unwrap_err();
710710
assert_eq!(error.to_string(), "invalid seek parameter");
711711
let response = error.response();
712-
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
712+
assert_snapshot!(response.status(), @"400 Bad Request");
713713

714714
// Ensures it still encodes compactly with a field struct
715715
#[derive(Debug, Default, Serialize, PartialEq)]

src/controllers/krate/delete.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ mod tests {
279279
");
280280

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

285285
assert_snapshot!(app.emails_snapshot().await);
@@ -316,7 +316,7 @@ mod tests {
316316
");
317317

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

322322
assert_snapshot!(app.emails_snapshot().await);
@@ -353,7 +353,7 @@ mod tests {
353353
");
354354

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

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

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

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

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

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

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

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

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

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

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

446446
assert_crate_exists(&anon, "foo", true).await;
@@ -468,7 +468,7 @@ mod tests {
468468
.await?;
469469

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

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

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

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

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

512512
assert_crate_exists(&anon, "foo", true).await;
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
---
22
source: src/controllers/trustpub/github_configs/create/tests.rs
3-
expression: app.emails_snapshot().await
3+
expression: response.json()
44
---
5-
To: foo@example.com
6-
From: crates.io <noreply@crates.io>
7-
Subject: crates.io: Trusted Publishing configration added to foo
8-
Content-Type: text/plain; charset=utf-8
9-
Content-Transfer-Encoding: quoted-printable
10-
11-
Hello foo!
12-
13-
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.
14-
15-
Trusted Publishing configuration:
16-
17-
- Repository owner: rust-lang
18-
- Repository name: foo-rs
19-
- Workflow filename: publish.yml
20-
- Environment: (not set)
21-
22-
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.
23-
24-
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.
5+
{
6+
"github_config": {
7+
"crate": "foo",
8+
"created_at": "[datetime]",
9+
"environment": null,
10+
"id": 1,
11+
"repository_name": "foo-rs",
12+
"repository_owner": "rust-lang",
13+
"repository_owner_id": 42,
14+
"workflow_filename": "publish.yml"
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
source: src/controllers/trustpub/github_configs/create/tests.rs
3+
expression: app.emails_snapshot().await
4+
---
5+
To: foo@example.com
6+
From: crates.io <noreply@crates.io>
7+
Subject: crates.io: Trusted Publishing configration added to foo
8+
Content-Type: text/plain; charset=utf-8
9+
Content-Transfer-Encoding: quoted-printable
10+
11+
Hello foo!
12+
13+
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.
14+
15+
Trusted Publishing configuration:
16+
17+
- Repository owner: rust-lang
18+
- Repository name: foo-rs
19+
- Workflow filename: publish.yml
20+
- Environment: (not set)
21+
22+
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.
23+
24+
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.

src/controllers/trustpub/github_configs/create/snapshots/crates_io__controllers__trustpub__github_configs__create__tests__happy_path.snap

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/controllers/trustpub/github_configs/create/tests.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crates_io_database::schema::{emails, trustpub_configs_github};
66
use crates_io_github::{GitHubError, GitHubUser, MockGitHubClient};
77
use diesel::prelude::*;
88
use diesel_async::RunQueryDsl;
9-
use http::StatusCode;
109
use insta::{assert_json_snapshot, assert_snapshot};
1110
use serde_json::json;
1211

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

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

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

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

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

106105
Ok(())
@@ -109,7 +108,7 @@ async fn test_empty_body() -> anyhow::Result<()> {
109108
#[tokio::test(flavor = "multi_thread")]
110109
async fn test_empty_json_object() -> anyhow::Result<()> {
111110
let (_app, response) = run_test("{}").await;
112-
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
111+
assert_snapshot!(response.status(), @"422 Unprocessable Entity");
113112
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"}]}"#);
114113

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

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

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

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

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

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

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

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

191190
Ok(())
@@ -215,7 +214,7 @@ async fn test_unauthenticated() -> anyhow::Result<()> {
215214
}))?;
216215

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

221220
Ok(())
@@ -245,7 +244,7 @@ async fn test_token_auth() -> anyhow::Result<()> {
245244
}))?;
246245

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

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

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

275274
Ok(())
@@ -301,7 +300,7 @@ async fn test_non_owner() -> anyhow::Result<()> {
301300
}))?;
302301

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

307306
Ok(())
@@ -333,7 +332,7 @@ async fn test_unknown_github_user() -> anyhow::Result<()> {
333332
}))?;
334333

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

339338
Ok(())
@@ -365,7 +364,7 @@ async fn test_github_error() -> anyhow::Result<()> {
365364
}))?;
366365

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

371370
Ok(())
@@ -400,7 +399,7 @@ async fn test_unverified_email() -> anyhow::Result<()> {
400399
}))?;
401400

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

406405
Ok(())

0 commit comments

Comments
 (0)