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.

220 lines
7.5 KiB

6 years ago
9 years ago
6 years ago
6 years ago
6 years ago
6 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_shows_api_keys(self):
  23. self.project.api_key_readonly = "R" * 32
  24. self.project.save()
  25. self.client.login(username="[email protected]", password="password")
  26. form = {"show_api_keys": "1"}
  27. r = self.client.post("/accounts/profile/", form)
  28. self.assertEqual(r.status_code, 200)
  29. self.assertContains(r, "X" * 32)
  30. self.assertContains(r, "R" * 32)
  31. def test_it_creates_api_key(self):
  32. self.client.login(username="[email protected]", password="password")
  33. form = {"create_api_keys": "1"}
  34. r = self.client.post("/accounts/profile/", form)
  35. self.assertEqual(r.status_code, 200)
  36. self.project.refresh_from_db()
  37. api_key = self.project.api_key
  38. self.assertTrue(len(api_key) > 10)
  39. self.assertFalse("b'" in api_key)
  40. def test_it_revokes_api_key(self):
  41. self.project.api_key_readonly = "R" * 32
  42. self.project.save()
  43. self.client.login(username="[email protected]", password="password")
  44. form = {"revoke_api_keys": "1"}
  45. r = self.client.post("/accounts/profile/", form)
  46. assert r.status_code == 200
  47. self.project.refresh_from_db()
  48. self.assertEqual(self.project.api_key, "")
  49. self.assertEqual(self.project.api_key_readonly, "")
  50. def test_it_sends_report(self):
  51. check = Check(name="Test Check", user=self.alice, project=self.project)
  52. check.last_ping = now()
  53. check.save()
  54. sent = self.profile.send_report()
  55. self.assertTrue(sent)
  56. # And an email should have been sent
  57. self.assertEqual(len(mail.outbox), 1)
  58. message = mail.outbox[0]
  59. self.assertEqual(message.subject, 'Monthly Report')
  60. self.assertIn("Test Check", message.body)
  61. def test_it_skips_report_if_no_pings(self):
  62. check = Check(name="Test Check", user=self.alice, project=self.project)
  63. check.save()
  64. sent = self.profile.send_report()
  65. self.assertFalse(sent)
  66. self.assertEqual(len(mail.outbox), 0)
  67. def test_it_skips_report_if_no_recent_pings(self):
  68. check = Check(name="Test Check", user=self.alice, project=self.project)
  69. check.last_ping = now() - td(days=365)
  70. check.save()
  71. sent = self.profile.send_report()
  72. self.assertFalse(sent)
  73. self.assertEqual(len(mail.outbox), 0)
  74. def test_it_sends_nag(self):
  75. check = Check(name="Test Check", user=self.alice, project=self.project)
  76. check.status = "down"
  77. check.last_ping = now()
  78. check.save()
  79. self.profile.nag_period = td(hours=1)
  80. self.profile.save()
  81. sent = self.profile.send_report(nag=True)
  82. self.assertTrue(sent)
  83. # And an email should have been sent
  84. self.assertEqual(len(mail.outbox), 1)
  85. message = mail.outbox[0]
  86. self.assertEqual(message.subject, 'Reminder: 1 check still down')
  87. self.assertIn("Test Check", message.body)
  88. def test_it_skips_nag_if_none_down(self):
  89. check = Check(name="Test Check", user=self.alice, project=self.project)
  90. check.last_ping = now()
  91. check.save()
  92. self.profile.nag_period = td(hours=1)
  93. self.profile.save()
  94. sent = self.profile.send_report(nag=True)
  95. self.assertFalse(sent)
  96. self.assertEqual(len(mail.outbox), 0)
  97. def test_it_adds_team_member(self):
  98. self.client.login(username="[email protected]", password="password")
  99. form = {"invite_team_member": "1", "email": "[email protected]"}
  100. r = self.client.post("/accounts/profile/", form)
  101. self.assertEqual(r.status_code, 200)
  102. members = self.project.member_set.all()
  103. self.assertEqual(members.count(), 2)
  104. frank_found = False
  105. for member in members.all():
  106. self.assertEqual(member.project, self.project)
  107. if member.user.email == "[email protected]":
  108. frank_found = True
  109. self.assertTrue(frank_found)
  110. # And an email should have been sent
  111. subj = ('You have been invited to join'
  112. ' [email protected] on %s' % settings.SITE_NAME)
  113. self.assertEqual(mail.outbox[0].subject, subj)
  114. def test_it_checks_team_size(self):
  115. self.profile.team_limit = 0
  116. self.profile.save()
  117. self.client.login(username="[email protected]", password="password")
  118. form = {"invite_team_member": "1", "email": "[email protected]"}
  119. r = self.client.post("/accounts/profile/", form)
  120. self.assertEqual(r.status_code, 403)
  121. def test_it_removes_team_member(self):
  122. self.client.login(username="[email protected]", password="password")
  123. form = {"remove_team_member": "1", "email": "[email protected]"}
  124. r = self.client.post("/accounts/profile/", form)
  125. self.assertEqual(r.status_code, 200)
  126. self.assertEqual(Member.objects.count(), 0)
  127. self.bobs_profile.refresh_from_db()
  128. self.assertEqual(self.bobs_profile.current_team, None)
  129. self.assertEqual(self.bobs_profile.current_project, None)
  130. def test_it_sets_team_name(self):
  131. self.client.login(username="[email protected]", password="password")
  132. form = {"set_team_name": "1", "team_name": "Alpha Team"}
  133. r = self.client.post("/accounts/profile/", form)
  134. self.assertEqual(r.status_code, 200)
  135. self.profile.refresh_from_db()
  136. self.assertEqual(self.profile.team_name, "Alpha Team")
  137. self.project.refresh_from_db()
  138. self.assertEqual(self.project.name, "Alpha Team")
  139. def test_it_switches_to_own_team(self):
  140. self.client.login(username="[email protected]", password="password")
  141. self.client.get("/accounts/profile/")
  142. # After visiting the profile page, team should be switched back
  143. # to user's default team.
  144. self.bobs_profile.refresh_from_db()
  145. self.assertEqual(self.bobs_profile.current_team, self.bobs_profile)
  146. self.assertEqual(self.bobs_profile.current_project, self.bobs_project)
  147. def test_it_sends_change_email_link(self):
  148. self.client.login(username="[email protected]", password="password")
  149. form = {"change_email": "1"}
  150. r = self.client.post("/accounts/profile/", form)
  151. assert r.status_code == 302
  152. # profile.token should be set now
  153. self.profile.refresh_from_db()
  154. token = self.profile.token
  155. self.assertTrue(len(token) > 10)
  156. # And an email should have been sent
  157. self.assertEqual(len(mail.outbox), 1)
  158. expected_subject = "Change email address on %s" % settings.SITE_NAME
  159. self.assertEqual(mail.outbox[0].subject, expected_subject)