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.

148 lines
5.0 KiB

9 years ago
  1. from datetime import timedelta as td
  2. from django.core import mail
  3. from django.test.utils import override_settings
  4. from django.utils.timezone import now
  5. from hc.test import BaseTestCase
  6. from hc.accounts.models import Credential
  7. from hc.api.models import Check
  8. class ProfileTestCase(BaseTestCase):
  9. def test_it_shows_profile_page(self):
  10. self.client.login(username="[email protected]", password="password")
  11. r = self.client.get("/accounts/profile/")
  12. self.assertContains(r, "Email and Password")
  13. def test_it_sends_report(self):
  14. check = Check(project=self.project, name="Test Check")
  15. check.last_ping = now()
  16. check.save()
  17. sent = self.profile.send_report()
  18. self.assertTrue(sent)
  19. # And an email should have been sent
  20. self.assertEqual(len(mail.outbox), 1)
  21. message = mail.outbox[0]
  22. self.assertEqual(message.subject, "Monthly Report")
  23. self.assertIn("Test Check", message.body)
  24. def test_it_skips_report_if_no_pings(self):
  25. check = Check(project=self.project, name="Test Check")
  26. check.save()
  27. sent = self.profile.send_report()
  28. self.assertFalse(sent)
  29. self.assertEqual(len(mail.outbox), 0)
  30. def test_it_skips_report_if_no_recent_pings(self):
  31. check = Check(project=self.project, name="Test Check")
  32. check.last_ping = now() - td(days=365)
  33. check.save()
  34. sent = self.profile.send_report()
  35. self.assertFalse(sent)
  36. self.assertEqual(len(mail.outbox), 0)
  37. def test_it_sends_nag(self):
  38. check = Check(project=self.project, name="Test Check")
  39. check.status = "down"
  40. check.last_ping = now()
  41. check.save()
  42. self.profile.nag_period = td(hours=1)
  43. self.profile.save()
  44. sent = self.profile.send_report(nag=True)
  45. self.assertTrue(sent)
  46. # And an email should have been sent
  47. self.assertEqual(len(mail.outbox), 1)
  48. message = mail.outbox[0]
  49. self.assertEqual(message.subject, "Reminder: 1 check still down")
  50. self.assertIn("Test Check", message.body)
  51. def test_it_skips_nag_if_none_down(self):
  52. check = Check(project=self.project, name="Test Check")
  53. check.last_ping = now()
  54. check.save()
  55. self.profile.nag_period = td(hours=1)
  56. self.profile.save()
  57. sent = self.profile.send_report(nag=True)
  58. self.assertFalse(sent)
  59. self.assertEqual(len(mail.outbox), 0)
  60. def test_leaving_works(self):
  61. self.client.login(username="[email protected]", password="password")
  62. form = {"code": str(self.project.code), "leave_project": "1"}
  63. r = self.client.post("/accounts/profile/", form)
  64. self.assertContains(r, "Left project <strong>Alices Project</strong>")
  65. self.assertNotContains(r, "Member")
  66. self.bobs_profile.refresh_from_db()
  67. self.assertFalse(self.bob.memberships.exists())
  68. def test_leaving_checks_membership(self):
  69. self.client.login(username="[email protected]", password="password")
  70. form = {"code": str(self.project.code), "leave_project": "1"}
  71. r = self.client.post("/accounts/profile/", form)
  72. self.assertEqual(r.status_code, 400)
  73. def test_it_shows_project_membership(self):
  74. self.client.login(username="[email protected]", password="password")
  75. r = self.client.get("/accounts/profile/")
  76. self.assertContains(r, "Alices Project")
  77. self.assertContains(r, "Member")
  78. def test_it_shows_readonly_project_membership(self):
  79. self.bobs_membership.rw = False
  80. self.bobs_membership.save()
  81. self.client.login(username="[email protected]", password="password")
  82. r = self.client.get("/accounts/profile/")
  83. self.assertContains(r, "Alices Project")
  84. self.assertContains(r, "Read-only")
  85. def test_it_handles_no_projects(self):
  86. self.project.delete()
  87. self.client.login(username="[email protected]", password="password")
  88. r = self.client.get("/accounts/profile/")
  89. self.assertContains(r, "You do not have any projects. Create one!")
  90. @override_settings(RP_ID=None)
  91. def test_it_hides_2fa_section_if_rp_id_not_set(self):
  92. self.client.login(username="[email protected]", password="password")
  93. r = self.client.get("/accounts/profile/")
  94. self.assertNotContains(r, "Two-factor Authentication")
  95. @override_settings(RP_ID="testserver")
  96. def test_it_handles_no_credentials(self):
  97. self.client.login(username="[email protected]", password="password")
  98. r = self.client.get("/accounts/profile/")
  99. self.assertContains(r, "Two-factor Authentication")
  100. self.assertContains(r, "Your account has no registered security keys")
  101. @override_settings(RP_ID="testserver")
  102. def test_it_shows_security_key(self):
  103. Credential.objects.create(user=self.alice, name="Alices Key")
  104. self.client.login(username="[email protected]", password="password")
  105. r = self.client.get("/accounts/profile/")
  106. self.assertContains(r, "Alices Key")