From a0a4570390d58b58baa19c13b5ced4c79d31eaac Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Oct 13 2020 13:22:34 +0000 Subject: Port the tests to pytest and fix them Signed-off-by: Pierre-Yves Chibon --- diff --git a/README.rst b/README.rst index cee9608..604dcee 100644 --- a/README.rst +++ b/README.rst @@ -55,6 +55,9 @@ The dependency list is therefore: - `python-alembic`_ - `pytz`_ - `python-dateutil`_ +- `python-fedora-messaging`_ +- `python-flask-multistatic`_ +- `python-flask-oidc`_ Running a development instance: @@ -94,16 +97,21 @@ Testing: This project contains unit-tests allowing you to check if your server has all the dependencies correctly set. -To run them:: +To run them simply call:: - ./run_tests.sh + tox .. note:: To stop the test at the first error or failure you can try: :: - ./run_tests.sh -x + tox -- -x +.. note:: To run a single file you can try: + + :: + + tox -- tests/test_flask.py -x Reporting issues: diff --git a/fedocal/__init__.py b/fedocal/__init__.py index f905d5d..bb3fa94 100644 --- a/fedocal/__init__.py +++ b/fedocal/__init__.py @@ -49,7 +49,7 @@ from functools import wraps from pytz import common_timezones from six.moves.urllib.parse import urlparse, urljoin from sqlalchemy.exc import SQLAlchemyError -from werkzeug import secure_filename +from werkzeug.utils import secure_filename from fedocal.fedocal_babel import Babel from fedocal.fedocal_babel import gettext @@ -1577,6 +1577,7 @@ def location(loc_name, year, month, day): return flask.render_template( 'agenda.html', + now=datetime.datetime.utcnow(), location=loc_name, month=month_name, weekdays=weekdays, diff --git a/fedocal/fedocallib/__init__.py b/fedocal/fedocallib/__init__.py index 500fb3e..0ae4b1c 100644 --- a/fedocal/fedocallib/__init__.py +++ b/fedocal/fedocallib/__init__.py @@ -687,13 +687,14 @@ def add_meeting_to_vcal(ical, meeting, reminder=None): start = entry.add('dtstart') stop = entry.add('dtend') + tz = zoneinfo.gettz(meeting.meeting_timezone) if meeting.full_day: - start.value = meeting.meeting_date - stop.value = meeting.meeting_date_end + start.value = datetime.combine( + meeting.meeting_date, time(0)).replace(tzinfo=tz) + stop.value = datetime.combine( + meeting.meeting_date_end, time(0)).replace(tzinfo=tz) entry.add('transp').value = 'TRANSPARENT' else: - tz = zoneinfo.gettz(meeting.meeting_timezone) - dti_start = datetime.combine( meeting.meeting_date, meeting.meeting_time_start) start.value = dti_start.replace(tzinfo=tz) diff --git a/fedocal/fedocallib/fedmsgshim.py b/fedocal/fedocallib/fedmsgshim.py index 87aabf5..1937c3b 100644 --- a/fedocal/fedocallib/fedmsgshim.py +++ b/fedocal/fedocallib/fedmsgshim.py @@ -16,7 +16,7 @@ _log = logging.getLogger(__name__) def publish(topic, msg): # pragma: no cover - _log.debug('Publishing a message for %r: %s', topic, msg) + _log.debug('Publishing a message for %s: %s', topic, msg) try: message = fedora_messaging.api.Message( topic='fedocal.%s' % topic, @@ -29,4 +29,4 @@ def publish(topic, msg): # pragma: no cover 'Fedora Messaging broker rejected message %s: %s', message.id, e) except ConnectionException as e: - _log.exception('Error sending message %s: %s', message.id, e) \ No newline at end of file + _log.exception('Error sending message %s: %s', message.id, e) diff --git a/fedocal_cron.py b/fedocal_cron.py index f77ef43..cbaa055 100644 --- a/fedocal_cron.py +++ b/fedocal_cron.py @@ -46,8 +46,7 @@ def fedmsg_publish(meeting, meeting_id): :arg meeting_id: an int representing the meeting identifier in the database """ - _log.debug('Publishing a message for %r: %s', topic, msg) - + _log.debug('Publishing a message for meeting: %s', meeting_id) meeting_dict = meeting.to_json() meeting_dict['meeting_id'] = meeting_id @@ -56,7 +55,7 @@ def fedmsg_publish(meeting, meeting_id): meeting=meeting_dict, calendar=meeting.calendar.to_json() ) - fedmsg.publish('fedocal.reminder', message) + fedmsg.publish(topic='reminder', msg=message) def send_reminder_meeting(meeting, meeting_id): diff --git a/requirements.txt b/requirements.txt index 87ec29d..4b90907 100644 --- a/requirements.txt +++ b/requirements.txt @@ -20,8 +20,6 @@ psutil blinker # Required by flask >= 0.10 for the unit-tests funcsigs # Required by mock but somehow missed flask_multistatic -python-openid -python-openid_cla -python-openid_teams +flask_oidc fedora-messaging - +email_validator diff --git a/tests/test_cron.py b/tests/test_cron.py index ced96c0..2973a5e 100644 --- a/tests/test_cron.py +++ b/tests/test_cron.py @@ -36,7 +36,8 @@ import os from datetime import timedelta, datetime -from mock import patch +from fedora_messaging import api, testing +from mock import ANY, patch sys.path.insert(0, os.path.join(os.path.dirname( os.path.abspath(__file__)), '..')) @@ -68,13 +69,14 @@ class FakeSMTP(object): class Crontests(Modeltests): """ Cron tests. """ + maxDiff = None + def _clean(self): """ Clean a potentially existing test database. """ root, path = DB_PATH.split(':///', 1) if os.path.exists(path): os.unlink(path) - def setUp(self): """ Set up the environnment, ran before every tests. """ self._clean() @@ -104,8 +106,6 @@ class Crontests(Modeltests): self.session.commit() self.assertNotEqual(calendar, None) - fedocal_cron.fedmsg_init() - def tearDown(self): """ Remove the test.db database if there is one. """ self.session.close() @@ -143,7 +143,8 @@ class Crontests(Modeltests): self.session.commit() self.assertNotEqual(obj, None) - msgs = fedocal_cron.send_reminder() + with testing.mock_sends(): + msgs = fedocal_cron.send_reminder() self.assertEqual(len(msgs), 0) @@ -179,7 +180,33 @@ class Crontests(Modeltests): self.session.commit() self.assertNotEqual(obj, None) - msgs = fedocal_cron.send_reminder() + with testing.mock_sends(api.Message( + topic="fedocal.reminder", + body={ + 'meeting': { + 'meeting_id': 1, + 'meeting_name': 'Test meeting with reminder', + 'meeting_manager': ['pingou'], + 'meeting_date': ANY, + 'meeting_date_end': ANY, + 'meeting_time_start': ANY, + 'meeting_time_stop': ANY, + 'meeting_timezone': 'UTC', + 'meeting_information': 'This is a test meeting with reminder', + 'meeting_location': None, + 'calendar_name': 'test_calendar' + }, + 'calendar': { + 'calendar_name': 'test_calendar', + 'calendar_contact': 'test@example.com', + 'calendar_description': 'This is a test calendar', + 'calendar_editor_group': 'fi-apprentice', + 'calendar_admin_group': 'infrastructure-main2', + 'calendar_status': 'Enabled' + } + } + )): + msgs = fedocal_cron.send_reminder() self.assertEqual(len(msgs), 1) self.assertEqual(msgs[0]['To'], 'list@lists.fp.o') @@ -217,7 +244,33 @@ class Crontests(Modeltests): self.session.commit() self.assertNotEqual(obj, None) - msgs = fedocal_cron.send_reminder() + with testing.mock_sends(api.Message( + topic="fedocal.reminder", + body={ + 'meeting': { + 'meeting_id': 1, + 'meeting_name': 'Test meeting with reminder', + 'meeting_manager': ['pingou'], + 'meeting_date': ANY, + 'meeting_date_end': ANY, + 'meeting_time_start': ANY, + 'meeting_time_stop': ANY, + 'meeting_timezone': 'UTC', + 'meeting_information': 'This is a test meeting with reminder', + 'meeting_location': None, + 'calendar_name': 'test_calendar' + }, + 'calendar': { + 'calendar_name': 'test_calendar', + 'calendar_contact': 'test@example.com', + 'calendar_description': 'This is a test calendar', + 'calendar_editor_group': 'fi-apprentice', + 'calendar_admin_group': 'infrastructure-main2', + 'calendar_status': 'Enabled' + } + } + )): + msgs = fedocal_cron.send_reminder() self.assertEqual(len(msgs), 1) self.assertEqual(msgs[0]['To'], 'pingou@fp.o, pingou@p.fr') diff --git a/tests/test_flask.py b/tests/test_flask.py index 5b735f7..b9abc4d 100644 --- a/tests/test_flask.py +++ b/tests/test_flask.py @@ -379,8 +379,8 @@ class Flasktests(Modeltests): self.assertIn(' ', output_text) self.assertIn(' ', output_text) self.assertIn(' ', output_text) - self.assertEqual(output_text.count('