From 06b65a7882eb0617db6a16323352d44ed1bb1964 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 26 Feb 2019 10:57:05 +0100 Subject: [PATCH] add unittests, woo --- passbook/audit/models.py | 7 +- passbook/core/tests/test_login.py | 10 -- .../core/tests/test_views_authentication.py | 147 ++++++++++++++++++ passbook/core/tests/test_views_overview.py | 21 +++ passbook/core/tests/test_views_user.py | 43 +++++ passbook/core/tests/test_views_utils.py | 25 +++ passbook/core/views/user.py | 1 + 7 files changed, 242 insertions(+), 12 deletions(-) delete mode 100644 passbook/core/tests/test_login.py create mode 100644 passbook/core/tests/test_views_authentication.py create mode 100644 passbook/core/tests/test_views_overview.py create mode 100644 passbook/core/tests/test_views_user.py create mode 100644 passbook/core/tests/test_views_utils.py diff --git a/passbook/audit/models.py b/passbook/audit/models.py index 810b9e8ee..f43cd5564 100644 --- a/passbook/audit/models.py +++ b/passbook/audit/models.py @@ -51,7 +51,10 @@ class AuditEntry(UUIDModel): def create(action, request, **kwargs): """Create AuditEntry from arguments""" client_ip, _ = get_client_ip(request) - user = request.user + if not hasattr(request, 'user'): + user = None + else: + user = request.user if isinstance(user, AnonymousUser): user = kwargs.get('user', None) entry = AuditEntry.objects.create( @@ -60,7 +63,7 @@ class AuditEntry(UUIDModel): # User 255.255.255.255 as fallback if IP cannot be determined request_ip=client_ip or '255.255.255.255', context=kwargs) - LOGGER.debug("Logged %s from %s (%s)", action, request.user, client_ip) + LOGGER.debug("Logged %s from %s (%s)", action, user, client_ip) return entry def save(self, *args, **kwargs): diff --git a/passbook/core/tests/test_login.py b/passbook/core/tests/test_login.py deleted file mode 100644 index f55190b5f..000000000 --- a/passbook/core/tests/test_login.py +++ /dev/null @@ -1,10 +0,0 @@ -"""passbook core login test""" - -from django.test import TestCase - - -class LoginTest(TestCase): - """Test login""" - - def test(self): - """Stub test""" diff --git a/passbook/core/tests/test_views_authentication.py b/passbook/core/tests/test_views_authentication.py new file mode 100644 index 000000000..5b8ad3dcb --- /dev/null +++ b/passbook/core/tests/test_views_authentication.py @@ -0,0 +1,147 @@ +"""passbook Core Account Test""" + +from django.test import TestCase +from django.urls import reverse + +from passbook.core.forms.authentication import LoginForm, SignUpForm +from passbook.core.models import User + + +class TestAuthenticationViews(TestCase): + """passbook Core Account Test""" + + def setUp(self): + super().setUp() + self.sign_up_data = { + 'first_name': 'Test', + 'last_name': 'User', + 'username': 'beryjuorg', + 'email': 'unittest@passbook.beryju.org', + 'password': 'B3ryju0rg!', + 'password_repeat': 'B3ryju0rg!', + } + self.login_data = { + 'uid_field': 'unittest@example.com', + } + self.user = User.objects.create_superuser( + username='unittest user', + email='unittest@example.com', + password='test123') + + def test_sign_up_view(self): + """Test account.sign_up view (Anonymous)""" + self.client.logout() + response = self.client.get(reverse('passbook_core:auth-sign-up')) + self.assertEqual(response.status_code, 200) + + def test_login_view(self): + """Test account.login view (Anonymous)""" + self.client.logout() + response = self.client.get(reverse('passbook_core:auth-login')) + self.assertEqual(response.status_code, 200) + # test login with post + form = LoginForm(self.login_data) + self.assertTrue(form.is_valid()) + + response = self.client.post(reverse('passbook_core:auth-login'), data=form.cleaned_data) + self.assertEqual(response.status_code, 302) + + def test_logout_view(self): + """Test account.logout view""" + self.client.force_login(self.user) + response = self.client.get(reverse('passbook_core:auth-logout')) + self.assertEqual(response.status_code, 302) + + def test_sign_up_view_auth(self): + """Test account.sign_up view (Authenticated)""" + self.client.force_login(self.user) + response = self.client.get(reverse('passbook_core:auth-logout')) + self.assertEqual(response.status_code, 302) + + def test_login_view_auth(self): + """Test account.login view (Authenticated)""" + self.client.force_login(self.user) + response = self.client.get(reverse('passbook_core:auth-login')) + self.assertEqual(response.status_code, 302) + + def test_login_view_post(self): + """Test account.login view POST (Anonymous)""" + login_response = self.client.post(reverse('passbook_core:auth-login'), data=self.login_data) + self.assertEqual(login_response.status_code, 302) + self.assertEqual(login_response.url, reverse('passbook_core:auth-process')) + + def test_sign_up_view_post(self): + """Test account.sign_up view POST (Anonymous)""" + form = SignUpForm(self.sign_up_data) + self.assertTrue(form.is_valid()) + + response = self.client.post(reverse('passbook_core:auth-sign-up'), data=form.cleaned_data) + self.assertEqual(response.status_code, 302) + + # def test_reset_password_init_view(self): + # """Test account.reset_password_init view POST (Anonymous)""" + # form = SignUpForm(self.sign_up_data) + # self.assertTrue(form.is_valid()) + + # res = test_request(accounts.SignUpView.as_view(), + # method='POST', + # req_kwargs=form.cleaned_data) + # self.assertEqual(res.status_code, 302) + + # res = test_request(accounts.PasswordResetInitView.as_view()) + # self.assertEqual(res.status_code, 200) + + # def test_resend_confirmation(self): + # """Test AccountController.resend_confirmation""" + # form = SignUpForm(self.sign_up_data) + # self.assertTrue(form.is_valid()) + + # res = test_request(accounts.SignUpView.as_view(), + # method='POST', + # req_kwargs=form.cleaned_data) + # self.assertEqual(res.status_code, 302) + # user = User.objects.get(email=self.sign_up_data['email']) + # # Invalidate all other links for this user + # old_acs = AccountConfirmation.objects.filter( + # user=user) + # for old_ac in old_acs: + # old_ac.confirmed = True + # old_ac.save() + # # Create Account Confirmation UUID + # new_ac = AccountConfirmation.objects.create(user=user) + # self.assertFalse(new_ac.is_expired) + # on_user_confirm_resend.send( + # sender=None, + # user=user, + # request=None) + + # def test_reset_passowrd(self): + # """Test reset password POST""" + # # Signup user first + # sign_up_form = SignUpForm(self.sign_up_data) + # self.assertTrue(sign_up_form.is_valid()) + + # sign_up_res = test_request(accounts.SignUpView.as_view(), + # method='POST', + # req_kwargs=sign_up_form.cleaned_data) + # self.assertEqual(sign_up_res.status_code, 302) + + # user = User.objects.get(email=self.sign_up_data['email']) + # # Invalidate all other links for this user + # old_acs = AccountConfirmation.objects.filter( + # user=user) + # for old_ac in old_acs: + # old_ac.confirmed = True + # old_ac.save() + # # Create Account Confirmation UUID + # new_ac = AccountConfirmation.objects.create(user=user) + # self.assertFalse(new_ac.is_expired) + # uuid = AccountConfirmation.objects.filter(user=user).first().pk + # reset_res = test_request(accounts.PasswordResetFinishView.as_view(), + # method='POST', + # user=user, + # url_kwargs={'uuid': uuid}, + # req_kwargs=self.change_data) + + # self.assertEqual(reset_res.status_code, 302) + # self.assertEqual(reset_res.url, reverse('common-index')) diff --git a/passbook/core/tests/test_views_overview.py b/passbook/core/tests/test_views_overview.py new file mode 100644 index 000000000..4bfb6fa9a --- /dev/null +++ b/passbook/core/tests/test_views_overview.py @@ -0,0 +1,21 @@ +"""passbook user view tests""" +from django.shortcuts import reverse +from django.test import TestCase + +from passbook.core.models import User + + +class TestOverviewViews(TestCase): + """Test Overview Views""" + + def setUp(self): + super().setUp() + self.user = User.objects.create_superuser( + username='unittest user', + email='unittest@example.com', + password='test123') + self.client.force_login(self.user) + + def test_overview(self): + """Test UserSettingsView""" + self.assertEqual(self.client.get(reverse('passbook_core:overview')).status_code, 200) diff --git a/passbook/core/tests/test_views_user.py b/passbook/core/tests/test_views_user.py new file mode 100644 index 000000000..8c17fe2c4 --- /dev/null +++ b/passbook/core/tests/test_views_user.py @@ -0,0 +1,43 @@ +"""passbook user view tests""" +from django.shortcuts import reverse +from django.test import TestCase + +from passbook.core.forms.users import PasswordChangeForm +from passbook.core.models import User + + +class TestUserViews(TestCase): + """Test User Views""" + + def setUp(self): + super().setUp() + self.user = User.objects.create_superuser( + username='unittest user', + email='unittest@example.com', + password='test123') + self.client.force_login(self.user) + + def test_user_settings(self): + """Test UserSettingsView""" + self.assertEqual(self.client.get(reverse('passbook_core:user-settings')).status_code, 200) + + def test_user_delete(self): + """Test UserDeleteView""" + self.assertEqual(self.client.post(reverse('passbook_core:user-delete')).status_code, 302) + self.assertEqual(User.objects.filter(username='unittest user').exists(), False) + self.setUp() + + def test_user_change_password(self): + """Test UserChangePasswordView""" + form_data = { + 'password': 'test2', + 'password_repeat': 'test2' + } + form = PasswordChangeForm(data=form_data) + self.assertTrue(form.is_valid()) + self.assertEqual(self.client.get( + reverse('passbook_core:user-change-password')).status_code, 200) + self.assertEqual(self.client.post( + reverse('passbook_core:user-change-password'), data=form_data).status_code, 302) + self.user.refresh_from_db() + self.assertTrue(self.user.check_password('test2')) diff --git a/passbook/core/tests/test_views_utils.py b/passbook/core/tests/test_views_utils.py new file mode 100644 index 000000000..a9367fca4 --- /dev/null +++ b/passbook/core/tests/test_views_utils.py @@ -0,0 +1,25 @@ +"""passbook util view tests""" + +from django.test import RequestFactory, TestCase + +from passbook.core.views.utils import LoadingView, PermissionDeniedView + + +class TestUtilViews(TestCase): + """Test Utility Views""" + + def setUp(self): + self.factory = RequestFactory() + + def test_loading_view(self): + """Test loading view""" + request = self.factory.get('something') + response = LoadingView.as_view(target_url='somestring')(request) + response.render() + self.assertIn('somestring', response.content.decode('utf-8')) + + def test_permission_denied_view(self): + """Test PermissionDeniedView""" + request = self.factory.get('something') + response = PermissionDeniedView.as_view()(request) + self.assertEqual(response.status_code, 200) diff --git a/passbook/core/views/user.py b/passbook/core/views/user.py index fafcd3158..aed89e1aa 100644 --- a/passbook/core/views/user.py +++ b/passbook/core/views/user.py @@ -11,6 +11,7 @@ from passbook.lib.config import CONFIG class UserSettingsView(UpdateView): """Update User settings""" + template_name = 'user/settings.html' form_class = UserDetailForm