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.

169 lines
6.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. from django.core import mail
  2. from django.utils.timezone import now
  3. from hc.accounts.models import Member
  4. from hc.api.models import Check
  5. from hc.test import BaseTestCase
  6. class TransferProjectTestCase(BaseTestCase):
  7. def setUp(self):
  8. super().setUp()
  9. Check.objects.create(project=self.project)
  10. self.url = "/projects/%s/settings/" % self.project.code
  11. def test_transfer_project_works(self):
  12. self.client.login(username="[email protected]", password="password")
  13. form = {"transfer_project": "1", "email": "[email protected]"}
  14. r = self.client.post(self.url, form)
  15. self.assertContains(r, "Transfer initiated!")
  16. self.bobs_membership.refresh_from_db()
  17. self.assertIsNotNone(self.bobs_membership.transfer_request_date)
  18. # Bob should receive an email notification
  19. self.assertEqual(len(mail.outbox), 1)
  20. body = mail.outbox[0].body
  21. self.assertTrue("/?next=" + self.url in body)
  22. def test_transfer_project_checks_ownership(self):
  23. self.client.login(username="[email protected]", password="password")
  24. form = {"transfer_project": "1", "email": "[email protected]"}
  25. r = self.client.post(self.url, form)
  26. self.assertEqual(r.status_code, 403)
  27. def test_transfer_project_checks_membership(self):
  28. self.client.login(username="[email protected]", password="password")
  29. form = {"transfer_project": "1", "email": "[email protected]"}
  30. r = self.client.post(self.url, form)
  31. self.assertEqual(r.status_code, 400)
  32. def test_cancel_works(self):
  33. self.bobs_membership.transfer_request_date = now()
  34. self.bobs_membership.save()
  35. self.client.login(username="[email protected]", password="password")
  36. r = self.client.post(self.url, {"cancel_transfer": "1"})
  37. self.assertContains(r, "Transfer cancelled!")
  38. self.bobs_membership.refresh_from_db()
  39. self.assertIsNone(self.bobs_membership.transfer_request_date)
  40. def test_cancel_checks_ownership(self):
  41. self.bobs_membership.transfer_request_date = now()
  42. self.bobs_membership.save()
  43. self.client.login(username="[email protected]", password="password")
  44. r = self.client.post(self.url, {"cancel_transfer": "1"})
  45. self.assertEqual(r.status_code, 403)
  46. self.bobs_membership.refresh_from_db()
  47. self.assertIsNotNone(self.bobs_membership.transfer_request_date)
  48. def test_it_shows_transfer_request(self):
  49. self.bobs_membership.transfer_request_date = now()
  50. self.bobs_membership.save()
  51. self.client.login(username="[email protected]", password="password")
  52. r = self.client.get(self.url)
  53. self.assertContains(r, "would like to transfer")
  54. self.assertNotContains(r, "upgrade your account first")
  55. def test_it_shows_transfer_request_with_limit_notice(self):
  56. self.bobs_membership.transfer_request_date = now()
  57. self.bobs_membership.save()
  58. self.bobs_profile.check_limit = 0
  59. self.bobs_profile.save()
  60. self.client.login(username="[email protected]", password="password")
  61. r = self.client.get(self.url)
  62. self.assertContains(r, "upgrade your account first")
  63. def test_accept_works(self):
  64. self.bobs_membership.transfer_request_date = now()
  65. self.bobs_membership.save()
  66. self.client.login(username="[email protected]", password="password")
  67. r = self.client.post(self.url, {"accept_transfer": "1"})
  68. self.assertContains(r, "You are now the owner of this project!")
  69. self.project.refresh_from_db()
  70. # Bob should now be the owner
  71. self.assertEqual(self.project.owner, self.bob)
  72. # Alice, the previous owner, should now be a member
  73. m = Member.objects.get(project=self.project, user=self.alice)
  74. self.assertEqual(m.role, Member.Role.REGULAR)
  75. def test_accept_requires_a_transfer_request(self):
  76. self.client.login(username="[email protected]", password="password")
  77. r = self.client.post(self.url, {"accept_transfer": "1"})
  78. self.assertEqual(r.status_code, 403)
  79. self.project.refresh_from_db()
  80. # Alice should still be the owner
  81. self.assertEqual(self.project.owner, self.alice)
  82. def test_only_the_proposed_owner_can_accept(self):
  83. self.bobs_membership.transfer_request_date = now()
  84. self.bobs_membership.save()
  85. self.client.login(username="[email protected]", password="password")
  86. r = self.client.post(self.url, {"accept_transfer": "1"})
  87. self.assertEqual(r.status_code, 403)
  88. def test_it_checks_limits(self):
  89. self.bobs_membership.transfer_request_date = now()
  90. self.bobs_membership.save()
  91. self.bobs_profile.check_limit = 0
  92. self.bobs_profile.save()
  93. self.client.login(username="[email protected]", password="password")
  94. r = self.client.post(self.url, {"accept_transfer": "1"})
  95. self.assertEqual(r.status_code, 400)
  96. def test_reject_works(self):
  97. self.bobs_membership.transfer_request_date = now()
  98. self.bobs_membership.save()
  99. self.client.login(username="[email protected]", password="password")
  100. r = self.client.post(self.url, {"reject_transfer": "1"})
  101. self.assertEqual(r.status_code, 200)
  102. self.project.refresh_from_db()
  103. # Alice should still be the owner
  104. self.assertEqual(self.project.owner, self.alice)
  105. # The transfer_request_date should be cleared out
  106. self.bobs_membership.refresh_from_db()
  107. self.assertIsNone(self.bobs_membership.transfer_request_date)
  108. def test_only_the_proposed_owner_can_reject(self):
  109. self.bobs_membership.transfer_request_date = now()
  110. self.bobs_membership.save()
  111. self.client.login(username="[email protected]", password="password")
  112. r = self.client.post(self.url, {"reject_transfer": "1"})
  113. self.assertEqual(r.status_code, 403)
  114. def test_readonly_user_can_accept(self):
  115. self.bobs_membership.transfer_request_date = now()
  116. self.bobs_membership.role = "r"
  117. self.bobs_membership.save()
  118. self.client.login(username="[email protected]", password="password")
  119. self.client.post(self.url, {"accept_transfer": "1"})
  120. self.project.refresh_from_db()
  121. # Bob should now be the owner
  122. self.assertEqual(self.project.owner, self.bob)
  123. # Alice, the previous owner, should now be a *regular* member
  124. m = Member.objects.get(user=self.alice, project=self.project)
  125. self.assertEqual(m.role, "w")