|
| 1 | +import datetime |
| 2 | +from typing import Dict |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from stream_chat.async_chat.client import StreamChatAsync |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.incremental |
| 10 | +class TestLiveLocations: |
| 11 | + @pytest.fixture(autouse=True) |
| 12 | + @pytest.mark.asyncio |
| 13 | + async def setup_channel_for_shared_locations(self, channel): |
| 14 | + await channel.update_partial( |
| 15 | + {"config_overrides": {"shared_locations": True}}, |
| 16 | + ) |
| 17 | + yield |
| 18 | + await channel.update_partial( |
| 19 | + {"config_overrides": {"shared_locations": False}}, |
| 20 | + ) |
| 21 | + |
| 22 | + async def test_get_user_locations( |
| 23 | + self, client: StreamChatAsync, channel, random_user: Dict |
| 24 | + ): |
| 25 | + # Create a message to attach location to |
| 26 | + now = datetime.datetime.now(datetime.timezone.utc) |
| 27 | + one_hour_later = now + datetime.timedelta(hours=1) |
| 28 | + shared_location = { |
| 29 | + "created_by_device_id": "test_device_id", |
| 30 | + "latitude": 37.7749, |
| 31 | + "longitude": -122.4194, |
| 32 | + "end_at": one_hour_later.isoformat(), |
| 33 | + } |
| 34 | + |
| 35 | + channel.send_message( |
| 36 | + {"text": "Message with location", "shared_location": shared_location}, |
| 37 | + random_user["id"], |
| 38 | + ) |
| 39 | + |
| 40 | + # Get user locations |
| 41 | + response = await client.get_user_locations(random_user["id"]) |
| 42 | + |
| 43 | + assert "active_live_locations" in response |
| 44 | + assert isinstance(response["active_live_locations"], list) |
| 45 | + |
| 46 | + async def test_update_user_location( |
| 47 | + self, client: StreamChatAsync, channel, random_user: Dict |
| 48 | + ): |
| 49 | + # Create a message to attach location to |
| 50 | + now = datetime.datetime.now(datetime.timezone.utc) |
| 51 | + one_hour_later = now + datetime.timedelta(hours=1) |
| 52 | + shared_location = { |
| 53 | + "created_by_device_id": "test_device_id", |
| 54 | + "latitude": 37.7749, |
| 55 | + "longitude": -122.4194, |
| 56 | + "end_at": one_hour_later.isoformat(), |
| 57 | + } |
| 58 | + |
| 59 | + msg = await channel.send_message( |
| 60 | + {"text": "Message with location", "shared_location": shared_location}, |
| 61 | + random_user["id"], |
| 62 | + ) |
| 63 | + message_id = msg["message"]["id"] |
| 64 | + |
| 65 | + # Update user location |
| 66 | + location_data = { |
| 67 | + "created_by_device_id": "test_device_id", |
| 68 | + "latitude": 37.7749, |
| 69 | + "longitude": -122.4194, |
| 70 | + } |
| 71 | + response = await client.update_user_location( |
| 72 | + random_user["id"], message_id, location_data |
| 73 | + ) |
| 74 | + |
| 75 | + assert response["latitude"] == location_data["latitude"] |
| 76 | + assert response["longitude"] == location_data["longitude"] |
| 77 | + |
| 78 | + # Get user locations to verify |
| 79 | + locations_response = await client.get_user_locations(random_user["id"]) |
| 80 | + assert "active_live_locations" in locations_response |
| 81 | + assert len(locations_response["active_live_locations"]) > 0 |
| 82 | + location = locations_response["active_live_locations"][0] |
| 83 | + assert location["latitude"] == location_data["latitude"] |
| 84 | + assert location["longitude"] == location_data["longitude"] |
0 commit comments