"""Integration tests for Kafka-Salesforce Sync Lambda connection.""" import base64 import hashlib import json import os import time from boto3 import client data_path = 'tests/integration/test_data/salesforce_data.json' def random_email(): """Generate random email.""" return 'test_' + base64.b64encode( os.urandom(6)).decode('ascii') + '@test.com' def configure_data(email, file): """Configure variable data from test data.""" data = json.load(file) data['Email'] = email object_id = generate_salesforce_id(data['Email'], data['Company']) return data, object_id def generate_salesforce_id(email: str, artist_name: str) -> bytes: """Generate salesforce id hash.""" data = ''.join(s.lower() for s in [email, artist_name]) data += 'mdtSXLGxWbQWTmb5lFQZNvS4iAKFjbJT8HJOooA' email_hash = hashlib.sha256(data.encode()) return email_hash.hexdigest().encode() def get_last_partition_position(consumer): """Return partition and latest partition position.""" consumer.poll(500) consumer.seek_to_end() topic_partition = consumer.assignment() partition = list(topic_partition)[0] return partition, consumer.position(partition) def test_salesforce_connector(get_consumer, get_producer): """Test lambda writes Salesforce messages to event.gdaLeads.""" consumer = get_consumer('event.gdaLeads') partition, start_position = get_last_partition_position(consumer) with open(data_path) as f: data, object_id = configure_data(random_email(), f) producer = get_producer # send message to the topic from which Salesforce sink reads producer.send( 'event.gdaSignup', json.dumps(data).encode(), headers=[ ('OrchardHeader.CorrelationId', b'uuid-uuid'), ('CamelHeader.sObjectIdValue', object_id) ] ) message_found = False producer.close() # wait for the lead to be ingested and processed by Salesforce time.sleep(10) # invoke sync lambda which queries Salesforce and writes to event.gdaLeads lambda_client = client('lambda') lambda_client.invoke( FunctionName='qa-lambda-gda-sks-leads', InvocationType='Event') # check if the rejected lead is present in event.gdaLeads consumer.poll(60000) consumer.seek(partition, start_position) for index, message in enumerate(consumer): message_found = True assert index < 1 payload = json.loads(message.value.decode('utf-8')) assert payload['Email'] == data['Email'].lower() assert message_found consumer.close()