Skip to content

Commit 80d1907

Browse files
committed
test: add an integration test
Signed-off-by: hi-rustin <[email protected]>
1 parent 8ead4cf commit 80d1907

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

src/typosquat/database.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ mod tests {
174174
let mut faker = Faker::new();
175175

176176
// Set up two users.
177-
let user_a = faker.user(&mut conn, "a")?;
178-
let user_b = faker.user(&mut conn, "b")?;
177+
let user_a = faker.user(&mut conn, "a", None)?;
178+
let user_b = faker.user(&mut conn, "b", None)?;
179179

180180
// Set up three crates with various ownership schemes.
181181
let _top_a = faker.crate_and_version(&mut conn, "a", "Hello", &user_a, 2)?;

src/typosquat/test_util.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,15 @@ impl Faker {
101101
))
102102
}
103103

104-
pub fn user(&mut self, conn: &mut PgConnection, login: &str) -> anyhow::Result<User> {
104+
pub fn user(
105+
&mut self,
106+
conn: &mut PgConnection,
107+
login: &str,
108+
email: Option<String>,
109+
) -> anyhow::Result<User> {
105110
Ok(
106111
NewUser::new(self.next_id(), login, None, None, "token").create_or_update(
107-
None,
112+
email.as_deref(),
108113
&self.emails,
109114
conn,
110115
)?,

src/worker/jobs/expiry_notification.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,53 @@ impl Email for ExpiryNotificationEmail {
8787
)
8888
}
8989
}
90+
91+
#[cfg(test)]
92+
mod tests {
93+
use super::*;
94+
use crate::{
95+
models::token::ApiToken, schema::api_tokens, test_util::test_db_connection,
96+
typosquat::test_util::Faker, util::token::PlainToken,
97+
};
98+
use diesel::{QueryDsl, SelectableHelper};
99+
use lettre::Address;
100+
101+
#[tokio::test]
102+
async fn test_expiry_notification() -> anyhow::Result<()> {
103+
let emails = Emails::new_in_memory();
104+
let (_test_db, mut conn) = test_db_connection();
105+
let mut faker = Faker::new();
106+
107+
// Set up a user and a token that is about to expire.
108+
let user = faker.user(&mut conn, "a", Some("[email protected]".to_owned()))?;
109+
let token = PlainToken::generate();
110+
let expired_at = diesel::dsl::now;
111+
112+
let token: ApiToken = diesel::insert_into(api_tokens::table)
113+
.values((
114+
api_tokens::user_id.eq(user.id),
115+
api_tokens::name.eq("test_token"),
116+
api_tokens::token.eq(token.hashed()),
117+
api_tokens::expired_at.eq(expired_at),
118+
))
119+
.returning(ApiToken::as_returning())
120+
.get_result(&mut conn)?;
121+
122+
// Check that the token is about to expire.
123+
check(&emails, &mut conn)?;
124+
125+
// Check that an email was sent.
126+
let sent_mail = emails.mails_in_memory().unwrap();
127+
assert_eq!(sent_mail.len(), 1);
128+
let sent = &sent_mail[0];
129+
assert_eq!(&sent.0.to(), &["[email protected]".parse::<Address>()?]);
130+
assert!(sent.1.contains("Your token is about to expire"));
131+
let update_token = api_tokens::table
132+
.filter(api_tokens::id.eq(token.id))
133+
.select(ApiToken::as_select())
134+
.first::<ApiToken>(&mut conn)?;
135+
assert!(update_token.expiry_notification_at.is_some());
136+
137+
Ok(())
138+
}
139+
}

src/worker/jobs/typosquat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,14 @@ mod tests {
130130
let mut faker = Faker::new();
131131

132132
// Set up a user and a popular crate to match against.
133-
let user = faker.user(&mut conn, "a")?;
133+
let user = faker.user(&mut conn, "a", None)?;
134134
faker.crate_and_version(&mut conn, "my-crate", "It's awesome", &user, 100)?;
135135

136136
// Prime the cache so it only includes the crate we just created.
137137
let cache = Cache::new(vec!["[email protected]".to_string()], &mut conn)?;
138138

139139
// Now we'll create new crates: one problematic, one not so.
140-
let other_user = faker.user(&mut conn, "b")?;
140+
let other_user = faker.user(&mut conn, "b", None)?;
141141
let (angel, _version) = faker.crate_and_version(
142142
&mut conn,
143143
"innocent-crate",

0 commit comments

Comments
 (0)