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.

248 lines
9.2 KiB

5 years ago
  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, Project
  6. from hc.api.models import TokenBucket
  7. class ProjectTestCase(BaseTestCase):
  8. def setUp(self):
  9. super().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]", "rw": "1"}
  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(
  55. project=self.project, user__email="[email protected]"
  56. )
  57. # The read-write flag should be set
  58. self.assertTrue(member.rw)
  59. # The new user should not have their own project
  60. self.assertFalse(member.user.project_set.exists())
  61. # And an email should have been sent
  62. subj = f"You have been invited to join Alices Project on {settings.SITE_NAME}"
  63. self.assertHTMLEqual(mail.outbox[0].subject, subj)
  64. def test_it_adds_readonly_team_member(self):
  65. self.client.login(username="[email protected]", password="password")
  66. form = {"invite_team_member": "1", "email": "[email protected]"}
  67. r = self.client.post(self.url, form)
  68. self.assertEqual(r.status_code, 200)
  69. member = Member.objects.get(
  70. project=self.project, user__email="[email protected]"
  71. )
  72. # The new user should not have their own project
  73. self.assertFalse(member.rw)
  74. def test_it_adds_member_from_another_team(self):
  75. # With team limit at zero, we should not be able to invite any new users
  76. self.profile.team_limit = 0
  77. self.profile.save()
  78. # But Charlie will have an existing membership in another Alice's project
  79. # so Alice *should* be able to invite Charlie:
  80. p2 = Project.objects.create(owner=self.alice)
  81. Member.objects.create(user=self.charlie, project=p2)
  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, 200)
  86. q = Member.objects.filter(project=self.project, user=self.charlie)
  87. self.assertEqual(q.count(), 1)
  88. # And this should not have affected the rate limit:
  89. q = TokenBucket.objects.filter(value="invite-%d" % self.alice.id)
  90. self.assertFalse(q.exists())
  91. def test_it_rejects_duplicate_membership(self):
  92. self.client.login(username="[email protected]", password="password")
  93. form = {"invite_team_member": "1", "email": "[email protected]"}
  94. r = self.client.post(self.url, form)
  95. self.assertContains(r, "[email protected] is already a member")
  96. # The number of memberships should have not increased
  97. self.assertEqual(self.project.member_set.count(), 1)
  98. def test_it_rejects_owner_as_a_member(self):
  99. self.client.login(username="[email protected]", password="password")
  100. form = {"invite_team_member": "1", "email": "[email protected]"}
  101. r = self.client.post(self.url, form)
  102. self.assertContains(r, "[email protected] is already a member")
  103. # The number of memberships should have not increased
  104. self.assertEqual(self.project.member_set.count(), 1)
  105. def test_it_rejects_too_long_email_addresses(self):
  106. self.client.login(username="[email protected]", password="password")
  107. aaa = "a" * 300
  108. form = {"invite_team_member": "1", "email": f"frank+{aaa}@example.org"}
  109. r = self.client.post(self.url, form)
  110. self.assertEqual(r.status_code, 200)
  111. # No email should have been sent
  112. self.assertEqual(len(mail.outbox), 0)
  113. @override_settings(SECRET_KEY="test-secret")
  114. def test_it_rate_limits_invites(self):
  115. obj = TokenBucket(value="invite-%d" % self.alice.id)
  116. obj.tokens = 0
  117. obj.save()
  118. self.client.login(username="[email protected]", password="password")
  119. form = {"invite_team_member": "1", "email": "[email protected]"}
  120. r = self.client.post(self.url, form)
  121. self.assertContains(r, "Too Many Requests")
  122. self.assertEqual(len(mail.outbox), 0)
  123. def test_it_requires_owner_to_add_team_member(self):
  124. self.client.login(username="[email protected]", password="password")
  125. form = {"invite_team_member": "1", "email": "[email protected]"}
  126. r = self.client.post(self.url, form)
  127. self.assertEqual(r.status_code, 403)
  128. def test_it_checks_team_size(self):
  129. self.profile.team_limit = 0
  130. self.profile.save()
  131. self.client.login(username="[email protected]", password="password")
  132. form = {"invite_team_member": "1", "email": "[email protected]"}
  133. r = self.client.post(self.url, form)
  134. self.assertEqual(r.status_code, 403)
  135. def test_it_removes_team_member(self):
  136. self.client.login(username="[email protected]", password="password")
  137. form = {"remove_team_member": "1", "email": "[email protected]"}
  138. r = self.client.post(self.url, form)
  139. self.assertEqual(r.status_code, 200)
  140. self.assertFalse(Member.objects.exists())
  141. def test_it_requires_owner_to_remove_team_member(self):
  142. self.client.login(username="[email protected]", password="password")
  143. form = {"remove_team_member": "1", "email": "[email protected]"}
  144. r = self.client.post(self.url, form)
  145. self.assertEqual(r.status_code, 403)
  146. def test_it_checks_membership_when_removing_team_member(self):
  147. self.client.login(username="[email protected]", password="password")
  148. url = "/projects/%s/settings/" % self.charlies_project.code
  149. form = {"remove_team_member": "1", "email": "[email protected]"}
  150. r = self.client.post(url, form)
  151. self.assertEqual(r.status_code, 400)
  152. def test_it_sets_project_name(self):
  153. self.client.login(username="[email protected]", password="password")
  154. form = {"set_project_name": "1", "name": "Alpha Team"}
  155. r = self.client.post(self.url, form)
  156. self.assertEqual(r.status_code, 200)
  157. self.project.refresh_from_db()
  158. self.assertEqual(self.project.name, "Alpha Team")
  159. def test_it_shows_invite_suggestions(self):
  160. p2 = Project.objects.create(owner=self.alice)
  161. self.client.login(username="[email protected]", password="password")
  162. r = self.client.get("/projects/%s/settings/" % p2.code)
  163. self.assertContains(r, "Add Users from Other Teams")
  164. self.assertContains(r, "[email protected]")
  165. def test_it_checks_rw_access_when_updating_project_name(self):
  166. self.bobs_membership.rw = False
  167. self.bobs_membership.save()
  168. self.client.login(username="[email protected]", password="password")
  169. form = {"set_project_name": "1", "name": "Alpha Team"}
  170. r = self.client.post(self.url, form)
  171. self.assertEqual(r.status_code, 403)
  172. def test_it_hides_actions_for_readonly_users(self):
  173. self.bobs_membership.rw = False
  174. self.bobs_membership.save()
  175. self.client.login(username="[email protected]", password="password")
  176. r = self.client.get(self.url)
  177. self.assertNotContains(r, "#set-project-name-modal", status_code=200)
  178. self.assertNotContains(r, "Show API Keys")