Skip to content

Commit

Permalink
[req-changes] Fixed the sleep time period
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed Feb 13, 2024
1 parent 0738531 commit 011e882
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 3 additions & 3 deletions openwisp_users/tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import random
from time import sleep

from celery import shared_task
Expand Down Expand Up @@ -77,8 +78,7 @@ def password_expiration_email():
)
# Avoid overloading the SMTP server by sending multiple
# emails continuously.
email_counts += 1
if email_counts > 10:
email_counts = 0
sleep(10)
else:
email_counts += 1
sleep(random.randint(1, 2))
6 changes: 4 additions & 2 deletions openwisp_users/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,14 @@ def test_password_expiration_mail_sleep(self, mocked_sleep):
user_expiry_date = now().today() - timedelta(
days=(app_settings.USER_PASSWORD_EXPIRATION - 7)
)
for i in range(12):
for i in range(11):
self._create_user(username=f'user{i}', email=f'user{i}@example.com')
EmailAddress.objects.update(verified=True)
User.objects.update(password_updated=user_expiry_date)
password_expiration_email.delay()
mocked_sleep.assert_called_with(10)
mocked_sleep.assert_called()
self.assertGreaterEqual(mocked_sleep.call_args[0][0], 1)
self.assertLessEqual(mocked_sleep.call_args[0][0], 2)

@patch.object(app_settings, 'USER_PASSWORD_EXPIRATION', 0)
@patch.object(app_settings, 'STAFF_USER_PASSWORD_EXPIRATION', 0)
Expand Down

0 comments on commit 011e882

Please sign in to comment.