add unittests, woo
This commit is contained in:
parent
ff9bc8aa70
commit
06b65a7882
|
@ -51,7 +51,10 @@ class AuditEntry(UUIDModel):
|
||||||
def create(action, request, **kwargs):
|
def create(action, request, **kwargs):
|
||||||
"""Create AuditEntry from arguments"""
|
"""Create AuditEntry from arguments"""
|
||||||
client_ip, _ = get_client_ip(request)
|
client_ip, _ = get_client_ip(request)
|
||||||
user = request.user
|
if not hasattr(request, 'user'):
|
||||||
|
user = None
|
||||||
|
else:
|
||||||
|
user = request.user
|
||||||
if isinstance(user, AnonymousUser):
|
if isinstance(user, AnonymousUser):
|
||||||
user = kwargs.get('user', None)
|
user = kwargs.get('user', None)
|
||||||
entry = AuditEntry.objects.create(
|
entry = AuditEntry.objects.create(
|
||||||
|
@ -60,7 +63,7 @@ class AuditEntry(UUIDModel):
|
||||||
# User 255.255.255.255 as fallback if IP cannot be determined
|
# User 255.255.255.255 as fallback if IP cannot be determined
|
||||||
request_ip=client_ip or '255.255.255.255',
|
request_ip=client_ip or '255.255.255.255',
|
||||||
context=kwargs)
|
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
|
return entry
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
"""passbook core login test"""
|
|
||||||
|
|
||||||
from django.test import TestCase
|
|
||||||
|
|
||||||
|
|
||||||
class LoginTest(TestCase):
|
|
||||||
"""Test login"""
|
|
||||||
|
|
||||||
def test(self):
|
|
||||||
"""Stub test"""
|
|
|
@ -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'))
|
|
@ -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)
|
|
@ -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'))
|
|
@ -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)
|
|
@ -11,6 +11,7 @@ from passbook.lib.config import CONFIG
|
||||||
|
|
||||||
class UserSettingsView(UpdateView):
|
class UserSettingsView(UpdateView):
|
||||||
"""Update User settings"""
|
"""Update User settings"""
|
||||||
|
|
||||||
template_name = 'user/settings.html'
|
template_name = 'user/settings.html'
|
||||||
form_class = UserDetailForm
|
form_class = UserDetailForm
|
||||||
|
|
||||||
|
|
Reference in New Issue