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.

151 lines
5.8 KiB

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