mirror of
https://github.com/ansible/awx.git
synced 2026-07-03 04:18:02 -02:30
Added an awx-manage command for generating OAuth2 token.
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import pytest
|
||||
import string
|
||||
import random
|
||||
import StringIO
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.management import call_command
|
||||
from awx.main.models.oauth import OAuth2AccessToken
|
||||
from django.core.management.base import CommandError
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.inventory_import
|
||||
class TestOAuth2CreateCommand:
|
||||
def test_no_user_option(self):
|
||||
out = StringIO.StringIO()
|
||||
with pytest.raises(CommandError) as excinfo:
|
||||
call_command('create_oauth2_token', stdout=out)
|
||||
assert 'Username not supplied.' in excinfo.value.message
|
||||
out.close()
|
||||
|
||||
def test_non_existing_user(self):
|
||||
out = StringIO.StringIO()
|
||||
fake_username = ''
|
||||
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))
|
||||
arg = '--user=' + fake_username
|
||||
with pytest.raises(CommandError) as excinfo:
|
||||
call_command('create_oauth2_token', arg, stdout=out)
|
||||
assert 'The user does not exist.' in excinfo.value.message
|
||||
out.close()
|
||||
|
||||
def test_correct_user(self, alice):
|
||||
out = StringIO.StringIO()
|
||||
arg = '--user=' + 'alice'
|
||||
call_command('create_oauth2_token', arg, stdout=out)
|
||||
generated_token = out.getvalue().strip()
|
||||
assert OAuth2AccessToken.objects.filter(user=alice, token=generated_token).count() == 1
|
||||
assert OAuth2AccessToken.objects.get(user=alice, token=generated_token).scope == 'write'
|
||||
out.close()
|
||||
Reference in New Issue
Block a user