"""Integration tests for API endpoints.""" import requests import json import base64 import os from . import schema_returned QA_GDA_GATEWAY_URL = 'https://qa-gda-gateway.theorchard.io/artist-submission' # generate unique test email for payload UNIQUE_TEST_EMAIL = 'test_' + \ base64.b64encode(os.urandom(6)).decode('ascii') + '@test.com' TEST_EVENT_BODY = { 'spotify_followers': 10, 'instagram_url': None, 'youtube_url': None, 'deezer_url': None, 'amazon_music_url': None, 'facebook_url': None, 'twitter_url': None, 'tiktok_url': None, 'soundcloud_url': None, 'other_url': None, 'language': 'en', 'email': UNIQUE_TEST_EMAIL, 'first_name': 'TestFirstName', 'last_name': 'TestLastName', 'company': 'Limp Bizkit', 'role': 'Artist or Band', 'spotify_url': 'https://spotify.com/artist/7M1FPw29m5FbicYzS2xdpi', 'country': 'USA', 'external_test': 'true', 'currency': 'USD', 'signing_entity': 'GBR' } def test_succeeds(get_auth0_token): """Test successful response for authorized request.""" headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer {}'.format(get_auth0_token) } response = requests.post(QA_GDA_GATEWAY_URL, json=TEST_EVENT_BODY, headers=headers) assert response.status_code == 200, \ 'Result of POST was {}, expected 200.'.format(response) def test_unauthorized(): """Test expected 401 response for unauthorized request.""" headers = { 'Content-Type': 'application/json', 'Authorization': 'ZZZZZZZZZZZZZZZZZZZ' } response = requests.post(QA_GDA_GATEWAY_URL, json=TEST_EVENT_BODY, headers=headers) assert response.status_code == 401, \ 'Result of POST was {}, expected 401.'.format(response) def test_event_created(get_consumer, get_auth0_token): """Test Kafka Topic event created.""" headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer {}'.format(get_auth0_token) } requests.post(QA_GDA_GATEWAY_URL, json=TEST_EVENT_BODY, headers=headers) consumer = get_consumer('event.gdaSubmitApplication') consumer.poll(10000) # go to last position (this is the position after the last message) consumer.seek_to_end() # get partition list (only 1 partition so we just use that) topic_partition = consumer.assignment() partition = list(topic_partition)[0] # get target position (current position -1) i.e. last message in list target_position = consumer.position(partition) - 1 # go to target position consumer.seek(partition, target_position) 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'] == UNIQUE_TEST_EMAIL consumer.close() def test_event_and_validate_schema(get_consumer, get_auth0_token): """Test Kafka Topic event object.""" headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer {}'.format(get_auth0_token) } requests.post(QA_GDA_GATEWAY_URL, json=TEST_EVENT_BODY, headers=headers) consumer = get_consumer('event.gdaSubmitApplication') consumer.poll(10000) # go to last position (this is the position after the last message) consumer.seek_to_end() # get partition list (only 1 partition so we just use that) topic_partition = consumer.assignment() partition = list(topic_partition)[0] # get target position (current position -1) i.e. last message in list target_position = consumer.position(partition) - 1 # go to target position consumer.seek(partition, target_position) for index, message in enumerate(consumer): # ensure only 1 message was found assert index < 1 # validates response schema message_value = json.loads(message.value.decode('utf-8')) schema_returned.ArtistSignupKafkaSchema().load(message_value) consumer.close()