-
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.
fix: do not send email for expired token
- Loading branch information
1 parent
e6c8e45
commit d0efb44
Showing
2 changed files
with
27 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,7 @@ mod tests { | |
models::token::ApiToken, schema::api_tokens, test_util::test_db_connection, | ||
typosquat::test_util::Faker, util::token::PlainToken, | ||
}; | ||
use diesel::dsl::IntervalDsl; | ||
use diesel::{QueryDsl, SelectableHelper}; | ||
use lettre::Address; | ||
|
||
|
@@ -112,14 +113,13 @@ mod tests { | |
// 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), | ||
api_tokens::expired_at.eq(now.nullable() + 2.days()), | ||
)) | ||
.returning(ApiToken::as_returning()) | ||
.get_result(&mut conn)?; | ||
|
@@ -139,6 +139,25 @@ mod tests { | |
.first::<ApiToken>(&mut conn)?; | ||
assert!(update_token.expiry_notification_at.is_some()); | ||
|
||
// Insert a already expired token. | ||
let token = PlainToken::generate(); | ||
diesel::insert_into(api_tokens::table) | ||
.values(( | ||
api_tokens::user_id.eq(user.id), | ||
api_tokens::name.eq("expired_token"), | ||
api_tokens::token.eq(token.hashed()), | ||
api_tokens::expired_at.eq(diesel::dsl::now.nullable() - 1.day()), | ||
)) | ||
.returning(ApiToken::as_returning()) | ||
.get_result(&mut conn)?; | ||
|
||
// Check that the token is not about to expire. | ||
check(&emails, &mut conn)?; | ||
|
||
// Check that no email was sent. | ||
let sent_mail = emails.mails_in_memory().unwrap(); | ||
assert_eq!(sent_mail.len(), 1); | ||
|
||
Ok(()) | ||
} | ||
} |