diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 57893c34..7f606b7e 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -45,7 +45,6 @@ You want to contribute some code? Great! Here are a few steps to get you started (venv) $ cd testproj (venv) $ python manage.py migrate - (venv) $ python manage.py shell -c "import createsuperuser" (venv) $ python manage.py runserver (venv) $ firefox localhost:8000/swagger/ diff --git a/Procfile b/Procfile index 4141a3bd..89df9580 100644 --- a/Procfile +++ b/Procfile @@ -1,2 +1,2 @@ -release: python testproj/manage.py migrate && python testproj/manage.py shell -c "import createsuperuser" +release: python testproj/manage.py migrate web: gunicorn --chdir testproj testproj.wsgi --log-file - diff --git a/README.rst b/README.rst index e10f8bd2..8626a9b7 100644 --- a/README.rst +++ b/README.rst @@ -297,7 +297,6 @@ For additional usage examples, you can take a look at the test project in the `` (venv) $ python -m pip install -U pip setuptools (venv) $ pip install -U -r requirements.txt (venv) $ python manage.py migrate - (venv) $ python manage.py shell -c "import createsuperuser" (venv) $ python manage.py runserver (venv) $ firefox localhost:8000/swagger/ diff --git a/testproj/createsuperuser.py b/testproj/createsuperuser.py deleted file mode 100644 index 1fe82947..00000000 --- a/testproj/createsuperuser.py +++ /dev/null @@ -1,15 +0,0 @@ -from __future__ import print_function - -from django.contrib.auth.models import User -from django.db.utils import IntegrityError - -username = 'admin' -email = 'admin@admin.admin' -password = 'passwordadmin' - -try: - User.objects.create_superuser(username, email, password) -except IntegrityError: - print("User '%s <%s>' already exists" % (username, email)) -else: - print("Created superuser '%s <%s>' with password '%s'" % (username, email, password)) diff --git a/testproj/users/migrations/0001_create_admin_user.py b/testproj/users/migrations/0001_create_admin_user.py new file mode 100644 index 00000000..b59ee214 --- /dev/null +++ b/testproj/users/migrations/0001_create_admin_user.py @@ -0,0 +1,37 @@ +# Generated by Django 2.1.3 on 2018-12-19 08:07 +import sys + +from django.conf import settings +from django.contrib.auth.hashers import make_password +from django.db import migrations, IntegrityError + + +def add_default_user(apps, schema_editor): + username = 'admin' + email = 'admin@admin.admin' + password = 'passwordadmin' + User = apps.get_model(settings.AUTH_USER_MODEL) + + try: + admin = User( + username=username, + email=email, + password=make_password(password), + is_superuser=True, + is_staff=True + ) + admin.save() + except IntegrityError: + sys.stdout.write(" User '%s <%s>' already exists..." % (username, email)) + else: + sys.stdout.write(" Created superuser '%s <%s>' with password '%s'!" % (username, email, password)) + + +class Migration(migrations.Migration): + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.RunPython(add_default_user) + ] diff --git a/testproj/users/migrations/__init__.py b/testproj/users/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/conftest.py b/tests/conftest.py index 0c9e0c11..3cafaa75 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -22,8 +22,7 @@ def mock_schema_request(db): from rest_framework.test import force_authenticate factory = APIRequestFactory() - user = User.objects.create_user(username='admin', is_staff=True, is_superuser=True) - + user = User.objects.get(username='admin') request = factory.get('/swagger.json') force_authenticate(request, user=user) request = APIView().initialize_request(request) diff --git a/tests/test_management.py b/tests/test_management.py index 0ce1c557..3baf7886 100644 --- a/tests/test_management.py +++ b/tests/test_management.py @@ -6,7 +6,6 @@ from collections import OrderedDict import pytest -from django.contrib.auth.models import User from drf_yasg import openapi from drf_yasg.codecs import yaml_sane_load @@ -14,8 +13,6 @@ def test_reference_schema(call_generate_swagger, db, reference_schema): - User.objects.create_superuser('admin', 'admin@admin.admin', 'blabla') - output = call_generate_swagger(format='yaml', api_url='http://test.local:8002/', user='admin') output_schema = yaml_sane_load(output) assert output_schema == reference_schema