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.

91 lines
3.2 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_sends_link(self):
  11. form = {"identity": "[email protected]"}
  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.sms_limit, 500)
  20. self.assertEqual(profile.call_limit, 500)
  21. # And email sent
  22. self.assertEqual(len(mail.outbox), 1)
  23. subject = "Log in to %s" % settings.SITE_NAME
  24. self.assertEqual(mail.outbox[0].subject, subject)
  25. # A project should have been created
  26. project = Project.objects.get()
  27. self.assertEqual(project.owner, user)
  28. self.assertEqual(project.badge_key, user.username)
  29. # And check should be associated with the new user
  30. check = Check.objects.get()
  31. self.assertEqual(check.name, "My First Check")
  32. self.assertEqual(check.project, project)
  33. # A channel should have been created
  34. channel = Channel.objects.get()
  35. self.assertEqual(channel.project, project)
  36. @override_settings(USE_PAYMENTS=True)
  37. def test_it_sets_high_limits(self):
  38. form = {"identity": "[email protected]"}
  39. self.client.post("/accounts/signup/", form)
  40. # A profile should have been created
  41. profile = Profile.objects.get()
  42. self.assertEqual(profile.sms_limit, 5)
  43. self.assertEqual(profile.call_limit, 0)
  44. @override_settings(REGISTRATION_OPEN=False)
  45. def test_it_obeys_registration_open(self):
  46. form = {"identity": "[email protected]"}
  47. r = self.client.post("/accounts/signup/", form)
  48. self.assertEqual(r.status_code, 403)
  49. def test_it_ignores_case(self):
  50. form = {"identity": "[email protected]"}
  51. self.client.post("/accounts/signup/", form)
  52. # There should be exactly one user:
  53. q = User.objects.filter(email="[email protected]")
  54. self.assertTrue(q.exists)
  55. def test_it_checks_for_existing_users(self):
  56. alice = User(username="alice", email="[email protected]")
  57. alice.save()
  58. form = {"identity": "[email protected]"}
  59. r = self.client.post("/accounts/signup/", form)
  60. self.assertContains(r, "already exists")
  61. def test_it_checks_syntax(self):
  62. form = {"identity": "alice at example org"}
  63. r = self.client.post("/accounts/signup/", form)
  64. self.assertContains(r, "Enter a valid email address")
  65. def test_it_checks_length(self):
  66. aaa = "a" * 300
  67. form = {"identity": f"alice+{aaa}@example.org"}
  68. r = self.client.post("/accounts/signup/", form)
  69. self.assertContains(r, "Address is too long.")
  70. self.assertFalse(User.objects.exists())