"""Test to ensure salsesforce config is not altered.""" import json import os import boto3 from botocore.exceptions import ClientError import requests from simple_salesforce import Salesforce from .api_gateway import ApiGateway from .db_connection import DBConnection from .integration_test_helper import IntegrationTestHelper spotify_id = '0Ppjh3TEy1VYLQoyKmIxaD' sf_domain = os.environ.get('SALESFORCE_DOMAIN') env = os.environ.get('ENV') email = 'deleteme@sforcetest.com' NEO4J_QA = os.environ.get('NEO4J_QA') NEO4J_USER = os.environ.get('NEO4J_USER') NEO4J_PASSWORD = os.environ.get('NEO4J_PASSWORD') region_name = 'us-east-1' def get_salesforce_secrets(secrets): """Get secrets from aws needed to query salesforce.""" secret_path = f'{env}/lambda-gda-sforce-kafka-sync/' session = boto3.session.Session() client = session.client( service_name='secretsmanager', region_name=region_name, ) secret_map = {} for secret in secrets: try: get_secret_value_response = client.get_secret_value( SecretId=f'{secret_path}{secret}' ) secret_map[secret] = get_secret_value_response['SecretString'] except ClientError as e: print(e) raise return secret_map def connect_to_salesforce(secret_map): """Return salesforce connection.""" oauth_url = f'https://{sf_domain}.salesforce.com/services/oauth2/token' payload = '&'.join([ f'client_id={secret_map["INTEGRATION_TEST_CONSUMER_KEY"]}', f'client_secret={secret_map["INTEGRATION_TEST_CONSUMER_SECRET"]}', 'grant_type=refresh_token', f'refresh_token={secret_map["INTEGRATION_TEST_REFRESH_TOKEN"]}' ]) headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.request( 'POST', oauth_url, data=payload, headers=headers) credentials = response.json() return Salesforce( instance_url=credentials['instance_url'], session_id=credentials['access_token'], version='54.0') def clear_existing_salesforce_test_data(secret_map): """Delete salesforce data.""" sf = connect_to_salesforce(secret_map) spotify_url = f'https://open.spotify.com/artist/{spotify_id}' lead_qry = f"SELECT Id FROM Lead where SpotifyUrl__c = '{spotify_url}'" results = sf.query(lead_qry)['records'] for record in results: sf.lead.delete(record['Id']) contact_qry = f"SELECT Id, AccountId FROM Contact WHERE email = '{email}'" results = sf.query(contact_qry)['records'] for record in results: sf.contact.delete(record['Id']) sf.account.delete(record['AccountId']) def test_salesforce_output_correct(get_consumer): """Posts data to api gateway, asserts on kafka data, cleans salesforce.""" if env == 'qa': conn = DBConnection(NEO4J_QA, NEO4J_USER, NEO4J_PASSWORD) conn.remove_existing_label_participant(spotify_id) conn.close() consumer = get_consumer('event.gdaApproval') helper = IntegrationTestHelper() partition, start_position = helper.get_last_partition_position(consumer) secrets = ['INTEGRATION_TEST_CONSUMER_KEY', 'INTEGRATION_TEST_CONSUMER_SECRET', 'INTEGRATION_TEST_REFRESH_TOKEN'] secret_map = get_salesforce_secrets(secrets) clear_existing_salesforce_test_data(secret_map) api_gateway = ApiGateway(env) api_gateway.post_form_to_gda_gateway(spotify_id, email) consumer.poll(120000) last_position = helper.get_last_partition_position(consumer)[1] assert last_position > start_position consumer.seek(partition, last_position - 1) for index, message in enumerate(consumer): # ensure only 1 message was found assert index < 1 message_value = json.loads(message.value.decode('utf-8')) # check email matches test data assert message_value['Email'] == email consumer.close() clear_existing_salesforce_test_data(secret_map)