2019-03-10 01:08:09 +00:00
|
|
|
"""passbook Core Authentication Test"""
|
|
|
|
import string
|
|
|
|
from random import SystemRandom
|
|
|
|
|
|
|
|
from django.contrib.auth.models import AnonymousUser
|
|
|
|
from django.contrib.sessions.middleware import SessionMiddleware
|
|
|
|
from django.test import RequestFactory, TestCase
|
|
|
|
from django.urls import reverse
|
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
from passbook.core.models import User
|
|
|
|
from passbook.factors.dummy.models import DummyFactor
|
|
|
|
from passbook.factors.password.models import PasswordFactor
|
|
|
|
from passbook.factors.view import AuthenticationView
|
2019-03-10 01:08:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestFactorAuthentication(TestCase):
|
|
|
|
"""passbook Core Authentication Test"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
2019-12-31 11:51:16 +00:00
|
|
|
self.password = "".join(
|
|
|
|
SystemRandom().choice(string.ascii_uppercase + string.digits)
|
|
|
|
for _ in range(8)
|
|
|
|
)
|
|
|
|
self.factor, _ = PasswordFactor.objects.get_or_create(
|
|
|
|
slug="password",
|
|
|
|
defaults={
|
|
|
|
"name": "password",
|
|
|
|
"slug": "password",
|
|
|
|
"order": 0,
|
|
|
|
"backends": ["django.contrib.auth.backends.ModelBackend"],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.user = User.objects.create_user(
|
|
|
|
username="test", email="test@test.test", password=self.password
|
|
|
|
)
|
2019-03-10 01:08:09 +00:00
|
|
|
|
|
|
|
def test_unauthenticated_raw(self):
|
|
|
|
"""test direct call to AuthenticationView"""
|
2019-12-31 11:51:16 +00:00
|
|
|
response = self.client.get(reverse("passbook_core:auth-process"))
|
2020-02-18 15:20:38 +00:00
|
|
|
# Response should be 400 since no pending user is set
|
|
|
|
self.assertEqual(response.status_code, 400)
|
2019-03-10 01:08:09 +00:00
|
|
|
|
|
|
|
def test_unauthenticated_prepared(self):
|
|
|
|
"""test direct call but with pending_uesr in session"""
|
2019-12-31 11:51:16 +00:00
|
|
|
request = RequestFactory().get(reverse("passbook_core:auth-process"))
|
2019-03-10 01:08:09 +00:00
|
|
|
request.user = AnonymousUser()
|
|
|
|
request.session = {}
|
|
|
|
request.session[AuthenticationView.SESSION_PENDING_USER] = self.user.pk
|
|
|
|
|
|
|
|
response = AuthenticationView.as_view()(request)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_no_factors(self):
|
|
|
|
"""Test with all factors disabled"""
|
|
|
|
self.factor.enabled = False
|
|
|
|
self.factor.save()
|
2019-12-31 11:51:16 +00:00
|
|
|
request = RequestFactory().get(reverse("passbook_core:auth-process"))
|
2019-03-10 01:08:09 +00:00
|
|
|
request.user = AnonymousUser()
|
|
|
|
request.session = {}
|
|
|
|
request.session[AuthenticationView.SESSION_PENDING_USER] = self.user.pk
|
|
|
|
|
|
|
|
response = AuthenticationView.as_view()(request)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
2019-12-31 11:51:16 +00:00
|
|
|
self.assertEqual(response.url, reverse("passbook_core:auth-denied"))
|
2019-03-10 01:08:09 +00:00
|
|
|
self.factor.enabled = True
|
|
|
|
self.factor.save()
|
|
|
|
|
|
|
|
def test_authenticated(self):
|
|
|
|
"""Test with already logged in user"""
|
|
|
|
self.client.force_login(self.user)
|
2019-12-31 11:51:16 +00:00
|
|
|
response = self.client.get(reverse("passbook_core:auth-process"))
|
2020-02-18 15:20:38 +00:00
|
|
|
# Response should be 400 since no pending user is set
|
|
|
|
self.assertEqual(response.status_code, 400)
|
2019-03-10 01:08:09 +00:00
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
def test_unauthenticated_post(self):
|
|
|
|
"""Test post request as unauthenticated user"""
|
2019-12-31 11:51:16 +00:00
|
|
|
request = RequestFactory().post(
|
|
|
|
reverse("passbook_core:auth-process"), data={"password": self.password}
|
|
|
|
)
|
2019-03-10 01:08:09 +00:00
|
|
|
request.user = AnonymousUser()
|
|
|
|
middleware = SessionMiddleware()
|
|
|
|
middleware.process_request(request)
|
2019-10-01 09:08:56 +00:00
|
|
|
request.session.save() # pylint: disable=no-member
|
2019-03-10 01:08:09 +00:00
|
|
|
request.session[AuthenticationView.SESSION_PENDING_USER] = self.user.pk
|
|
|
|
|
|
|
|
response = AuthenticationView.as_view()(request)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
2019-12-31 11:51:16 +00:00
|
|
|
self.assertEqual(response.url, reverse("passbook_core:overview"))
|
2019-03-10 01:08:09 +00:00
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
def test_unauthenticated_post_invalid(self):
|
|
|
|
"""Test post request as unauthenticated user"""
|
2019-12-31 11:51:16 +00:00
|
|
|
request = RequestFactory().post(
|
|
|
|
reverse("passbook_core:auth-process"),
|
|
|
|
data={"password": self.password + "a"},
|
|
|
|
)
|
2019-03-10 01:08:09 +00:00
|
|
|
request.user = AnonymousUser()
|
|
|
|
middleware = SessionMiddleware()
|
|
|
|
middleware.process_request(request)
|
2019-10-01 09:08:56 +00:00
|
|
|
request.session.save() # pylint: disable=no-member
|
2019-03-10 01:08:09 +00:00
|
|
|
request.session[AuthenticationView.SESSION_PENDING_USER] = self.user.pk
|
|
|
|
|
|
|
|
response = AuthenticationView.as_view()(request)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
def test_multifactor(self):
|
|
|
|
"""Test view with multiple active factors"""
|
2019-12-31 11:51:16 +00:00
|
|
|
DummyFactor.objects.get_or_create(name="dummy", slug="dummy", order=1)
|
|
|
|
request = RequestFactory().post(
|
|
|
|
reverse("passbook_core:auth-process"), data={"password": self.password}
|
|
|
|
)
|
2019-03-10 01:08:09 +00:00
|
|
|
request.user = AnonymousUser()
|
|
|
|
middleware = SessionMiddleware()
|
|
|
|
middleware.process_request(request)
|
2019-10-01 09:08:56 +00:00
|
|
|
request.session.save() # pylint: disable=no-member
|
2019-03-10 01:08:09 +00:00
|
|
|
request.session[AuthenticationView.SESSION_PENDING_USER] = self.user.pk
|
|
|
|
|
|
|
|
response = AuthenticationView.as_view()(request)
|
|
|
|
session_copy = request.session.items()
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
# Verify view redirects to itself after auth
|
2019-12-31 11:51:16 +00:00
|
|
|
self.assertEqual(response.url, reverse("passbook_core:auth-process"))
|
2019-03-10 01:08:09 +00:00
|
|
|
|
|
|
|
# Run another request with same session which should result in a logged in user
|
2019-12-31 11:51:16 +00:00
|
|
|
request = RequestFactory().post(reverse("passbook_core:auth-process"))
|
2019-03-10 01:08:09 +00:00
|
|
|
request.user = AnonymousUser()
|
|
|
|
middleware = SessionMiddleware()
|
|
|
|
middleware.process_request(request)
|
|
|
|
for key, value in session_copy:
|
|
|
|
request.session[key] = value
|
2019-12-31 11:51:16 +00:00
|
|
|
request.session.save() # pylint: disable=no-member
|
2019-03-10 01:08:09 +00:00
|
|
|
response = AuthenticationView.as_view()(request)
|
|
|
|
self.assertEqual(response.status_code, 302)
|
2019-12-31 11:51:16 +00:00
|
|
|
self.assertEqual(response.url, reverse("passbook_core:overview"))
|