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.

122 lines
4.3 KiB

  1. from django.core import mail
  2. from django.conf import settings
  3. from hc.test import BaseTestCase
  4. from hc.accounts.models import Member
  5. class ProfileTestCase(BaseTestCase):
  6. def setUp(self):
  7. super(ProfileTestCase, self).setUp()
  8. self.url = "/projects/%s/settings/" % self.project.code
  9. def test_it_checks_access(self):
  10. self.client.login(username="[email protected]", password="password")
  11. r = self.client.get(self.url)
  12. self.assertEqual(r.status_code, 404)
  13. def test_it_shows_api_keys(self):
  14. self.project.api_key_readonly = "R" * 32
  15. self.project.save()
  16. self.client.login(username="[email protected]", password="password")
  17. form = {"show_api_keys": "1"}
  18. r = self.client.post(self.url, form)
  19. self.assertEqual(r.status_code, 200)
  20. self.assertContains(r, "X" * 32)
  21. self.assertContains(r, "R" * 32)
  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(self.url, form)
  26. self.assertEqual(r.status_code, 200)
  27. self.project.refresh_from_db()
  28. api_key = self.project.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.project.api_key_readonly = "R" * 32
  33. self.project.save()
  34. self.client.login(username="[email protected]", password="password")
  35. form = {"revoke_api_keys": "1"}
  36. r = self.client.post(self.url, form)
  37. self.assertEqual(r.status_code, 200)
  38. self.project.refresh_from_db()
  39. self.assertEqual(self.project.api_key, "")
  40. self.assertEqual(self.project.api_key_readonly, "")
  41. def test_it_adds_team_member(self):
  42. self.client.login(username="[email protected]", password="password")
  43. form = {"invite_team_member": "1", "email": "[email protected]"}
  44. r = self.client.post(self.url, form)
  45. self.assertEqual(r.status_code, 200)
  46. members = self.project.member_set.all()
  47. self.assertEqual(members.count(), 2)
  48. member = Member.objects.get(project=self.project,
  49. user__email="[email protected]")
  50. profile = member.user.profile
  51. self.assertEqual(profile.current_project, self.project)
  52. # The new user should not have their own project
  53. self.assertFalse(member.user.project_set.exists())
  54. # And an email should have been sent
  55. subj = ("You have been invited to join"
  56. " Alice's Project on %s" % settings.SITE_NAME)
  57. self.assertEqual(mail.outbox[0].subject, subj)
  58. def test_it_checks_team_size(self):
  59. self.profile.team_limit = 0
  60. self.profile.save()
  61. self.client.login(username="[email protected]", password="password")
  62. form = {"invite_team_member": "1", "email": "[email protected]"}
  63. r = self.client.post(self.url, form)
  64. self.assertEqual(r.status_code, 403)
  65. def test_it_removes_team_member(self):
  66. self.client.login(username="[email protected]", password="password")
  67. form = {"remove_team_member": "1", "email": "[email protected]"}
  68. r = self.client.post(self.url, form)
  69. self.assertEqual(r.status_code, 200)
  70. self.assertEqual(Member.objects.count(), 0)
  71. self.bobs_profile.refresh_from_db()
  72. self.assertEqual(self.bobs_profile.current_project, None)
  73. def test_it_checks_membership_when_removing_team_member(self):
  74. self.client.login(username="[email protected]", password="password")
  75. url = "/projects/%s/settings/" % self.charlies_project.code
  76. form = {"remove_team_member": "1", "email": "[email protected]"}
  77. r = self.client.post(url, form)
  78. self.assertEqual(r.status_code, 400)
  79. self.profile.refresh_from_db()
  80. self.assertIsNotNone(self.profile.current_project)
  81. def test_it_sets_project_name(self):
  82. self.client.login(username="[email protected]", password="password")
  83. form = {"set_project_name": "1", "name": "Alpha Team"}
  84. r = self.client.post(self.url, form)
  85. self.assertEqual(r.status_code, 200)
  86. self.project.refresh_from_db()
  87. self.assertEqual(self.project.name, "Alpha Team")