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.

174 lines
5.9 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_key": "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. def test_it_revokes_api_key(self):
  31. self.client.login(username="[email protected]", password="password")
  32. form = {"revoke_api_key": "1"}
  33. r = self.client.post("/accounts/profile/", form)
  34. assert r.status_code == 200
  35. self.profile.refresh_from_db()
  36. self.assertEqual(self.profile.api_key, "")
  37. def test_it_sends_report(self):
  38. check = Check(name="Test Check", user=self.alice)
  39. check.last_ping = now()
  40. check.save()
  41. sent = self.profile.send_report()
  42. self.assertTrue(sent)
  43. # And an email should have been sent
  44. self.assertEqual(len(mail.outbox), 1)
  45. message = mail.outbox[0]
  46. self.assertEqual(message.subject, 'Monthly Report')
  47. self.assertIn("Test Check", message.body)
  48. def test_it_sends_nag(self):
  49. check = Check(name="Test Check", user=self.alice)
  50. check.status = "down"
  51. check.last_ping = now()
  52. check.save()
  53. self.profile.nag_period = td(hours=1)
  54. self.profile.save()
  55. sent = self.profile.send_report(nag=True)
  56. self.assertTrue(sent)
  57. # And an email should have been sent
  58. self.assertEqual(len(mail.outbox), 1)
  59. message = mail.outbox[0]
  60. self.assertEqual(message.subject, 'Reminder: 1 check still down')
  61. self.assertIn("Test Check", message.body)
  62. def test_it_skips_nag_if_none_down(self):
  63. check = Check(name="Test Check", user=self.alice)
  64. check.last_ping = now()
  65. check.save()
  66. self.profile.nag_period = td(hours=1)
  67. self.profile.save()
  68. sent = self.profile.send_report(nag=True)
  69. self.assertFalse(sent)
  70. self.assertEqual(len(mail.outbox), 0)
  71. def test_it_adds_team_member(self):
  72. self.client.login(username="[email protected]", password="password")
  73. form = {"invite_team_member": "1", "email": "[email protected]"}
  74. r = self.client.post("/accounts/profile/", form)
  75. self.assertEqual(r.status_code, 200)
  76. member_emails = set()
  77. for member in self.profile.member_set.all():
  78. member_emails.add(member.user.email)
  79. self.assertEqual(len(member_emails), 2)
  80. self.assertTrue("[email protected]" in member_emails)
  81. # And an email should have been sent
  82. subj = ('You have been invited to join'
  83. ' [email protected] on %s' % settings.SITE_NAME)
  84. self.assertEqual(mail.outbox[0].subject, subj)
  85. def test_it_checks_team_size(self):
  86. self.profile.team_limit = 0
  87. self.profile.save()
  88. self.client.login(username="[email protected]", password="password")
  89. form = {"invite_team_member": "1", "email": "[email protected]"}
  90. r = self.client.post("/accounts/profile/", form)
  91. self.assertEqual(r.status_code, 403)
  92. def test_it_removes_team_member(self):
  93. self.client.login(username="[email protected]", password="password")
  94. form = {"remove_team_member": "1", "email": "[email protected]"}
  95. r = self.client.post("/accounts/profile/", form)
  96. self.assertEqual(r.status_code, 200)
  97. self.assertEqual(Member.objects.count(), 0)
  98. self.bobs_profile.refresh_from_db()
  99. self.assertEqual(self.bobs_profile.current_team, None)
  100. def test_it_sets_team_name(self):
  101. self.client.login(username="[email protected]", password="password")
  102. form = {"set_team_name": "1", "team_name": "Alpha Team"}
  103. r = self.client.post("/accounts/profile/", form)
  104. self.assertEqual(r.status_code, 200)
  105. self.profile.refresh_from_db()
  106. self.assertEqual(self.profile.team_name, "Alpha Team")
  107. def test_it_switches_to_own_team(self):
  108. self.client.login(username="[email protected]", password="password")
  109. self.client.get("/accounts/profile/")
  110. # After visiting the profile page, team should be switched back
  111. # to user's default team.
  112. self.bobs_profile.refresh_from_db()
  113. self.assertEqual(self.bobs_profile.current_team, self.bobs_profile)
  114. def test_it_sends_change_email_link(self):
  115. self.client.login(username="[email protected]", password="password")
  116. form = {"change_email": "1"}
  117. r = self.client.post("/accounts/profile/", form)
  118. assert r.status_code == 302
  119. # profile.token should be set now
  120. self.profile.refresh_from_db()
  121. token = self.profile.token
  122. self.assertTrue(len(token) > 10)
  123. # And an email should have been sent
  124. self.assertEqual(len(mail.outbox), 1)
  125. expected_subject = "Change email address on %s" % settings.SITE_NAME
  126. self.assertEqual(mail.outbox[0].subject, expected_subject)