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.

106 lines
3.5 KiB

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