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.

198 lines
6.6 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. from datetime import timedelta as td
  2. from django.core import mail
  3. from django.conf import settings
  4. from django.utils.timezone import now
  5. from hc.test import BaseTestCase
  6. from hc.accounts.models import Member
  7. from hc.api.models import Check
  8. class ProfileTestCase(BaseTestCase):
  9. def test_it_sends_set_password_link(self):
  10. self.client.login(username="[email protected]", password="password")
  11. form = {"set_password": "1"}
  12. r = self.client.post("/accounts/profile/", form)
  13. assert r.status_code == 302
  14. # profile.token should be set now
  15. self.profile.refresh_from_db()
  16. token = self.profile.token
  17. self.assertTrue(len(token) > 10)
  18. # And an email should have been sent
  19. self.assertEqual(len(mail.outbox), 1)
  20. expected_subject = "Set password on %s" % settings.SITE_NAME
  21. self.assertEqual(mail.outbox[0].subject, expected_subject)
  22. def test_it_creates_api_key(self):
  23. self.client.login(username="[email protected]", password="password")
  24. form = {"create_api_keys": "1"}
  25. r = self.client.post("/accounts/profile/", form)
  26. self.assertEqual(r.status_code, 200)
  27. self.profile.refresh_from_db()
  28. api_key = self.profile.api_key
  29. self.assertTrue(len(api_key) > 10)
  30. self.assertFalse("b'" in api_key)
  31. def test_it_revokes_api_key(self):
  32. self.profile.api_key_readonly = "R" * 32
  33. self.profile.save()
  34. self.client.login(username="[email protected]", password="password")
  35. form = {"revoke_api_keys": "1"}
  36. r = self.client.post("/accounts/profile/", form)
  37. assert r.status_code == 200
  38. self.profile.refresh_from_db()
  39. self.assertEqual(self.profile.api_key, "")
  40. self.assertEqual(self.profile.api_key_readonly, "")
  41. def test_it_sends_report(self):
  42. check = Check(name="Test Check", user=self.alice)
  43. check.last_ping = now()
  44. check.save()
  45. sent = self.profile.send_report()
  46. self.assertTrue(sent)
  47. # And an email should have been sent
  48. self.assertEqual(len(mail.outbox), 1)
  49. message = mail.outbox[0]
  50. self.assertEqual(message.subject, 'Monthly Report')
  51. self.assertIn("Test Check", message.body)
  52. def test_it_skips_report_if_no_pings(self):
  53. check = Check(name="Test Check", user=self.alice)
  54. check.save()
  55. sent = self.profile.send_report()
  56. self.assertFalse(sent)
  57. self.assertEqual(len(mail.outbox), 0)
  58. def test_it_skips_report_if_no_recent_pings(self):
  59. check = Check(name="Test Check", user=self.alice)
  60. check.last_ping = now() - td(days=365)
  61. check.save()
  62. sent = self.profile.send_report()
  63. self.assertFalse(sent)
  64. self.assertEqual(len(mail.outbox), 0)
  65. def test_it_sends_nag(self):
  66. check = Check(name="Test Check", user=self.alice)
  67. check.status = "down"
  68. check.last_ping = now()
  69. check.save()
  70. self.profile.nag_period = td(hours=1)
  71. self.profile.save()
  72. sent = self.profile.send_report(nag=True)
  73. self.assertTrue(sent)
  74. # And an email should have been sent
  75. self.assertEqual(len(mail.outbox), 1)
  76. message = mail.outbox[0]
  77. self.assertEqual(message.subject, 'Reminder: 1 check still down')
  78. self.assertIn("Test Check", message.body)
  79. def test_it_skips_nag_if_none_down(self):
  80. check = Check(name="Test Check", user=self.alice)
  81. check.last_ping = now()
  82. check.save()
  83. self.profile.nag_period = td(hours=1)
  84. self.profile.save()
  85. sent = self.profile.send_report(nag=True)
  86. self.assertFalse(sent)
  87. self.assertEqual(len(mail.outbox), 0)
  88. def test_it_adds_team_member(self):
  89. self.client.login(username="[email protected]", password="password")
  90. form = {"invite_team_member": "1", "email": "[email protected]"}
  91. r = self.client.post("/accounts/profile/", form)
  92. self.assertEqual(r.status_code, 200)
  93. member_emails = set()
  94. for member in self.profile.member_set.all():
  95. member_emails.add(member.user.email)
  96. self.assertEqual(len(member_emails), 2)
  97. self.assertTrue("[email protected]" in member_emails)
  98. # And an email should have been sent
  99. subj = ('You have been invited to join'
  100. ' [email protected] on %s' % settings.SITE_NAME)
  101. self.assertEqual(mail.outbox[0].subject, subj)
  102. def test_it_checks_team_size(self):
  103. self.profile.team_limit = 0
  104. self.profile.save()
  105. self.client.login(username="[email protected]", password="password")
  106. form = {"invite_team_member": "1", "email": "[email protected]"}
  107. r = self.client.post("/accounts/profile/", form)
  108. self.assertEqual(r.status_code, 403)
  109. def test_it_removes_team_member(self):
  110. self.client.login(username="[email protected]", password="password")
  111. form = {"remove_team_member": "1", "email": "[email protected]"}
  112. r = self.client.post("/accounts/profile/", form)
  113. self.assertEqual(r.status_code, 200)
  114. self.assertEqual(Member.objects.count(), 0)
  115. self.bobs_profile.refresh_from_db()
  116. self.assertEqual(self.bobs_profile.current_team, None)
  117. def test_it_sets_team_name(self):
  118. self.client.login(username="[email protected]", password="password")
  119. form = {"set_team_name": "1", "team_name": "Alpha Team"}
  120. r = self.client.post("/accounts/profile/", form)
  121. self.assertEqual(r.status_code, 200)
  122. self.profile.refresh_from_db()
  123. self.assertEqual(self.profile.team_name, "Alpha Team")
  124. def test_it_switches_to_own_team(self):
  125. self.client.login(username="[email protected]", password="password")
  126. self.client.get("/accounts/profile/")
  127. # After visiting the profile page, team should be switched back
  128. # to user's default team.
  129. self.bobs_profile.refresh_from_db()
  130. self.assertEqual(self.bobs_profile.current_team, self.bobs_profile)
  131. def test_it_sends_change_email_link(self):
  132. self.client.login(username="[email protected]", password="password")
  133. form = {"change_email": "1"}
  134. r = self.client.post("/accounts/profile/", form)
  135. assert r.status_code == 302
  136. # profile.token should be set now
  137. self.profile.refresh_from_db()
  138. token = self.profile.token
  139. self.assertTrue(len(token) > 10)
  140. # And an email should have been sent
  141. self.assertEqual(len(mail.outbox), 1)
  142. expected_subject = "Change email address on %s" % settings.SITE_NAME
  143. self.assertEqual(mail.outbox[0].subject, expected_subject)