From e41d0d83c23f52fbdb7e32f826ad4e629a0798a2 Mon Sep 17 00:00:00 2001 From: Ben C Date: Fri, 11 Nov 2022 22:29:50 -0500 Subject: [PATCH] Re-Add Tests --- Users/forms.py | 86 +------------------------- Users/ldap_mock.py | 1 - tests/test_auth.py | 146 +++++++++++++++++++++++++-------------------- tests/test_misc.py | 24 +++++++- 4 files changed, 105 insertions(+), 152 deletions(-) diff --git a/Users/forms.py b/Users/forms.py index 118e48e..b3ee848 100644 --- a/Users/forms.py +++ b/Users/forms.py @@ -2,95 +2,11 @@ This file defines the forms used in the Users app """ -from re import fullmatch - -from django.conf import settings -from django.forms import ModelForm, TextInput, CharField, ValidationError +from django.forms import ModelForm from .models import User -class PartialInput(TextInput): - """ - This widget defines a field that utilizes bootstrap's form groups to auto-fill parts of it. - - :cvar template_name: The template this widget renders to - """ - - template_name = "widgets/partial-input.html" - - def __init__(self, *args, **kwargs): - """ - This function is run to instantiate the widget - """ - - self.prefixes = kwargs.get("prefixes", ()) - self.suffixes = kwargs.get("suffixes", ()) - super(PartialInput, self).__init__(*args, **kwargs) - - def format_value(self, value: str) -> str: - """ - This function defines how the widget's value will be rendered to a string - - :param value: The value of the widget - :type value: str - """ - - if value is not None and value != "": - value = super(PartialInput, self).format_value(value) - if len(self.prefixes) > 0: - value = value[len("".join(self.prefixes)) :] - if len(self.suffixes) > 0: - value = value[: len("".join(self.suffixes)) * -1] - return value - - def get_context(self, name, value, attrs) -> dict[str, object]: - """ - This function defines additional context to pass to the widget's template - - :param name: The name of the widget - :type name: str - :param value: The value of the widget - :type value: str - :param attrs: The attributes to render in the template - :type attrs: dict - :returns: context to pass to the template - :rtype: dict - """ - - context = super(PartialInput, self).get_context(name, value, attrs) - context["widget"]["prefixes"] = self.prefixes - context["widget"]["suffixes"] = self.suffixes - return context - - -class PartialField(CharField): - """ - This field utilizes bootstrap's form groups to autofill parts of it. - """ - - widget = PartialInput() - - is_admin = False - - def clean(self, value) -> str: - """ - This function is run to convert the value to a string - It adds the prefixes and suffixes to the value - - :param value: The value of the field - :type value: str - :returns: The value formatted to an email - :rtype: str - """ - - if self.is_admin is False: - value = super(PartialField, self).clean(value) - if not fullmatch(r"\d{3}", value): - raise ValidationError("Must be three digits") - return f'{"".join(self.widget.prefixes)}{value}{"".join(self.widget.suffixes)}' - - class FinishUserForm(ModelForm): """ This form is used to finish setting up a user when syncing them from ActiveDirectory diff --git a/Users/ldap_mock.py b/Users/ldap_mock.py index e293b1b..ca19e57 100644 --- a/Users/ldap_mock.py +++ b/Users/ldap_mock.py @@ -1,7 +1,6 @@ """ This file provides a fake ldap server to test with """ -from typing import Optional from uuid import uuid4 from django.conf import settings diff --git a/tests/test_auth.py b/tests/test_auth.py index 560ccb4..a9b4be1 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -155,68 +155,84 @@ class LDAPAuthTest(SimpleBaseCase): LDAPMockAuthentication.users = [] -# @override_settings(AUTHENTICATION_BACKENDS=['Users.ldap_mock.LDAPMockAuthentication'], LDAP_DOMAIN="example", -# LDAP_BASE_CONTEXT="ou=ITP Users,dc=itp,dc=example", LDAP_URL="0.0.0.0") -# class UserCleanupTest(SimpleBaseCase): -# url = reverse('user-cleanup') -# -# def setUp(self) -> None: -# LDAPMockAuthentication.users = { -# 'admin': { -# 'password': "admin_password123", -# 'first': "First", -# 'last': "Last", -# 'ou': None -# }, -# 'student': { -# 'password': "student_password123", -# 'first': "SFirst", -# 'last': "SLast", -# 'ou': "AM" -# } -# } -# self.client.post(reverse('login'), {'username': 'admin', 'password': "admin_password123"}) -# self.client.post(reverse('login'), {'username': 'student', 'password': "student_password123"}) -# self.old_user = User.objects.create_user(username='example\\old_user') -# self.old_user.set_unusable_password() -# self.old_user.save() -# self.student = User.objects.get(username="example\\student") -# self.admin = User.objects.get(username="example\\admin") -# self.client.force_login(self.admin) -# -# def test_cleanup_users(self): -# self.client.post(self.url, {'userPassword': "admin_password123"}) -# self.assertFalse(User.objects.filter(username="example\\old_user").exists()) -# self.assertTrue(User.objects.filter(username="example\\student").exists()) -# -# def test_no_password(self): -# response = self.client.post(self.url, {'userPassword': ""}) -# self.assertTrue(User.objects.filter(username="example\\old_user").exists()) -# self.assertTrue(User.objects.filter(username="example\\student").exists()) -# self.assertMessage(response, "Please provide a password") -# -# def test_password_wrong(self): -# response = self.client.post(self.url, {'userPassword': "wrong password"}) -# self.assertTrue(User.objects.filter(username="example\\old_user").exists()) -# self.assertTrue(User.objects.filter(username="example\\student").exists()) -# self.assertMessage(response, "The password you provided was incorrect, please check it and try again.") -# -# def test_insufficient_perms(self): -# self.student.is_superuser = True -# self.student.save() -# self.client.force_login(self.student) -# response = self.client.post(self.url, {'userPassword': "student_password123"}) -# self.assertTrue(User.objects.filter(username="example\\old_user").exists()) -# self.assertTrue(User.objects.filter(username="example\\student").exists()) -# self.assertMessage(response, "You lack permissions to perform this action.") - -# @override_settings(AUTHENTICATION_BACKENDS=['Users.ldap_auth.LDAPAuthentication']) -# def test_cant_connect(self) -> None: -# self.client.force_login(self.admin) -# response = self.client.post(self.url, {'userPassword': "admin_password123"}) -# self.assertTrue(User.objects.filter(username="example\\old_user").exists()) -# self.assertTrue(User.objects.filter(username="example\\student").exists()) -# self.assertMessage(response, "Can't connect to ActiveDirectory, please try again later.") -# -# def tearDown(self) -> None: -# LDAPMockAuthentication.users = [] +@override_settings( + AUTHENTICATION_BACKENDS=["Users.ldap_mock.LDAPMockAuthentication"], + LDAP_DOMAIN="example", + LDAP_BASE_CONTEXT="ou=ITP Users,dc=itp,dc=example", + LDAP_ADMIN_NAME="example\\admin", + LDAP_URL="0.0.0.0", +) +class UserCleanupTest(SimpleBaseCase): + url = reverse("user-cleanup") + + def setUp(self) -> None: + LDAPMockAuthentication.users = { + "admin": { + "password": "admin_password123", + "first": "First", + "last": "Last", + "ou": "AM", + }, + "student": { + "password": "student_password123", + "first": "SFirst", + "last": "SLast", + "ou": "AM", + }, + } + self.client.post( + reverse("login"), {"username": "admin", "password": "admin_password123"} + ) + self.client.post( + reverse("login"), {"username": "student", "password": "student_password123"} + ) + self.old_user = User.objects.create_user(username="example\\old_user") + self.old_user.set_unusable_password() + self.old_user.save() + self.student = User.objects.get(username="example\\student") + self.admin = User.objects.get(username="example\\admin") + self.client.force_login(self.admin) + + def test_cleanup_users(self): + self.client.post(self.url, {"userPassword": "admin_password123"}) + self.assertFalse(User.objects.filter(username="example\\old_user").exists()) + self.assertTrue(User.objects.filter(username="example\\student").exists()) + + def test_no_password(self): + response = self.client.post(self.url, {"userPassword": ""}) + self.assertTrue(User.objects.filter(username="example\\old_user").exists()) + self.assertTrue(User.objects.filter(username="example\\student").exists()) + self.assertMessage(response, "Please provide a password") + + def test_password_wrong(self): + response = self.client.post(self.url, {"userPassword": "wrong password"}) + self.assertTrue(User.objects.filter(username="example\\old_user").exists()) + self.assertTrue(User.objects.filter(username="example\\student").exists()) + self.assertMessage( + response, + "The password you provided was incorrect, please check it and try again.", + ) + + def test_insufficient_perms(self): + self.student.is_superuser = True + self.student.save() + self.client.force_login(self.student) + response = self.client.post(self.url, {"userPassword": "student_password123"}) + self.assertTrue(User.objects.filter(username="example\\old_user").exists()) + self.assertTrue(User.objects.filter(username="example\\student").exists()) + self.assertMessage(response, "You lack permissions to perform this action.") + + +@override_settings(AUTHENTICATION_BACKENDS=["Users.ldap_auth.LDAPAuthentication"]) +def test_cant_connect(self) -> None: + self.client.force_login(self.admin) + response = self.client.post(self.url, {"userPassword": "admin_password123"}) + self.assertTrue(User.objects.filter(username="example\\old_user").exists()) + self.assertTrue(User.objects.filter(username="example\\student").exists()) + self.assertMessage( + response, "Can't connect to ActiveDirectory, please try again later." + ) + + +def tearDown(self) -> None: + LDAPMockAuthentication.users = [] diff --git a/tests/test_misc.py b/tests/test_misc.py index 9b843de..cb37794 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -1,9 +1,31 @@ from django.test import TestCase, RequestFactory from django.urls import reverse +from Main.views import error_500_handler from Users.models import User from tests.testing_base import BaseCase -from Main.views import error_500_handler + + +class UserSetupTest(BaseCase): + test_users = BaseCase.USER_SINGLE_STUDENT + test_review = False + + def test_get_user_setup(self): + self.get( + "test-user", + reverse("user-setup", kwargs={"pk": self.users.get("test-user").id}), + ) + + def test_user_setup(self): + self.post( + "test-user", + reverse("user-setup", kwargs={"pk": self.users.get("test-user").id}), + {"receive_notifications": "false"}, + ) + self.assertEqual( + User.objects.get(pk=self.users.get("test-user").id).receive_notifications, + False, + ) class TestErrors(TestCase): -- knotmirror 0.0.1