Handle invalid User situation

This commit is contained in:
Andrea Grandi 2017-05-31 23:01:35 +01:00
parent d198b1abe6
commit be590d61c0
2 changed files with 11 additions and 1 deletions

View File

@ -16,5 +16,11 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
username = options['username'] username = options['username']
token = self.create_user_token(username)
try:
token = self.create_user_token(username)
except User.DoesNotExist:
print('Cannot create the Token: user {0} does not exist'.format(
username
))
print('Generated token {0} for user {1}'.format(token.key, username)) print('Generated token {0} for user {1}'.format(token.key, username))

View File

@ -48,3 +48,7 @@ class AuthTokenCommandTests(TestCase):
assert token is not None assert token is not None
token_saved = Token.objects.first() token_saved = Token.objects.first()
assert token.key == token_saved.key assert token.key == token_saved.key
def test_command_create_user_token_invalid_user(self):
with pytest.raises(User.DoesNotExist):
AuthTokenCommand().create_user_token('not_existing_user')