示例#1
0
    def test_create_crypto_key(self):
        # Setup Expected Response
        name = 'name3373707'
        expected_response = {'name': name}
        expected_response = resources_pb2.CryptoKey(**expected_response)

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

        # Setup Request
        parent = client.key_ring_path('[PROJECT]', '[LOCATION]', '[KEY_RING]')
        crypto_key_id = 'my-app-key'
        purpose = enums.CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT
        seconds = 2147483647
        next_rotation_time = {'seconds': seconds}
        seconds_2 = 604800
        rotation_period = {'seconds': seconds_2}
        crypto_key = {
            'purpose': purpose,
            'next_rotation_time': next_rotation_time,
            'rotation_period': rotation_period
        }

        response = client.create_crypto_key(parent, crypto_key_id, crypto_key)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = service_pb2.CreateCryptoKeyRequest(
            parent=parent, crypto_key_id=crypto_key_id, crypto_key=crypto_key)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_create_crypto_key(self):
        # Setup Expected Response
        name = "name3373707"
        expected_response = {"name": name}
        expected_response = resources_pb2.CryptoKey(**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
        parent = client.key_ring_path("[PROJECT]", "[LOCATION]", "[KEY_RING]")
        crypto_key_id = "my-app-key"
        purpose = enums.CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT
        seconds = 2147483647
        next_rotation_time = {"seconds": seconds}
        seconds_2 = 604800
        rotation_period = {"seconds": seconds_2}
        crypto_key = {
            "purpose": purpose,
            "next_rotation_time": next_rotation_time,
            "rotation_period": rotation_period,
        }

        response = client.create_crypto_key(parent, crypto_key_id, crypto_key)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = service_pb2.CreateCryptoKeyRequest(
            parent=parent, crypto_key_id=crypto_key_id, crypto_key=crypto_key)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request