Skip to content

Commit 7bf0db5

Browse files
committed
controllers/user/resend: Add basic test suite
1 parent bb7d089 commit 7bf0db5

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/controllers/user/resend.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,47 @@ pub async fn regenerate_token_and_send(
5252
})
5353
.await
5454
}
55+
56+
#[cfg(test)]
57+
mod tests {
58+
use crate::tests::util::{RequestHelper, TestApp};
59+
use http::StatusCode;
60+
use insta::assert_snapshot;
61+
62+
#[tokio::test(flavor = "multi_thread")]
63+
async fn test_no_auth() {
64+
let (app, anon, user) = TestApp::init().with_user();
65+
66+
let url = format!("/api/v1/users/{}/resend", user.as_model().id);
67+
let response = anon.put::<()>(&url, "").await;
68+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
69+
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"this action requires authentication"}]}"#);
70+
71+
assert_eq!(app.emails().len(), 0);
72+
}
73+
74+
#[tokio::test(flavor = "multi_thread")]
75+
async fn test_wrong_user() {
76+
let (app, _anon, user) = TestApp::init().with_user();
77+
let user2 = app.db_new_user("bar");
78+
79+
let url = format!("/api/v1/users/{}/resend", user2.as_model().id);
80+
let response = user.put::<()>(&url, "").await;
81+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
82+
assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"current user does not match requested user"}]}"#);
83+
84+
assert_eq!(app.emails().len(), 0);
85+
}
86+
87+
#[tokio::test(flavor = "multi_thread")]
88+
async fn test_happy_path() {
89+
let (app, _anon, user) = TestApp::init().with_user();
90+
91+
let url = format!("/api/v1/users/{}/resend", user.as_model().id);
92+
let response = user.put::<()>(&url, "").await;
93+
assert_eq!(response.status(), StatusCode::OK);
94+
assert_snapshot!(response.text(), @r###"{"ok":true}"###);
95+
96+
assert_snapshot!(app.emails_snapshot());
97+
}
98+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: src/controllers/user/resend.rs
3+
expression: app.emails_snapshot()
4+
snapshot_kind: text
5+
---
6+
To: foo@example.com
7+
From: crates.io <noreply@crates.io>
8+
Subject: crates.io: Please confirm your email address
9+
Content-Type: text/plain; charset=utf-8
10+
Content-Transfer-Encoding: 7bit
11+
12+
Hello foo! Welcome to crates.io. Please click the
13+
link below to verify your email address. Thank you!
14+
15+
https://crates.io/confirm/[confirm-token]

0 commit comments

Comments
 (0)