Skip to content

Commit

Permalink
fix tz in get_booking_urls + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mamico committed Aug 9, 2023
1 parent 0621a99 commit 2a312d3
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 92 deletions.
12 changes: 4 additions & 8 deletions src/redturtle/prenotazioni/browser/prenotazioni_context_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from plone.memoize.view import memoize
from Products.Five.browser import BrowserView
from redturtle.prenotazioni import _
from redturtle.prenotazioni import datetime_with_tz
from redturtle.prenotazioni import get_or_create_obj
from redturtle.prenotazioni import tznow
from redturtle.prenotazioni.adapters.booker import IBooker
Expand Down Expand Up @@ -277,19 +276,16 @@ def get_booking_urls(self, day, slot, slot_min_size=0, gate=None):
if self.maximum_bookable_date:
if day > self.maximum_bookable_date.date():
return []
date = day.strftime("%Y-%m-%d")
# date = day.strftime("%Y-%m-%d")
params = self.remembered_params.copy()
times = slot.get_values_hr_every(300, slot_min_size=slot_min_size)
base_url = self.base_booking_url
urls = []
now = tznow()
# times are in localtime
for t in times:
booking_date_str = "T".join((date, t))
booking_date = datetime_with_tz(booking_date_str)

# needed to build the url
params["form.booking_date"] = booking_date_str

booking_date = hm2DT(day, t)
params["form.booking_date"] = booking_date.isoformat()
if gate:
params["gate"] = gate
urls.append(
Expand Down
3 changes: 2 additions & 1 deletion src/redturtle/prenotazioni/restapi/services/booking/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from zope.component import queryMultiAdapter
from zope.interface import alsoProvides


# src/redturtle/prenotazioni/browser/prenotazione_add.py


Expand Down Expand Up @@ -80,4 +81,4 @@ def validate(self):
mapping=dict(booking_type=data["booking_type"]),
)
)
raise BadRequest(msg)
raise BadRequest(msg)
139 changes: 58 additions & 81 deletions src/redturtle/prenotazioni/tests/test_available_slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@
from redturtle.prenotazioni.testing import REDTURTLE_PRENOTAZIONI_API_FUNCTIONAL_TESTING

import calendar
import pytz
import transaction
import unittest


class TestAvailableSlots(unittest.TestCase):
layer = REDTURTLE_PRENOTAZIONI_API_FUNCTIONAL_TESTING
maxDiff = None
timezone = "Europe/Rome"

def dt_local_to_json(self, value):
return json_compatible(
pytz.timezone(self.timezone).localize(value).astimezone(pytz.utc)
)

def setUp(self):
self.app = self.layer["app"]
Expand All @@ -37,63 +45,16 @@ def setUp(self):
title="Prenota foo",
description="",
daData=date.today(),
week_table=[
{
"day": "Lunedì",
"morning_start": "0700",
"morning_end": "1000",
"afternoon_start": None,
"afternoon_end": None,
},
{
"day": "Martedì",
"morning_start": None,
"morning_end": None,
"afternoon_start": None,
"afternoon_end": None,
},
{
"day": "Mercoledì",
"morning_start": None,
"morning_end": None,
"afternoon_start": None,
"afternoon_end": None,
},
{
"day": "Giovedì",
"morning_start": None,
"morning_end": None,
"afternoon_start": None,
"afternoon_end": None,
},
{
"day": "Venerdì",
"morning_start": None,
"morning_end": None,
"afternoon_start": None,
"afternoon_end": None,
},
{
"day": "Sabato",
"morning_start": None,
"morning_end": None,
"afternoon_start": None,
"afternoon_end": None,
},
{
"day": "Domenica",
"morning_start": None,
"morning_end": None,
"afternoon_start": None,
"afternoon_end": None,
},
],
booking_types=[
{"name": "Type A", "duration": "30"},
{"name": "Type B", "duration": "90"},
],
gates=["Gate A"],
)
week_table = self.folder_prenotazioni.week_table
week_table[0]["morning_start"] = "0700"
week_table[0]["morning_end"] = "1000"
self.folder_prenotazioni.week_table = week_table

year = api.content.create(
container=self.folder_prenotazioni, type="PrenotazioniYear", title="Year"
Expand All @@ -102,6 +63,12 @@ def setUp(self):
self.day_folder = api.content.create(
container=week, type="PrenotazioniDay", title="Day"
)

api.portal.set_registry_record(
"plone.portal_timezone",
self.timezone,
)

transaction.commit()

def tearDown(self):
Expand Down Expand Up @@ -192,10 +159,8 @@ def test_month_slots_called_without_params_return_available_slots_of_current_mon
# first free slot is at 7:30
self.assertEqual(
response.json()["items"][0],
json_compatible(
datetime_with_tz(
datetime(current_year, current_month, monday, 7, 30)
)
self.dt_local_to_json(
datetime(current_year, current_month, monday, 7, 30)
),
)

Expand All @@ -207,6 +172,7 @@ def test_if_start_and_not_end_return_all_available_slots_for_that_month(
current_month = now.month
next_month = current_month + 1

# all mondays in next month
response = self.api_session.get(
"{}/@available-slots?start={}".format(
self.folder_prenotazioni.absolute_url(),
Expand All @@ -219,14 +185,21 @@ def test_if_start_and_not_end_return_all_available_slots_for_that_month(
for week in calendar.monthcalendar(current_year, next_month):
monday = week[0]
if monday > 0:
for hour in [7, 8, 9]:
expected.append(
json_compatible(
datetime_with_tz(
datetime(current_year, next_month, monday, hour, 0)
)
)
expected.append(
self.dt_local_to_json(
datetime(current_year, next_month, monday, 7, 0)
)
)
expected.append(
self.dt_local_to_json(
datetime(current_year, next_month, monday, 8, 0)
)
)
expected.append(
self.dt_local_to_json(
datetime(current_year, next_month, monday, 9, 0)
)
)
self.assertEqual(expected, response.json()["items"])

def test_if_start_and_end_return_all_available_slots_between_these_dates(
Expand All @@ -237,27 +210,34 @@ def test_if_start_and_end_return_all_available_slots_between_these_dates(
current_month = now.month
next_month = current_month + 1

# all mondays in the first 10 days of next month
response = self.api_session.get(
"{}/@available-slots?start={}&end={}".format(
self.folder_prenotazioni.absolute_url(),
json_compatible(date(current_year, next_month, 1)),
json_compatible(date(current_year, next_month, 10)),
)
)

# get next mondays in current month
expected = []
for week in calendar.monthcalendar(current_year, next_month):
monday = week[0]
if monday > 0 and monday < 10:
for hour in [7, 8, 9]:
expected.append(
json_compatible(
datetime_with_tz(
datetime(current_year, next_month, monday, hour, 0)
)
)
if monday > 0 and monday <= 10:
expected.append(
self.dt_local_to_json(
datetime(current_year, next_month, monday, 7, 0)
)
)
expected.append(
self.dt_local_to_json(
datetime(current_year, next_month, monday, 8, 0)
)
)
expected.append(
self.dt_local_to_json(
datetime(current_year, next_month, monday, 9, 0)
)
)
self.assertEqual(expected, response.json()["items"])

def test_raise_error_if_start_is_greater_than_end(
Expand Down Expand Up @@ -316,21 +296,18 @@ def test_month_slots_notBeforeDays_honored(
"{}/@available-slots".format(folder.absolute_url())
)

tomorrow = json_compatible(
datetime_with_tz(
datetime.now().replace(hour=7, minute=0, second=0, microsecond=0)
+ timedelta(days=1)
)
tomorrow = datetime.now() + timedelta(days=1)
tomorrow_7_0 = self.dt_local_to_json(
tomorrow.replace(hour=7, minute=0, second=0, microsecond=0)
)
self.assertNotIn(tomorrow, response.json()["items"])

self.assertNotIn(tomorrow_7_0, response.json()["items"])

folder.notBeforeDays = 0
transaction.commit()

response = self.api_session.get(
"{}/@available-slots".format(folder.absolute_url())
)
self.assertIn(tomorrow, response.json()["items"])
response = self.api_session.get(f"{folder.absolute_url()}/@available-slots")
self.assertIn(tomorrow_7_0, response.json()["items"])

@unittest.skipIf(date.today().day > 20, "issue testing in the last days of a month")
def test_month_slots_filtered_by_booking_type(self):
Expand Down
29 changes: 27 additions & 2 deletions src/redturtle/prenotazioni/tests/test_vacation_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from plone.restapi.testing import RelativeSession
from redturtle.prenotazioni.testing import REDTURTLE_PRENOTAZIONI_API_FUNCTIONAL_TESTING

import pytz
import transaction
import unittest

Expand Down Expand Up @@ -50,18 +51,23 @@ def setUp(self):
row["morning_end"] = "1200"
self.folder_prenotazioni.week_table = week_table

self.next_monday = datetime.now() + timedelta(
self.next_monday = datetime.now().astimezone(pytz.utc) + timedelta(
days=(datetime.today().weekday()) % 7 + 7
)

api.content.transition(obj=self.folder_prenotazioni, transition="publish")

api.portal.set_registry_record(
"plone.portal_timezone",
"Europe/Rome",
)

transaction.commit()

def tearDown(self):
self.api_session_admin.close()
self.api_session_anon.close()

# TODO: creare un nuovo file di test per `vacation` ?
def test_add_vacation(self):
start = self.next_monday.replace(hour=10, minute=0)
end = self.next_monday.replace(hour=11, minute=30)
Expand Down Expand Up @@ -114,3 +120,22 @@ def test_add_vacation(self):
"type": "BookerException",
},
)

def test_add_vacation_wrong_hours(self):
start = self.next_monday.replace(hour=20, minute=0)
end = self.next_monday.replace(hour=21, minute=30)
gate = self.folder_prenotazioni.gates[0]
res = self.api_session_admin.post(
f"{self.folder_prenotazioni.absolute_url()}/@vacation",
json={
"start": start.isoformat(),
"end": end.isoformat(),
"gate": gate,
"title": "vacation foo",
},
)
self.assertEqual(res.status_code, 400)
self.assertEqual(
res.json()["message"],
"Nessuno slot creato, verificare la corretteza dei dati inseriti",
)

0 comments on commit 2a312d3

Please sign in to comment.