test_user_create_api.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.testutils import UserTestCase
  6. class UserCreateTests(UserTestCase):
  7. """
  8. tests for new user registration (POST to /api/users/)
  9. """
  10. def test_empty_request(self):
  11. """empty request errors with code 400"""
  12. response = self.client.post('/api/users/')
  13. self.assertEqual(response.status_code, 400)
  14. def test_authenticated_request(self):
  15. """authentiated user request errors with code 403"""
  16. self.login_user(self.get_authenticated_user())
  17. response = self.client.post('/api/users/')
  18. self.assertEqual(response.status_code, 403)
  19. def test_registration_off_request(self):
  20. """registrations off request errors with code 403"""
  21. settings.override_setting('account_activation', 'closed')
  22. response = self.client.post('/api/users/')
  23. self.assertEqual(response.status_code, 403)
  24. self.assertIn('closed', response.content)
  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.assertEqual(response.status_code, 200)
  33. self.assertIn('active', response.content)
  34. self.assertIn('Bob', response.content)
  35. self.assertIn('bob@bob.com', response.content)
  36. User = get_user_model()
  37. User.objects.get_by_username('Bob')
  38. User.objects.get_by_email('bob@bob.com')
  39. response = self.client.get(reverse('misago:index'))
  40. self.assertIn('Bob', response.content)
  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.assertEqual(response.status_code, 200)
  50. self.assertIn('user', response.content)
  51. self.assertIn('Bob', response.content)
  52. self.assertIn('bob@bob.com', response.content)
  53. User = get_user_model()
  54. User.objects.get_by_username('Bob')
  55. User.objects.get_by_email('bob@bob.com')
  56. self.assertIn('Welcome', mail.outbox[0].subject)
  57. def test_registration_creates_admin_activated_user(self):
  58. """api creates admin activated user on POST"""
  59. settings.override_setting('account_activation', 'admin')
  60. response = self.client.post('/api/users/',
  61. data={'username': 'Bob',
  62. 'email': 'bob@bob.com',
  63. 'password': 'pass123'})
  64. self.assertEqual(response.status_code, 200)
  65. self.assertIn('admin', response.content)
  66. self.assertIn('Bob', response.content)
  67. self.assertIn('bob@bob.com', response.content)
  68. User = get_user_model()
  69. User.objects.get_by_username('Bob')
  70. User.objects.get_by_email('bob@bob.com')
  71. self.assertIn('Welcome', mail.outbox[0].subject)