text: """Test mediate grant message handler."""
  import pytest
  from asynctest import TestCase as AsyncTestCase
  
  from ......connections.models.conn_record import ConnRecord
  from ......messaging.base_handler import HandlerException
  from ......messaging.request_context import RequestContext
  from ......messaging.responder import MockResponder
  from ...messages.mediate_grant import MediationGrant
  from ...models.mediation_record import MediationRecord
  from ..mediation_grant_handler import MediationGrantHandler
  
  TEST_CONN_ID = "conn-id"
  TEST_VERKEY = "3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"
  TEST_ENDPOINT = "https://example.com"
  
  
  class TestMediationGrantHandler(AsyncTestCase):
      """Test mediate grant message handler."""
  
      async def setUp(self):
          """Setup test dependencies."""
          self.context = RequestContext.test_context()
          self.session = await self.context.session()
          self.context.message = MediationGrant(
              endpoint=TEST_ENDPOINT, routing_keys=[TEST_VERKEY]
          )
          self.context.connection_ready = True
          self.context.connection_record = ConnRecord(connection_id=TEST_CONN_ID)
  
      async def test_handler_no_active_connection(self):
          handler, responder = MediationGrantHandler(), MockResponder()
          self.context.connection_ready = False
          with pytest.raises(HandlerException) as exc:
              await handler.handle(self.context, responder)
              assert "no active connection" in str(exc.value)
  
      async def test_handler_no_mediation_record(self):
          handler, responder = MediationGrantHandler(), MockResponder()
          with pytest.raises(HandlerException) as exc:
              await handler.handle(self.context, responder)
              assert "has not been requested" in str(exc.value)
  
      async def test_handler(self):
          handler, responder = MediationGrantHandler(), MockResponder()
          await MediationRecord(connection_id=TEST_CONN_ID).save(self.session)
          await handler.handle(self.context, responder)
          record = await MediationRecord.retrieve_by_connection_id(
              self.session, TEST_CONN_ID
          )
          assert record
          assert record.state == MediationRecord.STATE_GRANTED
          assert record.endpoint == TEST_ENDPOINT
          assert record.routing_keys == [TEST_VERKEY]
  