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)
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
"""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&y=2" />'
|
||||
'<input name="__RequestVerificationToken" type="hidden" value="TOK" />'
|
||||
)
|
||||
assert extract_login_fields(html) == {
|
||||
"ReturnUrl": "/cb?x=1&y=2",
|
||||
"__RequestVerificationToken": "TOK",
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
"""Tests for wyby_sync: planning logic with mocked Glutz API + WybyClient."""
|
||||
|
||||
from datetime import date
|
||||
|
||||
import pytest
|
||||
from conftest import USERS, code_row
|
||||
|
||||
from wyby_client import Booking
|
||||
from wyby_sync import Sync, parse_object_map
|
||||
|
||||
OBJ_ID = "34a24dc9-1111-1111-1111-111111111111"
|
||||
GROUP = {"id": 12, "label": "Gr. Gemeinschaftsraum Hueb. 8a", "class": 2}
|
||||
WYBY_USER = {"id": 55, "label": "Tsechung Jangchup 237251", "class": 1}
|
||||
FUTURE = date.today().year + 1 # keep fixtures in the future
|
||||
|
||||
|
||||
def booking(
|
||||
number="002237251",
|
||||
pin="237251",
|
||||
start=date(FUTURE, 12, 27),
|
||||
end=date(FUTURE, 12, 27),
|
||||
):
|
||||
return Booking(
|
||||
number=number,
|
||||
pin=pin,
|
||||
start_date=start,
|
||||
end_date=end,
|
||||
label=f"Tsechung Jangchup {pin}",
|
||||
object_id=OBJ_ID,
|
||||
object_name="Gemeinschaftsraum Huebwis",
|
||||
customer="Jangchup Tsechung",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sync():
|
||||
s = Sync(
|
||||
apply=False,
|
||||
horizon_days=90,
|
||||
object_map=[("34a24dc9", "Gr. Gemeinschaftsraum Hueb. 8a")],
|
||||
profile_id="2",
|
||||
)
|
||||
s.users = USERS + [GROUP, WYBY_USER]
|
||||
s.codes = []
|
||||
s.relations = []
|
||||
return s
|
||||
|
||||
|
||||
class TestParseObjectMap:
|
||||
def test_parse(self):
|
||||
assert parse_object_map("34a24dc9=Gr. Hueb, 8b1f91ff=Gr. Langäri") == [
|
||||
("34a24dc9", "Gr. Hueb"),
|
||||
("8b1f91ff", "Gr. Langäri"),
|
||||
]
|
||||
assert parse_object_map("") == []
|
||||
|
||||
|
||||
class TestPlanBooking:
|
||||
def test_new_booking_plans_relation_and_code(self, sync):
|
||||
sync.plan_booking(booking(), "12", date.today())
|
||||
planned = [(c, d) for _, c, d in sync.planned]
|
||||
assert ("UserGroupRelations", {"parentId": "12", "childId": "55"}) in planned
|
||||
codes = [d for c, d in planned if c == "Codes"]
|
||||
assert codes == [
|
||||
{
|
||||
"userId": "55",
|
||||
"code": "237251",
|
||||
"description": "wyby 002237251",
|
||||
"actionProfileId": "2",
|
||||
"validFrom": f"{FUTURE}-12-27T00:00:00",
|
||||
"validTo": f"{FUTURE}-12-27T23:59:59",
|
||||
}
|
||||
]
|
||||
assert not sync.skipped or all("already" not in s for s in sync.skipped)
|
||||
|
||||
def test_expired_booking_skipped(self, sync):
|
||||
sync.plan_booking(
|
||||
booking(start=date(2020, 1, 1), end=date(2020, 1, 1)), "12", date.today()
|
||||
)
|
||||
assert not sync.planned
|
||||
assert any("expired" in s for s in sync.skipped)
|
||||
|
||||
def test_already_full_day_skipped(self, sync):
|
||||
sync.codes = [
|
||||
code_row(
|
||||
id=99,
|
||||
userId=55,
|
||||
code="237251",
|
||||
validFrom=f"{FUTURE}-12-27T00:00:00.000",
|
||||
validTo=f"{FUTURE}-12-27T23:59:59.000",
|
||||
)
|
||||
]
|
||||
sync.relations = [{"parentId": "12", "childId": "55"}]
|
||||
sync.plan_booking(booking(), "12", date.today())
|
||||
assert not sync.planned
|
||||
assert any("already full-day" in s for s in sync.skipped)
|
||||
|
||||
def test_narrow_window_updates_existing_code(self, sync):
|
||||
sync.codes = [
|
||||
code_row(
|
||||
id=99,
|
||||
userId=55,
|
||||
code="237251",
|
||||
validFrom=f"{FUTURE}-12-27T12:45:00.000",
|
||||
validTo=f"{FUTURE}-12-27T17:15:00.000",
|
||||
)
|
||||
]
|
||||
sync.relations = [{"parentId": "12", "childId": "55"}]
|
||||
sync.plan_booking(booking(), "12", date.today())
|
||||
assert len(sync.planned) == 1
|
||||
_ctx, collection, data = sync.planned[0]
|
||||
assert collection == "Codes"
|
||||
assert data["id"] == "99"
|
||||
assert data["validFrom"] == f"{FUTURE}-12-27T00:00:00"
|
||||
assert data["validTo"] == f"{FUTURE}-12-27T23:59:59"
|
||||
assert data["actionProfileId"] == "2" # preserved
|
||||
|
||||
def test_unknown_user_plans_creation_first(self, sync):
|
||||
sync.plan_booking(booking(pin="999999", number="002299999"), "12", date.today())
|
||||
creations = [d for _, c, d in sync.planned if c == "UsersGroups"]
|
||||
assert creations == [{"label": "Tsechung Jangchup 999999", "class": "1"}]
|
||||
# no code planned in the same pass (user id unknown until created)
|
||||
assert not [d for _, c, d in sync.planned if c == "Codes"]
|
||||
|
||||
|
||||
class TestRun:
|
||||
def test_run_reports_failure_and_returns_false(self, sync, monkeypatch):
|
||||
import glutz_api
|
||||
|
||||
monkeypatch.setattr(
|
||||
glutz_api, "rpc", lambda m, p: (_ for _ in ()).throw(SystemExit("HTTP 500"))
|
||||
)
|
||||
sync.planned = [("ctx", "Codes", {"id": "1"})]
|
||||
assert sync.run() is False
|
||||
assert sync.failed == 1
|
||||
|
||||
def test_run_success(self, sync, monkeypatch):
|
||||
import glutz_api
|
||||
|
||||
monkeypatch.setattr(glutz_api, "rpc", lambda m, p: {"id": "9"})
|
||||
sync.planned = [("ctx", "Codes", {"id": "1", "code": "1"})]
|
||||
assert sync.run() is True
|
||||
Reference in New Issue
Block a user