def test_insert_data(s3_bucket): handler( APIGateway(path_parameters={ 'secret': 'iamsecret' }, body=dumps({ 'webhookEvent': 'jira:issue_created', 'issue': { 'key': 'TEST-1234', 'fields': { 'created': '2020-01-01T00:00:00.000-0000', 'updated': '2020-01-01T00:00:00.000-0000', 'status': { 'name': 'Open' }, 'labels': ['Test Testerson'], } } })).to_dict()) response = s3_bucket.Object(next(iter(s3_bucket.objects.all())).key).get() data = loads(response['Body'].read()) assert data['data']['issue'] == 'TEST-1234' and\ data['data']['customer'] == 'Test Testerson' and\ data['data']['issue_status'] == 'Open'
def test_invalid_bogus_signature(): res = handler( APIGateway(headers={ 'X-Slack-Signature': 'asdf', 'X-Slack-Request-Timestamp': '0' }).to_dict(), None) assert res['statusCode'] == 403
def test_insert_data_quiz_ingest(s3_bucket, test_data_quiz): print(type(test_data_quiz)) ingestHandler(APIGateway(headers={}, body=test_data_quiz).to_dict(), None) response = s3_bucket.Object(next(iter(s3_bucket.objects.all())).key).get() data = loads(response['Body'].read()) print(data) assert data['data']['tableName'] == 'test_quiz'
def test_valid_signature(test_data): signature = new('iamsecret'.encode(), test_data.encode(), sha1).hexdigest() headers = { 'X-Hub-Signature': f'sha1={signature}', 'X-GitHub-Event': 'test' } res = handler(APIGateway(headers=headers, body=test_data).to_dict(), None) assert res['statusCode'] == 200
def run(args: Namespace, _): if (args.eventFile): with open(args.eventFile) as json_file: args.event = json_file.read() args.eventType = 'APIGateway' elif (args.eventType): if args.eventType == 'APIGateway': api_gw = APIGateway(headers="{}", body="{}").to_json() args.event = api_gw else: args.event = '{}' config = load_serverless_config('functions') handlers = [(c['handler'], c['environment']) for c in config.values()] runners = [prepare_runner(handler, env, args) for handler, env in handlers] def run_lambdas_with_queue(ingest, process): ingest_run, ingest_env = ingest process_run, process_env = process ingest_env() with mock_sqs(): sqs = resource('sqs') sqs.create_queue(QueueName=environ.get('SQS_QUEUE_NAME')) ingest_run(loads(args.event)) messages = sqs.get_queue_by_name( QueueName=environ.get('SQS_QUEUE_NAME')).receive_messages() process_env() process_run({ 'Records': [{ 'messageId': msg.message_id, 'body': msg.body, 'attributes': msg.attributes, 'messageAttributes': msg.message_attributes } for msg in messages] }) len_runners = len(runners) if len_runners == 1: runner, setup_env = runners[0] setup_env() runner(loads(args.event)) elif len_runners == 2: ingest, process = runners run_lambdas_with_queue(ingest, process) elif len_runners == 3: ingest_1, ingest_2, process = runners # Always assume that the poller ingest lambda is the first function in the serverless-file if (args.eventType == 'APIGateway'): run_lambdas_with_queue(ingest_2, process) else: run_lambdas_with_queue(ingest_1, process) else: print('Number of runners not supported') return
def test_valid_signature(): data = dumps({'type': '', 'event': {'type': ''}}) sig_string = f'v0:0:{data}'.encode() signature = new('iamsecret'.encode(), sig_string, sha256).hexdigest() res = handler( APIGateway(headers={ 'X-Slack-Signature': f'v0={signature}', 'X-Slack-Request-Timestamp': '0' }, body=data).to_dict(), None) assert res['statusCode'] == 200
def test_insert_data(s3_bucket, test_data): signature = new('iamsecret'.encode(), test_data.encode(), sha1).hexdigest() handler( APIGateway(headers={ 'X-Hub-Signature': f'sha1={signature}', 'X-GitHub-Event': 'test' }, body=test_data).to_dict(), None) response = s3_bucket.Object(next(iter(s3_bucket.objects.all())).key).get() data = loads(response['Body'].read()) print(data) assert data['metadata']['event'] == 'test' and\ data['data']['id'] == 186853002
def test_valid(): res = handler( APIGateway(path_parameters={ 'secret': 'iamsecret' }, body=dumps({ 'webhookEvent': 'jira:issue_created', 'issue': { 'key': 'TEST-1234', 'fields': { 'created': '2020-01-01T00:00:00.000-0000', 'updated': '2020-01-01T00:00:00.000-0000', 'status': { 'name': 'Open' }, 'labels': ['Test Testerson'], } } })).to_dict()) assert res['statusCode'] == 200
def test_insert_message_data(s3_bucket, mocked_responses, test_message_data): sig_string = f'v0:0:{test_message_data}'.encode() signature = new('iamsecret'.encode(), sig_string, sha256).hexdigest() mocked_responses.add(GET, 'https://slack.com/api/channels.info', json={'channel': { 'name': 'Test' }}, status=200) handler( APIGateway(headers={ 'X-Slack-Signature': f'v0={signature}', 'X-Slack-Request-Timestamp': '0' }, body=test_message_data).to_dict(), None) response = s3_bucket.Object(next(iter(s3_bucket.objects.all())).key).get() data = loads(response['Body'].read()) assert data['data'][0]['emoji'] == 'thumbsup' and \ data['data'][0]['event_type'] == 'message'
def test_invalid(): res = handler(APIGateway().to_dict()) assert res['statusCode'] == 403
def test_invalid_bogus_signature(): headers = {'X-Hub-Signature': 'sha1=asdf', 'X-GitHub-Event': 'test'} res = handler(APIGateway(headers=headers).to_dict(), None) assert res['statusCode'] == 403
def test_invalid_no_signature(): res = handler(APIGateway().to_dict(), None) assert res['statusCode'] == 403
def test_insert_data_ingest(s3_bucket, test_data1): ingest_handler(APIGateway(headers={}, body=test_data1).to_dict(), None) response = s3_bucket.Object(next(iter(s3_bucket.objects.all())).key).get() data = loads(response['Body'].read()) assert data['data']['tableName'] == 'enkel_test_ark 1'