|
| 1 | +from datetime import datetime, timedelta, timezone |
1 | 2 | from http import HTTPStatus |
2 | 3 | from unittest import TestCase |
3 | 4 | from urllib.parse import urlparse |
@@ -203,6 +204,49 @@ def test_user_can_get_a_confirmation_token_resent(client, session): |
203 | 204 | ) |
204 | 205 |
|
205 | 206 |
|
| 207 | +@pytest.mark.parametrize( |
| 208 | + "last_confirmation_sent_at,is_rate_limited", |
| 209 | + [ |
| 210 | + (None, False), |
| 211 | + (datetime.now(timezone.utc) - timedelta(hours=2), False), |
| 212 | + (datetime.now(timezone.utc) - timedelta(minutes=10), True), |
| 213 | + ], |
| 214 | +) |
| 215 | +def test_user_rate_limited_if_resending_confirmation_too_soon( |
| 216 | + client, session, last_confirmation_sent_at, is_rate_limited |
| 217 | +): |
| 218 | + # Should only send email if not rate limited |
| 219 | + log_capture = TestCase.assertNoLogs if is_rate_limited else TestCase.assertLogs |
| 220 | + |
| 221 | + with ( |
| 222 | + current_app.test_request_context(), |
| 223 | + log_capture(current_app.logger) as log, |
| 224 | + ): |
| 225 | + _, user = login_user(client) |
| 226 | + user.last_confirmation_sent_at = last_confirmation_sent_at |
| 227 | + session.commit() |
| 228 | + |
| 229 | + rv = client.get(url_for("auth.resend_confirmation"), follow_redirects=True) |
| 230 | + |
| 231 | + if not is_rate_limited: |
| 232 | + assert b"A new confirmation email has been sent to you." in rv.data |
| 233 | + assert ( |
| 234 | + f"{current_app.config[KEY_OO_MAIL_SUBJECT_PREFIX]} Confirm Your Account" |
| 235 | + in str(log.output) |
| 236 | + ) |
| 237 | + |
| 238 | + # check that confirmation time was updated |
| 239 | + assert user.last_confirmation_sent_at > datetime.now( |
| 240 | + timezone.utc |
| 241 | + ) - timedelta(seconds=1) |
| 242 | + else: |
| 243 | + assert ( |
| 244 | + b"We already sent a confirmation email to you recently. Please try again later." |
| 245 | + in rv.data |
| 246 | + ) |
| 247 | + assert user.last_confirmation_sent_at == last_confirmation_sent_at |
| 248 | + |
| 249 | + |
206 | 250 | def test_user_can_get_password_reset_token_sent(client, session): |
207 | 251 | with ( |
208 | 252 | current_app.test_request_context(), |
@@ -247,6 +291,49 @@ def test_user_can_get_password_reset_token_sent_with_differently_cased_email( |
247 | 291 | ) |
248 | 292 |
|
249 | 293 |
|
| 294 | +@pytest.mark.parametrize( |
| 295 | + "last_reset_sent_at,is_rate_limited", |
| 296 | + [ |
| 297 | + (None, False), |
| 298 | + (datetime.now(timezone.utc) - timedelta(hours=2), False), |
| 299 | + (datetime.now(timezone.utc) - timedelta(minutes=10), True), |
| 300 | + ], |
| 301 | +) |
| 302 | +def test_user_rate_limited_if_resending_reset_too_soon( |
| 303 | + client, session, last_reset_sent_at, is_rate_limited |
| 304 | +): |
| 305 | + with ( |
| 306 | + current_app.test_request_context(), |
| 307 | + TestCase.assertLogs(current_app.logger) as log, |
| 308 | + ): |
| 309 | + user = User.query.filter_by(is_administrator=True).first() |
| 310 | + user.last_reset_sent_at = last_reset_sent_at |
| 311 | + session.commit() |
| 312 | + |
| 313 | + form = PasswordResetRequestForm(email=user.email) |
| 314 | + rv = client.post( |
| 315 | + url_for("auth.password_reset_request"), |
| 316 | + data=form.data, |
| 317 | + follow_redirects=True, |
| 318 | + ) |
| 319 | + |
| 320 | + assert b"An email with instructions to reset your password" in rv.data |
| 321 | + if not is_rate_limited: |
| 322 | + assert ( |
| 323 | + f"{current_app.config[KEY_OO_MAIL_SUBJECT_PREFIX]} Reset Your Password" |
| 324 | + in str(log.output) |
| 325 | + ) |
| 326 | + assert user.last_reset_sent_at > datetime.now(timezone.utc) - timedelta( |
| 327 | + seconds=1 |
| 328 | + ) |
| 329 | + else: |
| 330 | + assert ( |
| 331 | + f"{current_app.config[KEY_OO_MAIL_SUBJECT_PREFIX]} Reset Your Password" |
| 332 | + not in str(log.output) |
| 333 | + ) |
| 334 | + assert user.last_reset_sent_at == last_reset_sent_at |
| 335 | + |
| 336 | + |
250 | 337 | def test_user_can_get_reset_password_with_valid_token(client, session): |
251 | 338 | with current_app.test_request_context(): |
252 | 339 | user = User.query.filter_by(is_administrator=True).first() |
|
0 commit comments