Files
glutz-maintenances/tests/test_wyby_client.py
T
gurix 3056ab97bb Add wyby sync: proactive PIN provisioning from wyby admin portal
- wyby_client.py: headless login to admin.wyby.ch (OIDC auth-code flow with
  PKCE, cookie session) + booking extraction from the scheduler API
  (GetWallpaperData); PIN = last 6 digits of transaction Number; rental days
  derived from RentingStartDate / AllotmentRentingEndDate (exclusive midnight)
- wyby_sync.py: for every upcoming booking ensure Glutz user, group membership
  and PIN with full-day validity; idempotent, self-healing against wyby's own
  late interface runs; dry-run default
- deploy: wyby-sync.service + 15-min timer (24/7); extend_pins stays hourly
  as safety net
- 39 tests; requests added via uv (available system-wide for the oneshot)
2026-11-12 11:53:41 +01:00

137 lines
5.3 KiB
Python

"""Tests for wyby_client: booking extraction and login-page parsing."""
from datetime import date
import pytest
from wyby_client import WybyClient, extract_login_fields
WALLPAPER = {
"ROOT": {
"RentalObject": [
{
"Id": "34a24dc9-1111-1111-1111-111111111111",
"Name": "Gemeinschaftsraum Huebwis",
"RentalTransaction": [
{
"Id": "tx-1",
"Number": "002237251",
"CustomerName": "Tsechung",
"CustomerFirstName": "Jangchup",
"RentingStartDate": "2026-12-27T00:00:00+01:00",
"AllotmentRentingEndDate": "2026-12-28T00:00:00+01:00",
"refRentalTransactionStatusId": "a198f429",
},
{
"Id": "tx-2",
"Number": "002237200",
"CustomerName": "Intern",
"CustomerFirstName": "ASIG Forum Intern",
"RentingStartDate": "2026-11-23T18:00:00+01:00",
"AllotmentRentingEndDate": "2026-11-24T00:00:00+01:00",
"refRentalTransactionStatusId": "a198f429",
},
{
# canceled transaction (different status id) -> excluded
"Id": "tx-3",
"Number": "002299999",
"CustomerName": "Storno",
"CustomerFirstName": "Test",
"RentingStartDate": "2026-11-25T00:00:00+01:00",
"AllotmentRentingEndDate": "2026-11-26T00:00:00+01:00",
"refRentalTransactionStatusId": "canceled-status",
},
],
}
]
}
}
@pytest.fixture
def client():
return WybyClient(status_ids=("a198f429",))
class TestParseWallpaper:
def test_extracts_bookings_filters_canceled(self, client):
bookings = client.parse_wallpaper(WALLPAPER)
assert sorted(b.number for b in bookings) == ["002237200", "002237251"]
def test_booking_fields(self, client):
b = {x.number: x for x in client.parse_wallpaper(WALLPAPER)}["002237251"]
assert b.pin == "237251"
assert b.start_date.isoformat() == "2026-12-27"
assert b.end_date.isoformat() == "2026-12-27" # exclusive midnight
assert b.label == "Tsechung Jangchup 237251"
assert b.object_id == "34a24dc9-1111-1111-1111-111111111111"
assert b.object_name == "Gemeinschaftsraum Huebwis"
def test_evening_booking_ends_same_day(self, client):
b = {x.number: x for x in client.parse_wallpaper(WALLPAPER)}["002237200"]
assert (b.start_date, b.end_date) == (b.start_date, b.start_date)
def test_same_day_end_keeps_day(self, client):
wp = {
"ROOT": {
"RentalObject": [
{
"Id": "x",
"Name": "o",
"RentalTransaction": [
{
"Id": "t",
"Number": "000001",
"CustomerName": "A",
"CustomerFirstName": "B",
"RentingStartDate": "2026-12-05T15:00:00+01:00",
"AllotmentRentingEndDate": "2026-12-05T23:00:00+01:00",
"refRentalTransactionStatusId": "a198f429",
}
],
}
]
}
}
(b,) = client.parse_wallpaper(wp)
assert (b.start_date, b.end_date) == (date(2026, 12, 5), date(2026, 12, 5))
def test_missing_renting_start_falls_back_to_preparation(self, client):
wp = {
"ROOT": {
"RentalObject": [
{
"Id": "x",
"Name": "o",
"RentalTransaction": [
{
"Id": "t",
"Number": "000002",
"CustomerName": "A",
"CustomerFirstName": "B",
"RentingStartDate": None,
"PreparationStartDate": "2026-12-26T23:00:00+01:00",
"AllotmentRentingEndDate": "2026-12-28T00:00:00+01:00",
"refRentalTransactionStatusId": "a198f429",
}
],
}
]
}
}
(b,) = client.parse_wallpaper(wp)
assert b.start_date.isoformat() == "2026-12-27"
class TestLoginFields:
def test_extracts_return_url_and_token(self):
html = (
'<input type="hidden" id="ReturnUrl" name="ReturnUrl" '
'value="/cb?x=1&amp;y=2" />'
'<input name="__RequestVerificationToken" type="hidden" value="TOK" />'
)
assert extract_login_fields(html) == {
"ReturnUrl": "/cb?x=1&y=2",
"__RequestVerificationToken": "TOK",
}