Skip to content

Commit 57e3bb4

Browse files
authored
Merge pull request #9987 from Turbo87/async-test-app
Convert `TestApp` fns to async/await
2 parents 3a7ad5c + 8327b3d commit 57e3bb4

Some content is hidden

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

86 files changed

+600
-517
lines changed

src/controllers/user/resend.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ mod tests {
6464

6565
#[tokio::test(flavor = "multi_thread")]
6666
async fn test_no_auth() {
67-
let (app, anon, user) = TestApp::init().with_user();
67+
let (app, anon, user) = TestApp::init().with_user().await;
6868

6969
let url = format!("/api/v1/users/{}/resend", user.as_model().id);
7070
let response = anon.put::<()>(&url, "").await;
@@ -76,8 +76,8 @@ mod tests {
7676

7777
#[tokio::test(flavor = "multi_thread")]
7878
async fn test_wrong_user() {
79-
let (app, _anon, user) = TestApp::init().with_user();
80-
let user2 = app.db_new_user("bar");
79+
let (app, _anon, user) = TestApp::init().with_user().await;
80+
let user2 = app.db_new_user("bar").await;
8181

8282
let url = format!("/api/v1/users/{}/resend", user2.as_model().id);
8383
let response = user.put::<()>(&url, "").await;
@@ -89,7 +89,7 @@ mod tests {
8989

9090
#[tokio::test(flavor = "multi_thread")]
9191
async fn test_happy_path() {
92-
let (app, _anon, user) = TestApp::init().with_user();
92+
let (app, _anon, user) = TestApp::init().with_user().await;
9393

9494
let url = format!("/api/v1/users/{}/resend", user.as_model().id);
9595
let response = user.put::<()>(&url, "").await;

src/tests/account_lock.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async fn lock_account(app: &TestApp, user_id: i32, until: Option<NaiveDateTime>)
2626

2727
#[tokio::test(flavor = "multi_thread")]
2828
async fn account_locked_indefinitely() {
29-
let (app, _anon, user) = TestApp::init().with_user();
29+
let (app, _anon, user) = TestApp::init().with_user().await;
3030
lock_account(&app, user.as_model().id, None).await;
3131

3232
let response = user.get::<()>(URL).await;
@@ -41,7 +41,7 @@ async fn account_locked_with_future_expiry() {
4141
.unwrap()
4242
.naive_utc();
4343

44-
let (app, _anon, user) = TestApp::init().with_user();
44+
let (app, _anon, user) = TestApp::init().with_user().await;
4545
lock_account(&app, user.as_model().id, Some(until)).await;
4646

4747
let response = user.get::<()>(URL).await;
@@ -53,7 +53,7 @@ async fn account_locked_with_future_expiry() {
5353
async fn expired_account_lock() {
5454
let until = Utc::now().naive_utc() - Duration::days(1);
5555

56-
let (app, _anon, user) = TestApp::init().with_user();
56+
let (app, _anon, user) = TestApp::init().with_user().await;
5757
lock_account(&app, user.as_model().id, Some(until)).await;
5858

5959
user.get::<serde_json::Value>(URL).await.good();

src/tests/authentication.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ static URL: &str = "/api/v1/me/updates";
99

1010
#[tokio::test(flavor = "multi_thread")]
1111
async fn anonymous_user_unauthorized() {
12-
let (_, anon) = TestApp::init().empty();
12+
let (_, anon) = TestApp::init().empty().await;
1313
let response: Response<()> = anon.get(URL).await;
1414

1515
assert_eq!(response.status(), StatusCode::FORBIDDEN);
@@ -18,7 +18,7 @@ async fn anonymous_user_unauthorized() {
1818

1919
#[tokio::test(flavor = "multi_thread")]
2020
async fn token_auth_cannot_find_token() {
21-
let (_, anon) = TestApp::init().empty();
21+
let (_, anon) = TestApp::init().empty().await;
2222
let mut request = anon.request_builder(Method::GET, URL);
2323
request.header(header::AUTHORIZATION, "cio1tkfake-token");
2424
let response: Response<()> = anon.run(request).await;
@@ -32,7 +32,7 @@ async fn token_auth_cannot_find_token() {
3232
// the database, it is not possible to implement this same test for a token.
3333
#[tokio::test(flavor = "multi_thread")]
3434
async fn cookie_auth_cannot_find_user() {
35-
let (app, anon) = TestApp::init().empty();
35+
let (app, anon) = TestApp::init().empty().await;
3636

3737
let session_key = app.as_inner().session_key();
3838
let cookie = encode_session_header(session_key, -1);

src/tests/blocked_routes.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ async fn test_non_blocked_download_route() {
88
.with_config(|config| {
99
config.blocked_routes.clear();
1010
})
11-
.with_user();
11+
.with_user()
12+
.await;
1213

1314
let mut conn = app.db_conn();
1415

@@ -32,7 +33,8 @@ async fn test_blocked_download_route() {
3233
.blocked_routes
3334
.insert("/api/v1/crates/:crate_id/:version/download".into());
3435
})
35-
.with_user();
36+
.with_user()
37+
.await;
3638

3739
let mut conn = app.db_conn();
3840

src/tests/cors.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ async fn test_with_matching_origin() {
99
.with_config(|server| {
1010
server.allowed_origins = "https://crates.io".parse().unwrap();
1111
})
12-
.with_user();
12+
.with_user()
13+
.await;
1314

1415
let mut request = cookie.get_request("/api/v1/me");
1516
request.header("Origin", "https://crates.io");
@@ -24,7 +25,8 @@ async fn test_with_unknown_origin() {
2425
.with_config(|server| {
2526
server.allowed_origins = "https://crates.io".parse().unwrap();
2627
})
27-
.with_user();
28+
.with_user()
29+
.await;
2830

2931
let mut request = cookie.get_request("/api/v1/me");
3032
request.header("Origin", "https://evil.hacker.io");
@@ -40,7 +42,8 @@ async fn test_with_multiple_origins() {
4042
.with_config(|server| {
4143
server.allowed_origins = "https://crates.io".parse().unwrap();
4244
})
43-
.with_user();
45+
.with_user()
46+
.await;
4447

4548
let mut request = cookie.get_request("/api/v1/me");
4649
request.header("Origin", "https://evil.hacker.io");

src/tests/dump_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static PATH_DATE_RE: LazyLock<Regex> =
1515

1616
#[tokio::test(flavor = "multi_thread")]
1717
async fn test_dump_db_job() {
18-
let (app, _, _, token) = TestApp::full().with_token();
18+
let (app, _, _, token) = TestApp::full().with_token().await;
1919
let mut conn = app.db_conn();
2020

2121
CrateBuilder::new("test-crate", token.as_model().user_id).expect_build(&mut conn);

src/tests/github_secret_scanning.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ fn github_mock() -> MockGitHubClient {
4141

4242
#[tokio::test(flavor = "multi_thread")]
4343
async fn github_secret_alert_revokes_token() {
44-
let (app, anon, user, token) = TestApp::init().with_github(github_mock()).with_token();
44+
let (app, anon, user, token) = TestApp::init()
45+
.with_github(github_mock())
46+
.with_token()
47+
.await;
4548
let mut conn = app.async_db_conn().await;
4649

4750
// Ensure no emails were sent up to this point
@@ -99,7 +102,10 @@ async fn github_secret_alert_revokes_token() {
99102

100103
#[tokio::test(flavor = "multi_thread")]
101104
async fn github_secret_alert_for_revoked_token() {
102-
let (app, anon, user, token) = TestApp::init().with_github(github_mock()).with_token();
105+
let (app, anon, user, token) = TestApp::init()
106+
.with_github(github_mock())
107+
.with_token()
108+
.await;
103109
let mut conn = app.async_db_conn().await;
104110

105111
// Ensure no emails were sent up to this point
@@ -160,7 +166,10 @@ async fn github_secret_alert_for_revoked_token() {
160166

161167
#[tokio::test(flavor = "multi_thread")]
162168
async fn github_secret_alert_for_unknown_token() {
163-
let (app, anon, user, token) = TestApp::init().with_github(github_mock()).with_token();
169+
let (app, anon, user, token) = TestApp::init()
170+
.with_github(github_mock())
171+
.with_token()
172+
.await;
164173
let mut conn = app.async_db_conn().await;
165174

166175
// Ensure no emails were sent up to this point
@@ -202,7 +211,7 @@ async fn github_secret_alert_for_unknown_token() {
202211

203212
#[tokio::test(flavor = "multi_thread")]
204213
async fn github_secret_alert_invalid_signature_fails() {
205-
let (_, anon) = TestApp::init().with_github(github_mock()).empty();
214+
let (_, anon) = TestApp::init().with_github(github_mock()).empty().await;
206215

207216
// No headers or request body
208217
let request = anon.post_request(URL);

src/tests/issues/issue1205.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use insta::assert_snapshot;
99
async fn test_issue_1205() -> anyhow::Result<()> {
1010
const CRATE_NAME: &str = "deepspeech-sys";
1111

12-
let (app, _, _, user) = TestApp::full().with_github(github_mock()).with_token();
12+
let (app, _, _, user) = TestApp::full()
13+
.with_github(github_mock())
14+
.with_token()
15+
.await;
1316

1417
let mut conn = app.db_conn();
1518

src/tests/issues/issue2736.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use insta::assert_snapshot;
99
/// See <https://github.com/rust-lang/crates.io/issues/2736>.
1010
#[tokio::test(flavor = "multi_thread")]
1111
async fn test_issue_2736() -> anyhow::Result<()> {
12-
let (app, _) = TestApp::full().empty();
12+
let (app, _) = TestApp::full().empty().await;
1313
let mut conn = app.db_conn();
1414

1515
// - A user had a GitHub account named, let's say, `foo`
16-
let foo1 = app.db_new_user("foo");
16+
let foo1 = app.db_new_user("foo").await;
1717

1818
// - Another user `someone_else` added them as an owner of a crate
19-
let someone_else = app.db_new_user("someone_else");
19+
let someone_else = app.db_new_user("someone_else").await;
2020

2121
let krate = CrateBuilder::new("crate1", someone_else.as_model().id).expect_build(&mut conn);
2222

@@ -33,7 +33,7 @@ async fn test_issue_2736() -> anyhow::Result<()> {
3333
// - `foo` deleted their GitHub account (but crates.io has no real knowledge of this)
3434
// - `foo` recreated their GitHub account with the same username (because it was still available), but in this situation GitHub assigns them a new ID
3535
// - When `foo` now logs in to crates.io, it's a different account than their old `foo` crates.io account because of the new GitHub ID (and if it wasn't, this would be a security problem)
36-
let foo2 = app.db_new_user("foo");
36+
let foo2 = app.db_new_user("foo").await;
3737

3838
let github_ids = users::table
3939
.filter(users::gh_login.eq("foo"))

src/tests/krate/following.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async fn unfollow(crate_name: &str, user: &impl RequestHelper) {
3333
async fn test_unauthenticated_requests() {
3434
const CRATE_NAME: &str = "foo";
3535

36-
let (app, anon, user) = TestApp::init().with_user();
36+
let (app, anon, user) = TestApp::init().with_user().await;
3737
let mut conn = app.db_conn();
3838

3939
CrateBuilder::new(CRATE_NAME, user.as_model().id).expect_build(&mut conn);
@@ -61,7 +61,7 @@ async fn test_unauthenticated_requests() {
6161
async fn test_following() {
6262
const CRATE_NAME: &str = "foo_following";
6363

64-
let (app, _, user) = TestApp::init().with_user();
64+
let (app, _, user) = TestApp::init().with_user().await;
6565
let mut conn = app.db_conn();
6666

6767
CrateBuilder::new(CRATE_NAME, user.as_model().id).expect_build(&mut conn);
@@ -91,7 +91,7 @@ async fn test_following() {
9191

9292
#[tokio::test(flavor = "multi_thread")]
9393
async fn test_unknown_crate() {
94-
let (_, _, user) = TestApp::init().with_user();
94+
let (_, _, user) = TestApp::init().with_user().await;
9595

9696
let response = user
9797
.get::<()>("/api/v1/crates/unknown-crate/following")
@@ -117,7 +117,7 @@ async fn test_api_token_auth() {
117117
const CRATE_TO_FOLLOW: &str = "some_crate_to_follow";
118118
const CRATE_NOT_TO_FOLLOW: &str = "another_crate";
119119

120-
let (app, _, user, token) = TestApp::init().with_token();
120+
let (app, _, user, token) = TestApp::init().with_token().await;
121121
let mut conn = app.db_conn();
122122
let api_token = token.as_model();
123123

0 commit comments

Comments
 (0)