async def update_ride_request( *, ride_update: RideRequestUpdate, user: User = Depends(get_current_user), redis_cli: Redis = Depends(get_redis_cli), ): """ Updates ride request status """ receiver = await repositories.profile.get(redis_cli, user.user_id) if receiver is None: raise HTTPException(status_code=404, detail="Receiver not found.") sender = await repositories.profile.get(redis_cli, ride_update.sender) if sender is None: raise HTTPException(status_code=404, detail="Sender not found.") ride_request = RideRequest(sender=sender, receiver=receiver, status=ride_update.status) if not await repositories.rides.request_exists(redis_cli, ride_request): raise HTTPException(status_code=404, detail="Ride request not found.") await repositories.rides.update_request(redis_cli, ride_request) await repositories.stream.request_updated(redis_cli, ride_request) ride = await repositories.rides.get(redis_cli, ride_request.ride_id) assert ride is not None await repositories.stream.ride_updated(redis_cli, ride) return ride_request
def test_create_ride_request_when_sender_is_not_joined(client, profile_factory, make_token_headers): receiver = profile_factory(save=True) with client as cli: response = cli.post("/api/v1/rides", headers=make_token_headers(), json={"receiver": receiver.user_id}) assert response.status_code == 200 assert RideRequest(**response.json())
def test_ws_receive_ride_request_updated(client, profile_factory, make_token_headers, status, expected): loop = asyncio.get_event_loop() ride_request = RideRequest( sender=profile_factory(role=Role.driver), receiver=profile_factory(role=Role.hitchhiker), status=status ) with client as cli: with cli.websocket_connect("/api/v1/ws", headers=make_token_headers(ride_request.sender.user_id)) as ws: asyncio.run_coroutine_threadsafe( repositories.stream.request_updated(cli.app.redis_cli, ride_request), loop ) assert wait_for_message_type(ws, expected)
def test_update_ride(client, profile_factory, make_token_headers, role): receiver = profile_factory(role=role) sender = profile_factory(role=Role.opposite(receiver.role)) ride_request = RideRequest(sender=sender, receiver=receiver, status=RideRequestStatus.requested) with client as cli: loop = asyncio.get_event_loop() loop.run_until_complete( repositories.rides.create_request(cli.app.redis_cli, ride_request)) response = cli.put( f"/api/v1/rides/{ride_request.ride_id}", headers=make_token_headers(receiver.user_id), json={ "status": RideRequestStatus.accepted.value, "sender": sender.user_id }, ) assert response.status_code == 200 assert RideRequest(**response.json())
def test_create_ride_request(client, profile_factory, make_token_headers, role): receiver = profile_factory(role=role) sender = profile_factory(role=Role.opposite(role)) with client as cli: headers = make_token_headers(sender.user_id) response = cli.post("/api/v1/rides", headers=headers, json={"receiver": receiver.user_id}) assert response.status_code == 200 assert RideRequest(**response.json())
def test_leaves_ride(client, profile_factory, make_token_headers, role): sender = profile_factory(role=role) receiver = profile_factory(role=Role.opposite(role)) ride_request = RideRequest(sender=sender, receiver=receiver, status=RideRequestStatus.accepted) with client as cli: loop = asyncio.get_event_loop() loop.run_until_complete( repositories.rides.update_request(cli.app.redis_cli, ride_request)) response = cli.delete("/api/v1/rides", headers=make_token_headers(sender.user_id)) assert response.status_code == 204 assert response.text == ""
def test_ws_receive_map_within_same_ride(client, location_factory, profile_factory, make_token_headers, role): ride_request = RideRequest( sender=profile_factory(role=role), receiver=profile_factory(role=Role.opposite(role)), status=RideRequestStatus.accepted, ) location = location_factory(user_id=ride_request.sender.user_id) location_factory(user_id=ride_request.receiver.user_id) with client as cli: loop = asyncio.get_event_loop() loop.run_until_complete(repositories.rides.update_request(cli.app.redis_cli, ride_request)) with cli.websocket_connect("/api/v1/ws", headers=make_token_headers(ride_request.receiver.user_id)) as ws: assert_map_item(ws.receive_json(), ride_request.sender.user_id, location, has_same_ride=True)
def test_ws_ride_cancelled(client, profile_factory, make_token_headers): ride_request = RideRequest( sender=profile_factory(role=Role.driver), receiver=profile_factory(role=Role.hitchhiker), status=RideRequestStatus.accepted, ) with client as cli: loop = asyncio.get_event_loop() loop.run_until_complete(repositories.rides.update_request(cli.app.redis_cli, ride_request)) ride = loop.run_until_complete(repositories.rides.get(cli.app.redis_cli, ride_request.ride_id)) with cli.websocket_connect("/api/v1/ws", headers=make_token_headers(ride_request.receiver.user_id)) as ws: asyncio.run_coroutine_threadsafe(repositories.stream.ride_cancelled(cli.app.redis_cli, ride), loop) assert wait_for_message_type(ws, MessageType.ride_cancelled)
def test_ws_receive_map_within_different_ride(client, location_factory, profile_factory, make_token_headers, role): ride_request = RideRequest( sender=profile_factory(role=role), receiver=profile_factory(role=Role.opposite(role)), status=RideRequestStatus.accepted, ) location_factory(user_id=ride_request.receiver.user_id) receiver = profile_factory(role=role) location_factory(user_id=receiver.user_id) with client as cli: loop = asyncio.get_event_loop() loop.run_until_complete(repositories.rides.update_request(cli.app.redis_cli, ride_request)) with cli.websocket_connect("/api/v1/ws", headers=make_token_headers(receiver.user_id)) as ws: assert ws.receive_json() == {"type": MessageType.map, "data": []}
async def create_ride_request(ride_create: RideRequestCreate, user: User = Depends(get_current_user), redis_cli: Redis = Depends(get_redis_cli)): """ Creates request for a ride """ receiver = await repositories.profile.get(redis_cli, ride_create.receiver) if receiver is None: raise HTTPException(status_code=404, detail="Receiver not found.") sender = await repositories.profile.get(redis_cli, user.user_id) if sender is None: sender = Profile.from_user(user, role=Role.opposite(receiver.role), destination=receiver.destination) await repositories.profile.save(redis_cli, sender) ride_request = RideRequest(sender=sender, receiver=receiver, status=RideRequestStatus.requested) await repositories.rides.create_request(redis_cli, ride_request) await repositories.stream.ride_requested(redis_cli, ride_request) return ride_request