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.

125 lines
4.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. from django.core import mail
  2. from hc.test import BaseTestCase
  3. from hc.accounts.models import Member
  4. from hc.api.models import Check
  5. from django.conf import settings
  6. class ProfileTestCase(BaseTestCase):
  7. def test_it_sends_set_password_link(self):
  8. self.client.login(username="[email protected]", password="password")
  9. form = {"set_password": "1"}
  10. r = self.client.post("/accounts/profile/", form)
  11. assert r.status_code == 302
  12. # profile.token should be set now
  13. self.alice.profile.refresh_from_db()
  14. token = self.alice.profile.token
  15. self.assertTrue(len(token) > 10)
  16. # And an email should have been sent
  17. self.assertEqual(len(mail.outbox), 1)
  18. expected_subject = 'Set password on {0}'.format(getattr(settings, "SITE_NAME"))
  19. self.assertEqual(mail.outbox[0].subject, expected_subject)
  20. def test_it_creates_api_key(self):
  21. self.client.login(username="[email protected]", password="password")
  22. form = {"create_api_key": "1"}
  23. r = self.client.post("/accounts/profile/", form)
  24. assert r.status_code == 200
  25. self.alice.profile.refresh_from_db()
  26. api_key = self.alice.profile.api_key
  27. self.assertTrue(len(api_key) > 10)
  28. def test_it_revokes_api_key(self):
  29. self.client.login(username="[email protected]", password="password")
  30. form = {"revoke_api_key": "1"}
  31. r = self.client.post("/accounts/profile/", form)
  32. assert r.status_code == 200
  33. self.alice.profile.refresh_from_db()
  34. self.assertEqual(self.alice.profile.api_key, "")
  35. def test_it_sends_report(self):
  36. check = Check(name="Test Check", user=self.alice)
  37. check.save()
  38. self.alice.profile.send_report()
  39. # And an email should have been sent
  40. self.assertEqual(len(mail.outbox), 1)
  41. message = mail.outbox[0]
  42. self.assertEqual(message.subject, 'Monthly Report')
  43. self.assertIn("Test Check", message.body)
  44. def test_it_adds_team_member(self):
  45. self.client.login(username="[email protected]", password="password")
  46. form = {"invite_team_member": "1", "email": "[email protected]"}
  47. r = self.client.post("/accounts/profile/", form)
  48. assert r.status_code == 200
  49. member_emails = set()
  50. for member in self.alice.profile.member_set.all():
  51. member_emails.add(member.user.email)
  52. self.assertEqual(len(member_emails), 2)
  53. self.assertTrue("[email protected]" in member_emails)
  54. # And an email should have been sent
  55. subj = ('You have been invited to join'
  56. ' [email protected] on {0}'.format(getattr(settings, "SITE_NAME")))
  57. self.assertEqual(mail.outbox[0].subject, subj)
  58. def test_add_team_member_checks_team_access_allowed_flag(self):
  59. self.client.login(username="[email protected]", password="password")
  60. form = {"invite_team_member": "1", "email": "[email protected]"}
  61. r = self.client.post("/accounts/profile/", form)
  62. assert r.status_code == 403
  63. def test_it_removes_team_member(self):
  64. self.client.login(username="[email protected]", password="password")
  65. form = {"remove_team_member": "1", "email": "[email protected]"}
  66. r = self.client.post("/accounts/profile/", form)
  67. assert r.status_code == 200
  68. self.assertEqual(Member.objects.count(), 0)
  69. self.bobs_profile.refresh_from_db()
  70. self.assertEqual(self.bobs_profile.current_team, None)
  71. def test_it_sets_team_name(self):
  72. self.client.login(username="[email protected]", password="password")
  73. form = {"set_team_name": "1", "team_name": "Alpha Team"}
  74. r = self.client.post("/accounts/profile/", form)
  75. assert r.status_code == 200
  76. self.alice.profile.refresh_from_db()
  77. self.assertEqual(self.alice.profile.team_name, "Alpha Team")
  78. def test_set_team_name_checks_team_access_allowed_flag(self):
  79. self.client.login(username="[email protected]", password="password")
  80. form = {"set_team_name": "1", "team_name": "Charlies Team"}
  81. r = self.client.post("/accounts/profile/", form)
  82. assert r.status_code == 403
  83. def test_it_switches_to_own_team(self):
  84. self.client.login(username="[email protected]", password="password")
  85. self.client.get("/accounts/profile/")
  86. # After visiting the profile page, team should be switched back
  87. # to user's default team.
  88. self.bobs_profile.refresh_from_db()
  89. self.assertEqual(self.bobs_profile.current_team, self.bobs_profile)