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.

79 lines
2.7 KiB

  1. from unittest.mock import patch
  2. from django.contrib.auth.models import User
  3. from hc.api.models import Check
  4. from hc.payments.models import Subscription
  5. from hc.test import BaseTestCase
  6. class CloseAccountTestCase(BaseTestCase):
  7. def test_it_requires_sudo_mode(self):
  8. self.client.login(username="[email protected]", password="password")
  9. r = self.client.get("/accounts/close/")
  10. self.assertContains(r, "We have sent a confirmation code")
  11. def test_it_shows_confirmation_form(self):
  12. self.client.login(username="[email protected]", password="password")
  13. self.set_sudo_flag()
  14. r = self.client.get("/accounts/close/")
  15. self.assertContains(r, "Close Account?")
  16. self.assertContains(r, "1 project")
  17. self.assertContains(r, "0 checks")
  18. @patch("hc.payments.models.braintree")
  19. def test_it_works(self, mock_braintree):
  20. Check.objects.create(project=self.project, tags="foo a-B_1 baz@")
  21. Subscription.objects.create(
  22. user=self.alice, subscription_id="123", customer_id="fake-customer-id"
  23. )
  24. self.client.login(username="[email protected]", password="password")
  25. self.set_sudo_flag()
  26. payload = {"confirmation": "[email protected]"}
  27. r = self.client.post("/accounts/close/", payload)
  28. self.assertRedirects(r, "/")
  29. # Alice should be gone
  30. alices = User.objects.filter(username="alice")
  31. self.assertFalse(alices.exists())
  32. # Check should be gone
  33. self.assertFalse(Check.objects.exists())
  34. # Subscription should have been canceled
  35. self.assertTrue(mock_braintree.Subscription.cancel.called)
  36. # Subscription should be gone
  37. self.assertFalse(Subscription.objects.exists())
  38. def test_it_requires_confirmation(self):
  39. self.client.login(username="[email protected]", password="password")
  40. self.set_sudo_flag()
  41. payload = {"confirmation": "incorrect"}
  42. r = self.client.post("/accounts/close/", payload)
  43. self.assertContains(r, "Close Account?")
  44. self.assertContains(r, "has-error")
  45. # Alice should be still present
  46. self.alice.refresh_from_db()
  47. self.profile.refresh_from_db()
  48. def test_partner_removal_works(self):
  49. self.client.login(username="[email protected]", password="password")
  50. self.set_sudo_flag()
  51. payload = {"confirmation": "[email protected]"}
  52. r = self.client.post("/accounts/close/", payload)
  53. self.assertRedirects(r, "/")
  54. # Alice should be still present
  55. self.alice.refresh_from_db()
  56. self.profile.refresh_from_db()
  57. # Bob should be gone
  58. bobs = User.objects.filter(username="bob")
  59. self.assertFalse(bobs.exists())