test_expirelinks.py 1.6 KB
Newer Older
David Haynes's avatar
David Haynes committed
1
2
3
"""
go/commands/test_expirelinks.py

4
5
Test that the function to expire Go links actually works.
"""
6
7
8
# Python stdlib Imports
from datetime import timedelta

9
# Django Imports
10
11
from django.contrib.auth.models import User
from django.core.management import call_command
David Haynes's avatar
David Haynes committed
12
13
from django.test import TestCase
from django.utils import timezone
14
15

# App Imports
16
from go.models import URL, RegisteredUser
17
18

class ExpireLinksTest(TestCase):
19
20
    def setUp(self):
        """
21
        Set up any variables such as dummy objects that will be utilized in
22
23
24
25
26
27
28
29
        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')
30

31
32
33
34
35
36
37
38
39
40
41
42
        # 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()
David Haynes's avatar
David Haynes committed
43
        second_url.save()
44
45

    def test_expirelinks(self):
46
        """
47
48
        Make a call to expire Go links and assert that the number of links has
        been reduced.
49
        """
50
51
        call_command('expirelinks')
        self.assertTrue(len(URL.objects.all()) == 1)