diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 80f166f7..173d0141 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -61,4 +61,4 @@ jobs: pip install --upgrade coveralls coveralls --service=github --finish env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/CHANGES.rst b/CHANGES.rst index 6109ea2f..f15d8cfe 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -29,6 +29,22 @@ Changelog - Add booking_refuse_message to Prenotazione stringinterp variables. [folix-01] +2.1.5 (2023-11-10) +------------------ + +- Fix release (2.1.4 was already made). + [cekk] + + +2.1.4 (2023-11-10) +------------------ + +- Fix week overrides when booking next year. + [cekk] + +- Bypass limit for out-of-office bookings. + [cekk] + 2.1.3 (2023-10-13) ------------------ diff --git a/setup.py b/setup.py index 2bebccde..b9a6f2ad 100644 --- a/setup.py +++ b/setup.py @@ -78,6 +78,7 @@ "plone.app.contenttypes", "plone.app.robotframework[debug]", "collective.MockMailHost", + "freezegun", ], "app_io": [ "bravado", diff --git a/src/redturtle/prenotazioni/browser/prenotazioni_context_state.py b/src/redturtle/prenotazioni/browser/prenotazioni_context_state.py index 7a6d5206..e9469fad 100644 --- a/src/redturtle/prenotazioni/browser/prenotazioni_context_state.py +++ b/src/redturtle/prenotazioni/browser/prenotazioni_context_state.py @@ -177,20 +177,27 @@ def get_week_overrides(self, day): from_day = int(override.get("from_day", "")) to_month = int(override.get("to_month", "")) to_day = int(override.get("to_day", "")) - toYear = day.year + to_year = day.year - if from_month > to_month: - # next year - toYear += 1 - - fromDate = date(day.year, from_month, from_day) - toDate = date(toYear, to_month, to_day) + if from_month <= to_month: + # same year (i.e from "10 aug" to "4 sep") + from_date = date(to_year, from_month, from_day) + to_date = date(to_year, to_month, to_day) + else: + # ends next year (i.e. from "20 dec" to "7 jan") + today_year = date.today().year + if today_year < to_year: + from_date = date(today_year, from_month, from_day) + to_date = date(to_year, to_month, to_day) + else: + from_date = date(to_year, from_month, from_day) + to_date = date(to_year + 1, to_month, to_day) if isinstance(day, datetime): - if fromDate <= day.date() <= toDate: + if from_date <= day.date() <= to_date: return override else: - if fromDate <= day <= toDate: + if from_date <= day <= to_date: return override return {} diff --git a/src/redturtle/prenotazioni/tests/test_available_slots.py b/src/redturtle/prenotazioni/tests/test_available_slots.py index 0c3b49f9..29c66391 100644 --- a/src/redturtle/prenotazioni/tests/test_available_slots.py +++ b/src/redturtle/prenotazioni/tests/test_available_slots.py @@ -5,6 +5,7 @@ import pytz import transaction +from freezegun import freeze_time from plone import api from plone.app.testing import ( SITE_OWNER_NAME, @@ -18,6 +19,8 @@ from redturtle.prenotazioni.adapters.booker import IBooker from redturtle.prenotazioni.testing import REDTURTLE_PRENOTAZIONI_API_FUNCTIONAL_TESTING +DATE_STR = "2023-05-14" + class TestAvailableSlots(unittest.TestCase): layer = REDTURTLE_PRENOTAZIONI_API_FUNCTIONAL_TESTING @@ -182,10 +185,7 @@ def test_month_slots_called_without_params_return_available_slots_of_current_mon ), ) - @unittest.skipIf( - date.today().day >= 20 or date.today().day <= 8, - "issue testing in the last days of a month", - ) + @freeze_time(DATE_STR) def test_if_start_and_not_end_return_all_available_slots_for_that_month( self, ): @@ -194,6 +194,9 @@ def test_if_start_and_not_end_return_all_available_slots_for_that_month( current_month = now.month next_month = current_month + 1 + self.folder_prenotazioni.daData = now + transaction.commit() + # all mondays in next month response = self.api_session.get( "{}/@available-slots?start={}".format( @@ -201,7 +204,6 @@ def test_if_start_and_not_end_return_all_available_slots_for_that_month( json_compatible(date(current_year, next_month, 1)), ) ) - # get next mondays in current month expected = [] for week in calendar.monthcalendar(current_year, next_month): diff --git a/src/redturtle/prenotazioni/tests/test_day.py b/src/redturtle/prenotazioni/tests/test_day.py index 41893a7a..a0923619 100644 --- a/src/redturtle/prenotazioni/tests/test_day.py +++ b/src/redturtle/prenotazioni/tests/test_day.py @@ -1,6 +1,7 @@ # -*- coding: UTF-8 -*- +import pytz import unittest -from datetime import date, datetime, timedelta +from datetime import date, timedelta from plone import api from plone.app.testing import ( @@ -11,6 +12,7 @@ ) from plone.restapi.interfaces import ISerializeToJson from plone.restapi.testing import RelativeSession +from redturtle.prenotazioni import tznow from transaction import commit from zope.component import getMultiAdapter from zope.globalrequest import getRequest @@ -65,13 +67,13 @@ def setUp(self): row["morning_end"] = "1000" self.folder_prenotazioni.week_table = week_table - self.today = datetime.now().replace(hour=8) - self.tomorrow = self.today + timedelta(1) - + # fix timezone api.portal.set_registry_record( "plone.portal_timezone", "Europe/Rome", ) + self.today = tznow().replace(hour=8) + self.tomorrow = self.today + timedelta(1) commit() @@ -106,10 +108,6 @@ def test_bookings_returned_by_gate(self): results, ) - @unittest.skipIf( - date.today().day >= 20 or date.today().day <= 8, - "issue testing in the last days of a month", - ) def test_pauses_returned(self): # le pause sono in localtime self.folder_prenotazioni.pause_table = [ @@ -127,10 +125,14 @@ def test_pauses_returned(self): results = response.json()["pauses"] # la risposta è in UTC + tomorrow_start_utc = self.tomorrow.replace(hour=7, minute=15).astimezone( + pytz.utc + ) + tomorrow_end_utc = self.tomorrow.replace(hour=8, minute=30).astimezone(pytz.utc) self.assertIn( { - "start": self.tomorrow.strftime("%Y-%m-%d") + "T05:15:00+00:00", - "end": self.tomorrow.strftime("%Y-%m-%d") + "T06:30:00+00:00", + "start": tomorrow_start_utc.strftime("%Y-%m-%dT%H:%M:00+00:00"), + "end": tomorrow_end_utc.strftime("%Y-%m-%dT%H:%M:00+00:00"), }, results, ) @@ -142,10 +144,6 @@ def test_bad_request_date(self): self.assertEqual(res.json()["type"], "BadRequest") self.assertEqual(res.status_code, 400) - @unittest.skipIf( - date.today().day >= 20 or date.today().day <= 8, - "issue testing in the last days of a month", - ) def test_daily_schedule(self): # TODO: testare con timezone differenti response = self.api_session.get( @@ -161,12 +159,19 @@ def test_daily_schedule(self): ) self.assertEqual(self.folder_prenotazioni.week_table[0]["morning_end"], "1000") # la risposta è in UTC + tomorrow_start_utc = self.tomorrow.replace(hour=7, minute=00).astimezone( + pytz.utc + ) + tomorrow_end_utc = self.tomorrow.replace(hour=10, minute=00).astimezone( + pytz.utc + ) + self.assertEqual( { "afternoon": {"start": None, "end": None}, "morning": { - "start": self.tomorrow.strftime("%Y-%m-%d") + "T05:00:00+00:00", - "end": self.tomorrow.strftime("%Y-%m-%d") + "T08:00:00+00:00", + "start": tomorrow_start_utc.strftime("%Y-%m-%dT%H:%M:00+00:00"), + "end": tomorrow_end_utc.strftime("%Y-%m-%dT%H:%M:00+00:00"), }, }, results["daily_schedule"], diff --git a/src/redturtle/prenotazioni/tests/test_gates_overrides.py b/src/redturtle/prenotazioni/tests/test_gates_overrides.py index 7eaaae40..b47342d3 100644 --- a/src/redturtle/prenotazioni/tests/test_gates_overrides.py +++ b/src/redturtle/prenotazioni/tests/test_gates_overrides.py @@ -91,9 +91,6 @@ def test_if_gates_override_not_set_use_default(self): type="PrenotazioniFolder", title="Prenota foo", daData=date.today(), - booking_types=[ - {"name": "Type A", "duration": "30"}, - ], gates=["Gate A"], week_table=WEEK_TABLE_SCHEMA, week_table_overrides=json.dumps( @@ -110,6 +107,14 @@ def test_if_gates_override_not_set_use_default(self): ] ), ) + api.content.create( + type="PrenotazioneType", + title="Type A", + duration=30, + container=self.folder_prenotazioni, + gates=["all"], + ) + view = api.content.get_view( name="prenotazioni_context_state", context=folder, diff --git a/src/redturtle/prenotazioni/tests/test_vacation_api.py b/src/redturtle/prenotazioni/tests/test_vacation_api.py index 43c7400f..6f90f0bc 100644 --- a/src/redturtle/prenotazioni/tests/test_vacation_api.py +++ b/src/redturtle/prenotazioni/tests/test_vacation_api.py @@ -121,9 +121,7 @@ def test_add_vacation(self): res = self.api_session_anon.post( self.folder_prenotazioni.absolute_url() + "/@booking", json={ - "booking_date": self.next_monday.replace( - hour=10, minute=10 - ).isoformat(), + "booking_date": start.isoformat(), "booking_type": "Type A", "fields": [ {"name": "title", "value": "Mario Rossi"}, @@ -131,7 +129,6 @@ def test_add_vacation(self): ], }, ) - self.assertEqual(res.status_code, 400) self.assertEqual( res.json()["message"], diff --git a/src/redturtle/prenotazioni/tests/test_week_table_overrides.py b/src/redturtle/prenotazioni/tests/test_week_table_overrides.py index 903a2554..eb90ccce 100644 --- a/src/redturtle/prenotazioni/tests/test_week_table_overrides.py +++ b/src/redturtle/prenotazioni/tests/test_week_table_overrides.py @@ -2,6 +2,7 @@ import json import unittest from datetime import date +from freezegun import freeze_time from plone import api from plone.app.testing import ( @@ -19,7 +20,7 @@ ) -class TestContextState(unittest.TestCase): +class TestWeekTableOverridesContextState(unittest.TestCase): layer = REDTURTLE_PRENOTAZIONI_FUNCTIONAL_TESTING def setUp(self): @@ -93,6 +94,7 @@ def test_if_day_is_in_overrides_use_override_week_table(self): json.loads(self.folder_prenotazioni.week_table_overrides)[0]["week_table"], ) + @freeze_time("2023-05-14") def test_override_between_year(self): self.folder_prenotazioni.week_table_overrides = json.dumps( [ @@ -114,18 +116,27 @@ def test_override_between_year(self): ] ) now = date.today() + + # if in range, return table overrides self.assertEqual( self.view.get_week_table(date(now.year, 12, 25)), json.loads(self.folder_prenotazioni.week_table_overrides)[0]["week_table"], ) + # if in range and next year, return table overrides + self.assertEqual( + self.view.get_week_table(date(now.year + 1, 1, 25)), + json.loads(self.folder_prenotazioni.week_table_overrides)[0]["week_table"], + ) + + # if out of range, return base table self.assertEqual( - self.view.get_week_table(date(now.year, 5, 10)), + self.view.get_week_table(date(now.year, 10, 10)), self.folder_prenotazioni.week_table, ) -class TestApiValidateDataOnPost(unittest.TestCase): +class TestWeekTableOverridesApiValidateDataOnPost(unittest.TestCase): layer = REDTURTLE_PRENOTAZIONI_API_FUNCTIONAL_TESTING def setUp(self):