2018-01-02 13:35:56 +03:00
|
|
|
import os
|
|
|
|
import sys
|
2014-03-02 15:40:30 +04:00
|
|
|
|
2018-01-02 13:35:56 +03:00
|
|
|
import django
|
|
|
|
from django.core import management
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption('--no-pkgroot', action='store_true', default=False,
|
|
|
|
help='Remove package root directory from sys.path, ensuring that '
|
|
|
|
'rest_framework is imported from the installed site-packages. '
|
|
|
|
'Used for testing the distribution.')
|
|
|
|
parser.addoption('--staticfiles', action='store_true', default=False,
|
|
|
|
help='Run tests with static files collection, using manifest '
|
|
|
|
'staticfiles storage. Used for testing the distribution.')
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
from django.conf import settings
|
2016-10-10 15:03:46 +03:00
|
|
|
|
2021-09-22 11:02:28 +03:00
|
|
|
# USE_L10N is deprecated, and will be removed in Django 5.0.
|
|
|
|
use_l10n = {"USE_L10N": True} if django.VERSION < (4, 0) else {}
|
2014-03-02 15:40:30 +04:00
|
|
|
settings.configure(
|
|
|
|
DEBUG_PROPAGATE_EXCEPTIONS=True,
|
2016-06-01 17:31:00 +03:00
|
|
|
DATABASES={
|
|
|
|
'default': {
|
|
|
|
'ENGINE': 'django.db.backends.sqlite3',
|
|
|
|
'NAME': ':memory:'
|
2021-03-03 14:15:39 +03:00
|
|
|
},
|
|
|
|
'secondary': {
|
|
|
|
'ENGINE': 'django.db.backends.sqlite3',
|
|
|
|
'NAME': ':memory:'
|
2016-06-01 17:31:00 +03:00
|
|
|
}
|
|
|
|
},
|
2014-09-01 12:07:05 +04:00
|
|
|
SITE_ID=1,
|
2014-03-02 15:40:30 +04:00
|
|
|
SECRET_KEY='not very secret in tests',
|
|
|
|
USE_I18N=True,
|
|
|
|
STATIC_URL='/static/',
|
|
|
|
ROOT_URLCONF='tests.urls',
|
2016-06-01 17:31:00 +03:00
|
|
|
TEMPLATES=[
|
|
|
|
{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'APP_DIRS': True,
|
2017-10-02 14:26:44 +03:00
|
|
|
'OPTIONS': {
|
|
|
|
"debug": True, # We want template errors to raise
|
|
|
|
}
|
2016-06-01 17:31:00 +03:00
|
|
|
},
|
|
|
|
],
|
2018-01-02 13:35:56 +03:00
|
|
|
MIDDLEWARE=(
|
|
|
|
'django.middleware.common.CommonMiddleware',
|
|
|
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
|
|
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
|
|
'django.contrib.messages.middleware.MessageMiddleware',
|
|
|
|
),
|
2014-03-02 15:40:30 +04:00
|
|
|
INSTALLED_APPS=(
|
2018-03-09 21:04:15 +03:00
|
|
|
'django.contrib.admin',
|
2014-03-02 15:40:30 +04:00
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes',
|
|
|
|
'django.contrib.sessions',
|
|
|
|
'django.contrib.sites',
|
2014-05-01 10:36:18 +04:00
|
|
|
'django.contrib.staticfiles',
|
2014-03-02 15:40:30 +04:00
|
|
|
'rest_framework',
|
|
|
|
'rest_framework.authtoken',
|
2019-02-14 11:30:53 +03:00
|
|
|
'tests.authentication',
|
2019-02-14 16:29:57 +03:00
|
|
|
'tests.generic_relations',
|
2022-08-30 04:55:27 +03:00
|
|
|
'tests.issue',
|
2017-11-06 17:56:57 +03:00
|
|
|
'tests.importable',
|
2014-03-02 15:40:30 +04:00
|
|
|
'tests',
|
|
|
|
),
|
|
|
|
PASSWORD_HASHERS=(
|
|
|
|
'django.contrib.auth.hashers.MD5PasswordHasher',
|
|
|
|
),
|
2021-09-22 11:02:28 +03:00
|
|
|
**use_l10n,
|
2014-03-02 15:40:30 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
# guardian is optional
|
2020-09-07 21:00:17 +03:00
|
|
|
try:
|
|
|
|
import guardian # NOQA
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
settings.ANONYMOUS_USER_ID = -1
|
|
|
|
settings.AUTHENTICATION_BACKENDS = (
|
|
|
|
'django.contrib.auth.backends.ModelBackend',
|
|
|
|
'guardian.backends.ObjectPermissionBackend',
|
|
|
|
)
|
|
|
|
settings.INSTALLED_APPS += (
|
|
|
|
'guardian',
|
|
|
|
)
|
2014-04-17 16:30:33 +04:00
|
|
|
|
2018-01-02 13:35:56 +03:00
|
|
|
if config.getoption('--no-pkgroot'):
|
|
|
|
sys.path.pop(0)
|
|
|
|
|
|
|
|
# import rest_framework before pytest re-adds the package root directory.
|
|
|
|
import rest_framework
|
|
|
|
package_dir = os.path.join(os.getcwd(), 'rest_framework')
|
|
|
|
assert not rest_framework.__file__.startswith(package_dir)
|
|
|
|
|
|
|
|
# Manifest storage will raise an exception if static files are not present (ie, a packaging failure).
|
|
|
|
if config.getoption('--staticfiles'):
|
|
|
|
import rest_framework
|
|
|
|
settings.STATIC_ROOT = os.path.join(os.path.dirname(rest_framework.__file__), 'static-root')
|
|
|
|
settings.STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
|
|
|
|
|
|
|
|
django.setup()
|
|
|
|
|
|
|
|
if config.getoption('--staticfiles'):
|
|
|
|
management.call_command('collectstatic', verbosity=0, interactive=False)
|