"""Integration tests for lambda function.""" import json from conftest import abacus_schedule_api_client from conftest import CONTRIBUTION_ID from conftest import CONTRIBUTOR_ID from conftest import db_select from conftest import dockerized_lambda_api_client from conftest import gql_get_contribution_info from conftest import gql_unlink_contribution_from_schedule import pytest @pytest.mark.jira('ACC-5591') def test_invoke_lambda(basic_headers): """Assert lambda action.""" dockerized_lambda = dockerized_lambda_api_client(basic_headers) abacus_schedule = abacus_schedule_api_client(basic_headers) # checking whether there's already a schedule present, if not - create one schedule_name = 'schedule_auto_add_integr_tests' response = db_select(schedule_name) schedule_id = 0 if len(response) == 0: schedule_body = { 'schedule_name': schedule_name, 'target_type': 'contributor', 'target_id': CONTRIBUTOR_ID, 'conditions': {'auto_add': True} } res_post = abacus_schedule.post_schedule(schedule_body) assert res_post.status_code == 201, \ 'ERROR CREATING A SCHEDULE. ERROR WAS: {}'.format(res_post.json()) schedule_id = res_post.json()['schedule_id'] else: schedule_id = response[0]['schedule_id'] # delete schedule's contributions (if any) res_put = abacus_schedule.delete_schedule_attachments(schedule_id, CONTRIBUTION_ID) assert res_put.status_code == 200 res_get = abacus_schedule.get_schedule_attachments_by_schedule_id(schedule_id) assert res_get.status_code == 200 assert res_get.json()['total_count'] == 0 gql_unlink_contribution_from_schedule( CONTRIBUTION_ID, None ) contribution_info = gql_get_contribution_info(CONTRIBUTION_ID) abacus_schedule_id = \ contribution_info['data']['nrContributionById']['abacusScheduleId'] assert abacus_schedule_id is None # trigger the lambda file_path = 'tests/integration/sample_event_integration_tests.json' with open(file_path) as json_file: json_data = json.load(json_file) res = dockerized_lambda.post_lambda('2015-03-31', json_data) assert res.status_code == 200 assert res.json() == {'status': 'OK'} # check contribution was added res_get = abacus_schedule.get_schedule_attachments_by_schedule_id(schedule_id) assert res_get.status_code == 200 assert res_get.json()['total_count'] > 0 contribution_info = gql_get_contribution_info(CONTRIBUTION_ID) abacus_schedule_id = \ contribution_info['data']['nrContributionById']['abacusScheduleId'] assert abacus_schedule_id == schedule_id