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.

81 lines
3.2 KiB

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