| 1 |
from django.contrib.auth.checks import ( |
| 2 |
check_middleware, |
| 3 |
check_models_permissions, |
| 4 |
check_user_model, |
| 5 |
) |
| 6 |
from django.contrib.auth.middleware import ( |
| 7 |
AuthenticationMiddleware, |
| 8 |
LoginRequiredMiddleware, |
| 9 |
) |
| 10 |
from django.contrib.auth.models import AbstractBaseUser |
| 11 |
from django.contrib.sessions.middleware import SessionMiddleware |
| 12 |
from django.core import checks |
| 13 |
from django.db import models |
| 14 |
from django.db.models import Q, UniqueConstraint |
| 15 |
from django.test import SimpleTestCase, override_settings, override_system_checks |
| 16 |
from django.test.utils import isolate_apps |
| 17 |
|
| 18 |
from .models import CustomUserNonUniqueUsername |
| 19 |
|
| 20 |
|
| 21 |
@isolate_apps("auth_tests", attr_name="apps") |
| 22 |
@override_system_checks([check_user_model]) |
| 23 |
class UserModelChecksTests(SimpleTestCase): |
| 24 |
@override_settings(AUTH_USER_MODEL="auth_tests.CustomUserNonListRequiredFields") |
| 25 |
def test_required_fields_is_list(self): |
| 26 |
"""REQUIRED_FIELDS should be a list.""" |
| 27 |
|
| 28 |
class CustomUserNonListRequiredFields(AbstractBaseUser): |
| 29 |
username = models.CharField(max_length=30, unique=True) |
| 30 |
date_of_birth = models.DateField() |
| 31 |
|
| 32 |
USERNAME_FIELD = "username" |
| 33 |
REQUIRED_FIELDS = "date_of_birth" |
| 34 |
|
| 35 |
errors = checks.run_checks(app_configs=self.apps.get_app_configs()) |
| 36 |
self.assertEqual( |
| 37 |
errors, |
| 38 |
[ |
| 39 |
checks.Error( |
| 40 |
"'REQUIRED_FIELDS' must be a list or tuple.", |
| 41 |
obj=CustomUserNonListRequiredFields, |
| 42 |
id="auth.E001", |
| 43 |
), |
| 44 |
], |
| 45 |
) |
| 46 |
|
| 47 |
@override_settings(AUTH_USER_MODEL="auth_tests.CustomUserBadRequiredFields") |
| 48 |
def test_username_not_in_required_fields(self): |
| 49 |
"""USERNAME_FIELD should not appear in REQUIRED_FIELDS.""" |
| 50 |
|
| 51 |
class CustomUserBadRequiredFields(AbstractBaseUser): |
| 52 |
username = models.CharField(max_length=30, unique=True) |
| 53 |
date_of_birth = models.DateField() |
| 54 |
|
| 55 |
USERNAME_FIELD = "username" |
| 56 |
REQUIRED_FIELDS = ["username", "date_of_birth"] |
| 57 |
|
| 58 |
errors = checks.run_checks(self.apps.get_app_configs()) |
| 59 |
self.assertEqual( |
| 60 |
errors, |
| 61 |
[ |
| 62 |
checks.Error( |
| 63 |
"The field named as the 'USERNAME_FIELD' for a custom user model " |
| 64 |
"must not be included in 'REQUIRED_FIELDS'.", |
| 65 |
hint=( |
| 66 |
"The 'USERNAME_FIELD' is currently set to 'username', you " |
| 67 |
"should remove 'username' from the 'REQUIRED_FIELDS'." |
| 68 |
), |
| 69 |
obj=CustomUserBadRequiredFields, |
| 70 |
id="auth.E002", |
| 71 |
), |
| 72 |
], |
| 73 |
) |
| 74 |
|
| 75 |
@override_settings(AUTH_USER_MODEL="auth_tests.CustomUserNonUniqueUsername") |
| 76 |
def test_username_non_unique(self): |
| 77 |
""" |
| 78 |
A non-unique USERNAME_FIELD raises an error only if the default |
| 79 |
authentication backend is used. Otherwise, a warning is raised. |
| 80 |
""" |
| 81 |
errors = checks.run_checks() |
| 82 |
self.assertEqual( |
| 83 |
errors, |
| 84 |
[ |
| 85 |
checks.Error( |
| 86 |
"'CustomUserNonUniqueUsername.username' must be " |
| 87 |
"unique because it is named as the 'USERNAME_FIELD'.", |
| 88 |
obj=CustomUserNonUniqueUsername, |
| 89 |
id="auth.E003", |
| 90 |
), |
| 91 |
], |
| 92 |
) |
| 93 |
with self.settings(AUTHENTICATION_BACKENDS=["my.custom.backend"]): |
| 94 |
errors = checks.run_checks() |
| 95 |
self.assertEqual( |
| 96 |
errors, |
| 97 |
[ |
| 98 |
checks.Warning( |
| 99 |
"'CustomUserNonUniqueUsername.username' is named as " |
| 100 |
"the 'USERNAME_FIELD', but it is not unique.", |
| 101 |
hint=( |
| 102 |
"Ensure that your authentication backend(s) can handle " |
| 103 |
"non-unique usernames." |
| 104 |
), |
| 105 |
obj=CustomUserNonUniqueUsername, |
| 106 |
id="auth.W004", |
| 107 |
), |
| 108 |
], |
| 109 |
) |
| 110 |
|
| 111 |
@override_settings(AUTH_USER_MODEL="auth_tests.CustomUserPartiallyUnique") |
| 112 |
def test_username_partially_unique(self): |
| 113 |
class CustomUserPartiallyUnique(AbstractBaseUser): |
| 114 |
username = models.CharField(max_length=30) |
| 115 |
USERNAME_FIELD = "username" |
| 116 |
|
| 117 |
class Meta: |
| 118 |
constraints = [ |
| 119 |
UniqueConstraint( |
| 120 |
fields=["username"], |
| 121 |
name="partial_username_unique", |
| 122 |
condition=Q(password__isnull=False), |
| 123 |
), |
| 124 |
] |
| 125 |
|
| 126 |
errors = checks.run_checks(app_configs=self.apps.get_app_configs()) |
| 127 |
self.assertEqual( |
| 128 |
errors, |
| 129 |
[ |
| 130 |
checks.Error( |
| 131 |
"'CustomUserPartiallyUnique.username' must be unique because " |
| 132 |
"it is named as the 'USERNAME_FIELD'.", |
| 133 |
obj=CustomUserPartiallyUnique, |
| 134 |
id="auth.E003", |
| 135 |
), |
| 136 |
], |
| 137 |
) |
| 138 |
with self.settings(AUTHENTICATION_BACKENDS=["my.custom.backend"]): |
| 139 |
errors = checks.run_checks(app_configs=self.apps.get_app_configs()) |
| 140 |
self.assertEqual( |
| 141 |
errors, |
| 142 |
[ |
| 143 |
checks.Warning( |
| 144 |
"'CustomUserPartiallyUnique.username' is named as the " |
| 145 |
"'USERNAME_FIELD', but it is not unique.", |
| 146 |
hint=( |
| 147 |
"Ensure that your authentication backend(s) can " |
| 148 |
"handle non-unique usernames." |
| 149 |
), |
| 150 |
obj=CustomUserPartiallyUnique, |
| 151 |
id="auth.W004", |
| 152 |
), |
| 153 |
], |
| 154 |
) |
| 155 |
|
| 156 |
@override_settings(AUTH_USER_MODEL="auth_tests.CustomUserUniqueConstraint") |
| 157 |
def test_username_unique_with_model_constraint(self): |
| 158 |
class CustomUserUniqueConstraint(AbstractBaseUser): |
| 159 |
username = models.CharField(max_length=30) |
| 160 |
USERNAME_FIELD = "username" |
| 161 |
|
| 162 |
class Meta: |
| 163 |
constraints = [ |
| 164 |
UniqueConstraint(fields=["username"], name="username_unique"), |
| 165 |
] |
| 166 |
|
| 167 |
self.assertEqual(checks.run_checks(app_configs=self.apps.get_app_configs()), []) |
| 168 |
with self.settings(AUTHENTICATION_BACKENDS=["my.custom.backend"]): |
| 169 |
errors = checks.run_checks(app_configs=self.apps.get_app_configs()) |
| 170 |
self.assertEqual(errors, []) |
| 171 |
|
| 172 |
@override_settings(AUTH_USER_MODEL="auth_tests.BadUser") |
| 173 |
def test_is_anonymous_authenticated_methods(self): |
| 174 |
""" |
| 175 |
<User Model>.is_anonymous/is_authenticated must not be methods. |
| 176 |
""" |
| 177 |
|
| 178 |
class BadUser(AbstractBaseUser): |
| 179 |
username = models.CharField(max_length=30, unique=True) |
| 180 |
USERNAME_FIELD = "username" |
| 181 |
|
| 182 |
def is_anonymous(self): |
| 183 |
return True |
| 184 |
|
| 185 |
def is_authenticated(self): |
| 186 |
return True |
| 187 |
|
| 188 |
errors = checks.run_checks(app_configs=self.apps.get_app_configs()) |
| 189 |
self.assertEqual( |
| 190 |
errors, |
| 191 |
[ |
| 192 |
checks.Critical( |
| 193 |
"%s.is_anonymous must be an attribute or property rather than " |
| 194 |
"a method. Ignoring this is a security issue as anonymous " |
| 195 |
"users will be treated as authenticated!" % BadUser, |
| 196 |
obj=BadUser, |
| 197 |
id="auth.C009", |
| 198 |
), |
| 199 |
checks.Critical( |
| 200 |
"%s.is_authenticated must be an attribute or property rather " |
| 201 |
"than a method. Ignoring this is a security issue as anonymous " |
| 202 |
"users will be treated as authenticated!" % BadUser, |
| 203 |
obj=BadUser, |
| 204 |
id="auth.C010", |
| 205 |
), |
| 206 |
], |
| 207 |
) |
| 208 |
|
| 209 |
|
| 210 |
@isolate_apps("auth_tests", attr_name="apps") |
| 211 |
@override_system_checks([check_models_permissions]) |
| 212 |
class ModelsPermissionsChecksTests(SimpleTestCase): |
| 213 |
def test_clashing_default_permissions(self): |
| 214 |
class Checked(models.Model): |
| 215 |
class Meta: |
| 216 |
permissions = [("change_checked", "Can edit permission (duplicate)")] |
| 217 |
|
| 218 |
errors = checks.run_checks(self.apps.get_app_configs()) |
| 219 |
self.assertEqual( |
| 220 |
errors, |
| 221 |
[ |
| 222 |
checks.Error( |
| 223 |
"The permission codenamed 'change_checked' clashes with a builtin " |
| 224 |
"permission for model 'auth_tests.Checked'.", |
| 225 |
obj=Checked, |
| 226 |
id="auth.E005", |
| 227 |
), |
| 228 |
], |
| 229 |
) |
| 230 |
|
| 231 |
def test_non_clashing_custom_permissions(self): |
| 232 |
class Checked(models.Model): |
| 233 |
class Meta: |
| 234 |
permissions = [ |
| 235 |
("my_custom_permission", "Some permission"), |
| 236 |
("other_one", "Some other permission"), |
| 237 |
] |
| 238 |
|
| 239 |
errors = checks.run_checks(self.apps.get_app_configs()) |
| 240 |
self.assertEqual(errors, []) |
| 241 |
|
| 242 |
def test_clashing_custom_permissions(self): |
| 243 |
class Checked(models.Model): |
| 244 |
class Meta: |
| 245 |
permissions = [ |
| 246 |
("my_custom_permission", "Some permission"), |
| 247 |
("other_one", "Some other permission"), |
| 248 |
( |
| 249 |
"my_custom_permission", |
| 250 |
"Some permission with duplicate permission code", |
| 251 |
), |
| 252 |
] |
| 253 |
|
| 254 |
errors = checks.run_checks(self.apps.get_app_configs()) |
| 255 |
self.assertEqual( |
| 256 |
errors, |
| 257 |
[ |
| 258 |
checks.Error( |
| 259 |
"The permission codenamed 'my_custom_permission' is duplicated for " |
| 260 |
"model 'auth_tests.Checked'.", |
| 261 |
obj=Checked, |
| 262 |
id="auth.E006", |
| 263 |
), |
| 264 |
], |
| 265 |
) |
| 266 |
|
| 267 |
def test_verbose_name_max_length(self): |
| 268 |
class Checked(models.Model): |
| 269 |
class Meta: |
| 270 |
verbose_name = ( |
| 271 |
"some ridiculously long verbose name that is out of control" * 5 |
| 272 |
) |
| 273 |
|
| 274 |
errors = checks.run_checks(self.apps.get_app_configs()) |
| 275 |
self.assertEqual( |
| 276 |
errors, |
| 277 |
[ |
| 278 |
checks.Error( |
| 279 |
"The verbose_name of model 'auth_tests.Checked' must be at most " |
| 280 |
"244 characters for its builtin permission names to be at most 255 " |
| 281 |
"characters.", |
| 282 |
obj=Checked, |
| 283 |
id="auth.E007", |
| 284 |
), |
| 285 |
], |
| 286 |
) |
| 287 |
|
| 288 |
def test_model_name_max_length(self): |
| 289 |
model_name = "X" * 94 |
| 290 |
model = type(model_name, (models.Model,), {"__module__": self.__module__}) |
| 291 |
errors = checks.run_checks(self.apps.get_app_configs()) |
| 292 |
self.assertEqual( |
| 293 |
errors, |
| 294 |
[ |
| 295 |
checks.Error( |
| 296 |
"The name of model 'auth_tests.%s' must be at most 93 " |
| 297 |
"characters for its builtin permission codenames to be at " |
| 298 |
"most 100 characters." % model_name, |
| 299 |
obj=model, |
| 300 |
id="auth.E011", |
| 301 |
), |
| 302 |
], |
| 303 |
) |
| 304 |
|
| 305 |
def test_custom_permission_name_max_length(self): |
| 306 |
custom_permission_name = ( |
| 307 |
"some ridiculously long verbose name that is out of control" * 5 |
| 308 |
) |
| 309 |
|
| 310 |
class Checked(models.Model): |
| 311 |
class Meta: |
| 312 |
permissions = [ |
| 313 |
("my_custom_permission", custom_permission_name), |
| 314 |
] |
| 315 |
|
| 316 |
errors = checks.run_checks(self.apps.get_app_configs()) |
| 317 |
self.assertEqual( |
| 318 |
errors, |
| 319 |
[ |
| 320 |
checks.Error( |
| 321 |
"The permission named '%s' of model 'auth_tests.Checked' is longer " |
| 322 |
"than 255 characters." % custom_permission_name, |
| 323 |
obj=Checked, |
| 324 |
id="auth.E008", |
| 325 |
), |
| 326 |
], |
| 327 |
) |
| 328 |
|
| 329 |
def test_custom_permission_codename_max_length(self): |
| 330 |
custom_permission_codename = "x" * 101 |
| 331 |
|
| 332 |
class Checked(models.Model): |
| 333 |
class Meta: |
| 334 |
permissions = [ |
| 335 |
(custom_permission_codename, "Custom permission"), |
| 336 |
] |
| 337 |
|
| 338 |
errors = checks.run_checks(self.apps.get_app_configs()) |
| 339 |
self.assertEqual( |
| 340 |
errors, |
| 341 |
[ |
| 342 |
checks.Error( |
| 343 |
"The permission codenamed '%s' of model 'auth_tests.Checked' " |
| 344 |
"is longer than 100 characters." % custom_permission_codename, |
| 345 |
obj=Checked, |
| 346 |
id="auth.E012", |
| 347 |
), |
| 348 |
], |
| 349 |
) |
| 350 |
|
| 351 |
def test_empty_default_permissions(self): |
| 352 |
class Checked(models.Model): |
| 353 |
class Meta: |
| 354 |
default_permissions = () |
| 355 |
|
| 356 |
self.assertEqual(checks.run_checks(self.apps.get_app_configs()), []) |
| 357 |
|
| 358 |
|
| 359 |
class LoginRequiredMiddlewareSubclass(LoginRequiredMiddleware): |
| 360 |
redirect_field_name = "redirect_to" |
| 361 |
|
| 362 |
|
| 363 |
class AuthenticationMiddlewareSubclass(AuthenticationMiddleware): |
| 364 |
pass |
| 365 |
|
| 366 |
|
| 367 |
class SessionMiddlewareSubclass(SessionMiddleware): |
| 368 |
pass |
| 369 |
|
| 370 |
|
| 371 |
@override_system_checks([check_middleware]) |
| 372 |
class MiddlewareChecksTests(SimpleTestCase): |
| 373 |
@override_settings( |
| 374 |
MIDDLEWARE=[ |
| 375 |
"auth_tests.test_checks.SessionMiddlewareSubclass", |
| 376 |
"auth_tests.test_checks.AuthenticationMiddlewareSubclass", |
| 377 |
"auth_tests.test_checks.LoginRequiredMiddlewareSubclass", |
| 378 |
] |
| 379 |
) |
| 380 |
def test_middleware_subclasses(self): |
| 381 |
errors = checks.run_checks() |
| 382 |
self.assertEqual(errors, []) |
| 383 |
|
| 384 |
@override_settings( |
| 385 |
MIDDLEWARE=[ |
| 386 |
"auth_tests.test_checks", |
| 387 |
"auth_tests.test_checks.NotExist", |
| 388 |
] |
| 389 |
) |
| 390 |
def test_invalid_middleware_skipped(self): |
| 391 |
errors = checks.run_checks() |
| 392 |
self.assertEqual(errors, []) |
| 393 |
|
| 394 |
@override_settings( |
| 395 |
MIDDLEWARE=[ |
| 396 |
"django.contrib.does.not.Exist", |
| 397 |
"django.contrib.sessions.middleware.SessionMiddleware", |
| 398 |
"django.contrib.auth.middleware.AuthenticationMiddleware", |
| 399 |
"django.contrib.auth.middleware.LoginRequiredMiddleware", |
| 400 |
] |
| 401 |
) |
| 402 |
def test_check_ignores_import_error_in_middleware(self): |
| 403 |
errors = checks.run_checks() |
| 404 |
self.assertEqual(errors, []) |
| 405 |
|
| 406 |
@override_settings( |
| 407 |
MIDDLEWARE=[ |
| 408 |
"django.contrib.sessions.middleware.SessionMiddleware", |
| 409 |
"django.contrib.auth.middleware.AuthenticationMiddleware", |
| 410 |
"django.contrib.auth.middleware.LoginRequiredMiddleware", |
| 411 |
] |
| 412 |
) |
| 413 |
def test_correct_order_with_login_required_middleware(self): |
| 414 |
errors = checks.run_checks() |
| 415 |
self.assertEqual(errors, []) |
| 416 |
|
| 417 |
@override_settings( |
| 418 |
MIDDLEWARE=[ |
| 419 |
"django.contrib.auth.middleware.LoginRequiredMiddleware", |
| 420 |
"django.contrib.auth.middleware.AuthenticationMiddleware", |
| 421 |
"django.contrib.sessions.middleware.SessionMiddleware", |
| 422 |
] |
| 423 |
) |
| 424 |
def test_incorrect_order_with_login_required_middleware(self): |
| 425 |
errors = checks.run_checks() |
| 426 |
self.assertEqual( |
| 427 |
errors, |
| 428 |
[ |
| 429 |
checks.Error( |
| 430 |
"In order to use django.contrib.auth.middleware." |
| 431 |
"LoginRequiredMiddleware, django.contrib.auth.middleware." |
| 432 |
"AuthenticationMiddleware must be defined before it in MIDDLEWARE.", |
| 433 |
id="auth.E013", |
| 434 |
) |
| 435 |
], |
| 436 |
) |
| 437 |
|
| 438 |
@override_settings( |
| 439 |
MIDDLEWARE=[ |
| 440 |
"django.contrib.auth.middleware.LoginRequiredMiddleware", |
| 441 |
] |
| 442 |
) |
| 443 |
def test_missing_authentication_with_login_required_middleware(self): |
| 444 |
errors = checks.run_checks() |
| 445 |
self.assertEqual( |
| 446 |
errors, |
| 447 |
[ |
| 448 |
checks.Error( |
| 449 |
"In order to use django.contrib.auth.middleware." |
| 450 |
"LoginRequiredMiddleware, django.contrib.auth.middleware." |
| 451 |
"AuthenticationMiddleware must be defined before it in MIDDLEWARE.", |
| 452 |
id="auth.E013", |
| 453 |
) |
| 454 |
], |
| 455 |
) |