Skip to content

Commit 02d6f24

Browse files
committed
chore: tested different failure vectors for email input
1 parent 701495c commit 02d6f24

File tree

5 files changed

+154
-20
lines changed

5 files changed

+154
-20
lines changed

Cargo.lock

Lines changed: 78 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ secrecy = { version = "0.8", features = ["serde"] }
2828
tracing-actix-web = "0.5"
2929
sqlx = { version = "0.5.7", default-features = false, features = ["offline", "runtime-actix-rustls", "macros", "postgres", "uuid", "chrono", "migrate"] }
3030
unicode-segmentation = "1"
31+
validator = "0.16.0"
32+
fake = "2.6.1"
3133

3234
[dev-dependencies]
3335
reqwest = "0.11.18"
3436
once_cell = "1"
37+
claim = "0.5"
3538

3639
[profile.release]
3740
strip = true

src/domain/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ mod subscriber_email;
33
mod subscriber_name;
44

55
pub use new_subscriber::NewSubscriber;
6+
pub use subscriber_email::SubscriberEmail;
67
pub use subscriber_name::SubscriberName;

src/domain/subscriber_email.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,71 @@
1+
use validator::validate_email;
12

3+
#[derive(Debug, PartialEq)]
4+
pub struct SubscriberEmail(String);
5+
6+
impl SubscriberEmail {
7+
pub fn parse(s: String) -> Result<SubscriberEmail, String> {
8+
match validate_email(&s) {
9+
true => Ok(Self(s)),
10+
false => Err(format!("`{}` is not a valid email address.", s)),
11+
}
12+
}
13+
}
14+
15+
impl AsRef<str> for SubscriberEmail {
16+
fn as_ref(&self) -> &str {
17+
&self.0
18+
}
19+
}
20+
21+
#[cfg(test)]
22+
mod tests {
23+
use super::*;
24+
use claim::{assert_err, assert_ok};
25+
use fake::faker::internet::en::SafeEmail;
26+
use fake::Fake;
27+
28+
#[test]
29+
fn empty_string_is_rejected() {
30+
let email = "".to_string();
31+
assert_err!(SubscriberEmail::parse(email));
32+
}
33+
34+
#[test]
35+
fn whitespace_only_string_is_rejected() {
36+
let email = " ".to_string();
37+
assert_err!(SubscriberEmail::parse(email));
38+
}
39+
40+
#[test]
41+
fn email_missing_at_symbol_is_rejected() {
42+
let email = "lawal.gmail.com".to_string();
43+
assert_err!(SubscriberEmail::parse(email));
44+
}
45+
46+
#[test]
47+
fn email_missing_subject_is_rejected() {
48+
let email = "@gmail.com".to_string();
49+
assert_err!(SubscriberEmail::parse(email));
50+
}
51+
52+
#[test]
53+
fn valid_email_is_parsed_successfully() {
54+
let email = "[email protected]".to_string();
55+
assert_ok!(SubscriberEmail::parse(email));
56+
}
57+
58+
#[test]
59+
fn email_with_double_at_symbol_is_rejected() {
60+
let email = "lawal@@gmail.com".to_string();
61+
assert_err!(SubscriberEmail::parse(email));
62+
}
63+
64+
#[test]
65+
fn valid_emails_are_parsed_successfully() {
66+
let email = SafeEmail().fake();
67+
println!("{}", email);
68+
assert_ok!(SubscriberEmail::parse(email));
69+
}
70+
71+
}

tests/health_check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ async fn subscribe_returns_a_400_when_fields_are_present_but_empty() {
172172
.expect("Failed to execute request.");
173173
// Assert
174174
assert_eq!(
175-
200,
175+
400,
176176
response.status().as_u16(),
177177
// Additional customised error message on test failure
178-
"The API did not return a 200 OK when the payload was {}.",
178+
"The API did not return a 400 bad request when the payload was {}.",
179179
error_message
180180
);
181181
}

0 commit comments

Comments
 (0)