test_expirelinks.py 1.65 KB
Newer Older
1 2 3
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division

4 5 6
# Python stdlib Imports
from datetime import timedelta

7 8
# Django Imports
from django.test import TestCase
9 10 11
from django.contrib.auth.models import User
from django.utils import timezone
from django.core.management import call_command
12 13

# App Imports
14 15
from .expirelinks import Command
from go.models import URL, RegisteredUser
16 17 18

class ExpireLinksTest(TestCase):
    """
19
    Test cases for the functions in expirelinks
20
    """
21

22 23 24 25 26 27 28 29 30 31 32 33
    def setUp(self):
        """
        Set up any variables such as dummy objects that will be utilised in
        testing methods
        """

        # Setup a blank URL object with an owner
        User.objects.create(username='dhaynes', password='password')
        get_user = User.objects.get(username='dhaynes')
        get_registered_user = RegisteredUser.objects.get(user=get_user)
        URL.objects.create(owner=get_registered_user, short='test')
        URL.objects.create(owner=get_registered_user, short='test-2')
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48
        # Get some dates
        yesterday = timezone.now() - timedelta(days=1)
        tomorrow = timezone.now() + timedelta(days=1)

        # Get the URL to apply it to
        current_url = URL.objects.get(short='test')
        second_url = URL.objects.get(short='test-2')

        # Apply the dates
        current_url.expires = yesterday
        second_url.expires = tomorrow
        current_url.save()

    def test_expirelinks(self):
49
        """
50
        Test that the expirelinks django admin command functions as intentioned.
51 52
        """

53 54 55
        call_command('expirelinks')

        self.assertTrue(len(URL.objects.all()) == 1)