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.

214 lines
7.9 KiB

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