Skip to content

Commit

Permalink
Create testproj default user in data migration
Browse files Browse the repository at this point in the history
  • Loading branch information
axnsan12 committed Dec 20, 2018
1 parent e98876b commit a7d3066
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 23 deletions.
1 change: 0 additions & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -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 -
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
15 changes: 0 additions & 15 deletions testproj/createsuperuser.py

This file was deleted.

37 changes: 37 additions & 0 deletions testproj/users/migrations/0001_create_admin_user.py
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]'
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)
]
Empty file.
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions tests/test_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
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
from drf_yasg.generators import OpenAPISchemaGenerator


def test_reference_schema(call_generate_swagger, db, reference_schema):
User.objects.create_superuser('admin', '[email protected]', '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
Expand Down

0 comments on commit a7d3066

Please sign in to comment.