test_user_create_api.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 misago.users.models import Online
  6. from misago.users.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.assertEqual(response.status_code, 403)
  25. self.assertIn('closed', response.content)
  26. def test_registration_creates_active_user(self):
  27. """api creates active and signed in user on POST"""
  28. settings.override_setting('account_activation', 'none')
  29. response = self.client.post('/api/users/',
  30. data={'username': 'Bob',
  31. 'email': 'bob@bob.com',
  32. 'password': 'pass123'})
  33. self.assertEqual(response.status_code, 200)
  34. self.assertIn('active', response.content)
  35. self.assertIn('Bob', response.content)
  36. self.assertIn('bob@bob.com', response.content)
  37. User = get_user_model()
  38. User.objects.get_by_username('Bob')
  39. test_user = User.objects.get_by_email('bob@bob.com')
  40. self.assertEqual(Online.objects.filter(user=test_user).count(), 1)
  41. response = self.client.get(reverse('misago:index'))
  42. self.assertIn('Bob', response.content)
  43. self.assertIn('Welcome', mail.outbox[0].subject)
  44. def test_registration_creates_inactive_user(self):
  45. """api creates inactive user on POST"""
  46. settings.override_setting('account_activation', 'user')
  47. response = self.client.post('/api/users/',
  48. data={'username': 'Bob',
  49. 'email': 'bob@bob.com',
  50. 'password': 'pass123'})
  51. self.assertEqual(response.status_code, 200)
  52. self.assertIn('user', response.content)
  53. self.assertIn('Bob', response.content)
  54. self.assertIn('bob@bob.com', response.content)
  55. User = get_user_model()
  56. User.objects.get_by_username('Bob')
  57. User.objects.get_by_email('bob@bob.com')
  58. self.assertIn('Welcome', mail.outbox[0].subject)
  59. def test_registration_creates_admin_activated_user(self):
  60. """api creates admin activated user on POST"""
  61. settings.override_setting('account_activation', 'admin')
  62. response = self.client.post('/api/users/',
  63. data={'username': 'Bob',
  64. 'email': 'bob@bob.com',
  65. 'password': 'pass123'})
  66. self.assertEqual(response.status_code, 200)
  67. self.assertIn('admin', response.content)
  68. self.assertIn('Bob', response.content)
  69. self.assertIn('bob@bob.com', response.content)
  70. User = get_user_model()
  71. User.objects.get_by_username('Bob')
  72. User.objects.get_by_email('bob@bob.com')
  73. self.assertIn('Welcome', mail.outbox[0].subject)