test_user_create_api.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from django.contrib.auth import get_user_model
  2. from django.core import mail
  3. from django.core.urlresolvers import reverse
  4. from misago.conf import settings
  5. from ..models import Online
  6. from ..testutils import UserTestCase
  7. class UserCreateTests(UserTestCase):
  8. """
  9. tests for new user registration (POST to /api/users/)
  10. """
  11. def test_empty_request(self):
  12. """empty request errors with code 400"""
  13. response = self.client.post('/api/users/')
  14. self.assertEqual(response.status_code, 400)
  15. def test_authenticated_request(self):
  16. """authentiated user request errors with code 403"""
  17. self.login_user(self.get_authenticated_user())
  18. response = self.client.post('/api/users/')
  19. self.assertEqual(response.status_code, 403)
  20. def test_registration_off_request(self):
  21. """registrations off request errors with code 403"""
  22. settings.override_setting('account_activation', 'closed')
  23. response = self.client.post('/api/users/')
  24. self.assertContains(response, 'closed', status_code=403)
  25. def test_registration_creates_active_user(self):
  26. """api creates active and signed in user on POST"""
  27. settings.override_setting('account_activation', 'none')
  28. response = self.client.post('/api/users/',
  29. data={'username': 'Bob',
  30. 'email': 'bob@bob.com',
  31. 'password': 'pass123'})
  32. self.assertContains(response, 'active')
  33. self.assertContains(response, 'Bob')
  34. self.assertContains(response, 'bob@bob.com')
  35. User = get_user_model()
  36. User.objects.get_by_username('Bob')
  37. test_user = User.objects.get_by_email('bob@bob.com')
  38. self.assertEqual(Online.objects.filter(user=test_user).count(), 1)
  39. response = self.client.get(reverse('misago:index'))
  40. self.assertContains(response, 'Bob')
  41. self.assertIn('Welcome', mail.outbox[0].subject)
  42. def test_registration_creates_inactive_user(self):
  43. """api creates inactive user on POST"""
  44. settings.override_setting('account_activation', 'user')
  45. response = self.client.post('/api/users/',
  46. data={'username': 'Bob',
  47. 'email': 'bob@bob.com',
  48. 'password': 'pass123'})
  49. self.assertContains(response, 'user')
  50. self.assertContains(response, 'Bob')
  51. self.assertContains(response, 'bob@bob.com')
  52. User = get_user_model()
  53. User.objects.get_by_username('Bob')
  54. User.objects.get_by_email('bob@bob.com')
  55. self.assertIn('Welcome', mail.outbox[0].subject)
  56. def test_registration_creates_admin_activated_user(self):
  57. """api creates admin activated user on POST"""
  58. settings.override_setting('account_activation', 'admin')
  59. response = self.client.post('/api/users/',
  60. data={'username': 'Bob',
  61. 'email': 'bob@bob.com',
  62. 'password': 'pass123'})
  63. self.assertContains(response, 'admin')
  64. self.assertContains(response, 'Bob')
  65. self.assertContains(response, 'bob@bob.com')
  66. User = get_user_model()
  67. User.objects.get_by_username('Bob')
  68. User.objects.get_by_email('bob@bob.com')
  69. self.assertIn('Welcome', mail.outbox[0].subject)