You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.6 KiB

  1. from datetime import timedelta as td
  2. import re
  3. from unittest.mock import Mock
  4. from django.core import mail
  5. from django.utils.timezone import now
  6. from hc.accounts.management.commands.senddeletionnotices import Command
  7. from hc.accounts.models import Member
  8. from hc.api.models import Check, Ping
  9. from hc.test import BaseTestCase
  10. def counts(result):
  11. """ Extract integer values from command's return value. """
  12. return [int(s) for s in re.findall(r"\d+", result)]
  13. class SendDeletionNoticesTestCase(BaseTestCase):
  14. def setUp(self):
  15. super().setUp()
  16. # Make alice eligible for notice -- signed up more than 1 year ago
  17. self.alice.date_joined = now() - td(days=500)
  18. self.alice.save()
  19. self.profile.sms_limit = 5
  20. self.profile.save()
  21. # remove members from alice's project
  22. self.project.member_set.all().delete()
  23. def test_it_sends_notice(self):
  24. cmd = Command(stdout=Mock())
  25. cmd.pause = Mock() # don't pause for 1s
  26. result = cmd.handle()
  27. self.assertEqual(counts(result), [1, 0, 0])
  28. self.profile.refresh_from_db()
  29. self.assertTrue(self.profile.deletion_notice_date)
  30. email = mail.outbox[0]
  31. self.assertEqual(email.subject, "Inactive Account Notification")
  32. def test_it_checks_last_login(self):
  33. # alice has logged in recently:
  34. self.alice.last_login = now() - td(days=15)
  35. self.alice.save()
  36. result = Command(stdout=Mock()).handle()
  37. self.assertEqual(counts(result), [0, 0, 0])
  38. self.profile.refresh_from_db()
  39. self.assertIsNone(self.profile.deletion_notice_date)
  40. def test_it_checks_date_joined(self):
  41. # alice signed up recently:
  42. self.alice.date_joined = now() - td(days=15)
  43. self.alice.save()
  44. result = Command(stdout=Mock()).handle()
  45. self.assertEqual(counts(result), [0, 0, 0])
  46. self.profile.refresh_from_db()
  47. self.assertIsNone(self.profile.deletion_notice_date)
  48. def test_it_checks_deletion_notice_date(self):
  49. # alice has already received a deletion notice
  50. self.profile.deletion_notice_date = now() - td(days=15)
  51. self.profile.save()
  52. result = Command(stdout=Mock()).handle()
  53. self.assertEqual(counts(result), [0, 0, 0])
  54. def test_it_checks_sms_limit(self):
  55. # alice has a paid account
  56. self.profile.sms_limit = 50
  57. self.profile.save()
  58. result = Command(stdout=Mock()).handle()
  59. self.assertEqual(counts(result), [0, 0, 0])
  60. self.profile.refresh_from_db()
  61. self.assertIsNone(self.profile.deletion_notice_date)
  62. def test_it_checks_team_members(self):
  63. # bob has access to alice's project
  64. Member.objects.create(user=self.bob, project=self.project)
  65. result = Command(stdout=Mock()).handle()
  66. self.assertEqual(counts(result), [0, 1, 0])
  67. self.profile.refresh_from_db()
  68. self.assertIsNone(self.profile.deletion_notice_date)
  69. def test_it_checks_recent_pings(self):
  70. check = Check.objects.create(project=self.project)
  71. Ping.objects.create(owner=check)
  72. result = Command(stdout=Mock()).handle()
  73. self.assertEqual(counts(result), [0, 0, 1])
  74. self.profile.refresh_from_db()
  75. self.assertIsNone(self.profile.deletion_notice_date)
  76. def test_it_checks_last_active_date(self):
  77. # alice has been browsing the site recently
  78. self.profile.last_active_date = now() - td(days=15)
  79. self.profile.save()
  80. result = Command(stdout=Mock()).handle()
  81. self.assertEqual(counts(result), [0, 0, 0])