def test_encrypt(self):
        # Setup Expected Response
        name_2 = "name2-1052831874"
        ciphertext = b"-72"
        expected_response = {"name": name_2, "ciphertext": ciphertext}
        expected_response = service_pb2.EncryptResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = kms_v1.KeyManagementServiceClient()

        # Setup Request
        name = client.crypto_key_path_path("[PROJECT]", "[LOCATION]",
                                           "[KEY_RING]", "[CRYPTO_KEY_PATH]")
        plaintext = b"-9"

        response = client.encrypt(name, plaintext)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = service_pb2.EncryptRequest(name=name,
                                                      plaintext=plaintext)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 2
0
    def test_encrypt(self):
        # Setup Expected Response
        name_2 = 'name2-1052831874'
        ciphertext = b'-72'
        expected_response = {'name': name_2, 'ciphertext': ciphertext}
        expected_response = service_pb2.EncryptResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = kms_v1.KeyManagementServiceClient(channel=channel)

        # Setup Request
        name = client.crypto_key_path_path('[PROJECT]', '[LOCATION]',
                                           '[KEY_RING]', '[CRYPTO_KEY_PATH]')
        plaintext = b'-9'

        response = client.encrypt(name, plaintext)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = service_pb2.EncryptRequest(name=name,
                                                      plaintext=plaintext)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request