# Path: user_management/api/models.py
# class AuthToken(models.Model):
#     """
#     Model for auth tokens with added functionality of controlling
#     expiration time of tokens.
# 
#     Similar to DRF's model but with extra `expires` field.
# 
#     It also has FK (not OneToOne relation) to user as the user can have
#     many tokens (multiple devices) in order to token expiration to work.
#     """
#     key = models.CharField(max_length=40, primary_key=True)
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         related_name='authtoken',
#         on_delete=models.CASCADE,
#     )
#     created = models.DateTimeField(default=timezone.now, editable=False)
#     expires = models.DateTimeField(default=update_expiry, editable=False)
# 
#     def __str__(self):
#         return self.key
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = self.generate_key()
#         return super(AuthToken, self).save(*args, **kwargs)
# 
#     def generate_key(self):
#         return binascii.hexlify(os.urandom(20)).decode()
# 
#     def update_expiry(self, commit=True):
#         """Update token's expiration datetime on every auth action."""
#         self.expires = update_expiry(self.created)
#         if commit:
#             self.save()
# 
# Path: user_management/models/tests/utils.py
# class APIRequestTestCase(Python2AssertMixin, BaseAPIRequestTestCase):
# class RequestTestCase(Python2AssertMixin, BaseRequestTestCase):
# 
# Path: user_management/models/tests/factories.py
# class AuthTokenFactory(factory.DjangoModelFactory):
#     key = factory.Sequence('key{}'.format)
#     user = factory.SubFactory(UserFactory)
# 
#     class Meta:
#         model = AuthToken
# 
# Path: user_management/api/management/commands/remove_expired_tokens.py
# class Command(BaseCommand):
#     def handle(self, *args, **options):


import datetime
import mock
from django.test.utils import override_settings
from django.utils import timezone
from user_management.api.models import AuthToken
from user_management.models.tests import utils
from user_management.models.tests.factories import AuthTokenFactory
from ..management.commands import remove_expired_tokens




class TestRemoveExpiredTokensManagementCommand(utils.APIRequestTestCase):
    def setUp(self):
        self.command = remove_expired_tokens.Command()
        self.command.stdout = mock.MagicMock()

    def test_no_tokens_removed(self):
        """Tests that non-expired tokens are not removed."""
        tomorrow = timezone.now() + datetime.timedelta(days=1)

# Path: user_management/api/models.py
# class AuthToken(models.Model):
#     """
#     Model for auth tokens with added functionality of controlling
#     expiration time of tokens.
# 
#     Similar to DRF's model but with extra `expires` field.
# 
#     It also has FK (not OneToOne relation) to user as the user can have
#     many tokens (multiple devices) in order to token expiration to work.
#     """
#     key = models.CharField(max_length=40, primary_key=True)
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         related_name='authtoken',
#         on_delete=models.CASCADE,
#     )
#     created = models.DateTimeField(default=timezone.now, editable=False)
#     expires = models.DateTimeField(default=update_expiry, editable=False)
# 
#     def __str__(self):
#         return self.key
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = self.generate_key()
#         return super(AuthToken, self).save(*args, **kwargs)
# 
#     def generate_key(self):
#         return binascii.hexlify(os.urandom(20)).decode()
# 
#     def update_expiry(self, commit=True):
#         """Update token's expiration datetime on every auth action."""
#         self.expires = update_expiry(self.created)
#         if commit:
#             self.save()
# 
# Path: user_management/models/tests/utils.py
# class APIRequestTestCase(Python2AssertMixin, BaseAPIRequestTestCase):
# class RequestTestCase(Python2AssertMixin, BaseRequestTestCase):
# 
# Path: user_management/models/tests/factories.py
# class AuthTokenFactory(factory.DjangoModelFactory):
#     key = factory.Sequence('key{}'.format)
#     user = factory.SubFactory(UserFactory)
# 
#     class Meta:
#         model = AuthToken
# 
# Path: user_management/api/management/commands/remove_expired_tokens.py
# class Command(BaseCommand):
#     def handle(self, *args, **options):


# Path: user_management/api/tests/test_management_commands.py
import datetime
import mock
from django.test.utils import override_settings
from django.utils import timezone
from user_management.api.models import AuthToken
from user_management.models.tests import utils
from user_management.models.tests.factories import AuthTokenFactory
from ..management.commands import remove_expired_tokens





class TestRemoveExpiredTokensManagementCommand(utils.APIRequestTestCase):
    def setUp(self):
        self.command = remove_expired_tokens.Command()
        self.command.stdout = mock.MagicMock()

    def test_no_tokens_removed(self):
        """Tests that non-expired tokens are not removed."""
        tomorrow = timezone.now() + datetime.timedelta(days=1)

        token = AuthTokenFactory.create(expires=tomorrow)