| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# This program is distributed in the hope that it will be useful, but WITHOUT |
| 3 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 4 |
# FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for |
| 5 |
# more details. |
| 6 |
# |
| 7 |
# You should have received a copy of the GNU General Public License version 3 |
| 8 |
# along with this program; if not, write to the Free Software Foundation, Inc., 51 |
| 9 |
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 10 |
# |
| 11 |
# (c) 2016-2025 Valentin Samir |
| 12 |
"""Tests module for utils""" |
| 13 |
import django |
| 14 |
from django.test import TestCase, RequestFactory |
| 15 |
from django.db import connection |
| 16 |
|
| 17 |
import warnings |
| 18 |
import datetime |
| 19 |
|
| 20 |
from cas_server import utils |
| 21 |
|
| 22 |
|
| 23 |
class CheckPasswordCase(TestCase): |
| 24 |
"""Tests for the utils function `utils.check_password`""" |
| 25 |
|
| 26 |
def setUp(self): |
| 27 |
"""Generate random bytes string that will be used ass passwords""" |
| 28 |
self.password1 = utils.gen_saml_id() |
| 29 |
self.password2 = utils.gen_saml_id() |
| 30 |
if not isinstance(self.password1, bytes): # pragma: no cover executed only in python3 |
| 31 |
self.password1 = self.password1.encode("utf8") |
| 32 |
self.password2 = self.password2.encode("utf8") |
| 33 |
|
| 34 |
def test_setup(self): |
| 35 |
"""check that generated password are bytes""" |
| 36 |
self.assertIsInstance(self.password1, bytes) |
| 37 |
self.assertIsInstance(self.password2, bytes) |
| 38 |
|
| 39 |
def test_plain(self): |
| 40 |
"""test the plain auth method""" |
| 41 |
self.assertTrue(utils.check_password("plain", self.password1, self.password1, "utf8")) |
| 42 |
self.assertFalse(utils.check_password("plain", self.password1, self.password2, "utf8")) |
| 43 |
|
| 44 |
def test_plain_unicode(self): |
| 45 |
"""test the plain auth method with unicode input""" |
| 46 |
self.assertTrue( |
| 47 |
utils.check_password( |
| 48 |
"plain", |
| 49 |
self.password1.decode("utf8"), |
| 50 |
self.password1.decode("utf8"), |
| 51 |
"utf8" |
| 52 |
) |
| 53 |
) |
| 54 |
self.assertFalse( |
| 55 |
utils.check_password( |
| 56 |
"plain", |
| 57 |
self.password1.decode("utf8"), |
| 58 |
self.password2.decode("utf8"), |
| 59 |
"utf8" |
| 60 |
) |
| 61 |
) |
| 62 |
|
| 63 |
def test_crypt(self): |
| 64 |
"""test the crypt auth method""" |
| 65 |
# Only run test if crypt is available |
| 66 |
if utils.crypt is None: |
| 67 |
return |
| 68 |
salts = ["$6$UVVAQvrMyXMF3FF3", "aa"] |
| 69 |
hashed_password1 = [] |
| 70 |
for salt in salts: |
| 71 |
hashed_password1.append( |
| 72 |
utils.crypt.crypt( |
| 73 |
self.password1.decode("utf8"), |
| 74 |
salt |
| 75 |
).encode("utf8") |
| 76 |
) |
| 77 |
|
| 78 |
for hp1 in hashed_password1: |
| 79 |
self.assertTrue(utils.check_password("crypt", self.password1, hp1, "utf8")) |
| 80 |
self.assertFalse(utils.check_password("crypt", self.password2, hp1, "utf8")) |
| 81 |
|
| 82 |
with self.assertRaises(ValueError): |
| 83 |
utils.check_password("crypt", self.password1, b"$truc$s$dsdsd", "utf8") |
| 84 |
|
| 85 |
def test_ldap_password_valid(self): |
| 86 |
"""test the ldap auth method with all the schemes""" |
| 87 |
salt = b"UVVAQvrMyXMF3FF3" |
| 88 |
schemes_salt = [b"{SMD5}", b"{SSHA}", b"{SSHA256}", b"{SSHA384}", b"{SSHA512}"] |
| 89 |
schemes_nosalt = [b"{MD5}", b"{SHA}", b"{SHA256}", b"{SHA384}", b"{SHA512}"] |
| 90 |
hashed_password1 = [] |
| 91 |
for scheme in schemes_salt: |
| 92 |
hashed_password1.append( |
| 93 |
utils.LdapHashUserPassword.hash(scheme, self.password1, salt, charset="utf8") |
| 94 |
) |
| 95 |
for scheme in schemes_nosalt: |
| 96 |
hashed_password1.append( |
| 97 |
utils.LdapHashUserPassword.hash(scheme, self.password1, charset="utf8") |
| 98 |
) |
| 99 |
if utils.crypt is not None: |
| 100 |
hashed_password1.append( |
| 101 |
utils.LdapHashUserPassword.hash( |
| 102 |
b"{CRYPT}", |
| 103 |
self.password1, |
| 104 |
b"$6$UVVAQvrMyXMF3FF3", |
| 105 |
charset="utf8" |
| 106 |
) |
| 107 |
) |
| 108 |
for hp1 in hashed_password1: |
| 109 |
self.assertIsInstance(hp1, bytes) |
| 110 |
self.assertTrue(utils.check_password("ldap", self.password1, hp1, "utf8")) |
| 111 |
self.assertFalse(utils.check_password("ldap", self.password2, hp1, "utf8")) |
| 112 |
|
| 113 |
def test_ldap_password_fail(self): |
| 114 |
"""test the ldap auth method with malformed hash or bad schemes""" |
| 115 |
salt = b"UVVAQvrMyXMF3FF3" |
| 116 |
schemes_salt = [b"{SMD5}", b"{SSHA}", b"{SSHA256}", b"{SSHA384}", b"{SSHA512}"] |
| 117 |
schemes_nosalt = [b"{MD5}", b"{SHA}", b"{SHA256}", b"{SHA384}", b"{SHA512}"] |
| 118 |
|
| 119 |
# first try to hash with bad parameters |
| 120 |
with self.assertRaises(utils.LdapHashUserPassword.BadScheme): |
| 121 |
utils.LdapHashUserPassword.hash(b"TOTO", self.password1) |
| 122 |
for scheme in schemes_nosalt: |
| 123 |
with self.assertRaises(utils.LdapHashUserPassword.BadScheme): |
| 124 |
utils.LdapHashUserPassword.hash(scheme, self.password1, salt) |
| 125 |
for scheme in schemes_salt: |
| 126 |
with self.assertRaises(utils.LdapHashUserPassword.BadScheme): |
| 127 |
utils.LdapHashUserPassword.hash(scheme, self.password1) |
| 128 |
if utils.crypt is not None: |
| 129 |
with self.assertRaises(utils.LdapHashUserPassword.BadSalt): |
| 130 |
utils.LdapHashUserPassword.hash(b'{CRYPT}', self.password1, b"$truc$toto") |
| 131 |
|
| 132 |
# then try to check hash with bad hashes |
| 133 |
with self.assertRaises(utils.LdapHashUserPassword.BadHash): |
| 134 |
utils.check_password("ldap", self.password1, b"TOTOssdsdsd", "utf8") |
| 135 |
for scheme in schemes_salt: |
| 136 |
# bad length |
| 137 |
with self.assertRaises(utils.LdapHashUserPassword.BadHash): |
| 138 |
utils.check_password("ldap", self.password1, scheme + b"dG90b3E8ZHNkcw==", "utf8") |
| 139 |
# bad base64 |
| 140 |
with self.assertRaises(utils.LdapHashUserPassword.BadHash): |
| 141 |
utils.check_password("ldap", self.password1, scheme + b"dG90b3E8ZHNkcw", "utf8") |
| 142 |
|
| 143 |
def test_hex(self): |
| 144 |
"""test all the hex_HASH method: the hashed password is a simple hash of the password""" |
| 145 |
hashes = ["md5", "sha1", "sha224", "sha256", "sha384", "sha512"] |
| 146 |
hashed_password1 = [] |
| 147 |
for hash_scheme in hashes: |
| 148 |
hashed_password1.append( |
| 149 |
( |
| 150 |
"hex_%s" % hash_scheme, |
| 151 |
getattr(utils.hashlib, hash_scheme)(self.password1).hexdigest() |
| 152 |
) |
| 153 |
) |
| 154 |
for (method, hp1) in hashed_password1: |
| 155 |
self.assertTrue(utils.check_password(method, self.password1, hp1, "utf8")) |
| 156 |
self.assertFalse(utils.check_password(method, self.password2, hp1, "utf8")) |
| 157 |
|
| 158 |
def test_bad_method(self): |
| 159 |
"""try to check password with a bad method, should raise a ValueError""" |
| 160 |
with self.assertRaises(ValueError): |
| 161 |
utils.check_password("test", self.password1, b"$truc$s$dsdsd", "utf8") |
| 162 |
|
| 163 |
|
| 164 |
class UtilsTestCase(TestCase): |
| 165 |
"""tests for some little utils functions""" |
| 166 |
def test_import_attr(self): |
| 167 |
""" |
| 168 |
test the import_attr function. Feeded with a dotted path string, it should |
| 169 |
import the dotted module and return that last componend of the dotted path |
| 170 |
(function, class or variable) |
| 171 |
""" |
| 172 |
with self.assertRaises(ImportError): |
| 173 |
utils.import_attr('toto.titi.tutu') |
| 174 |
with self.assertRaises(AttributeError): |
| 175 |
utils.import_attr('cas_server.utils.toto') |
| 176 |
with self.assertRaises(ValueError): |
| 177 |
utils.import_attr('toto') |
| 178 |
if django.VERSION < (3, 2): |
| 179 |
self.assertEqual( |
| 180 |
utils.import_attr('cas_server.default_app_config'), |
| 181 |
'cas_server.apps.CasAppConfig' |
| 182 |
) |
| 183 |
self.assertEqual(utils.import_attr(utils), utils) |
| 184 |
|
| 185 |
def test_update_url(self): |
| 186 |
""" |
| 187 |
test the update_url function. Given an url with possible GET parameter and a dict |
| 188 |
the function build a url with GET parameters updated by the dictionnary |
| 189 |
""" |
| 190 |
url1 = utils.update_url(u"https://www.example.com?toto=1", {u"tata": u"2"}) |
| 191 |
url2 = utils.update_url(b"https://www.example.com?toto=1", {b"tata": b"2"}) |
| 192 |
self.assertEqual(url1, u"https://www.example.com?tata=2&toto=1") |
| 193 |
self.assertEqual(url2, u"https://www.example.com?tata=2&toto=1") |
| 194 |
|
| 195 |
url3 = utils.update_url(u"https://www.example.com?toto=1", {u"toto": u"2"}) |
| 196 |
self.assertEqual(url3, u"https://www.example.com?toto=2") |
| 197 |
|
| 198 |
def test_crypt_salt_is_valid(self): |
| 199 |
"""test the function crypt_salt_is_valid who test if a crypt salt is valid""" |
| 200 |
self.assertFalse(utils.crypt_salt_is_valid("")) # len 0 |
| 201 |
self.assertFalse(utils.crypt_salt_is_valid("a")) # len 1 |
| 202 |
self.assertFalse(utils.crypt_salt_is_valid("$$")) # start with $ followed by $ |
| 203 |
self.assertFalse(utils.crypt_salt_is_valid("$toto")) # start with $ but no secondary $ |
| 204 |
self.assertFalse(utils.crypt_salt_is_valid("$toto$toto")) # algorithm toto not known |
| 205 |
|
| 206 |
def test_get_current_url(self): |
| 207 |
"""test the function get_current_url""" |
| 208 |
factory = RequestFactory() |
| 209 |
request = factory.get('/truc/muche?test=1') |
| 210 |
self.assertEqual(utils.get_current_url(request), 'http://testserver/truc/muche?test=1') |
| 211 |
self.assertEqual( |
| 212 |
utils.get_current_url(request, ignore_params={'test'}), |
| 213 |
'http://testserver/truc/muche' |
| 214 |
) |
| 215 |
|
| 216 |
def test_get_tuple(self): |
| 217 |
"""test the function get_tuple""" |
| 218 |
test_tuple = (1, 2, 3) |
| 219 |
for index, value in enumerate(test_tuple): |
| 220 |
self.assertEqual(utils.get_tuple(test_tuple, index), value) |
| 221 |
self.assertEqual(utils.get_tuple(test_tuple, 3), None) |
| 222 |
self.assertEqual(utils.get_tuple(test_tuple, 3, 'toto'), 'toto') |
| 223 |
self.assertEqual(utils.get_tuple(None, 3), None) |
| 224 |
|
| 225 |
def test_last_version(self): |
| 226 |
""" |
| 227 |
test the function last_version. An internet connection is needed, if you do not have |
| 228 |
one, this test will fail and you should ignore it. |
| 229 |
""" |
| 230 |
try: |
| 231 |
# first check if pypi is available |
| 232 |
utils.requests.get("https://pypi.org/simple/django-cas-server/") |
| 233 |
except utils.requests.exceptions.RequestException: |
| 234 |
warnings.warn( |
| 235 |
( |
| 236 |
"Pypi seems not available, perhaps you do not have internet access. " |
| 237 |
"Consequently, the test cas_server.tests.test_utils.UtilsTestCase.test_last_" |
| 238 |
"version is ignored" |
| 239 |
), |
| 240 |
RuntimeWarning |
| 241 |
) |
| 242 |
else: |
| 243 |
version = utils.last_version() |
| 244 |
self.assertIsInstance(version, str) |
| 245 |
self.assertEqual(len(version.split('.')), 3) |
| 246 |
|
| 247 |
# version is cached 24h so calling it a second time should return the save value |
| 248 |
self.assertEqual(version, utils.last_version()) |
| 249 |
|
| 250 |
def test_dictfetchall(self): |
| 251 |
"""test the function dictfetchall""" |
| 252 |
with connection.cursor() as curs: |
| 253 |
curs.execute("SELECT * FROM django_migrations") |
| 254 |
results = utils.dictfetchall(curs) |
| 255 |
self.assertIsInstance(results, list) |
| 256 |
self.assertTrue(len(results) > 0) |
| 257 |
for result in results: |
| 258 |
self.assertIsInstance(result, dict) |
| 259 |
self.assertIn('applied', result) |
| 260 |
self.assertIsInstance(result['applied'], datetime.datetime) |
| 261 |
|
| 262 |
def test_regexpr_validator(self): |
| 263 |
"""test the function regexpr_validator""" |
| 264 |
utils.regexpr_validator("^a$") |
| 265 |
with self.assertRaises(utils.ValidationError): |
| 266 |
utils.regexpr_validator("[") |