fargo/tests/test_commands.py

71 lines
2.3 KiB
Python

# fargo - document box
# Copyright (C) 2016-2019 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
from django.core.management import call_command
from django.contrib.auth.models import User
from fargo.fargo.models import UserDocument, Document
from fargo.oauth2.models import OAuth2TempFile, OAuth2Client
from django.core.files.base import ContentFile
def test_cleanup(freezer, john_doe):
start = freezer()
client = OAuth2Client.objects.create(client_name='c', redirect_uris='')
foo = Document.objects.create(content=ContentFile(b'foo', name='foo.txt'))
bar = Document.objects.create(content=ContentFile(b'bar', name='bar.txt'))
UserDocument.objects.create(user=john_doe,
document=foo,
filename='foo.txt',
title='',
description='')
OAuth2TempFile.objects.create(document=bar, client=client, filename='bar.txt')
call_command('fargo-cleanup')
assert UserDocument.objects.all().count()
assert OAuth2TempFile.objects.all().count()
assert Document.objects.all().count() == 2
User.objects.all().delete()
assert not UserDocument.objects.all().count()
assert Document.objects.all().count() == 2
call_command('fargo-cleanup')
assert Document.objects.all().count() == 2
freezer.move_to(start + datetime.timedelta(seconds=120))
call_command('fargo-cleanup')
assert Document.objects.all().count() == 1
freezer.move_to(start + datetime.timedelta(days=3))
call_command('fargo-cleanup')
assert not OAuth2TempFile.objects.count()
assert Document.objects.count()
call_command('fargo-cleanup')
assert not Document.objects.count()