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.

85 lines
3.5 KiB

5 years ago
  1. from django.test import SimpleTestCase, TestCase, RequestFactory, override_settings
  2. from django.http import HttpRequest
  3. from ..views import SignupView
  4. from django.conf import settings
  5. from api.tests.test_config import setenv
  6. @override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage')
  7. class AddCSSTemplateTagTest(SimpleTestCase):
  8. def test_rendered(self):
  9. with setenv('ALLOW_SIGNUP', 'True'):
  10. request = HttpRequest()
  11. request.method = 'GET'
  12. needle = '<input type="password" name="password1" class=" input" required id="id_password1">'
  13. self.assertInHTML(needle, str(SignupView.as_view()(request, as_string=True).content))
  14. @override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage')
  15. class ViewsTest(SimpleTestCase):
  16. """Class for testing views"""
  17. def test_mail_not_set_up(self):
  18. with setenv('ALLOW_SIGNUP', 'True'):
  19. if hasattr(settings, 'EMAIL_HOST'):
  20. has_EMAIL_HOST = True
  21. EMAIL_HOST = settings.EMAIL_HOST
  22. delattr(settings, 'EMAIL_HOST')
  23. else:
  24. has_EMAIL_HOST = False
  25. if hasattr(settings, 'EMAIL_BACKEND'):
  26. has_EMAIL_BACKEND = True
  27. EMAIL_BACKEND = settings.EMAIL_BACKEND
  28. delattr(settings, 'EMAIL_BACKEND')
  29. else:
  30. has_EMAIL_BACKEND = False
  31. request = HttpRequest()
  32. request.method = 'POST'
  33. response = SignupView.as_view()(request, as_string=True)
  34. if has_EMAIL_HOST:
  35. settings.EMAIL_HOST = EMAIL_HOST
  36. if has_EMAIL_BACKEND:
  37. settings.EMAIL_BACKEND = EMAIL_BACKEND
  38. needle = "<span>has not set up any emails</span>"
  39. self.assertInHTML(needle, str(response.content))
  40. def test_signup_not_allowed(self):
  41. with setenv('ALLOW_SIGNUP', 'True'):
  42. ALLOW_SIGNUP = settings.ALLOW_SIGNUP
  43. settings.ALLOW_SIGNUP = False
  44. request = HttpRequest()
  45. request.method = 'POST'
  46. response = SignupView.as_view()(request, as_string=True)
  47. settings.ALLOW_SIGNUP = ALLOW_SIGNUP
  48. self.assertEqual(response.status_code, 302)
  49. @override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage')
  50. class ViewsDBTest(TestCase):
  51. """Class for testing views with DB queries"""
  52. def test_form_submission(self):
  53. with setenv('ALLOW_SIGNUP', 'True'):
  54. self.factory = RequestFactory()
  55. if hasattr(settings, 'EMAIL_BACKEND'):
  56. EMAIL_BACKEND = settings.EMAIL_BACKEND
  57. else:
  58. EMAIL_BACKEND = False
  59. settings.EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
  60. request = self.factory.post('/signup')
  61. request.POST = {'username': 'username5648',
  62. 'email': 'email@example.com',
  63. 'password1': 'pwd0000Y00$$',
  64. 'password2': 'pwd0000Y00$$'
  65. }
  66. response = SignupView.as_view()(request)
  67. needle = '<span>emailed you instructions to activate your account</span>'
  68. if not EMAIL_BACKEND:
  69. delattr(settings, 'EMAIL_BACKEND')
  70. else:
  71. settings.EMAIL_BACKEND = EMAIL_BACKEND
  72. self.assertInHTML(needle, str(response.content))