|
1 | 1 | import json |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +import arrow |
2 | 5 |
|
3 | 6 | from inbox.api.ns_api import API_VERSIONS |
4 | 7 | from inbox.models import Calendar, Event |
@@ -415,3 +418,93 @@ def test_api_filter(db, api_client, calendar, default_namespace): |
415 | 418 | _filter = "calendar_id=0000000000000000000000000" |
416 | 419 | events = api_client.get_data("/events?" + _filter) |
417 | 420 | assert len(events) == 0 |
| 421 | + |
| 422 | + |
| 423 | +# -- POST /events/<id>/refresh tests -- |
| 424 | + |
| 425 | + |
| 426 | +def test_refresh_endpoint_updates_db(db, api_client, calendar): |
| 427 | + """Refresh endpoint fetches from provider and updates the local DB.""" |
| 428 | + event = add_fake_event( |
| 429 | + db.session, |
| 430 | + calendar.namespace_id, |
| 431 | + calendar=calendar, |
| 432 | + title="Old Title", |
| 433 | + description="Old description", |
| 434 | + ) |
| 435 | + event.participants = [ |
| 436 | + {"email": "alice@example.com", "name": "Alice", "status": "noreply"} |
| 437 | + ] |
| 438 | + db.session.commit() |
| 439 | + |
| 440 | + refreshed = Event.create( |
| 441 | + uid=event.uid, |
| 442 | + raw_data="{}", |
| 443 | + title="New Title", |
| 444 | + description="New description", |
| 445 | + start=arrow.get(2025, 9, 15, 12, 0, 0), |
| 446 | + end=arrow.get(2025, 9, 15, 13, 0, 0), |
| 447 | + all_day=False, |
| 448 | + busy=True, |
| 449 | + read_only=False, |
| 450 | + owner="Organizer <org@example.com>", |
| 451 | + is_owner=True, |
| 452 | + participants=[ |
| 453 | + {"email": "alice@example.com", "name": "Alice", "status": "yes"} |
| 454 | + ], |
| 455 | + status="confirmed", |
| 456 | + source="local", |
| 457 | + ) |
| 458 | + |
| 459 | + with mock.patch( |
| 460 | + "inbox.api.ns_api.GoogleEventsProvider" |
| 461 | + ) as mock_provider_cls: |
| 462 | + mock_provider = mock_provider_cls.return_value |
| 463 | + mock_provider.fetch_single_event.return_value = refreshed |
| 464 | + |
| 465 | + resp = api_client.post_data(f"/events/{event.public_id}/refresh", {}) |
| 466 | + |
| 467 | + assert resp.status_code == 200 |
| 468 | + |
| 469 | + data = json.loads(resp.data) |
| 470 | + assert data["title"] == "New Title" |
| 471 | + assert data["description"] == "New description" |
| 472 | + assert len(data["participants"]) == 1 |
| 473 | + assert data["participants"][0]["status"] == "yes" |
| 474 | + |
| 475 | + # Verify the DB was updated. |
| 476 | + db.session.refresh(event) |
| 477 | + assert event.title == "New Title" |
| 478 | + assert event.participants[0]["status"] == "yes" |
| 479 | + |
| 480 | + |
| 481 | +def test_refresh_endpoint_unknown_event(db, api_client, calendar): |
| 482 | + """Refresh endpoint returns 404 for an unknown public_id.""" |
| 483 | + unknown_id = generate_public_id() |
| 484 | + resp = api_client.post_data(f"/events/{unknown_id}/refresh", {}) |
| 485 | + assert resp.status_code == 404 |
| 486 | + |
| 487 | + |
| 488 | +def test_refresh_endpoint_provider_returns_none(db, api_client, calendar): |
| 489 | + """Refresh endpoint returns 404 when the provider can't find the event.""" |
| 490 | + event = add_fake_event( |
| 491 | + db.session, |
| 492 | + calendar.namespace_id, |
| 493 | + calendar=calendar, |
| 494 | + title="Existing Event", |
| 495 | + ) |
| 496 | + |
| 497 | + with mock.patch( |
| 498 | + "inbox.api.ns_api.GoogleEventsProvider" |
| 499 | + ) as mock_provider_cls: |
| 500 | + mock_provider = mock_provider_cls.return_value |
| 501 | + mock_provider.fetch_single_event.return_value = None |
| 502 | + |
| 503 | + resp = api_client.post_data(f"/events/{event.public_id}/refresh", {}) |
| 504 | + |
| 505 | + assert resp.status_code == 404 |
| 506 | + |
| 507 | + # Verify the DB record is NOT deleted. |
| 508 | + db.session.refresh(event) |
| 509 | + assert event.title == "Existing Event" |
| 510 | + assert event.deleted_at is None |
0 commit comments