@@ -100,6 +100,7 @@ mod tests {
100
100
models:: token:: ApiToken , schema:: api_tokens, test_util:: test_db_connection,
101
101
typosquat:: test_util:: Faker , util:: token:: PlainToken ,
102
102
} ;
103
+ use diesel:: dsl:: IntervalDsl ;
103
104
use diesel:: { QueryDsl , SelectableHelper } ;
104
105
use lettre:: Address ;
105
106
@@ -112,14 +113,13 @@ mod tests {
112
113
// Set up a user and a token that is about to expire.
113
114
let user = faker
. user ( & mut conn
, "a" , Some ( "[email protected] " . to_owned ( ) ) ) ?
;
114
115
let token = PlainToken :: generate ( ) ;
115
- let expired_at = diesel:: dsl:: now;
116
116
117
117
let token: ApiToken = diesel:: insert_into ( api_tokens:: table)
118
118
. values ( (
119
119
api_tokens:: user_id. eq ( user. id ) ,
120
120
api_tokens:: name. eq ( "test_token" ) ,
121
121
api_tokens:: token. eq ( token. hashed ( ) ) ,
122
- api_tokens:: expired_at. eq ( expired_at ) ,
122
+ api_tokens:: expired_at. eq ( now . nullable ( ) + ( EXPIRY_THRESHOLD - 1 ) . day ( ) )
123
123
) )
124
124
. returning ( ApiToken :: as_returning ( ) )
125
125
. get_result ( & mut conn) ?;
@@ -139,6 +139,25 @@ mod tests {
139
139
. first :: < ApiToken > ( & mut conn) ?;
140
140
assert ! ( update_token. expiry_notification_at. is_some( ) ) ;
141
141
142
+ // Insert a already expired token.
143
+ let token = PlainToken :: generate ( ) ;
144
+ diesel:: insert_into ( api_tokens:: table)
145
+ . values ( (
146
+ api_tokens:: user_id. eq ( user. id ) ,
147
+ api_tokens:: name. eq ( "expired_token" ) ,
148
+ api_tokens:: token. eq ( token. hashed ( ) ) ,
149
+ api_tokens:: expired_at. eq ( diesel:: dsl:: now. nullable ( ) - 1 . day ( ) ) ,
150
+ ) )
151
+ . returning ( ApiToken :: as_returning ( ) )
152
+ . get_result ( & mut conn) ?;
153
+
154
+ // Check that the token is not about to expire.
155
+ check ( & emails, & mut conn) ?;
156
+
157
+ // Check that no email was sent.
158
+ let sent_mail = emails. mails_in_memory ( ) . unwrap ( ) ;
159
+ assert_eq ! ( sent_mail. len( ) , 1 ) ;
160
+
142
161
Ok ( ( ) )
143
162
}
144
163
}
0 commit comments