Skip to content

Commit 2bceaf8

Browse files
committed
Backend logic should match frontend logic
1 parent 3be1bf3 commit 2bceaf8

2 files changed

Lines changed: 16 additions & 22 deletions

File tree

app/backend/src/couchers/servicers/requests.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,7 @@ def CreateHostRequest(
214214
if not from_date or not to_date:
215215
context.abort_with_error_code(grpc.StatusCode.INVALID_ARGUMENT, "invalid_date")
216216

217-
# Use UTC-12 (the most behind timezone) so any date that's "today"
218-
# anywhere on Earth is accepted. Prevents rejecting valid requests from
219-
# users whose timezone is behind the host's.
220-
today = today_in_timezone("Etc/GMT+12")
217+
today = today_in_timezone(recipient.timezone)
221218

222219
# request starts from the past
223220
if from_date < today:

app/backend/src/tests/test_requests.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -210,36 +210,33 @@ def test_create_request(db, moderator):
210210
assert e.value.details() == "You cannot request to stay with someone for longer than one year."
211211

212212

213-
def test_create_host_request_date_valid_in_requester_timezone(db):
214-
# Simulate a moment where the host's timezone (Europe/Helsinki, UTC+2) has
215-
# already rolled over to the next day while UTC is still on the previous day.
216-
# A from_date that equals "today" in UTC is valid from the requester's
217-
# perspective and must not be rejected just because the host is in a later
218-
# timezone. See: https://github.com/Couchers-org/couchers/issues/XXXX
213+
def test_create_host_request_rejects_date_past_in_host_timezone(db):
214+
# When the host's timezone has already rolled over to the next day, a
215+
# from_date of "today in UTC" is in the past from the host's perspective and
216+
# must be rejected. The frontend blocks this date before submission; the
217+
# backend enforces the same rule for consistency.
219218
user1, token1 = generate_user()
220219
# geom inside the fake Europe/Helsinki timezone polygon used in tests
221220
user2, _ = generate_user(geom=create_coordinate(61, 25))
222221

223-
# Helsinki is already on 2026-01-16; everything else is still 2026-01-15.
222+
# Helsinki is already on 2026-01-16; requester submits 2026-01-15.
224223
fake_today_by_tz = {"Europe/Helsinki": date(2026, 1, 16)}
225224

226225
with patch(
227226
"couchers.servicers.requests.today_in_timezone",
228227
side_effect=lambda tz: fake_today_by_tz.get(tz, date(2026, 1, 15)),
229228
):
230229
with requests_session(token1) as api:
231-
# 2026-01-15 is "today" in UTC but already "yesterday" in Helsinki.
232-
# The request should succeed — the requester must not be penalised
233-
# for the host happening to live in a later timezone.
234-
res = api.CreateHostRequest(
235-
requests_pb2.CreateHostRequestReq(
236-
host_user_id=user2.id,
237-
from_date="2026-01-15",
238-
to_date="2026-01-18",
239-
text=valid_request_text(),
230+
with pytest.raises(grpc.RpcError) as e:
231+
api.CreateHostRequest(
232+
requests_pb2.CreateHostRequestReq(
233+
host_user_id=user2.id,
234+
from_date="2026-01-15",
235+
to_date="2026-01-18",
236+
text=valid_request_text(),
237+
)
240238
)
241-
)
242-
assert res.host_request_id
239+
assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
243240

244241

245242
def test_create_host_request_date_valid_when_host_behind_requester(db):

0 commit comments

Comments
 (0)