Skip to content

Commit ad926bf

Browse files
committed
tests: Disable cargo compatibility status code enforcement
The middleware has its own unit tests to verify its functionality. Here, we can test what would happen if we toggled the status code enforcement off eventually.
1 parent df9329b commit ad926bf

File tree

9 files changed

+29
-25
lines changed

9 files changed

+29
-25
lines changed

src/tests/krate/publish/auth.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn new_wrong_token() {
1313
// Try to publish without a token
1414
let crate_to_publish = PublishBuilder::new("foo", "1.0.0");
1515
let response = anon.publish_crate(crate_to_publish);
16-
assert_eq!(response.status(), StatusCode::OK);
16+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
1717
assert_eq!(
1818
response.into_json(),
1919
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
@@ -29,7 +29,7 @@ fn new_wrong_token() {
2929

3030
let crate_to_publish = PublishBuilder::new("foo", "1.0.0");
3131
let response = token.publish_crate(crate_to_publish);
32-
assert_eq!(response.status(), StatusCode::OK);
32+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
3333
assert_eq!(
3434
response.into_json(),
3535
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })

src/tests/owners.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ fn owner_change_via_change_owner_token_with_wrong_crate_scope() {
366366
let body = json!({ "owners": [user2.gh_login] });
367367
let body = serde_json::to_vec(&body).unwrap();
368368
let response = token.put::<()>(&url, body);
369-
assert_eq!(response.status(), StatusCode::OK);
369+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
370370
assert_eq!(
371371
response.into_json(),
372372
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
@@ -388,7 +388,7 @@ fn owner_change_via_publish_token() {
388388
let body = json!({ "owners": [user2.gh_login] });
389389
let body = serde_json::to_vec(&body).unwrap();
390390
let response = token.put::<()>(&url, body);
391-
assert_eq!(response.status(), StatusCode::OK);
391+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
392392
assert_eq!(
393393
response.into_json(),
394394
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
@@ -409,7 +409,7 @@ fn owner_change_without_auth() {
409409
let body = json!({ "owners": [user2.gh_login] });
410410
let body = serde_json::to_vec(&body).unwrap();
411411
let response = anon.put::<()>(&url, body);
412-
assert_eq!(response.status(), StatusCode::OK);
412+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
413413
assert_eq!(
414414
response.into_json(),
415415
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })

src/tests/pagination.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn pagination_blocks_ip_from_cidr_block_list() {
2121
});
2222

2323
let response = anon.get_with_query::<()>("/api/v1/crates", "page=2&per_page=1");
24-
assert_eq!(response.status(), StatusCode::OK);
24+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
2525
assert_eq!(
2626
response.into_json(),
2727
json!({ "errors": [{ "detail": "requested page offset is too large" }] })

src/tests/read_only_mode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn cannot_hit_endpoint_which_writes_db_in_read_only_mode() {
3232
});
3333

3434
let response = token.delete::<()>("/api/v1/crates/foo_yank_read_only/1.0.0/yank");
35-
assert_eq!(response.status(), StatusCode::OK);
35+
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
3636
assert_json_snapshot!(response.into_json());
3737
}
3838

src/tests/routes/crates/list.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ fn invalid_seek_parameter() {
799799
let (_app, anon, _cookie) = TestApp::init().with_user();
800800

801801
let response = anon.get::<()>("/api/v1/crates?seek=broken");
802-
assert_eq!(response.status(), StatusCode::OK);
802+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
803803
assert_json_snapshot!(response.into_json());
804804
}
805805

@@ -816,15 +816,15 @@ fn pagination_parameters_only_accept_integers() {
816816

817817
let response =
818818
anon.get_with_query::<()>("/api/v1/crates", "page=1&per_page=100%22%EF%BC%8Cexception");
819-
assert_eq!(response.status(), StatusCode::OK);
819+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
820820
assert_eq!(
821821
response.into_json(),
822822
json!({ "errors": [{ "detail": "invalid digit found in string" }] })
823823
);
824824

825825
let response =
826826
anon.get_with_query::<()>("/api/v1/crates", "page=100%22%EF%BC%8Cexception&per_page=1");
827-
assert_eq!(response.status(), StatusCode::OK);
827+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
828828
assert_eq!(
829829
response.into_json(),
830830
json!({ "errors": [{ "detail": "invalid digit found in string" }] })

src/tests/routes/crates/versions/yank_unyank.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ mod auth {
115115
let (_, client, _) = prepare();
116116

117117
let response = client.yank(CRATE_NAME, CRATE_VERSION);
118-
assert_eq!(response.status(), StatusCode::OK);
118+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
119119
assert_eq!(
120120
response.into_json(),
121121
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
122122
);
123123

124124
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
125-
assert_eq!(response.status(), StatusCode::OK);
125+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
126126
assert_eq!(
127127
response.into_json(),
128128
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
@@ -182,14 +182,14 @@ mod auth {
182182
client.db_new_scoped_token("test-token", None, None, Some(expired_at.naive_utc()));
183183

184184
let response = client.yank(CRATE_NAME, CRATE_VERSION);
185-
assert_eq!(response.status(), StatusCode::OK);
185+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
186186
assert_eq!(
187187
response.into_json(),
188188
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
189189
);
190190

191191
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
192-
assert_eq!(response.status(), StatusCode::OK);
192+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
193193
assert_eq!(
194194
response.into_json(),
195195
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
@@ -222,14 +222,14 @@ mod auth {
222222
);
223223

224224
let response = client.yank(CRATE_NAME, CRATE_VERSION);
225-
assert_eq!(response.status(), StatusCode::OK);
225+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
226226
assert_eq!(
227227
response.into_json(),
228228
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
229229
);
230230

231231
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
232-
assert_eq!(response.status(), StatusCode::OK);
232+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
233233
assert_eq!(
234234
response.into_json(),
235235
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
@@ -286,14 +286,14 @@ mod auth {
286286
);
287287

288288
let response = client.yank(CRATE_NAME, CRATE_VERSION);
289-
assert_eq!(response.status(), StatusCode::OK);
289+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
290290
assert_eq!(
291291
response.into_json(),
292292
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
293293
);
294294

295295
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
296-
assert_eq!(response.status(), StatusCode::OK);
296+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
297297
assert_eq!(
298298
response.into_json(),
299299
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
@@ -311,14 +311,14 @@ mod auth {
311311
);
312312

313313
let response = client.yank(CRATE_NAME, CRATE_VERSION);
314-
assert_eq!(response.status(), StatusCode::OK);
314+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
315315
assert_eq!(
316316
response.into_json(),
317317
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
318318
);
319319

320320
let response = client.unyank(CRATE_NAME, CRATE_VERSION);
321-
assert_eq!(response.status(), StatusCode::OK);
321+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
322322
assert_eq!(
323323
response.into_json(),
324324
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })

src/tests/server.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ fn user_agent_is_required() {
1111

1212
let req = Request::get("/api/v1/crates").body("").unwrap();
1313
let resp = anon.run::<()>(req);
14-
assert_eq!(resp.status(), StatusCode::OK);
14+
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
1515
assert_json_snapshot!(resp.into_json());
1616

1717
let req = Request::get("/api/v1/crates")
1818
.header(header::USER_AGENT, "")
1919
.body("")
2020
.unwrap();
2121
let resp = anon.run::<()>(req);
22-
assert_eq!(resp.status(), StatusCode::OK);
22+
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
2323
assert_json_snapshot!(resp.into_json());
2424
}
2525

@@ -101,6 +101,6 @@ fn block_traffic_via_ip() {
101101
.empty();
102102

103103
let resp = anon.get::<()>("/api/v1/crates");
104-
assert_eq!(resp.status(), StatusCode::OK);
104+
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
105105
assert_json_snapshot!(resp.into_json());
106106
}

src/tests/util/response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<T> Response<T> {
7575
detail: String,
7676
}
7777

78-
assert_eq!(self.status(), StatusCode::OK);
78+
assert_eq!(self.status(), StatusCode::TOO_MANY_REQUESTS);
7979

8080
let expected_message_start = format!("{}. Please try again after ", action.error_message());
8181
let error: ErrorResponse = json(self.response);

src/tests/util/test_app.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,11 @@ fn simple_config() -> config::Server {
427427
version_id_cache_ttl: Duration::from_secs(5 * 60),
428428
cdn_user_agent: "Amazon CloudFront".to_string(),
429429
balance_capacity,
430-
use_cargo_compat_status_codes: true,
430+
431+
// The middleware has its own unit tests to verify its functionality.
432+
// Here, we can test what would happen if we toggled the status code
433+
// enforcement off eventually.
434+
use_cargo_compat_status_codes: false,
431435

432436
// The frontend code is not needed for the backend tests.
433437
serve_dist: false,

0 commit comments

Comments
 (0)