Skip to content

Commit

Permalink
#280 Set global password_hasher
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Nov 23, 2021
1 parent 1e772bf commit c56d2bf
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 66 deletions.
18 changes: 0 additions & 18 deletions chcemvediet/apps/accounts/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +0,0 @@
# vim: expandtab
# -*- coding: utf-8 -*-
from django.test import TestCase
from django.test.utils import override_settings


class AccountsTestCaseMixin(TestCase):

def _pre_setup(self):
super(AccountsTestCaseMixin, self)._pre_setup()
self.settings_override = override_settings(
PASSWORD_HASHERS=(u'django.contrib.auth.hashers.MD5PasswordHasher',),
)
self.settings_override.enable()

def _post_teardown(self):
self.settings_override.disable()
super(AccountsTestCaseMixin, self)._post_teardown()
10 changes: 3 additions & 7 deletions chcemvediet/apps/accounts/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
# vim: expandtab
# -*- coding: utf-8 -*-
import os

import lxml.html
from django.contrib.auth.models import User
from django.test import TestCase

from poleno.utils.urls import reverse

from . import AccountsTestCaseMixin


class LoginFormTest(AccountsTestCaseMixin, TestCase):
class LoginFormTest(TestCase):
u"""
Tests ``allauth`` ``account_login`` view using ``LoginForm`` form registered as
"account_login". Does not check ``account_login`` functionality, only checks functionality
Expand Down Expand Up @@ -51,7 +47,7 @@ def test_recaptcha_field_is_required(self):
response = self.client.post(reverse(u'account_login'), data, follow=True)
self.assertFormError(response, u'form', u'recaptcha', u'This field is required.')

class SignupFormTest(AccountsTestCaseMixin, TestCase):
class SignupFormTest(TestCase):
u"""
Tests ``allauth`` ``account_signup`` view using ``SignupForm`` form registered as
"account_signup". Does not check ``account_signup`` functionality, only checks functionality
Expand Down Expand Up @@ -207,7 +203,7 @@ def test_recaptcha_field_is_required(self):
response = self.client.post(reverse(u'account_signup'), data, follow=True)
self.assertFormError(response, u'form', u'recaptcha', u'This field is required.')

class ResetPasswordFormTest(AccountsTestCaseMixin, TestCase):
class ResetPasswordFormTest(TestCase):
u"""
Tests ``allauth`` ``password_reset`` view using ``ResetPasswordForm`` form registered as
"password_reset". Does not check ``password_reset`` functionality, only checks functionality
Expand Down
3 changes: 1 addition & 2 deletions chcemvediet/apps/accounts/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
from django.contrib.auth.models import User
from django.test import TestCase

from . import AccountsTestCaseMixin
from ..models import Profile
from ..signals import create_profile_on_user_post_save


class ProfileModelTest(AccountsTestCaseMixin, TestCase):
class ProfileModelTest(TestCase):
u"""
Tests ``Profile`` model.
"""
Expand Down
7 changes: 1 addition & 6 deletions chcemvediet/apps/accounts/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
# vim: expandtab
# -*- coding: utf-8 -*-
import functools
import mock

from django.contrib.auth.models import User
from django.test import TestCase

from poleno.utils.test import ViewTestCaseMixin
from poleno.utils.urls import reverse

from . import AccountsTestCaseMixin


class ProfileViewTest(AccountsTestCaseMixin, ViewTestCaseMixin, TestCase):
class ProfileViewTest(ViewTestCaseMixin, TestCase):
u"""
Tests ``profile()`` view registered as "accounts:profile".
"""
Expand Down
4 changes: 2 additions & 2 deletions chcemvediet/apps/inforequests/tests/test_views/test_mine.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def pre_mock_render(request, template, context):
WHERE "inforequests_branch"."inforequest_id" = %s
ORDER BY "inforequests_action"."created" DESC, "inforequests_action"."id" DESC
LIMIT 1
""") for _ in range(4)
]
""")
] * 4
patterns_successful_inforequests = [
squeeze(u"""
FROM "inforequests_action" WHERE "inforequests_action"."branch_id" = %s
Expand Down
24 changes: 12 additions & 12 deletions chcemvediet/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from django.template import Context, Template
from django.test import TestCase
from django.test.runner import DiscoverRunner
from django.test.utils import override_settings

from poleno.attachments.models import Attachment
from poleno.mail.models import Message, Recipient
Expand All @@ -28,22 +27,19 @@ class CustomTestRunner(DiscoverRunner):
-- Disabled logging while testing.
Source: http://stackoverflow.com/questions/5255657/how-can-i-disable-logging-while-running-unit-tests-in-python-django
-- Forced language code 'en'
-- Used MD5 password hasher, it's much faster then the production hasher.
-- Mocked google recaptcha.
Source: https://pypi.org/project/django-recaptcha/1.0.6/#unit-testing
"""

def setup_test_environment(self, **kwargs):
super(CustomTestRunner, self).setup_test_environment(**kwargs)
settings.LANGUAGE_CODE = u'en'
settings.PASSWORD_HASHERS = [u'django.contrib.auth.hashers.MD5PasswordHasher']
os.environ[u'RECAPTCHA_TESTING'] = u'True'
self.settings_override = override_settings(
PASSWORD_HASHERS=(u'django.contrib.auth.hashers.MD5PasswordHasher',),
)
self.settings_override.enable()

def teardown_test_environment(self, **kwargs):
del os.environ[u'RECAPTCHA_TESTING']
self.settings_override.disable()
super(CustomTestRunner, self).teardown_test_environment(**kwargs)

def run_tests(self, *args, **kwargs):
Expand All @@ -56,23 +52,23 @@ def _pre_setup(self):
super(ChcemvedietTestCaseMixin, self)._pre_setup()
self.counter = itertools.count()
self.user = self._create_user()
self.user1 = self._create_user()
self.user2 = self._create_user()
self.message = self._create_message()
self.recipient = self._create_recipient()
self.region = self._create_region()
self.district = self._create_district()
self.municipality = self._create_municipality()
self.neighbourhood = self._create_neighbourhood()
self.obligee = self._create_obligee()
self.obligee1 = self._create_obligee()
self.obligee2 = self._create_obligee()
self.obligee3 = self._create_obligee()
self.tag = self._create_obligee_tag()
self.group = self._create_obligee_group()
self.inforequest = self._create_inforequest()
self.branch = self._create_branch()
self.action = self._create_action()
self.user1 = self._create_user()
self.user2 = self._create_user()
self.obligee1 = self._create_obligee()
self.obligee2 = self._create_obligee()
self.obligee3 = self._create_obligee()


def _call_with_defaults(self, func, kwargs, defaults):
Expand Down Expand Up @@ -112,7 +108,11 @@ def _create_user(self, **kwargs):
def _login_user(self, user=None, password=u'default_testing_secret'):
if user is None:
user = self.user
self.client.login(username=user.username, password=password)
if not self.client.login(username=user.username, password=password):
raise Exception(
u'The provided credentials are incorrect:\nusername: {} password: {}.'.format(
user.username, password
))

def _logout_user(self):
self.client.logout()
Expand Down
6 changes: 0 additions & 6 deletions chcemvediet/tests/test_cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from django.contrib.auth.models import User
from django.contrib.sessions.models import Session
from django.test import TestCase
from django.test.utils import override_settings

from poleno.timewarp import timewarp
from poleno.utils.date import utc_datetime_from_local
Expand All @@ -20,13 +19,8 @@ def _pre_setup(self):
super(ClearExpiredSessionsCronjobTest, self)._pre_setup()
timewarp.enable()
timewarp.reset()
self.settings_override = override_settings(
PASSWORD_HASHERS=(u'django.contrib.auth.hashers.MD5PasswordHasher',),
)
self.settings_override.enable()

def _post_teardown(self):
self.settings_override.disable()
timewarp.reset()
super(ClearExpiredSessionsCronjobTest, self)._post_teardown()

Expand Down
1 change: 0 additions & 1 deletion poleno/attachments/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def setUp(self):

self.settings_override = override_settings(
MEDIA_ROOT=self.tempdir.path,
PASSWORD_HASHERS=(u'django.contrib.auth.hashers.MD5PasswordHasher',),
)
self.settings_override.enable()

Expand Down
1 change: 0 additions & 1 deletion poleno/attachments/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def setUp(self):

self.settings_override = override_settings(
MEDIA_ROOT=self.tempdir.path,
PASSWORD_HASHERS=(u'django.contrib.auth.hashers.MD5PasswordHasher',),
)
self.settings_override.enable()

Expand Down
1 change: 0 additions & 1 deletion poleno/attachments/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def setUp(self):

self.settings_override = override_settings(
MEDIA_ROOT=self.tempdir.path,
PASSWORD_HASHERS=(u'django.contrib.auth.hashers.MD5PasswordHasher',),
)
self.settings_override.enable()

Expand Down
9 changes: 0 additions & 9 deletions poleno/timewarp/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.test import TestCase
from django.test.utils import override_settings

from poleno.utils.urls import reverse

Expand All @@ -21,20 +20,12 @@ class TimewarpAdminTest(TestCase):
def setUp(self):
timewarp.enable()
timewarp.reset()

self.settings_override = override_settings(
PASSWORD_HASHERS=(u'django.contrib.auth.hashers.MD5PasswordHasher',),
)
self.settings_override.enable()

self.admin = User.objects.create_superuser(username=u'admin', email=u'[email protected]', password=u'top_secret')
self.client.login(username=u'admin', password=u'top_secret')

def tearDown(self):
timewarp.reset()

self.settings_override.disable()


def _parse_dt(self, value):
dt = datetime.datetime.strptime(value, u'%Y-%m-%d %H:%M:%S')
Expand Down
1 change: 0 additions & 1 deletion poleno/utils/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def login_required_with_exception_view(request):

def setUp(self):
self.settings_override = override_settings(
PASSWORD_HASHERS=(u'django.contrib.auth.hashers.MD5PasswordHasher',),
TEMPLATE_LOADERS=(u'django.template.loaders.filesystem.Loader',),
)
self.settings_override.enable()
Expand Down

0 comments on commit c56d2bf

Please sign in to comment.