Commit 1e90d0e8 authored by Samuel GAIST's avatar Samuel GAIST

[reports] Refactor tests and add command checks

parent 56c43fbf
Pipeline #43011 failed with stage
in 14 minutes and 36 seconds
This diff is collapsed.
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.web module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform 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. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
import io
from datetime import datetime
from datetime import timedelta
from django.conf import settings
from django.contrib.sites.models import Site
from django.core import mail
from django.core.management import call_command
from django.test import override_settings
from ...common.testutils import tearDownModule # noqa test runner will call it
from ...utils.tests.helpers import reload_urlconf
from ..models import Report
from .core import ReportTestCase
# ----------------------------------------------------------
class CleanReportManagementCommandTestCase(ReportTestCase):
def run_command(self):
"Runs the clean_attestation command"
new_io = io.StringIO()
call_command("clean_report", interactive=False, stdout=new_io)
return new_io.getvalue().strip()
def create_report(self):
report, _ = Report.objects.create_object(
self.johndoe, "report_name", "for testing", "report written for testing"
)
report.status = Report.LOCKED
report.save()
return report
def test_basic_usage(self):
"Check the operation of the reports management command"
command_output = self.run_command()
self.assertEqual(command_output, "0 locked report(s) successfully cleaned")
def test_valid_report(self):
self.create_report()
command_output = self.run_command()
self.assertEqual(command_output, "0 locked report(s) successfully cleaned")
def test_outdated_report(self):
report = self.create_report()
report.expiration_date = datetime.now() - timedelta(days=200)
report.save()
command_output = self.run_command()
self.assertEqual(command_output, "1 locked report(s) successfully cleaned")
class SendAttesttationWarningManagementTestCase(ReportTestCase):
def run_command(self):
"Runs the send_report_cleanup_warning_and_cleanup command"
new_io = io.StringIO()
call_command("send_report_cleanup_warning_and_cleanup", stdout=new_io)
return new_io.getvalue().strip()
def test_basic_usage(self):
mail.outbox = [] # mail outbox contains mails unrelated to this test
command_output = self.run_command()
self.assertEqual(len(mail.outbox), 0)
self.assertEqual(
command_output,
"No report(s) about to expire in 1 day(s)\nNo report(s) about to expire in 7 day(s)\nNo report(s) about to expire in 30 day(s)",
)
def test_outdated_report(self):
report, _ = Report.objects.create_object(
self.johndoe, "report_name", "for testing", "report written for testing"
)
report.status = Report.LOCKED
current_site = Site.objects.get_current()
for prefix in ["", "/platform"]:
with self.subTest(url_prefix=prefix):
with override_settings(URL_PREFIX=prefix):
reload_urlconf()
for expiration_reminder in settings.EXPIRATION_REMINDERS:
report.expiration_date = report.creation_date + timedelta(
days=expiration_reminder
)
report.save()
with self.subTest(reminder=expiration_reminder):
mail.outbox = []
self.run_command()
self.assertEqual(len(mail.outbox), 1)
self.assertTrue(
f"https://{current_site.domain}{prefix}"
in mail.outbox[0].body
)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment