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.

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