Skip to content

Commit

Permalink
Refactor stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilouf committed Dec 24, 2024
1 parent 7af38f1 commit 5482542
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lemarche/users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ def setUp(self):
self.frozen_last_year = frozen_now - relativedelta(years=1)
self.frozen_warning_date = self.frozen_last_year + relativedelta(days=7)

self.std_out = StringIO() # to read output from executed management commands

UserFactory(first_name="active_user", last_login=frozen_now)
UserFactory(
last_login=self.frozen_last_year,
Expand Down Expand Up @@ -240,8 +242,7 @@ def test_set_inactive_user(self):
def test_anonymize_command(self):
"""Test the admin command 'anonymize_old_users'"""

out = StringIO()
call_command("anonymize_old_users", stdout=out)
call_command("anonymize_old_users", stdout=self.std_out)

self.assertEqual(User.objects.filter(is_active=False).count(), 2)
# fixme flag anonyme tout ca
Expand All @@ -261,14 +262,13 @@ def test_anonymize_command(self):
# from UNUSABLE_PASSWORD_SUFFIX_LENGTH it should be 40, but we're pretty close
self.assertEqual(len(anonymized_user.password), 37)

self.assertIn("Utilisateurs anonymisés avec succès", out.getvalue())
self.assertIn("Utilisateurs anonymisés avec succès", self.std_out.getvalue())

def test_warn_command(self):
"""Test the admin command 'anonymize_old_users' to check if users are warned by email
before their account is being removed"""

out = StringIO()
call_command("anonymize_old_users", stdout=out)
call_command("anonymize_old_users", stdout=self.std_out)

log_qs = TemplateTransactionalSendLog.objects.all()
self.assertEqual(
Expand All @@ -277,7 +277,7 @@ def test_warn_command(self):
)

# Called twice to veryfi that emails are not sent multiple times
call_command("anonymize_old_users", stdout=out)
call_command("anonymize_old_users", stdout=self.std_out)
log_qs = TemplateTransactionalSendLog.objects.all()
self.assertEqual(
log_qs.count(),
Expand All @@ -293,18 +293,16 @@ def test_dryrun_anonymize_command(self):

original_qs_count = User.objects.filter(is_active=True).count()

out = StringIO()
call_command("anonymize_old_users", dry_run=True, stdout=out)
call_command("anonymize_old_users", dry_run=True, stdout=self.std_out)

self.assertEqual(original_qs_count, User.objects.filter(is_active=True).count())

self.assertIn("Utilisateurs anonymisés avec succès (2 traités)", out.getvalue())
self.assertIn("Utilisateurs anonymisés avec succès (2 traités)", self.std_out.getvalue())

def test_dryrun_warn_command(self):
"""Ensure that the database is not modified after dryrun and no email have been sent"""

out = StringIO()
call_command("anonymize_old_users", dry_run=True, stdout=out)
call_command("anonymize_old_users", dry_run=True, stdout=self.std_out)

self.assertFalse(TemplateTransactionalSendLog.objects.all())

Expand Down

0 comments on commit 5482542

Please sign in to comment.