mirror of
https://github.com/ansible/awx.git
synced 2026-03-29 06:45:09 -02:30
Merge pull request #1953 from YunfanZhang42/devel
Let create_oauth2_token print errors from std_err.
This commit is contained in:
@@ -1,25 +1,27 @@
|
|||||||
from awx.api.serializers import OAuth2TokenSerializer
|
# Django
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
|
||||||
|
# AWX
|
||||||
|
from awx.api.serializers import OAuth2TokenSerializer
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
"""Command that creates an OAuth2 token for a certain user. Returns the value of created token."""
|
"""Command that creates an OAuth2 token for a certain user. Returns the value of created token."""
|
||||||
help='Usage: awx-manage create_oauth2_token --user=username. Will return the value of created token.'
|
help='Creates an OAuth2 token for a user.'
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
parser.add_argument('--user', dest='user', type=str)
|
parser.add_argument('--user', dest='user', type=str)
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
if not options['user']:
|
if not options['user']:
|
||||||
self.stdout.write("Username not supplied. Usage: awx-manage create_oauth2_token --user=username.")
|
|
||||||
return
|
raise CommandError('Username not supplied. Usage: awx-manage create_oauth2_token --user=username.')
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(username=options['user'])
|
user = User.objects.get(username=options['user'])
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
self.stdout.write("The user does not exist.")
|
raise CommandError('The user does not exist.')
|
||||||
return
|
|
||||||
config = {'user': user, 'scope':'write'}
|
config = {'user': user, 'scope':'write'}
|
||||||
serializer_obj = OAuth2TokenSerializer()
|
serializer_obj = OAuth2TokenSerializer()
|
||||||
token_record = serializer_obj.create(config, True)
|
token_record = serializer_obj.create(config, True)
|
||||||
|
|||||||
@@ -1,19 +1,27 @@
|
|||||||
|
# Python
|
||||||
import pytest
|
import pytest
|
||||||
import string
|
import string
|
||||||
import random
|
import random
|
||||||
import StringIO
|
import StringIO
|
||||||
|
|
||||||
|
# Django
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
|
from django.core.management.base import CommandError
|
||||||
|
|
||||||
|
# AWX
|
||||||
from awx.main.models.oauth import OAuth2AccessToken
|
from awx.main.models.oauth import OAuth2AccessToken
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
@pytest.mark.inventory_import
|
@pytest.mark.inventory_import
|
||||||
class TestOAuth2CreateCommand:
|
class TestOAuth2CreateCommand:
|
||||||
def test_no_user_option(self):
|
def test_no_user_option(self):
|
||||||
out = StringIO.StringIO()
|
out = StringIO.StringIO()
|
||||||
call_command('create_oauth2_token', stdout=out)
|
with pytest.raises(CommandError) as excinfo:
|
||||||
assert 'Username not supplied.' in out.getvalue()
|
call_command('create_oauth2_token', stdout=out)
|
||||||
|
assert 'Username not supplied.' in excinfo.value.message
|
||||||
out.close()
|
out.close()
|
||||||
|
|
||||||
def test_non_existing_user(self):
|
def test_non_existing_user(self):
|
||||||
@@ -22,8 +30,9 @@ class TestOAuth2CreateCommand:
|
|||||||
while fake_username == '' or User.objects.filter(username=fake_username).exists():
|
while fake_username == '' or User.objects.filter(username=fake_username).exists():
|
||||||
fake_username = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(6))
|
fake_username = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(6))
|
||||||
arg = '--user=' + fake_username
|
arg = '--user=' + fake_username
|
||||||
call_command('create_oauth2_token', arg, stdout=out)
|
with pytest.raises(CommandError) as excinfo:
|
||||||
assert 'The user does not exist.' == out.getvalue().strip()
|
call_command('create_oauth2_token', arg, stdout=out)
|
||||||
|
assert 'The user does not exist.' in excinfo.value.message
|
||||||
out.close()
|
out.close()
|
||||||
|
|
||||||
def test_correct_user(self, alice):
|
def test_correct_user(self, alice):
|
||||||
|
|||||||
Reference in New Issue
Block a user