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.

157 lines
5.6 KiB

  1. from django.core import mail
  2. from django.conf import settings
  3. from django.test.utils import override_settings
  4. from hc.test import BaseTestCase
  5. from hc.accounts.models import Member
  6. from hc.api.models import TokenBucket
  7. class ProjectTestCase(BaseTestCase):
  8. def setUp(self):
  9. super(ProjectTestCase, self).setUp()
  10. self.url = "/projects/%s/settings/" % self.project.code
  11. def test_it_checks_access(self):
  12. self.client.login(username="[email protected]", password="password")
  13. r = self.client.get(self.url)
  14. self.assertEqual(r.status_code, 404)
  15. def test_it_allows_team_access(self):
  16. self.client.login(username="[email protected]", password="password")
  17. r = self.client.get(self.url)
  18. self.assertContains(r, "Change Project Name")
  19. def test_it_shows_api_keys(self):
  20. self.project.api_key_readonly = "R" * 32
  21. self.project.save()
  22. self.client.login(username="[email protected]", password="password")
  23. form = {"show_api_keys": "1"}
  24. r = self.client.post(self.url, form)
  25. self.assertEqual(r.status_code, 200)
  26. self.assertContains(r, "X" * 32)
  27. self.assertContains(r, "R" * 32)
  28. def test_it_creates_api_key(self):
  29. self.client.login(username="[email protected]", password="password")
  30. form = {"create_api_keys": "1"}
  31. r = self.client.post(self.url, form)
  32. self.assertEqual(r.status_code, 200)
  33. self.project.refresh_from_db()
  34. api_key = self.project.api_key
  35. self.assertTrue(len(api_key) > 10)
  36. self.assertFalse("b'" in api_key)
  37. def test_it_revokes_api_key(self):
  38. self.project.api_key_readonly = "R" * 32
  39. self.project.save()
  40. self.client.login(username="[email protected]", password="password")
  41. form = {"revoke_api_keys": "1"}
  42. r = self.client.post(self.url, form)
  43. self.assertEqual(r.status_code, 200)
  44. self.project.refresh_from_db()
  45. self.assertEqual(self.project.api_key, "")
  46. self.assertEqual(self.project.api_key_readonly, "")
  47. def test_it_adds_team_member(self):
  48. self.client.login(username="[email protected]", password="password")
  49. form = {"invite_team_member": "1", "email": "[email protected]"}
  50. r = self.client.post(self.url, form)
  51. self.assertEqual(r.status_code, 200)
  52. members = self.project.member_set.all()
  53. self.assertEqual(members.count(), 2)
  54. member = Member.objects.get(project=self.project,
  55. user__email="[email protected]")
  56. profile = member.user.profile
  57. self.assertEqual(profile.current_project, self.project)
  58. # The new user should not have their own project
  59. self.assertFalse(member.user.project_set.exists())
  60. # And an email should have been sent
  61. subj = ("You have been invited to join"
  62. " Alice's Project on %s" % settings.SITE_NAME)
  63. self.assertEqual(mail.outbox[0].subject, subj)
  64. @override_settings(SECRET_KEY="test-secret")
  65. def test_it_rate_limits_invites(self):
  66. obj = TokenBucket(value="invite-%d" % self.alice.id)
  67. obj.tokens = 0
  68. obj.save()
  69. self.client.login(username="[email protected]", password="password")
  70. form = {"invite_team_member": "1", "email": "[email protected]"}
  71. r = self.client.post(self.url, form)
  72. self.assertContains(r, "Too Many Requests")
  73. self.assertEqual(len(mail.outbox), 0)
  74. def test_it_requires_owner_to_add_team_member(self):
  75. self.client.login(username="[email protected]", password="password")
  76. form = {"invite_team_member": "1", "email": "[email protected]"}
  77. r = self.client.post(self.url, form)
  78. self.assertEqual(r.status_code, 403)
  79. def test_it_checks_team_size(self):
  80. self.profile.team_limit = 0
  81. self.profile.save()
  82. self.client.login(username="[email protected]", password="password")
  83. form = {"invite_team_member": "1", "email": "[email protected]"}
  84. r = self.client.post(self.url, form)
  85. self.assertEqual(r.status_code, 403)
  86. def test_it_removes_team_member(self):
  87. self.client.login(username="[email protected]", password="password")
  88. form = {"remove_team_member": "1", "email": "[email protected]"}
  89. r = self.client.post(self.url, form)
  90. self.assertEqual(r.status_code, 200)
  91. self.assertEqual(Member.objects.count(), 0)
  92. self.bobs_profile.refresh_from_db()
  93. self.assertEqual(self.bobs_profile.current_project, None)
  94. def test_it_requires_owner_to_remove_team_member(self):
  95. self.client.login(username="[email protected]", password="password")
  96. form = {"remove_team_member": "1", "email": "[email protected]"}
  97. r = self.client.post(self.url, form)
  98. self.assertEqual(r.status_code, 403)
  99. def test_it_checks_membership_when_removing_team_member(self):
  100. self.client.login(username="[email protected]", password="password")
  101. url = "/projects/%s/settings/" % self.charlies_project.code
  102. form = {"remove_team_member": "1", "email": "[email protected]"}
  103. r = self.client.post(url, form)
  104. self.assertEqual(r.status_code, 400)
  105. self.profile.refresh_from_db()
  106. self.assertIsNotNone(self.profile.current_project)
  107. def test_it_sets_project_name(self):
  108. self.client.login(username="[email protected]", password="password")
  109. form = {"set_project_name": "1", "name": "Alpha Team"}
  110. r = self.client.post(self.url, form)
  111. self.assertEqual(r.status_code, 200)
  112. self.project.refresh_from_db()
  113. self.assertEqual(self.project.name, "Alpha Team")