2019-04-30 18:53:44 +03:00
|
|
|
from io import StringIO
|
|
|
|
|
2017-01-15 11:41:47 +03:00
|
|
|
import pytest
|
|
|
|
from django.contrib.admin import site
|
|
|
|
from django.contrib.auth.models import User
|
2017-11-02 12:26:42 +03:00
|
|
|
from django.core.management import CommandError, call_command
|
2017-01-15 11:41:47 +03:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from rest_framework.authtoken.admin import TokenAdmin
|
2017-05-29 19:07:50 +03:00
|
|
|
from rest_framework.authtoken.management.commands.drf_create_token import \
|
|
|
|
Command as AuthTokenCommand
|
2017-01-15 11:41:47 +03:00
|
|
|
from rest_framework.authtoken.models import Token
|
|
|
|
from rest_framework.authtoken.serializers import AuthTokenSerializer
|
|
|
|
from rest_framework.exceptions import ValidationError
|
|
|
|
|
|
|
|
|
|
|
|
class AuthTokenTests(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.site = site
|
|
|
|
self.user = User.objects.create_user(username='test_user')
|
|
|
|
self.token = Token.objects.create(key='test token', user=self.user)
|
|
|
|
|
|
|
|
def test_model_admin_displayed_fields(self):
|
|
|
|
mock_request = object()
|
|
|
|
token_admin = TokenAdmin(self.token, self.site)
|
|
|
|
assert token_admin.get_fields(mock_request) == ('user',)
|
|
|
|
|
|
|
|
def test_token_string_representation(self):
|
|
|
|
assert str(self.token) == 'test token'
|
|
|
|
|
|
|
|
def test_validate_raise_error_if_no_credentials_provided(self):
|
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
AuthTokenSerializer().validate({})
|
2017-05-17 21:52:39 +03:00
|
|
|
|
|
|
|
def test_whitespace_in_password(self):
|
|
|
|
data = {'username': self.user.username, 'password': 'test pass '}
|
|
|
|
self.user.set_password(data['password'])
|
|
|
|
self.user.save()
|
|
|
|
assert AuthTokenSerializer(data=data).is_valid()
|
2017-05-29 19:07:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
class AuthTokenCommandTests(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.site = site
|
|
|
|
self.user = User.objects.create_user(username='test_user')
|
|
|
|
|
|
|
|
def test_command_create_user_token(self):
|
2017-06-03 13:58:01 +03:00
|
|
|
token = AuthTokenCommand().create_user_token(self.user.username, False)
|
2017-05-29 19:07:50 +03:00
|
|
|
assert token is not None
|
|
|
|
token_saved = Token.objects.first()
|
|
|
|
assert token.key == token_saved.key
|
2017-06-01 01:01:35 +03:00
|
|
|
|
|
|
|
def test_command_create_user_token_invalid_user(self):
|
|
|
|
with pytest.raises(User.DoesNotExist):
|
2017-06-03 13:58:01 +03:00
|
|
|
AuthTokenCommand().create_user_token('not_existing_user', False)
|
|
|
|
|
|
|
|
def test_command_reset_user_token(self):
|
|
|
|
AuthTokenCommand().create_user_token(self.user.username, False)
|
|
|
|
first_token_key = Token.objects.first().key
|
|
|
|
AuthTokenCommand().create_user_token(self.user.username, True)
|
|
|
|
second_token_key = Token.objects.first().key
|
|
|
|
|
|
|
|
assert first_token_key != second_token_key
|
|
|
|
|
|
|
|
def test_command_do_not_reset_user_token(self):
|
|
|
|
AuthTokenCommand().create_user_token(self.user.username, False)
|
|
|
|
first_token_key = Token.objects.first().key
|
|
|
|
AuthTokenCommand().create_user_token(self.user.username, False)
|
|
|
|
second_token_key = Token.objects.first().key
|
|
|
|
|
|
|
|
assert first_token_key == second_token_key
|
2017-09-12 16:03:29 +03:00
|
|
|
|
2017-11-02 12:26:42 +03:00
|
|
|
def test_command_raising_error_for_invalid_user(self):
|
|
|
|
out = StringIO()
|
|
|
|
with pytest.raises(CommandError):
|
|
|
|
call_command('drf_create_token', 'not_existing_user', stdout=out)
|
|
|
|
|
2017-09-12 16:03:29 +03:00
|
|
|
def test_command_output(self):
|
|
|
|
out = StringIO()
|
|
|
|
call_command('drf_create_token', self.user.username, stdout=out)
|
|
|
|
token_saved = Token.objects.first()
|
|
|
|
self.assertIn('Generated token', out.getvalue())
|
|
|
|
self.assertIn(self.user.username, out.getvalue())
|
|
|
|
self.assertIn(token_saved.key, out.getvalue())
|