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.

104 lines
3.7 KiB

  1. from django.contrib.auth.models import User
  2. from django.core import mail
  3. from django.test import TestCase
  4. from django.test.utils import override_settings
  5. from hc.accounts.models import Profile, Project
  6. from hc.api.models import Channel, Check
  7. from django.conf import settings
  8. class SignupTestCase(TestCase):
  9. @override_settings(USE_PAYMENTS=False)
  10. def test_it_works(self):
  11. form = {"identity": "[email protected]", "tz": "Europe/Riga"}
  12. r = self.client.post("/accounts/signup/", form)
  13. self.assertContains(r, "Account created")
  14. self.assertIn("auto-login", r.cookies)
  15. # An user should have been created
  16. user = User.objects.get()
  17. # A profile should have been created
  18. profile = Profile.objects.get()
  19. self.assertEqual(profile.check_limit, 500)
  20. self.assertEqual(profile.sms_limit, 500)
  21. self.assertEqual(profile.call_limit, 500)
  22. self.assertEqual(profile.tz, "Europe/Riga")
  23. # And email sent
  24. self.assertEqual(len(mail.outbox), 1)
  25. subject = "Log in to %s" % settings.SITE_NAME
  26. self.assertEqual(mail.outbox[0].subject, subject)
  27. # A project should have been created
  28. project = Project.objects.get()
  29. self.assertEqual(project.owner, user)
  30. self.assertEqual(project.badge_key, user.username)
  31. # And check should be associated with the new user
  32. check = Check.objects.get()
  33. self.assertEqual(check.name, "My First Check")
  34. self.assertEqual(check.project, project)
  35. # A channel should have been created
  36. channel = Channel.objects.get()
  37. self.assertEqual(channel.project, project)
  38. @override_settings(USE_PAYMENTS=True)
  39. def test_it_sets_limits(self):
  40. form = {"identity": "[email protected]", "tz": ""}
  41. self.client.post("/accounts/signup/", form)
  42. profile = Profile.objects.get()
  43. self.assertEqual(profile.check_limit, 20)
  44. self.assertEqual(profile.sms_limit, 5)
  45. self.assertEqual(profile.call_limit, 0)
  46. @override_settings(REGISTRATION_OPEN=False)
  47. def test_it_obeys_registration_open(self):
  48. form = {"identity": "[email protected]", "tz": ""}
  49. r = self.client.post("/accounts/signup/", form)
  50. self.assertEqual(r.status_code, 403)
  51. def test_it_ignores_case(self):
  52. form = {"identity": "[email protected]", "tz": ""}
  53. self.client.post("/accounts/signup/", form)
  54. # There should be exactly one user:
  55. q = User.objects.filter(email="[email protected]")
  56. self.assertTrue(q.exists)
  57. def test_it_checks_for_existing_users(self):
  58. alice = User(username="alice", email="[email protected]")
  59. alice.save()
  60. form = {"identity": "[email protected]", "tz": ""}
  61. r = self.client.post("/accounts/signup/", form)
  62. self.assertContains(r, "already exists")
  63. def test_it_checks_syntax(self):
  64. form = {"identity": "alice at example org", "tz": ""}
  65. r = self.client.post("/accounts/signup/", form)
  66. self.assertContains(r, "Enter a valid email address")
  67. def test_it_checks_length(self):
  68. aaa = "a" * 300
  69. form = {"identity": f"alice+{aaa}@example.org", "tz": ""}
  70. r = self.client.post("/accounts/signup/", form)
  71. self.assertContains(r, "Address is too long.")
  72. self.assertFalse(User.objects.exists())
  73. @override_settings(USE_PAYMENTS=False)
  74. def test_it_ignores_bad_tz(self):
  75. form = {"identity": "[email protected]", "tz": "Foo/Bar"}
  76. r = self.client.post("/accounts/signup/", form)
  77. self.assertContains(r, "Account created")
  78. self.assertIn("auto-login", r.cookies)
  79. profile = Profile.objects.get()
  80. self.assertEqual(profile.tz, "UTC")