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.

65 lines
2.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 Project
  6. from hc.api.models import Channel, Check
  7. from django.conf import settings
  8. class SignupTestCase(TestCase):
  9. def test_it_sends_link(self):
  10. form = {"identity": "[email protected]"}
  11. r = self.client.post("/accounts/signup/", form)
  12. self.assertContains(r, "Account created")
  13. # An user should have been created
  14. user = User.objects.get()
  15. # And email sent
  16. self.assertEqual(len(mail.outbox), 1)
  17. subject = "Log in to %s" % settings.SITE_NAME
  18. self.assertEqual(mail.outbox[0].subject, subject)
  19. # A project should have been created
  20. project = Project.objects.get()
  21. self.assertEqual(project.owner, user)
  22. self.assertEqual(project.badge_key, user.username)
  23. # And check should be associated with the new user
  24. check = Check.objects.get()
  25. self.assertEqual(check.name, "My First Check")
  26. self.assertEqual(check.project, project)
  27. # A channel should have been created
  28. channel = Channel.objects.get()
  29. self.assertEqual(channel.project, project)
  30. @override_settings(REGISTRATION_OPEN=False)
  31. def test_it_obeys_registration_open(self):
  32. form = {"identity": "[email protected]"}
  33. r = self.client.post("/accounts/signup/", form)
  34. self.assertEqual(r.status_code, 403)
  35. def test_it_ignores_case(self):
  36. form = {"identity": "[email protected]"}
  37. self.client.post("/accounts/signup/", form)
  38. # There should be exactly one user:
  39. q = User.objects.filter(email="[email protected]")
  40. self.assertTrue(q.exists)
  41. def test_it_checks_for_existing_users(self):
  42. alice = User(username="alice", email="[email protected]")
  43. alice.save()
  44. form = {"identity": "[email protected]"}
  45. r = self.client.post("/accounts/signup/", form)
  46. self.assertContains(r, "already exists")
  47. def test_it_checks_syntax(self):
  48. form = {"identity": "alice at example org"}
  49. r = self.client.post("/accounts/signup/", form)
  50. self.assertContains(r, "Enter a valid email address")