Skip to content

Commit

Permalink
Enable test running and debugging in VS Code
Browse files Browse the repository at this point in the history
Enable test running and debugging through VS Code, using pytest. Also remove Django Debug Toolbar because it was causing issues when running the tests through VS Code, and I haven't really used it since I added it.
  • Loading branch information
iamlogand committed Dec 2, 2023
1 parent 8964168 commit 510c172
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 104 deletions.
2 changes: 1 addition & 1 deletion backend/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"args": ["runserver"],
"django": true,
"justMyCode": true
}
},
]
}
3 changes: 2 additions & 1 deletion backend/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"editor.tabSize": 4,
"editor.insertSpaces": true
"editor.insertSpaces": true,
"python.testing.pytestEnabled": true,
}
3 changes: 3 additions & 0 deletions backend/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
DJANGO_SETTINGS_MODULE=rorapp.settings
python_files=*tests.py
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ daphne==4.0.0
dj-database-url==2.0.0
Django==4.2.3
django-cors-headers==3.13.0
django-debug-toolbar==4.1.0
djangorestframework==3.14.0
djangorestframework-simplejwt==5.2.2
psycopg2-binary==2.9.6
python-dotenv==0.21.0
pytest==7.4.0
pytest-django==4.7.0
6 changes: 0 additions & 6 deletions backend/rorsite/middleware.py

This file was deleted.

145 changes: 61 additions & 84 deletions backend/rorsite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,103 +20,87 @@

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
load_dotenv(os.path.join(BASE_DIR, '.env'))
load_dotenv(os.path.join(BASE_DIR, ".env"))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv('SECRET_KEY')
SECRET_KEY = os.getenv("SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True if os.getenv('DEBUG') == "True" else False
DEBUG = True if os.getenv("DEBUG") == "True" else False

ALLOWED_HOSTS = [
'localhost',
'127.0.0.1',
'api.roronline.com',
'www.roronline.com'
]
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "api.roronline.com", "www.roronline.com"]

CORS_ALLOWED_ORIGINS = [
os.getenv('FRONTEND_ORIGIN')
]
CORS_ALLOWED_ORIGINS = [os.getenv("FRONTEND_ORIGIN")]

CSRF_TRUSTED_ORIGINS = [
'https://api.roronline.com',
'http://127.0.0.1:8000'
]
CSRF_TRUSTED_ORIGINS = ["https://api.roronline.com", "http://127.0.0.1:8000"]

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")


# Application definition

INSTALLED_APPS = [
'daphne',
'rorapp.apps.RorappConfig',
'rest_framework',
'corsheaders',
'channels',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"daphne",
"rorapp.apps.RorappConfig",
"rest_framework",
"corsheaders",
"channels",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]

if 'test' not in sys.argv:
# Add debug toolbar app when not testing
INSTALLED_APPS.append('debug_toolbar')

MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"corsheaders.middleware.CorsMiddleware",
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

if 'test' not in sys.argv:
# Add debug toolbar middleware when not testing
MIDDLEWARE.insert(0, 'rorsite.middleware.InternalIPsDebugToolbarMiddleware')

ROOT_URLCONF = 'rorsite.urls'
ROOT_URLCONF = "rorsite.urls"

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]

WSGI_APPLICATION = 'rorsite.wsgi.application'
WSGI_APPLICATION = "rorsite.wsgi.application"


# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
'default': dj_database_url.config(default='postgres://{}:{}@{}:{}/{}'.format(
os.getenv('RDS_USERNAME'),
os.getenv('RDS_PASSWORD'),
os.getenv('RDS_HOSTNAME'),
os.getenv('RDS_PORT'),
os.getenv('RDS_NAME')
))
"default": dj_database_url.config(
default="postgres://{}:{}@{}:{}/{}".format(
os.getenv("RDS_USERNAME"),
os.getenv("RDS_PASSWORD"),
os.getenv("RDS_HOSTNAME"),
os.getenv("RDS_PORT"),
os.getenv("RDS_NAME"),
)
)
}


Expand All @@ -125,26 +109,26 @@

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]


# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/

LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = "en-us"

TIME_ZONE = 'UTC'
TIME_ZONE = "UTC"

USE_I18N = True

Expand All @@ -154,36 +138,29 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")


# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"


# Authentication

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
"DEFAULT_AUTHENTICATION_CLASSES": (
"rest_framework_simplejwt.authentication.JWTAuthentication",
"rest_framework.authentication.BasicAuthentication",
"rest_framework.authentication.SessionAuthentication",
)
}

SIMPLE_JWT = {
"ACCESS_TOKEN_LIFETIME": timedelta(hours=1),
"REFRESH_TOKEN_LIFETIME": timedelta(days=1)
}


# Debug toolbar

DEBUG_TOOLBAR_CONFIG = {
"SHOW_TOOLBAR_CALLBACK": lambda r: DEBUG,
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
}


Expand All @@ -195,7 +172,7 @@
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [(os.getenv('REDIS_HOSTNAME'), 6379)],
"hosts": [(os.getenv("REDIS_HOSTNAME"), 6379)],
},
},
}
12 changes: 1 addition & 11 deletions backend/rorsite/urls.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
from django.urls import path, include
from django.contrib import admin
from django.conf import settings

urlpatterns = [
path('', include('rorapp.urls')),
path('admin/', admin.site.urls)
]

if settings.DEBUG:
import debug_toolbar
urlpatterns = [
path('__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
urlpatterns = [path("", include("rorapp.urls")), path("admin/", admin.site.urls)]

0 comments on commit 510c172

Please sign in to comment.