Skip to content

Commit d0e6331

Browse files
0xPoeTurbo87
authored andcommitted
test: add an integration test
Signed-off-by: hi-rustin <[email protected]>
1 parent 74751b5 commit d0e6331

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/worker/jobs/expiry_notification.rs

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

0 commit comments

Comments
 (0)