async def test__fetch_public_key_when_public_key_already_set( self, mock_interaction_server: interaction_server_impl.InteractionServer ): mock_lock = mock.AsyncMock() mock_public_key = object() mock_interaction_server._application_fetch_lock = mock_lock mock_interaction_server._public_key = mock_public_key assert await mock_interaction_server._fetch_public_key() is mock_public_key mock_lock.__aenter__.assert_awaited_once() mock_lock.__aexit__.assert_awaited_once()
async def test__fetch_public_key_fetch_with_bot_token( self, mock_interaction_server: interaction_server_impl.InteractionServer, mock_rest_client: rest_impl.RESTClientImpl, ): mock_rest_client.token_type = "Bot" mock_interaction_server._application_fetch_lock = mock.AsyncMock() mock_rest_client.fetch_application.return_value.public_key = ( b"\xf3\xfd\xfc\x81\xfcU\x00\xe5;V\x15\xc6H\xab4Ip\x07\x1bR\xc2b9\x86\xa9\\e\xfa\xcbw\xd7\x0b" ) result = await mock_interaction_server._fetch_public_key() assert result == nacl.signing.VerifyKey(mock_rest_client.fetch_application.return_value.public_key) mock_rest_client.fetch_authorization.assert_not_called() mock_rest_client.fetch_application.assert_awaited_once() mock_interaction_server._application_fetch_lock.__aenter__.assert_awaited_once() mock_interaction_server._application_fetch_lock.__aexit__.assert_awaited_once()
async def test__fetch_public_key_with_bearer_token( self, mock_interaction_server: interaction_server_impl.InteractionServer, mock_rest_client: rest_impl.RESTClientImpl, ): mock_rest_client.token_type = "Bearer" mock_interaction_server._application_fetch_lock = mock.AsyncMock() mock_rest_client.fetch_authorization.return_value.application.public_key = ( b'\x81\xa9\xc0\xee"\xf0%\xd1CF\x82Uh\x16.>\x9b\xcf[\x1f\xa4\xfcsb\xc3\xf4x\xf9\xe0z\xad\xed' ) result = await mock_interaction_server._fetch_public_key() assert result == nacl.signing.VerifyKey( mock_rest_client.fetch_authorization.return_value.application.public_key ) mock_rest_client.fetch_authorization.assert_awaited_once() mock_rest_client.fetch_application.assert_not_called() mock_interaction_server._application_fetch_lock.__aenter__.assert_awaited_once() mock_interaction_server._application_fetch_lock.__aexit__.assert_awaited_once()
async def test___fetch_public_key_when_lock_is_None_gets_new_lock_and_doesnt_overwrite_existing_ones( self, mock_interaction_server: interaction_server_impl.InteractionServer, mock_rest_client: rest_impl.RESTClientImpl, ): mock_rest_client.token_type = "Bot" mock_interaction_server._application_fetch_lock = None mock_rest_client.fetch_application.return_value.public_key = ( b"e\xb9\xf8\xac]eH\xb1\xe1D\xafaW\xdd\x1c.\xc1s\xfd<\x82\t\xeaO\xd4w\xaf\xc4\x1b\xd0\x8f\xc5" ) results = [] with mock.patch.object(asyncio, "Lock") as lock_class: # Run some times to make sure it does not overwrite it for _ in range(5): results.append(await mock_interaction_server._fetch_public_key()) assert results[0] == nacl.signing.VerifyKey(mock_rest_client.fetch_application.return_value.public_key) assert all(result is results[0] for result in results) assert mock_interaction_server._application_fetch_lock is lock_class.return_value lock_class.assert_called_once_with() lock_class.return_value.__aenter__.assert_has_awaits([mock.call() for _ in range(5)]) lock_class.return_value.__aexit__.assert_has_awaits([mock.call(None, None, None) for _ in range(5)])