Merge pull request #19571 from open-craft/jill/dot-app-cmd

SE-614 Adds optional args to create_dot_application command
This commit is contained in:
Brian Mesick
2019-01-14 14:12:39 -05:00
committed by GitHub
2 changed files with 67 additions and 7 deletions

View File

@@ -40,12 +40,30 @@ class Command(BaseCommand):
dest='redirect_uris',
default='',
help='The redirect URI(s) for this application. Multiple URIs should be space separated.')
parser.add_argument('--public',
action='store_true',
dest='public',
default=False,
help='Make the application public? Confidential by default.')
parser.add_argument('--client-id',
action='store',
dest='client_id',
default='',
help='The client_id for this application. If omitted, one will be generated.')
parser.add_argument('--client-secret',
action='store',
dest='client_secret',
default='',
help='The client_secret for this application. If omitted, one will be generated.')
def handle(self, *args, **options):
app_name = options['name']
username = options['username']
grant_type = options['grant_type']
redirect_uris = options['redirect_uris']
client_type = Application.CLIENT_PUBLIC if options['public'] else Application.CLIENT_CONFIDENTIAL
client_id = options['client_id']
client_secret = options['client_secret']
user = User.objects.get(username=username)
@@ -56,13 +74,19 @@ class Command(BaseCommand):
))
return
application = Application.objects.create(
create_kwargs = dict(
name=app_name,
user=user,
redirect_uris=redirect_uris,
client_type=Application.CLIENT_CONFIDENTIAL,
client_type=client_type,
authorization_grant_type=grant_type
)
if client_id:
create_kwargs['client_id'] = client_id
if client_secret:
create_kwargs['client_secret'] = client_secret
application = Application.objects.create(**create_kwargs)
application.save()
logger.info('Created {} application with id: {}, client_id: {}, and client_secret: {}'.format(
app_name,

View File

@@ -3,6 +3,8 @@ Tests the ``create_dot_application`` management command.
"""
from __future__ import absolute_import, unicode_literals
import ddt
from django.core.management import call_command
from django.test import TestCase
from oauth2_provider.models import get_application_model
@@ -15,6 +17,7 @@ from ..create_dot_application import Command
Application = get_application_model()
@ddt.ddt
class TestCreateDotApplication(TestCase):
"""
Tests the ``create_dot_application`` management command.
@@ -27,20 +30,53 @@ class TestCreateDotApplication(TestCase):
super(TestCreateDotApplication, self).tearDown()
Application.objects.filter(user=self.user).delete()
def test_create_dot_application(self):
call_command(Command(), 'testing_application', self.user.username)
@ddt.data(
(None, None, None, None),
(None, None, 'client-abc', None),
(None, None, None, 'great-big-secret'),
('password', True, 'client-dce', 'has-a-great-big-secret'),
)
@ddt.unpack
def test_create_dot_application(self, grant_type, public, client_id, client_secret):
# Add optional arguments if provided
call_args = ['testing_application', self.user.username]
if grant_type:
call_args.append('--grant-type')
call_args.append(grant_type)
else:
grant_type = Application.GRANT_CLIENT_CREDENTIALS
if public:
call_args.append('--public')
client_type = Application.CLIENT_PUBLIC
else:
client_type = Application.CLIENT_CONFIDENTIAL
if client_id:
call_args.append('--client-id')
call_args.append(client_id)
if client_secret:
call_args.append('--client-secret')
call_args.append(client_secret)
call_command(Command(), *call_args)
apps = Application.objects.filter(name='testing_application')
self.assertEqual(1, len(apps))
application = apps[0]
self.assertEqual('testing_application', application.name)
self.assertEqual(self.user, application.user)
self.assertEqual(Application.GRANT_CLIENT_CREDENTIALS, application.authorization_grant_type)
self.assertEqual(Application.CLIENT_CONFIDENTIAL, application.client_type)
self.assertEqual(grant_type, application.authorization_grant_type)
self.assertEqual(client_type, application.client_type)
self.assertEqual('', application.redirect_uris)
if client_id:
self.assertEqual(client_id, application.client_id)
if client_secret:
self.assertEqual(client_secret, application.client_secret)
# When called a second time with the same arguments, the command should
# exit gracefully without creating a second application.
call_command(Command(), 'testing_application', self.user.username)
call_command(Command(), *call_args)
apps = Application.objects.filter(name='testing_application')
self.assertEqual(1, len(apps))