1
1
use crate :: models:: ApiToken ;
2
+ use crate :: schema:: api_tokens;
2
3
use crate :: { email:: Email , models:: User , worker:: Environment , Emails } ;
3
4
use anyhow:: anyhow;
4
5
use chrono:: SecondsFormat ;
@@ -57,10 +58,7 @@ fn check(emails: &Emails, conn: &mut PgConnection) -> anyhow::Result<()> {
57
58
Ok ( _) => {
58
59
// Update the token to prevent duplicate notifications.
59
60
diesel:: update ( token)
60
- . set (
61
- crate :: schema:: api_tokens:: expiry_notification_at
62
- . eq ( now. nullable ( ) ) ,
63
- )
61
+ . set ( api_tokens:: expiry_notification_at. eq ( now. nullable ( ) ) )
64
62
. execute ( conn) ?;
65
63
}
66
64
Err ( e) => {
@@ -110,6 +108,7 @@ mod tests {
110
108
models:: token:: ApiToken , schema:: api_tokens, test_util:: test_db_connection,
111
109
util:: token:: PlainToken ,
112
110
} ;
111
+ use diesel:: dsl:: IntervalDsl ;
113
112
use diesel:: { QueryDsl , SelectableHelper } ;
114
113
use lettre:: Address ;
115
114
@@ -125,14 +124,13 @@ mod tests {
125
124
& mut conn,
126
125
) ?;
127
126
let token = PlainToken :: generate ( ) ;
128
- let expired_at = diesel:: dsl:: now;
129
127
130
128
let token: ApiToken = diesel:: insert_into ( api_tokens:: table)
131
129
. values ( (
132
130
api_tokens:: user_id. eq ( user. id ) ,
133
131
api_tokens:: name. eq ( "test_token" ) ,
134
132
api_tokens:: token. eq ( token. hashed ( ) ) ,
135
- api_tokens:: expired_at. eq ( expired_at ) ,
133
+ api_tokens:: expired_at. eq ( now . nullable ( ) + ( EXPIRY_THRESHOLD - 1 ) . day ( ) ) ,
136
134
) )
137
135
. returning ( ApiToken :: as_returning ( ) )
138
136
. get_result ( & mut conn) ?;
@@ -152,6 +150,25 @@ mod tests {
152
150
. first :: < ApiToken > ( & mut conn) ?;
153
151
assert ! ( update_token. expiry_notification_at. is_some( ) ) ;
154
152
153
+ // Insert a already expired token.
154
+ let token = PlainToken :: generate ( ) ;
155
+ diesel:: insert_into ( api_tokens:: table)
156
+ . values ( (
157
+ api_tokens:: user_id. eq ( user. id ) ,
158
+ api_tokens:: name. eq ( "expired_token" ) ,
159
+ api_tokens:: token. eq ( token. hashed ( ) ) ,
160
+ api_tokens:: expired_at. eq ( diesel:: dsl:: now. nullable ( ) - 1 . day ( ) ) ,
161
+ ) )
162
+ . returning ( ApiToken :: as_returning ( ) )
163
+ . get_result ( & mut conn) ?;
164
+
165
+ // Check that the token is not about to expire.
166
+ check ( & emails, & mut conn) ?;
167
+
168
+ // Check that no email was sent.
169
+ let sent_mail = emails. mails_in_memory ( ) . unwrap ( ) ;
170
+ assert_eq ! ( sent_mail. len( ) , 1 ) ;
171
+
155
172
Ok ( ( ) )
156
173
}
157
174
}
0 commit comments