-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests now start their own PostgreSQL
- Loading branch information
Showing
11 changed files
with
69 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.contrib.admin.views.decorators import staff_member_required | ||
from django.http import HttpResponse | ||
|
||
|
||
@staff_member_required | ||
def dashboard(request): | ||
return HttpResponse("<title>Django SQL Dashboard</title>") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[tool.isort] | ||
profile = "black" | ||
multi_line_output = 3 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[pytest] | ||
addopts = -p pytest_use_postgresql | ||
DJANGO_SETTINGS_MODULE = config.settings | ||
site_dirs = test_project/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import os | ||
|
||
import pytest | ||
from dj_database_url import parse | ||
from django.conf import settings | ||
from testing.postgresql import Postgresql | ||
|
||
_POSTGRESQL = Postgresql() | ||
|
||
|
||
@pytest.hookimpl(tryfirst=True) | ||
def pytest_load_initial_conftests(early_config, parser, args): | ||
os.environ["DJANGO_SETTINGS_MODULE"] = early_config.getini("DJANGO_SETTINGS_MODULE") | ||
settings.DATABASES["default"] = parse(_POSTGRESQL.url()) | ||
|
||
|
||
def pytest_unconfigure(config): | ||
_POSTGRESQL.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,7 @@ | ||
""" | ||
ASGI config for test_project project. | ||
It exposes the ASGI callable as a module-level variable named ``application``. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/ | ||
""" | ||
|
||
import os | ||
|
||
from django.core.asgi import get_asgi_application | ||
|
||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_project.settings") | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") | ||
|
||
application = get_asgi_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
from django.urls import path | ||
from django.contrib import admin | ||
from django.http import HttpResponse | ||
from django.urls import path | ||
|
||
from django_sql_dashboard.views import dashboard | ||
|
||
urlpatterns = [ | ||
path("dashboard", dashboard), | ||
path("admin/", admin.site.urls), | ||
path("200", lambda request: HttpResponse("Status 200")), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,7 @@ | ||
""" | ||
WSGI config for test_project project. | ||
It exposes the WSGI callable as a module-level variable named ``application``. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/ | ||
""" | ||
|
||
import os | ||
|
||
from django.core.wsgi import get_wsgi_application | ||
|
||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_project.settings") | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") | ||
|
||
application = get_wsgi_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def test_staff_users_only(client): | ||
response = client.get("/dashboard") | ||
assert response.status_code == 302 | ||
assert response.url == "/admin/login/?next=/dashboard" | ||
|
||
|
||
def test_staff_users_allowed(admin_client): | ||
response = admin_client.get("/dashboard") | ||
assert response.status_code == 200 | ||
assert b"<title>Django SQL Dashboard</title>" in response.content |