-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: hi-rustin <[email protected]>
- Loading branch information
1 parent
8ead4cf
commit 80d1907
Showing
4 changed files
with
61 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,3 +87,53 @@ impl Email for ExpiryNotificationEmail { | |
) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::{ | ||
models::token::ApiToken, schema::api_tokens, test_util::test_db_connection, | ||
typosquat::test_util::Faker, util::token::PlainToken, | ||
}; | ||
use diesel::{QueryDsl, SelectableHelper}; | ||
use lettre::Address; | ||
|
||
#[tokio::test] | ||
async fn test_expiry_notification() -> anyhow::Result<()> { | ||
let emails = Emails::new_in_memory(); | ||
let (_test_db, mut conn) = test_db_connection(); | ||
let mut faker = Faker::new(); | ||
|
||
// Set up a user and a token that is about to expire. | ||
let user = faker.user(&mut conn, "a", Some("[email protected]".to_owned()))?; | ||
let token = PlainToken::generate(); | ||
let expired_at = diesel::dsl::now; | ||
|
||
let token: ApiToken = diesel::insert_into(api_tokens::table) | ||
.values(( | ||
api_tokens::user_id.eq(user.id), | ||
api_tokens::name.eq("test_token"), | ||
api_tokens::token.eq(token.hashed()), | ||
api_tokens::expired_at.eq(expired_at), | ||
)) | ||
.returning(ApiToken::as_returning()) | ||
.get_result(&mut conn)?; | ||
|
||
// Check that the token is about to expire. | ||
check(&emails, &mut conn)?; | ||
|
||
// Check that an email was sent. | ||
let sent_mail = emails.mails_in_memory().unwrap(); | ||
assert_eq!(sent_mail.len(), 1); | ||
let sent = &sent_mail[0]; | ||
assert_eq!(&sent.0.to(), &["[email protected]".parse::<Address>()?]); | ||
assert!(sent.1.contains("Your token is about to expire")); | ||
let update_token = api_tokens::table | ||
.filter(api_tokens::id.eq(token.id)) | ||
.select(ApiToken::as_select()) | ||
.first::<ApiToken>(&mut conn)?; | ||
assert!(update_token.expiry_notification_at.is_some()); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,14 +130,14 @@ mod tests { | |
let mut faker = Faker::new(); | ||
|
||
// Set up a user and a popular crate to match against. | ||
let user = faker.user(&mut conn, "a")?; | ||
let user = faker.user(&mut conn, "a", None)?; | ||
faker.crate_and_version(&mut conn, "my-crate", "It's awesome", &user, 100)?; | ||
|
||
// Prime the cache so it only includes the crate we just created. | ||
let cache = Cache::new(vec!["[email protected]".to_string()], &mut conn)?; | ||
|
||
// Now we'll create new crates: one problematic, one not so. | ||
let other_user = faker.user(&mut conn, "b")?; | ||
let other_user = faker.user(&mut conn, "b", None)?; | ||
let (angel, _version) = faker.crate_and_version( | ||
&mut conn, | ||
"innocent-crate", | ||
|